xref: /freebsd/sys/vm/vm_fault.c (revision 3f1c66b8d2161beced5e04cb73f9c55392192c44)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
426f9a767SRodney W. Grimes  * Copyright (c) 1994 John S. Dyson
526f9a767SRodney W. Grimes  * All rights reserved.
626f9a767SRodney W. Grimes  * Copyright (c) 1994 David Greenman
726f9a767SRodney W. Grimes  * All rights reserved.
826f9a767SRodney W. Grimes  *
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
11df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
12df8bae1dSRodney W. Grimes  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
225929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
23df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
24df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
25df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
26df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
27df8bae1dSRodney W. Grimes  *    without specific prior written permission.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
40df8bae1dSRodney W. Grimes  *
413c4dd356SDavid Greenman  *	from: @(#)vm_fault.c	8.4 (Berkeley) 1/12/94
42df8bae1dSRodney W. Grimes  *
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
45df8bae1dSRodney W. Grimes  * All rights reserved.
46df8bae1dSRodney W. Grimes  *
47df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
48df8bae1dSRodney W. Grimes  *
49df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
50df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
51df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
52df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
53df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
54df8bae1dSRodney W. Grimes  *
55df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
57df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58df8bae1dSRodney W. Grimes  *
59df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
60df8bae1dSRodney W. Grimes  *
61df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
62df8bae1dSRodney W. Grimes  *  School of Computer Science
63df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
64df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
65df8bae1dSRodney W. Grimes  *
66df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
67df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
68df8bae1dSRodney W. Grimes  */
69df8bae1dSRodney W. Grimes 
70df8bae1dSRodney W. Grimes /*
71df8bae1dSRodney W. Grimes  *	Page fault handling module.
72df8bae1dSRodney W. Grimes  */
73874651b1SDavid E. O'Brien 
74874651b1SDavid E. O'Brien #include <sys/cdefs.h>
75874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
76874651b1SDavid E. O'Brien 
7735818d2eSJohn Baldwin #include "opt_ktrace.h"
78f8a47341SAlan Cox #include "opt_vm.h"
79f8a47341SAlan Cox 
80df8bae1dSRodney W. Grimes #include <sys/param.h>
81df8bae1dSRodney W. Grimes #include <sys/systm.h>
824edf4a58SJohn Baldwin #include <sys/kernel.h>
83fb919e4dSMark Murray #include <sys/lock.h>
84a8b0f100SAlan Cox #include <sys/mman.h>
8526f9a767SRodney W. Grimes #include <sys/proc.h>
86ae34b6ffSEdward Tomasz Napierala #include <sys/racct.h>
8726f9a767SRodney W. Grimes #include <sys/resourcevar.h>
8889f6b863SAttilio Rao #include <sys/rwlock.h>
8923955314SAlfred Perlstein #include <sys/sysctl.h>
904edf4a58SJohn Baldwin #include <sys/vmmeter.h>
914edf4a58SJohn Baldwin #include <sys/vnode.h>
9235818d2eSJohn Baldwin #ifdef KTRACE
9335818d2eSJohn Baldwin #include <sys/ktrace.h>
9435818d2eSJohn Baldwin #endif
95df8bae1dSRodney W. Grimes 
96df8bae1dSRodney W. Grimes #include <vm/vm.h>
97efeaf95aSDavid Greenman #include <vm/vm_param.h>
98efeaf95aSDavid Greenman #include <vm/pmap.h>
99efeaf95aSDavid Greenman #include <vm/vm_map.h>
100efeaf95aSDavid Greenman #include <vm/vm_object.h>
101df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
102df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
103a83c285cSDavid Greenman #include <vm/vm_kern.h>
10424a1cce3SDavid Greenman #include <vm/vm_pager.h>
105efeaf95aSDavid Greenman #include <vm/vm_extern.h>
106dfdf9abdSAlan Cox #include <vm/vm_reserv.h>
107df8bae1dSRodney W. Grimes 
108566526a9SAlan Cox #define PFBAK 4
109566526a9SAlan Cox #define PFFOR 4
110566526a9SAlan Cox 
1115268042bSAlan Cox #define	VM_FAULT_READ_DEFAULT	(1 + VM_FAULT_READ_AHEAD_INIT)
11213458803SAlan Cox #define	VM_FAULT_READ_MAX	(1 + VM_FAULT_READ_AHEAD_MAX)
113a8b0f100SAlan Cox 
114a8b0f100SAlan Cox #define	VM_FAULT_DONTNEED_MIN	1048576
11526f9a767SRodney W. Grimes 
1164866e085SJohn Dyson struct faultstate {
1174866e085SJohn Dyson 	vm_page_t m;
1184866e085SJohn Dyson 	vm_object_t object;
1194866e085SJohn Dyson 	vm_pindex_t pindex;
1204866e085SJohn Dyson 	vm_page_t first_m;
1214866e085SJohn Dyson 	vm_object_t	first_object;
1224866e085SJohn Dyson 	vm_pindex_t first_pindex;
1234866e085SJohn Dyson 	vm_map_t map;
1244866e085SJohn Dyson 	vm_map_entry_t entry;
12525adb370SBrian Feldman 	int lookup_still_valid;
1264866e085SJohn Dyson 	struct vnode *vp;
1274866e085SJohn Dyson };
1284866e085SJohn Dyson 
129a8b0f100SAlan Cox static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr,
130a8b0f100SAlan Cox 	    int ahead);
13163281952SAlan Cox static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
132b0cd2017SGleb Smirnoff 	    int backward, int forward);
13313458803SAlan Cox 
13462a59e8fSWarner Losh static inline void
1354866e085SJohn Dyson release_page(struct faultstate *fs)
1364866e085SJohn Dyson {
1370d0be82aSKonstantin Belousov 
138c7aebda8SAttilio Rao 	vm_page_xunbusy(fs->m);
1392965a453SKip Macy 	vm_page_lock(fs->m);
1404866e085SJohn Dyson 	vm_page_deactivate(fs->m);
1412965a453SKip Macy 	vm_page_unlock(fs->m);
1424866e085SJohn Dyson 	fs->m = NULL;
1434866e085SJohn Dyson }
1444866e085SJohn Dyson 
14562a59e8fSWarner Losh static inline void
1464866e085SJohn Dyson unlock_map(struct faultstate *fs)
1474866e085SJohn Dyson {
1480d0be82aSKonstantin Belousov 
14925adb370SBrian Feldman 	if (fs->lookup_still_valid) {
1504866e085SJohn Dyson 		vm_map_lookup_done(fs->map, fs->entry);
15125adb370SBrian Feldman 		fs->lookup_still_valid = FALSE;
1524866e085SJohn Dyson 	}
1534866e085SJohn Dyson }
1544866e085SJohn Dyson 
1554866e085SJohn Dyson static void
156a51b0840SAlan Cox unlock_and_deallocate(struct faultstate *fs)
1574866e085SJohn Dyson {
158f29ba63eSAlan Cox 
1594866e085SJohn Dyson 	vm_object_pip_wakeup(fs->object);
16089f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(fs->object);
1614866e085SJohn Dyson 	if (fs->object != fs->first_object) {
16289f6b863SAttilio Rao 		VM_OBJECT_WLOCK(fs->first_object);
1632965a453SKip Macy 		vm_page_lock(fs->first_m);
1644866e085SJohn Dyson 		vm_page_free(fs->first_m);
1652965a453SKip Macy 		vm_page_unlock(fs->first_m);
1664866e085SJohn Dyson 		vm_object_pip_wakeup(fs->first_object);
16789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(fs->first_object);
1684866e085SJohn Dyson 		fs->first_m = NULL;
1694866e085SJohn Dyson 	}
1704866e085SJohn Dyson 	vm_object_deallocate(fs->first_object);
1714866e085SJohn Dyson 	unlock_map(fs);
1724866e085SJohn Dyson 	if (fs->vp != NULL) {
1730cddd8f0SMatthew Dillon 		vput(fs->vp);
1744866e085SJohn Dyson 		fs->vp = NULL;
1754866e085SJohn Dyson 	}
1764866e085SJohn Dyson }
1774866e085SJohn Dyson 
178a36f5532SKonstantin Belousov static void
179a36f5532SKonstantin Belousov vm_fault_dirty(vm_map_entry_t entry, vm_page_t m, vm_prot_t prot,
180a36f5532SKonstantin Belousov     vm_prot_t fault_type, int fault_flags, boolean_t set_wd)
181a36f5532SKonstantin Belousov {
182a36f5532SKonstantin Belousov 	boolean_t need_dirty;
183a36f5532SKonstantin Belousov 
184a36f5532SKonstantin Belousov 	if (((prot & VM_PROT_WRITE) == 0 &&
185a36f5532SKonstantin Belousov 	    (fault_flags & VM_FAULT_DIRTY) == 0) ||
186a36f5532SKonstantin Belousov 	    (m->oflags & VPO_UNMANAGED) != 0)
187a36f5532SKonstantin Belousov 		return;
188a36f5532SKonstantin Belousov 
189a36f5532SKonstantin Belousov 	VM_OBJECT_ASSERT_LOCKED(m->object);
190a36f5532SKonstantin Belousov 
191a36f5532SKonstantin Belousov 	need_dirty = ((fault_type & VM_PROT_WRITE) != 0 &&
1926a875bf9SKonstantin Belousov 	    (fault_flags & VM_FAULT_WIRE) == 0) ||
193a36f5532SKonstantin Belousov 	    (fault_flags & VM_FAULT_DIRTY) != 0;
194a36f5532SKonstantin Belousov 
195a36f5532SKonstantin Belousov 	if (set_wd)
196a36f5532SKonstantin Belousov 		vm_object_set_writeable_dirty(m->object);
197a36f5532SKonstantin Belousov 	else
198a36f5532SKonstantin Belousov 		/*
199a36f5532SKonstantin Belousov 		 * If two callers of vm_fault_dirty() with set_wd ==
200a36f5532SKonstantin Belousov 		 * FALSE, one for the map entry with MAP_ENTRY_NOSYNC
201a36f5532SKonstantin Belousov 		 * flag set, other with flag clear, race, it is
202a36f5532SKonstantin Belousov 		 * possible for the no-NOSYNC thread to see m->dirty
203a36f5532SKonstantin Belousov 		 * != 0 and not clear VPO_NOSYNC.  Take vm_page lock
204a36f5532SKonstantin Belousov 		 * around manipulation of VPO_NOSYNC and
205a36f5532SKonstantin Belousov 		 * vm_page_dirty() call, to avoid the race and keep
206a36f5532SKonstantin Belousov 		 * m->oflags consistent.
207a36f5532SKonstantin Belousov 		 */
208a36f5532SKonstantin Belousov 		vm_page_lock(m);
209a36f5532SKonstantin Belousov 
210a36f5532SKonstantin Belousov 	/*
211a36f5532SKonstantin Belousov 	 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
212a36f5532SKonstantin Belousov 	 * if the page is already dirty to prevent data written with
213a36f5532SKonstantin Belousov 	 * the expectation of being synced from not being synced.
214a36f5532SKonstantin Belousov 	 * Likewise if this entry does not request NOSYNC then make
215a36f5532SKonstantin Belousov 	 * sure the page isn't marked NOSYNC.  Applications sharing
216a36f5532SKonstantin Belousov 	 * data should use the same flags to avoid ping ponging.
217a36f5532SKonstantin Belousov 	 */
218a36f5532SKonstantin Belousov 	if ((entry->eflags & MAP_ENTRY_NOSYNC) != 0) {
219a36f5532SKonstantin Belousov 		if (m->dirty == 0) {
220a36f5532SKonstantin Belousov 			m->oflags |= VPO_NOSYNC;
221a36f5532SKonstantin Belousov 		}
222a36f5532SKonstantin Belousov 	} else {
223a36f5532SKonstantin Belousov 		m->oflags &= ~VPO_NOSYNC;
224a36f5532SKonstantin Belousov 	}
225a36f5532SKonstantin Belousov 
226a36f5532SKonstantin Belousov 	/*
227a36f5532SKonstantin Belousov 	 * If the fault is a write, we know that this page is being
228a36f5532SKonstantin Belousov 	 * written NOW so dirty it explicitly to save on
229a36f5532SKonstantin Belousov 	 * pmap_is_modified() calls later.
230a36f5532SKonstantin Belousov 	 *
231a36f5532SKonstantin Belousov 	 * Also tell the backing pager, if any, that it should remove
232a36f5532SKonstantin Belousov 	 * any swap backing since the page is now dirty.
233a36f5532SKonstantin Belousov 	 */
234a36f5532SKonstantin Belousov 	if (need_dirty)
235a36f5532SKonstantin Belousov 		vm_page_dirty(m);
236a36f5532SKonstantin Belousov 	if (!set_wd)
237a36f5532SKonstantin Belousov 		vm_page_unlock(m);
238a36f5532SKonstantin Belousov 	if (need_dirty)
239a36f5532SKonstantin Belousov 		vm_pager_page_unswapped(m);
240a36f5532SKonstantin Belousov }
241a36f5532SKonstantin Belousov 
242df8bae1dSRodney W. Grimes /*
243df8bae1dSRodney W. Grimes  *	vm_fault:
244df8bae1dSRodney W. Grimes  *
245956f3135SPhilippe Charnier  *	Handle a page fault occurring at the given address,
246df8bae1dSRodney W. Grimes  *	requiring the given permissions, in the map specified.
247df8bae1dSRodney W. Grimes  *	If successful, the page is inserted into the
248df8bae1dSRodney W. Grimes  *	associated physical map.
249df8bae1dSRodney W. Grimes  *
250df8bae1dSRodney W. Grimes  *	NOTE: the given address should be truncated to the
251df8bae1dSRodney W. Grimes  *	proper page address.
252df8bae1dSRodney W. Grimes  *
253df8bae1dSRodney W. Grimes  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
254df8bae1dSRodney W. Grimes  *	a standard error specifying why the fault is fatal is returned.
255df8bae1dSRodney W. Grimes  *
256df8bae1dSRodney W. Grimes  *	The map in question must be referenced, and remains so.
2570cddd8f0SMatthew Dillon  *	Caller may hold no locks.
258df8bae1dSRodney W. Grimes  */
259df8bae1dSRodney W. Grimes int
26023955314SAlfred Perlstein vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
26123955314SAlfred Perlstein     int fault_flags)
26223955314SAlfred Perlstein {
26335818d2eSJohn Baldwin 	struct thread *td;
26435818d2eSJohn Baldwin 	int result;
265acd11c74SAlan Cox 
26635818d2eSJohn Baldwin 	td = curthread;
26735818d2eSJohn Baldwin 	if ((td->td_pflags & TDP_NOFAULTING) != 0)
2682801687dSKonstantin Belousov 		return (KERN_PROTECTION_FAILURE);
26935818d2eSJohn Baldwin #ifdef KTRACE
27035818d2eSJohn Baldwin 	if (map != kernel_map && KTRPOINT(td, KTR_FAULT))
27135818d2eSJohn Baldwin 		ktrfault(vaddr, fault_type);
27235818d2eSJohn Baldwin #endif
273be996836SAttilio Rao 	result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags,
274be996836SAttilio Rao 	    NULL);
27535818d2eSJohn Baldwin #ifdef KTRACE
27635818d2eSJohn Baldwin 	if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND))
27735818d2eSJohn Baldwin 		ktrfaultend(result);
27835818d2eSJohn Baldwin #endif
27935818d2eSJohn Baldwin 	return (result);
280acd11c74SAlan Cox }
281acd11c74SAlan Cox 
282acd11c74SAlan Cox int
283be996836SAttilio Rao vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
284acd11c74SAlan Cox     int fault_flags, vm_page_t *m_hold)
285acd11c74SAlan Cox {
286df8bae1dSRodney W. Grimes 	vm_prot_t prot;
287b0cd2017SGleb Smirnoff 	int alloc_req, era, faultcount, nera, result;
288*3f1c66b8SKonstantin Belousov 	boolean_t dead, growstack, is_first_object_locked, wired;
2892d8acc0fSJohn Dyson 	int map_generation;
290df8bae1dSRodney W. Grimes 	vm_object_t next_object;
2914866e085SJohn Dyson 	int hardfault;
2924866e085SJohn Dyson 	struct faultstate fs;
293d2bf64c3SKonstantin Belousov 	struct vnode *vp;
294afe55ca3SKonstantin Belousov 	vm_page_t m;
295*3f1c66b8SKonstantin Belousov 	int ahead, behind, cluster_offset, error, locked;
296df8bae1dSRodney W. Grimes 
2974866e085SJohn Dyson 	hardfault = 0;
2986139043bSAlan Cox 	growstack = TRUE;
29967596082SAttilio Rao 	PCPU_INC(cnt.v_vm_faults);
300d2bf64c3SKonstantin Belousov 	fs.vp = NULL;
301b0cd2017SGleb Smirnoff 	faultcount = 0;
302df8bae1dSRodney W. Grimes 
303df8bae1dSRodney W. Grimes RetryFault:;
304df8bae1dSRodney W. Grimes 
305df8bae1dSRodney W. Grimes 	/*
3060d94caffSDavid Greenman 	 * Find the backing store object and offset into it to begin the
3070d94caffSDavid Greenman 	 * search.
308df8bae1dSRodney W. Grimes 	 */
30940360b1bSMatthew Dillon 	fs.map = map;
31092de35b0SAlan Cox 	result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry,
31192de35b0SAlan Cox 	    &fs.first_object, &fs.first_pindex, &prot, &wired);
31292de35b0SAlan Cox 	if (result != KERN_SUCCESS) {
3136139043bSAlan Cox 		if (growstack && result == KERN_INVALID_ADDRESS &&
3142db65ab4SAlan Cox 		    map != kernel_map) {
3156139043bSAlan Cox 			result = vm_map_growstack(curproc, vaddr);
316a976eb5eSAlan Cox 			if (result != KERN_SUCCESS)
3176139043bSAlan Cox 				return (KERN_FAILURE);
3186139043bSAlan Cox 			growstack = FALSE;
3196139043bSAlan Cox 			goto RetryFault;
3206139043bSAlan Cox 		}
32192de35b0SAlan Cox 		return (result);
32209e0c6ccSJohn Dyson 	}
32309e0c6ccSJohn Dyson 
3244866e085SJohn Dyson 	map_generation = fs.map->timestamp;
3252d8acc0fSJohn Dyson 
3264866e085SJohn Dyson 	if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
32747221757SJohn Dyson 		panic("vm_fault: fault on nofault entry, addr: %lx",
32892c4c4ebSBruce Evans 		    (u_long)vaddr);
3297aaaa4fdSJohn Dyson 	}
3307aaaa4fdSJohn Dyson 
3314f9c9114SKonstantin Belousov 	if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
3324f9c9114SKonstantin Belousov 	    fs.entry->wiring_thread != curthread) {
3334f9c9114SKonstantin Belousov 		vm_map_unlock_read(fs.map);
3344f9c9114SKonstantin Belousov 		vm_map_lock(fs.map);
3354f9c9114SKonstantin Belousov 		if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
3364f9c9114SKonstantin Belousov 		    (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
33785af31a4SKonstantin Belousov 			if (fs.vp != NULL) {
33885af31a4SKonstantin Belousov 				vput(fs.vp);
33985af31a4SKonstantin Belousov 				fs.vp = NULL;
34085af31a4SKonstantin Belousov 			}
3414f9c9114SKonstantin Belousov 			fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
3424f9c9114SKonstantin Belousov 			vm_map_unlock_and_wait(fs.map, 0);
3434f9c9114SKonstantin Belousov 		} else
3444f9c9114SKonstantin Belousov 			vm_map_unlock(fs.map);
3454f9c9114SKonstantin Belousov 		goto RetryFault;
3464f9c9114SKonstantin Belousov 	}
3474f9c9114SKonstantin Belousov 
348afe55ca3SKonstantin Belousov 	if (wired)
349afe55ca3SKonstantin Belousov 		fault_type = prot | (fault_type & VM_PROT_COPY);
3506a875bf9SKonstantin Belousov 	else
3516a875bf9SKonstantin Belousov 		KASSERT((fault_flags & VM_FAULT_WIRE) == 0,
3526a875bf9SKonstantin Belousov 		    ("!wired && VM_FAULT_WIRE"));
353afe55ca3SKonstantin Belousov 
354afe55ca3SKonstantin Belousov 	if (fs.vp == NULL /* avoid locked vnode leak */ &&
3556a875bf9SKonstantin Belousov 	    (fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0 &&
356afe55ca3SKonstantin Belousov 	    /* avoid calling vm_object_set_writeable_dirty() */
357afe55ca3SKonstantin Belousov 	    ((prot & VM_PROT_WRITE) == 0 ||
358f40cb1c6SKonstantin Belousov 	    (fs.first_object->type != OBJT_VNODE &&
359f40cb1c6SKonstantin Belousov 	    (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
360afe55ca3SKonstantin Belousov 	    (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0)) {
361afe55ca3SKonstantin Belousov 		VM_OBJECT_RLOCK(fs.first_object);
362afe55ca3SKonstantin Belousov 		if ((prot & VM_PROT_WRITE) != 0 &&
363f40cb1c6SKonstantin Belousov 		    (fs.first_object->type == OBJT_VNODE ||
364f40cb1c6SKonstantin Belousov 		    (fs.first_object->flags & OBJ_TMPFS_NODE) != 0) &&
365afe55ca3SKonstantin Belousov 		    (fs.first_object->flags & OBJ_MIGHTBEDIRTY) == 0)
366afe55ca3SKonstantin Belousov 			goto fast_failed;
367afe55ca3SKonstantin Belousov 		m = vm_page_lookup(fs.first_object, fs.first_pindex);
368b9ce8cc2SAlan Cox 		/* A busy page can be mapped for read|execute access. */
369b9ce8cc2SAlan Cox 		if (m == NULL || ((prot & VM_PROT_WRITE) != 0 &&
370b9ce8cc2SAlan Cox 		    vm_page_busied(m)) || m->valid != VM_PAGE_BITS_ALL)
371afe55ca3SKonstantin Belousov 			goto fast_failed;
372afe55ca3SKonstantin Belousov 		result = pmap_enter(fs.map->pmap, vaddr, m, prot,
373afe55ca3SKonstantin Belousov 		   fault_type | PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED :
374afe55ca3SKonstantin Belousov 		   0), 0);
375afe55ca3SKonstantin Belousov 		if (result != KERN_SUCCESS)
376afe55ca3SKonstantin Belousov 			goto fast_failed;
377afe55ca3SKonstantin Belousov 		if (m_hold != NULL) {
378afe55ca3SKonstantin Belousov 			*m_hold = m;
379afe55ca3SKonstantin Belousov 			vm_page_lock(m);
380afe55ca3SKonstantin Belousov 			vm_page_hold(m);
381afe55ca3SKonstantin Belousov 			vm_page_unlock(m);
382afe55ca3SKonstantin Belousov 		}
383a36f5532SKonstantin Belousov 		vm_fault_dirty(fs.entry, m, prot, fault_type, fault_flags,
384a36f5532SKonstantin Belousov 		    FALSE);
385afe55ca3SKonstantin Belousov 		VM_OBJECT_RUNLOCK(fs.first_object);
386afe55ca3SKonstantin Belousov 		if (!wired)
387b0cd2017SGleb Smirnoff 			vm_fault_prefault(&fs, vaddr, PFBAK, PFFOR);
388afe55ca3SKonstantin Belousov 		vm_map_lookup_done(fs.map, fs.entry);
389afe55ca3SKonstantin Belousov 		curthread->td_ru.ru_minflt++;
390afe55ca3SKonstantin Belousov 		return (KERN_SUCCESS);
391afe55ca3SKonstantin Belousov fast_failed:
392afe55ca3SKonstantin Belousov 		if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) {
393afe55ca3SKonstantin Belousov 			VM_OBJECT_RUNLOCK(fs.first_object);
394afe55ca3SKonstantin Belousov 			VM_OBJECT_WLOCK(fs.first_object);
395afe55ca3SKonstantin Belousov 		}
396afe55ca3SKonstantin Belousov 	} else {
397afe55ca3SKonstantin Belousov 		VM_OBJECT_WLOCK(fs.first_object);
398afe55ca3SKonstantin Belousov 	}
399afe55ca3SKonstantin Belousov 
40095e5e988SJohn Dyson 	/*
40195e5e988SJohn Dyson 	 * Make a reference to this object to prevent its disposal while we
40295e5e988SJohn Dyson 	 * are messing with it.  Once we have the reference, the map is free
40395e5e988SJohn Dyson 	 * to be diddled.  Since objects reference their shadows (and copies),
40495e5e988SJohn Dyson 	 * they will stay around as well.
405fe8e0238SMatthew Dillon 	 *
406fe8e0238SMatthew Dillon 	 * Bump the paging-in-progress count to prevent size changes (e.g.
407fe8e0238SMatthew Dillon 	 * truncation operations) during I/O.  This must be done after
408fe8e0238SMatthew Dillon 	 * obtaining the vnode lock in order to avoid possible deadlocks.
40995e5e988SJohn Dyson 	 */
410a976eb5eSAlan Cox 	vm_object_reference_locked(fs.first_object);
411d474eaaaSDoug Rabson 	vm_object_pip_add(fs.first_object, 1);
41295e5e988SJohn Dyson 
41325adb370SBrian Feldman 	fs.lookup_still_valid = TRUE;
414df8bae1dSRodney W. Grimes 
4154866e085SJohn Dyson 	fs.first_m = NULL;
416df8bae1dSRodney W. Grimes 
417df8bae1dSRodney W. Grimes 	/*
418df8bae1dSRodney W. Grimes 	 * Search for the page at object/offset.
419df8bae1dSRodney W. Grimes 	 */
4204866e085SJohn Dyson 	fs.object = fs.first_object;
4214866e085SJohn Dyson 	fs.pindex = fs.first_pindex;
422df8bae1dSRodney W. Grimes 	while (TRUE) {
4231c7c3c6aSMatthew Dillon 		/*
424725441f6SKonstantin Belousov 		 * If the object is marked for imminent termination,
425725441f6SKonstantin Belousov 		 * we retry here, since the collapse pass has raced
426725441f6SKonstantin Belousov 		 * with us.  Otherwise, if we see terminally dead
427725441f6SKonstantin Belousov 		 * object, return fail.
4281c7c3c6aSMatthew Dillon 		 */
429725441f6SKonstantin Belousov 		if ((fs.object->flags & OBJ_DEAD) != 0) {
430725441f6SKonstantin Belousov 			dead = fs.object->type == OBJT_DEAD;
4314866e085SJohn Dyson 			unlock_and_deallocate(&fs);
432725441f6SKonstantin Belousov 			if (dead)
43347221757SJohn Dyson 				return (KERN_PROTECTION_FAILURE);
434725441f6SKonstantin Belousov 			pause("vmf_de", 1);
435725441f6SKonstantin Belousov 			goto RetryFault;
43647221757SJohn Dyson 		}
43747221757SJohn Dyson 
4381c7c3c6aSMatthew Dillon 		/*
4391c7c3c6aSMatthew Dillon 		 * See if page is resident
4401c7c3c6aSMatthew Dillon 		 */
4414866e085SJohn Dyson 		fs.m = vm_page_lookup(fs.object, fs.pindex);
4424866e085SJohn Dyson 		if (fs.m != NULL) {
44398cb733cSKenneth D. Merry 			/*
4441c7c3c6aSMatthew Dillon 			 * Wait/Retry if the page is busy.  We have to do this
445c7aebda8SAttilio Rao 			 * if the page is either exclusive or shared busy
446c7aebda8SAttilio Rao 			 * because the vm_pager may be using read busy for
447c7aebda8SAttilio Rao 			 * pageouts (and even pageins if it is the vnode
448c7aebda8SAttilio Rao 			 * pager), and we could end up trying to pagein and
449c7aebda8SAttilio Rao 			 * pageout the same page simultaneously.
4501c7c3c6aSMatthew Dillon 			 *
4511c7c3c6aSMatthew Dillon 			 * We can theoretically allow the busy case on a read
4521c7c3c6aSMatthew Dillon 			 * fault if the page is marked valid, but since such
4531c7c3c6aSMatthew Dillon 			 * pages are typically already pmap'd, putting that
4541c7c3c6aSMatthew Dillon 			 * special case in might be more effort then it is
4551c7c3c6aSMatthew Dillon 			 * worth.  We cannot under any circumstances mess
456c7aebda8SAttilio Rao 			 * around with a shared busied page except, perhaps,
4571c7c3c6aSMatthew Dillon 			 * to pmap it.
458df8bae1dSRodney W. Grimes 			 */
459c7aebda8SAttilio Rao 			if (vm_page_busied(fs.m)) {
460b88b6c9dSAlan Cox 				/*
461b88b6c9dSAlan Cox 				 * Reference the page before unlocking and
462b88b6c9dSAlan Cox 				 * sleeping so that the page daemon is less
463b88b6c9dSAlan Cox 				 * likely to reclaim it.
464b88b6c9dSAlan Cox 				 */
4653407fefeSKonstantin Belousov 				vm_page_aflag_set(fs.m, PGA_REFERENCED);
466a51b0840SAlan Cox 				if (fs.object != fs.first_object) {
46789f6b863SAttilio Rao 					if (!VM_OBJECT_TRYWLOCK(
468a6e38685SKonstantin Belousov 					    fs.first_object)) {
46989f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(fs.object);
47089f6b863SAttilio Rao 						VM_OBJECT_WLOCK(fs.first_object);
47189f6b863SAttilio Rao 						VM_OBJECT_WLOCK(fs.object);
472a6e38685SKonstantin Belousov 					}
4732965a453SKip Macy 					vm_page_lock(fs.first_m);
474a51b0840SAlan Cox 					vm_page_free(fs.first_m);
4752965a453SKip Macy 					vm_page_unlock(fs.first_m);
476a51b0840SAlan Cox 					vm_object_pip_wakeup(fs.first_object);
47789f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(fs.first_object);
478a51b0840SAlan Cox 					fs.first_m = NULL;
479a51b0840SAlan Cox 				}
480a51b0840SAlan Cox 				unlock_map(&fs);
481a51b0840SAlan Cox 				if (fs.m == vm_page_lookup(fs.object,
482a51b0840SAlan Cox 				    fs.pindex)) {
483c7aebda8SAttilio Rao 					vm_page_sleep_if_busy(fs.m, "vmpfw");
484a51b0840SAlan Cox 				}
485a51b0840SAlan Cox 				vm_object_pip_wakeup(fs.object);
48689f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(fs.object);
48767596082SAttilio Rao 				PCPU_INC(cnt.v_intrans);
4884866e085SJohn Dyson 				vm_object_deallocate(fs.first_object);
489df8bae1dSRodney W. Grimes 				goto RetryFault;
490df8bae1dSRodney W. Grimes 			}
4913846a822SKonstantin Belousov 			vm_page_lock(fs.m);
4928d220203SAlan Cox 			vm_page_remque(fs.m);
4932965a453SKip Macy 			vm_page_unlock(fs.m);
4947615edaaSMatthew Dillon 
4951c7c3c6aSMatthew Dillon 			/*
4961c7c3c6aSMatthew Dillon 			 * Mark page busy for other processes, and the
4971c7c3c6aSMatthew Dillon 			 * pagedaemon.  If it still isn't completely valid
4981c7c3c6aSMatthew Dillon 			 * (readable), jump to readrest, else break-out ( we
4991c7c3c6aSMatthew Dillon 			 * found the page ).
5001c7c3c6aSMatthew Dillon 			 */
501c7aebda8SAttilio Rao 			vm_page_xbusy(fs.m);
502ff5958e7SAlan Cox 			if (fs.m->valid != VM_PAGE_BITS_ALL)
5030d94caffSDavid Greenman 				goto readrest;
504df8bae1dSRodney W. Grimes 			break;
505df8bae1dSRodney W. Grimes 		}
50610b4196bSAlan Cox 		KASSERT(fs.m == NULL, ("fs.m should be NULL, not %p", fs.m));
5071c7c3c6aSMatthew Dillon 
5081c7c3c6aSMatthew Dillon 		/*
50910b4196bSAlan Cox 		 * Page is not resident.  If the pager might contain the page
51010b4196bSAlan Cox 		 * or this is the beginning of the search, allocate a new
51110b4196bSAlan Cox 		 * page.  (Default objects are zero-fill, so there is no real
51210b4196bSAlan Cox 		 * pager for them.)
5131c7c3c6aSMatthew Dillon 		 */
5146a875bf9SKonstantin Belousov 		if (fs.object->type != OBJT_DEFAULT ||
5156a875bf9SKonstantin Belousov 		    fs.object == fs.first_object) {
5164866e085SJohn Dyson 			if (fs.pindex >= fs.object->size) {
5174866e085SJohn Dyson 				unlock_and_deallocate(&fs);
5185f55e841SDavid Greenman 				return (KERN_PROTECTION_FAILURE);
5195f55e841SDavid Greenman 			}
52022ba64e8SJohn Dyson 
521df8bae1dSRodney W. Grimes 			/*
5220d94caffSDavid Greenman 			 * Allocate a new page for this object/offset pair.
5233f1c4c4fSKonstantin Belousov 			 *
5243f1c4c4fSKonstantin Belousov 			 * Unlocked read of the p_flag is harmless. At
5253f1c4c4fSKonstantin Belousov 			 * worst, the P_KILLED might be not observed
5263f1c4c4fSKonstantin Belousov 			 * there, and allocation can fail, causing
5273f1c4c4fSKonstantin Belousov 			 * restart and new reading of the p_flag.
528df8bae1dSRodney W. Grimes 			 */
5293f1c4c4fSKonstantin Belousov 			if (!vm_page_count_severe() || P_KILLED(curproc)) {
530f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
5313d653db0SAlan Cox 				vm_object_color(fs.object, atop(vaddr) -
5323d653db0SAlan Cox 				    fs.pindex);
533f8a47341SAlan Cox #endif
5343f1c4c4fSKonstantin Belousov 				alloc_req = P_KILLED(curproc) ?
5353f1c4c4fSKonstantin Belousov 				    VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
5363f1c4c4fSKonstantin Belousov 				if (fs.object->type != OBJT_VNODE &&
5373f1c4c4fSKonstantin Belousov 				    fs.object->backing_object == NULL)
5383f1c4c4fSKonstantin Belousov 					alloc_req |= VM_ALLOC_ZERO;
5394866e085SJohn Dyson 				fs.m = vm_page_alloc(fs.object, fs.pindex,
5403f1c4c4fSKonstantin Belousov 				    alloc_req);
54140360b1bSMatthew Dillon 			}
5424866e085SJohn Dyson 			if (fs.m == NULL) {
5434866e085SJohn Dyson 				unlock_and_deallocate(&fs);
544ef6020d1SMike Silbersack 				VM_WAITPFAULT;
545df8bae1dSRodney W. Grimes 				goto RetryFault;
5460a2e596aSAlan Cox 			} else if (fs.m->valid == VM_PAGE_BITS_ALL)
5474ab8ab92SKonstantin Belousov 				break;
548df8bae1dSRodney W. Grimes 		}
54947221757SJohn Dyson 
5500d94caffSDavid Greenman readrest:
5511c7c3c6aSMatthew Dillon 		/*
55210b4196bSAlan Cox 		 * We have either allocated a new page or found an existing
55310b4196bSAlan Cox 		 * page that is only partially valid.
55440360b1bSMatthew Dillon 		 *
55540360b1bSMatthew Dillon 		 * Attempt to fault-in the page if there is a chance that the
55640360b1bSMatthew Dillon 		 * pager has it, and potentially fault in additional pages
55710b4196bSAlan Cox 		 * at the same time.
5581c7c3c6aSMatthew Dillon 		 */
5596a875bf9SKonstantin Belousov 		if (fs.object->type != OBJT_DEFAULT) {
560df8bae1dSRodney W. Grimes 			int rv;
5617f866e4bSAlan Cox 			u_char behavior = vm_map_entry_behavior(fs.entry);
562867a482dSJohn Dyson 
5635268042bSAlan Cox 			era = fs.entry->read_ahead;
5643f1c4c4fSKonstantin Belousov 			if (behavior == MAP_ENTRY_BEHAV_RANDOM ||
5653f1c4c4fSKonstantin Belousov 			    P_KILLED(curproc)) {
566867a482dSJohn Dyson 				behind = 0;
5675268042bSAlan Cox 				nera = 0;
56813458803SAlan Cox 				ahead = 0;
56913458803SAlan Cox 			} else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
57013458803SAlan Cox 				behind = 0;
5715268042bSAlan Cox 				nera = VM_FAULT_READ_AHEAD_MAX;
5725268042bSAlan Cox 				ahead = nera;
57313458803SAlan Cox 				if (fs.pindex == fs.entry->next_read)
574a8b0f100SAlan Cox 					vm_fault_dontneed(&fs, vaddr, ahead);
5755268042bSAlan Cox 			} else if (fs.pindex == fs.entry->next_read) {
57613458803SAlan Cox 				/*
5775268042bSAlan Cox 				 * This is a sequential fault.  Arithmetically
5785268042bSAlan Cox 				 * increase the requested number of pages in
5795268042bSAlan Cox 				 * the read-ahead window.  The requested
5805268042bSAlan Cox 				 * number of pages is "# of sequential faults
5815268042bSAlan Cox 				 * x (read ahead min + 1) + read ahead min"
58213458803SAlan Cox 				 */
5835268042bSAlan Cox 				behind = 0;
5845268042bSAlan Cox 				nera = VM_FAULT_READ_AHEAD_MIN;
5855268042bSAlan Cox 				if (era > 0) {
5865268042bSAlan Cox 					nera += era + 1;
58713458803SAlan Cox 					if (nera > VM_FAULT_READ_AHEAD_MAX)
58813458803SAlan Cox 						nera = VM_FAULT_READ_AHEAD_MAX;
5895268042bSAlan Cox 				}
59013458803SAlan Cox 				ahead = nera;
59113458803SAlan Cox 				if (era == VM_FAULT_READ_AHEAD_MAX)
592a8b0f100SAlan Cox 					vm_fault_dontneed(&fs, vaddr, ahead);
5935268042bSAlan Cox 			} else {
5945268042bSAlan Cox 				/*
5955268042bSAlan Cox 				 * This is a non-sequential fault.  Request a
5965268042bSAlan Cox 				 * cluster of pages that is aligned to a
5975268042bSAlan Cox 				 * VM_FAULT_READ_DEFAULT page offset boundary
5985268042bSAlan Cox 				 * within the object.  Alignment to a page
5995268042bSAlan Cox 				 * offset boundary is more likely to coincide
6005268042bSAlan Cox 				 * with the underlying file system block than
6015268042bSAlan Cox 				 * alignment to a virtual address boundary.
6025268042bSAlan Cox 				 */
6035268042bSAlan Cox 				cluster_offset = fs.pindex %
6045268042bSAlan Cox 				    VM_FAULT_READ_DEFAULT;
6055268042bSAlan Cox 				behind = ulmin(cluster_offset,
6065268042bSAlan Cox 				    atop(vaddr - fs.entry->start));
6075268042bSAlan Cox 				nera = 0;
6085268042bSAlan Cox 				ahead = VM_FAULT_READ_DEFAULT - 1 -
6095268042bSAlan Cox 				    cluster_offset;
610867a482dSJohn Dyson 			}
6115268042bSAlan Cox 			ahead = ulmin(ahead, atop(fs.entry->end - vaddr) - 1);
6125268042bSAlan Cox 			if (era != nera)
6135268042bSAlan Cox 				fs.entry->read_ahead = nera;
614d2bf64c3SKonstantin Belousov 
615d2bf64c3SKonstantin Belousov 			/*
616d2bf64c3SKonstantin Belousov 			 * Call the pager to retrieve the data, if any, after
617d2bf64c3SKonstantin Belousov 			 * releasing the lock on the map.  We hold a ref on
618c7aebda8SAttilio Rao 			 * fs.object and the pages are exclusive busied.
619d2bf64c3SKonstantin Belousov 			 */
620d2bf64c3SKonstantin Belousov 			unlock_map(&fs);
621d2bf64c3SKonstantin Belousov 
622d2bf64c3SKonstantin Belousov 			if (fs.object->type == OBJT_VNODE) {
623d2bf64c3SKonstantin Belousov 				vp = fs.object->handle;
624d2bf64c3SKonstantin Belousov 				if (vp == fs.vp)
625d2bf64c3SKonstantin Belousov 					goto vnode_locked;
626d2bf64c3SKonstantin Belousov 				else if (fs.vp != NULL) {
627d2bf64c3SKonstantin Belousov 					vput(fs.vp);
628d2bf64c3SKonstantin Belousov 					fs.vp = NULL;
629d2bf64c3SKonstantin Belousov 				}
630d2bf64c3SKonstantin Belousov 				locked = VOP_ISLOCKED(vp);
631d2bf64c3SKonstantin Belousov 
632d2bf64c3SKonstantin Belousov 				if (locked != LK_EXCLUSIVE)
633d2bf64c3SKonstantin Belousov 					locked = LK_SHARED;
634d2bf64c3SKonstantin Belousov 				/* Do not sleep for vnode lock while fs.m is busy */
635d2bf64c3SKonstantin Belousov 				error = vget(vp, locked | LK_CANRECURSE |
636d2bf64c3SKonstantin Belousov 				    LK_NOWAIT, curthread);
637d2bf64c3SKonstantin Belousov 				if (error != 0) {
638d2bf64c3SKonstantin Belousov 					vhold(vp);
639d2bf64c3SKonstantin Belousov 					release_page(&fs);
640d2bf64c3SKonstantin Belousov 					unlock_and_deallocate(&fs);
641d2bf64c3SKonstantin Belousov 					error = vget(vp, locked | LK_RETRY |
642d2bf64c3SKonstantin Belousov 					    LK_CANRECURSE, curthread);
643d2bf64c3SKonstantin Belousov 					vdrop(vp);
644d2bf64c3SKonstantin Belousov 					fs.vp = vp;
645d2bf64c3SKonstantin Belousov 					KASSERT(error == 0,
646d2bf64c3SKonstantin Belousov 					    ("vm_fault: vget failed"));
647d2bf64c3SKonstantin Belousov 					goto RetryFault;
648d2bf64c3SKonstantin Belousov 				}
649d2bf64c3SKonstantin Belousov 				fs.vp = vp;
650d2bf64c3SKonstantin Belousov 			}
651d2bf64c3SKonstantin Belousov vnode_locked:
652d2bf64c3SKonstantin Belousov 			KASSERT(fs.vp == NULL || !fs.map->system_map,
653d2bf64c3SKonstantin Belousov 			    ("vm_fault: vnode-backed object mapped by system map"));
654d2bf64c3SKonstantin Belousov 
655df8bae1dSRodney W. Grimes 			/*
656b0cd2017SGleb Smirnoff 			 * Page in the requested page and hint the pager,
657b0cd2017SGleb Smirnoff 			 * that it may bring up surrounding pages.
65826f9a767SRodney W. Grimes 			 */
659b0cd2017SGleb Smirnoff 			rv = vm_pager_get_pages(fs.object, &fs.m, 1,
660b0cd2017SGleb Smirnoff 			    &behind, &ahead);
66126f9a767SRodney W. Grimes 			if (rv == VM_PAGER_OK) {
662b0cd2017SGleb Smirnoff 				faultcount = behind + 1 + ahead;
66326f9a767SRodney W. Grimes 				hardfault++;
6641c7c3c6aSMatthew Dillon 				break; /* break to PAGE HAS BEEN FOUND */
665df8bae1dSRodney W. Grimes 			}
666a83c285cSDavid Greenman 			if (rv == VM_PAGER_ERROR)
667f3679e35SDavid Greenman 				printf("vm_fault: pager read error, pid %d (%s)\n",
668f3679e35SDavid Greenman 				    curproc->p_pid, curproc->p_comm);
669521ddf39SAlan Cox 
67026f9a767SRodney W. Grimes 			/*
671521ddf39SAlan Cox 			 * If an I/O error occurred or the requested page was
672521ddf39SAlan Cox 			 * outside the range of the pager, clean up and return
673521ddf39SAlan Cox 			 * an error.
67426f9a767SRodney W. Grimes 			 */
675521ddf39SAlan Cox 			if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) {
6762965a453SKip Macy 				vm_page_lock(fs.m);
6774866e085SJohn Dyson 				vm_page_free(fs.m);
6782965a453SKip Macy 				vm_page_unlock(fs.m);
6794866e085SJohn Dyson 				fs.m = NULL;
6804866e085SJohn Dyson 				unlock_and_deallocate(&fs);
681521ddf39SAlan Cox 				return (rv == VM_PAGER_ERROR ? KERN_FAILURE :
682521ddf39SAlan Cox 				    KERN_PROTECTION_FAILURE);
68326f9a767SRodney W. Grimes 			}
684521ddf39SAlan Cox 
685521ddf39SAlan Cox 			/*
686521ddf39SAlan Cox 			 * The requested page does not exist at this object/
687521ddf39SAlan Cox 			 * offset.  Remove the invalid page from the object,
688521ddf39SAlan Cox 			 * waking up anyone waiting for it, and continue on to
689521ddf39SAlan Cox 			 * the next object.  However, if this is the top-level
690521ddf39SAlan Cox 			 * object, we must leave the busy page in place to
691521ddf39SAlan Cox 			 * prevent another process from rushing past us, and
692521ddf39SAlan Cox 			 * inserting the page in that object at the same time
693521ddf39SAlan Cox 			 * that we are.
694521ddf39SAlan Cox 			 */
6954866e085SJohn Dyson 			if (fs.object != fs.first_object) {
6962965a453SKip Macy 				vm_page_lock(fs.m);
6974866e085SJohn Dyson 				vm_page_free(fs.m);
6982965a453SKip Macy 				vm_page_unlock(fs.m);
6994866e085SJohn Dyson 				fs.m = NULL;
700df8bae1dSRodney W. Grimes 			}
701df8bae1dSRodney W. Grimes 		}
70240360b1bSMatthew Dillon 
703df8bae1dSRodney W. Grimes 		/*
7041c7c3c6aSMatthew Dillon 		 * We get here if the object has default pager (or unwiring)
7051c7c3c6aSMatthew Dillon 		 * or the pager doesn't have the page.
706df8bae1dSRodney W. Grimes 		 */
7074866e085SJohn Dyson 		if (fs.object == fs.first_object)
7084866e085SJohn Dyson 			fs.first_m = fs.m;
709df8bae1dSRodney W. Grimes 
710df8bae1dSRodney W. Grimes 		/*
7110d94caffSDavid Greenman 		 * Move on to the next object.  Lock the next object before
7120d94caffSDavid Greenman 		 * unlocking the current one.
713df8bae1dSRodney W. Grimes 		 */
7144866e085SJohn Dyson 		next_object = fs.object->backing_object;
715df8bae1dSRodney W. Grimes 		if (next_object == NULL) {
716df8bae1dSRodney W. Grimes 			/*
7170d94caffSDavid Greenman 			 * If there's no object left, fill the page in the top
7180d94caffSDavid Greenman 			 * object with zeros.
719df8bae1dSRodney W. Grimes 			 */
7204866e085SJohn Dyson 			if (fs.object != fs.first_object) {
7214866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
72289f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(fs.object);
723df8bae1dSRodney W. Grimes 
7244866e085SJohn Dyson 				fs.object = fs.first_object;
7254866e085SJohn Dyson 				fs.pindex = fs.first_pindex;
7264866e085SJohn Dyson 				fs.m = fs.first_m;
72789f6b863SAttilio Rao 				VM_OBJECT_WLOCK(fs.object);
728df8bae1dSRodney W. Grimes 			}
7294866e085SJohn Dyson 			fs.first_m = NULL;
730df8bae1dSRodney W. Grimes 
7314221e284SAlan Cox 			/*
7324221e284SAlan Cox 			 * Zero the page if necessary and mark it valid.
7334221e284SAlan Cox 			 */
7344866e085SJohn Dyson 			if ((fs.m->flags & PG_ZERO) == 0) {
735fff6062aSAlan Cox 				pmap_zero_page(fs.m);
7364221e284SAlan Cox 			} else {
73767596082SAttilio Rao 				PCPU_INC(cnt.v_ozfod);
7384221e284SAlan Cox 			}
73967596082SAttilio Rao 			PCPU_INC(cnt.v_zfod);
7404221e284SAlan Cox 			fs.m->valid = VM_PAGE_BITS_ALL;
7417b9b301cSAlan Cox 			/* Don't try to prefault neighboring pages. */
7427b9b301cSAlan Cox 			faultcount = 1;
7431c7c3c6aSMatthew Dillon 			break;	/* break to PAGE HAS BEEN FOUND */
7440d94caffSDavid Greenman 		} else {
745c8567c3aSAlan Cox 			KASSERT(fs.object != next_object,
746c8567c3aSAlan Cox 			    ("object loop %p", next_object));
74789f6b863SAttilio Rao 			VM_OBJECT_WLOCK(next_object);
748c8567c3aSAlan Cox 			vm_object_pip_add(next_object, 1);
749c8567c3aSAlan Cox 			if (fs.object != fs.first_object)
7504866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
7516753423cSAlan Cox 			fs.pindex +=
7526753423cSAlan Cox 			    OFF_TO_IDX(fs.object->backing_object_offset);
75389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(fs.object);
7544866e085SJohn Dyson 			fs.object = next_object;
755df8bae1dSRodney W. Grimes 		}
756df8bae1dSRodney W. Grimes 	}
7571c7c3c6aSMatthew Dillon 
758c7aebda8SAttilio Rao 	vm_page_assert_xbusied(fs.m);
759df8bae1dSRodney W. Grimes 
760df8bae1dSRodney W. Grimes 	/*
7610d94caffSDavid Greenman 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
762df8bae1dSRodney W. Grimes 	 * is held.]
763df8bae1dSRodney W. Grimes 	 */
764df8bae1dSRodney W. Grimes 
765df8bae1dSRodney W. Grimes 	/*
7660d94caffSDavid Greenman 	 * If the page is being written, but isn't already owned by the
7670d94caffSDavid Greenman 	 * top-level object, we have to copy it into a new page owned by the
7680d94caffSDavid Greenman 	 * top-level object.
769df8bae1dSRodney W. Grimes 	 */
7704866e085SJohn Dyson 	if (fs.object != fs.first_object) {
771df8bae1dSRodney W. Grimes 		/*
7720d94caffSDavid Greenman 		 * We only really need to copy if we want to write it.
773df8bae1dSRodney W. Grimes 		 */
774a6d42a0dSAlan Cox 		if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
775df8bae1dSRodney W. Grimes 			/*
7761c7c3c6aSMatthew Dillon 			 * This allows pages to be virtually copied from a
7771c7c3c6aSMatthew Dillon 			 * backing_object into the first_object, where the
7781c7c3c6aSMatthew Dillon 			 * backing object has no other refs to it, and cannot
7791c7c3c6aSMatthew Dillon 			 * gain any more refs.  Instead of a bcopy, we just
7801c7c3c6aSMatthew Dillon 			 * move the page from the backing object to the
7811c7c3c6aSMatthew Dillon 			 * first object.  Note that we must mark the page
7821c7c3c6aSMatthew Dillon 			 * dirty in the first object so that it will go out
7831c7c3c6aSMatthew Dillon 			 * to swap when needed.
784df8bae1dSRodney W. Grimes 			 */
785ebf75125SAlan Cox 			is_first_object_locked = FALSE;
786e50346b5SAlan Cox 			if (
787de5f6a77SJohn Dyson 				/*
788de5f6a77SJohn Dyson 				 * Only one shadow object
789de5f6a77SJohn Dyson 				 */
7904866e085SJohn Dyson 				(fs.object->shadow_count == 1) &&
791de5f6a77SJohn Dyson 				/*
792de5f6a77SJohn Dyson 				 * No COW refs, except us
793de5f6a77SJohn Dyson 				 */
7944866e085SJohn Dyson 				(fs.object->ref_count == 1) &&
795de5f6a77SJohn Dyson 				/*
7965929bcfaSPhilippe Charnier 				 * No one else can look this object up
797de5f6a77SJohn Dyson 				 */
7984866e085SJohn Dyson 				(fs.object->handle == NULL) &&
799de5f6a77SJohn Dyson 				/*
800de5f6a77SJohn Dyson 				 * No other ways to look the object up
801de5f6a77SJohn Dyson 				 */
8024866e085SJohn Dyson 				((fs.object->type == OBJT_DEFAULT) ||
8034866e085SJohn Dyson 				 (fs.object->type == OBJT_SWAP)) &&
80489f6b863SAttilio Rao 			    (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
805de5f6a77SJohn Dyson 				/*
806de5f6a77SJohn Dyson 				 * We don't chase down the shadow chain
807de5f6a77SJohn Dyson 				 */
808e50346b5SAlan Cox 			    fs.object == fs.first_object->backing_object) {
809bccdea45SAlan Cox 				vm_page_lock(fs.m);
810bccdea45SAlan Cox 				vm_page_remove(fs.m);
811bccdea45SAlan Cox 				vm_page_unlock(fs.m);
812eb00b276SAlan Cox 				vm_page_lock(fs.first_m);
813bccdea45SAlan Cox 				vm_page_replace_checked(fs.m, fs.first_object,
814bccdea45SAlan Cox 				    fs.first_pindex, fs.first_m);
8156fee422eSConrad Meyer 				vm_page_free(fs.first_m);
8166fee422eSConrad Meyer 				vm_page_unlock(fs.first_m);
817bccdea45SAlan Cox 				vm_page_dirty(fs.m);
818dfdf9abdSAlan Cox #if VM_NRESERVLEVEL > 0
819dfdf9abdSAlan Cox 				/*
820dfdf9abdSAlan Cox 				 * Rename the reservation.
821dfdf9abdSAlan Cox 				 */
822dfdf9abdSAlan Cox 				vm_reserv_rename(fs.m, fs.first_object,
823dfdf9abdSAlan Cox 				    fs.object, OFF_TO_IDX(
824dfdf9abdSAlan Cox 				    fs.first_object->backing_object_offset));
825dfdf9abdSAlan Cox #endif
826bccdea45SAlan Cox 				/*
827bccdea45SAlan Cox 				 * Removing the page from the backing object
828bccdea45SAlan Cox 				 * unbusied it.
829bccdea45SAlan Cox 				 */
830c7aebda8SAttilio Rao 				vm_page_xbusy(fs.m);
831d98ddc46SAlan Cox 				fs.first_m = fs.m;
8324866e085SJohn Dyson 				fs.m = NULL;
83367596082SAttilio Rao 				PCPU_INC(cnt.v_cow_optim);
834de5f6a77SJohn Dyson 			} else {
835de5f6a77SJohn Dyson 				/*
836de5f6a77SJohn Dyson 				 * Oh, well, lets copy it.
837de5f6a77SJohn Dyson 				 */
838669890eaSAlan Cox 				pmap_copy_page(fs.m, fs.first_m);
839669890eaSAlan Cox 				fs.first_m->valid = VM_PAGE_BITS_ALL;
840d8778512SAlan Cox 				if (wired && (fault_flags &
8416a875bf9SKonstantin Belousov 				    VM_FAULT_WIRE) == 0) {
8422965a453SKip Macy 					vm_page_lock(fs.first_m);
843d8778512SAlan Cox 					vm_page_wire(fs.first_m);
8442965a453SKip Macy 					vm_page_unlock(fs.first_m);
8452965a453SKip Macy 
8462965a453SKip Macy 					vm_page_lock(fs.m);
8473ae10f74SAttilio Rao 					vm_page_unwire(fs.m, PQ_INACTIVE);
8482965a453SKip Macy 					vm_page_unlock(fs.m);
849de5f6a77SJohn Dyson 				}
850df8bae1dSRodney W. Grimes 				/*
851df8bae1dSRodney W. Grimes 				 * We no longer need the old page or object.
852df8bae1dSRodney W. Grimes 				 */
8534866e085SJohn Dyson 				release_page(&fs);
854de5f6a77SJohn Dyson 			}
8551c7c3c6aSMatthew Dillon 			/*
8561c7c3c6aSMatthew Dillon 			 * fs.object != fs.first_object due to above
8571c7c3c6aSMatthew Dillon 			 * conditional
8581c7c3c6aSMatthew Dillon 			 */
8594866e085SJohn Dyson 			vm_object_pip_wakeup(fs.object);
86089f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(fs.object);
861df8bae1dSRodney W. Grimes 			/*
862df8bae1dSRodney W. Grimes 			 * Only use the new page below...
863df8bae1dSRodney W. Grimes 			 */
8644866e085SJohn Dyson 			fs.object = fs.first_object;
8654866e085SJohn Dyson 			fs.pindex = fs.first_pindex;
866d98ddc46SAlan Cox 			fs.m = fs.first_m;
867f29ba63eSAlan Cox 			if (!is_first_object_locked)
86889f6b863SAttilio Rao 				VM_OBJECT_WLOCK(fs.object);
86967596082SAttilio Rao 			PCPU_INC(cnt.v_cow_faults);
8704d34e019SKonstantin Belousov 			curthread->td_cow++;
8710d94caffSDavid Greenman 		} else {
872df8bae1dSRodney W. Grimes 			prot &= ~VM_PROT_WRITE;
873df8bae1dSRodney W. Grimes 		}
874df8bae1dSRodney W. Grimes 	}
875df8bae1dSRodney W. Grimes 
876df8bae1dSRodney W. Grimes 	/*
8770d94caffSDavid Greenman 	 * We must verify that the maps have not changed since our last
8780d94caffSDavid Greenman 	 * lookup.
879df8bae1dSRodney W. Grimes 	 */
88019dc5607STor Egge 	if (!fs.lookup_still_valid) {
881df8bae1dSRodney W. Grimes 		vm_object_t retry_object;
882a316d390SJohn Dyson 		vm_pindex_t retry_pindex;
883df8bae1dSRodney W. Grimes 		vm_prot_t retry_prot;
884df8bae1dSRodney W. Grimes 
88519dc5607STor Egge 		if (!vm_map_trylock_read(fs.map)) {
886b823bbd6SMatthew Dillon 			release_page(&fs);
887b823bbd6SMatthew Dillon 			unlock_and_deallocate(&fs);
888b823bbd6SMatthew Dillon 			goto RetryFault;
889b823bbd6SMatthew Dillon 		}
89019dc5607STor Egge 		fs.lookup_still_valid = TRUE;
89119dc5607STor Egge 		if (fs.map->timestamp != map_generation) {
89219dc5607STor Egge 			result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
8934866e085SJohn Dyson 			    &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
894df8bae1dSRodney W. Grimes 
895df8bae1dSRodney W. Grimes 			/*
89644ed3417STor Egge 			 * If we don't need the page any longer, put it on the inactive
8970d94caffSDavid Greenman 			 * list (the easiest thing to do here).  If no one needs it,
8980d94caffSDavid Greenman 			 * pageout will grab it eventually.
899df8bae1dSRodney W. Grimes 			 */
900df8bae1dSRodney W. Grimes 			if (result != KERN_SUCCESS) {
9014866e085SJohn Dyson 				release_page(&fs);
9024866e085SJohn Dyson 				unlock_and_deallocate(&fs);
90319dc5607STor Egge 
90419dc5607STor Egge 				/*
90519dc5607STor Egge 				 * If retry of map lookup would have blocked then
90619dc5607STor Egge 				 * retry fault from start.
90719dc5607STor Egge 				 */
90819dc5607STor Egge 				if (result == KERN_FAILURE)
90919dc5607STor Egge 					goto RetryFault;
910df8bae1dSRodney W. Grimes 				return (result);
911df8bae1dSRodney W. Grimes 			}
9124866e085SJohn Dyson 			if ((retry_object != fs.first_object) ||
9134866e085SJohn Dyson 			    (retry_pindex != fs.first_pindex)) {
9144866e085SJohn Dyson 				release_page(&fs);
9154866e085SJohn Dyson 				unlock_and_deallocate(&fs);
916df8bae1dSRodney W. Grimes 				goto RetryFault;
917df8bae1dSRodney W. Grimes 			}
91819dc5607STor Egge 
919df8bae1dSRodney W. Grimes 			/*
9200d94caffSDavid Greenman 			 * Check whether the protection has changed or the object has
9210d94caffSDavid Greenman 			 * been copied while we left the map unlocked. Changing from
9220d94caffSDavid Greenman 			 * read to write permission is OK - we leave the page
9230d94caffSDavid Greenman 			 * write-protected, and catch the write fault. Changing from
9240d94caffSDavid Greenman 			 * write to read permission means that we can't mark the page
9250d94caffSDavid Greenman 			 * write-enabled after all.
926df8bae1dSRodney W. Grimes 			 */
927df8bae1dSRodney W. Grimes 			prot &= retry_prot;
928df8bae1dSRodney W. Grimes 		}
92919dc5607STor Egge 	}
930d2bf64c3SKonstantin Belousov 	/*
9315758fe71SAlan Cox 	 * If the page was filled by a pager, update the map entry's
932b0cd2017SGleb Smirnoff 	 * last read offset.
933d2bf64c3SKonstantin Belousov 	 *
934d2bf64c3SKonstantin Belousov 	 * XXX The following assignment modifies the map
935d2bf64c3SKonstantin Belousov 	 * without holding a write lock on it.
936d2bf64c3SKonstantin Belousov 	 */
9375758fe71SAlan Cox 	if (hardfault)
938b0cd2017SGleb Smirnoff 		fs.entry->next_read = fs.pindex + ahead + 1;
939d2bf64c3SKonstantin Belousov 
940a36f5532SKonstantin Belousov 	vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags, TRUE);
941c7aebda8SAttilio Rao 	vm_page_assert_xbusied(fs.m);
942c7aebda8SAttilio Rao 
9434221e284SAlan Cox 	/*
94478cfe1f7SAlan Cox 	 * Page must be completely valid or it is not fit to
9454221e284SAlan Cox 	 * map into user space.  vm_pager_get_pages() ensures this.
9464221e284SAlan Cox 	 */
94778cfe1f7SAlan Cox 	KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
94878cfe1f7SAlan Cox 	    ("vm_fault: page %p partially invalid", fs.m));
94989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(fs.object);
950cbfbaad8SAlan Cox 
95186735996SAlan Cox 	/*
95286735996SAlan Cox 	 * Put this page into the physical map.  We had to do the unlock above
95386735996SAlan Cox 	 * because pmap_enter() may sleep.  We don't put the page
95486735996SAlan Cox 	 * back on the active queue until later so that the pageout daemon
95586735996SAlan Cox 	 * won't find it (yet).
95686735996SAlan Cox 	 */
95739ffa8c1SKonstantin Belousov 	pmap_enter(fs.map->pmap, vaddr, fs.m, prot,
95839ffa8c1SKonstantin Belousov 	    fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0);
9596a875bf9SKonstantin Belousov 	if (faultcount != 1 && (fault_flags & VM_FAULT_WIRE) == 0 &&
9607b9b301cSAlan Cox 	    wired == 0)
961b0cd2017SGleb Smirnoff 		vm_fault_prefault(&fs, vaddr,
962b0cd2017SGleb Smirnoff 		    faultcount > 0 ? behind : PFBAK,
963b0cd2017SGleb Smirnoff 		    faultcount > 0 ? ahead : PFFOR);
96489f6b863SAttilio Rao 	VM_OBJECT_WLOCK(fs.object);
9652965a453SKip Macy 	vm_page_lock(fs.m);
966ff97964aSJohn Dyson 
967df8bae1dSRodney W. Grimes 	/*
9680d94caffSDavid Greenman 	 * If the page is not wired down, then put it where the pageout daemon
9690d94caffSDavid Greenman 	 * can find it.
970df8bae1dSRodney W. Grimes 	 */
9716a875bf9SKonstantin Belousov 	if ((fault_flags & VM_FAULT_WIRE) != 0) {
9726a875bf9SKonstantin Belousov 		KASSERT(wired, ("VM_FAULT_WIRE && !wired"));
9734866e085SJohn Dyson 		vm_page_wire(fs.m);
97403679e23SAlan Cox 	} else
9754866e085SJohn Dyson 		vm_page_activate(fs.m);
976acd11c74SAlan Cox 	if (m_hold != NULL) {
977acd11c74SAlan Cox 		*m_hold = fs.m;
978acd11c74SAlan Cox 		vm_page_hold(fs.m);
979acd11c74SAlan Cox 	}
9802965a453SKip Macy 	vm_page_unlock(fs.m);
981c7aebda8SAttilio Rao 	vm_page_xunbusy(fs.m);
982eeec6babSJohn Baldwin 
983eebf3286SAlan Cox 	/*
984eebf3286SAlan Cox 	 * Unlock everything, and return
985eebf3286SAlan Cox 	 */
986eebf3286SAlan Cox 	unlock_and_deallocate(&fs);
987b3a01bdfSAndrey Zonov 	if (hardfault) {
988b3a01bdfSAndrey Zonov 		PCPU_INC(cnt.v_io_faults);
9891c4bcd05SJeff Roberson 		curthread->td_ru.ru_majflt++;
990ae34b6ffSEdward Tomasz Napierala #ifdef RACCT
991ae34b6ffSEdward Tomasz Napierala 		if (racct_enable && fs.object->type == OBJT_VNODE) {
992ae34b6ffSEdward Tomasz Napierala 			PROC_LOCK(curproc);
993ae34b6ffSEdward Tomasz Napierala 			if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
994ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_WRITEBPS,
995ae34b6ffSEdward Tomasz Napierala 				    PAGE_SIZE + behind * PAGE_SIZE);
996ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_WRITEIOPS, 1);
997ae34b6ffSEdward Tomasz Napierala 			} else {
998ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_READBPS,
999ae34b6ffSEdward Tomasz Napierala 				    PAGE_SIZE + ahead * PAGE_SIZE);
1000ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_READIOPS, 1);
1001ae34b6ffSEdward Tomasz Napierala 			}
1002ae34b6ffSEdward Tomasz Napierala 			PROC_UNLOCK(curproc);
1003ae34b6ffSEdward Tomasz Napierala 		}
1004ae34b6ffSEdward Tomasz Napierala #endif
1005b3a01bdfSAndrey Zonov 	} else
10061c4bcd05SJeff Roberson 		curthread->td_ru.ru_minflt++;
1007df8bae1dSRodney W. Grimes 
1008df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1009df8bae1dSRodney W. Grimes }
1010df8bae1dSRodney W. Grimes 
1011df8bae1dSRodney W. Grimes /*
1012a8b0f100SAlan Cox  * Speed up the reclamation of pages that precede the faulting pindex within
1013a8b0f100SAlan Cox  * the first object of the shadow chain.  Essentially, perform the equivalent
1014a8b0f100SAlan Cox  * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes
1015a8b0f100SAlan Cox  * the faulting pindex by the cluster size when the pages read by vm_fault()
1016a8b0f100SAlan Cox  * cross a cluster-size boundary.  The cluster size is the greater of the
1017a8b0f100SAlan Cox  * smallest superpage size and VM_FAULT_DONTNEED_MIN.
1018a8b0f100SAlan Cox  *
1019a8b0f100SAlan Cox  * When "fs->first_object" is a shadow object, the pages in the backing object
1020a8b0f100SAlan Cox  * that precede the faulting pindex are deactivated by vm_fault().  So, this
1021a8b0f100SAlan Cox  * function must only be concerned with pages in the first object.
102213458803SAlan Cox  */
102313458803SAlan Cox static void
1024a8b0f100SAlan Cox vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead)
102513458803SAlan Cox {
1026a8b0f100SAlan Cox 	vm_map_entry_t entry;
102713458803SAlan Cox 	vm_object_t first_object, object;
1028a8b0f100SAlan Cox 	vm_offset_t end, start;
1029a8b0f100SAlan Cox 	vm_page_t m, m_next;
1030a8b0f100SAlan Cox 	vm_pindex_t pend, pstart;
1031a8b0f100SAlan Cox 	vm_size_t size;
103213458803SAlan Cox 
103313458803SAlan Cox 	object = fs->object;
103489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
103513458803SAlan Cox 	first_object = fs->first_object;
103613458803SAlan Cox 	if (first_object != object) {
1037b5ab20c0SAlan Cox 		if (!VM_OBJECT_TRYWLOCK(first_object)) {
103889f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
1039b5ab20c0SAlan Cox 			VM_OBJECT_WLOCK(first_object);
104089f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
104113458803SAlan Cox 		}
104213458803SAlan Cox 	}
1043a8b0f100SAlan Cox 	/* Neither fictitious nor unmanaged pages can be reclaimed. */
104428634820SAlan Cox 	if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
1045a8b0f100SAlan Cox 		size = VM_FAULT_DONTNEED_MIN;
1046a8b0f100SAlan Cox 		if (MAXPAGESIZES > 1 && size < pagesizes[1])
1047a8b0f100SAlan Cox 			size = pagesizes[1];
1048a8b0f100SAlan Cox 		end = rounddown2(vaddr, size);
1049a8b0f100SAlan Cox 		if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) &&
1050a8b0f100SAlan Cox 		    (entry = fs->entry)->start < end) {
1051a8b0f100SAlan Cox 			if (end - entry->start < size)
1052a8b0f100SAlan Cox 				start = entry->start;
105313458803SAlan Cox 			else
1054a8b0f100SAlan Cox 				start = end - size;
1055a8b0f100SAlan Cox 			pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED);
1056a8b0f100SAlan Cox 			pstart = OFF_TO_IDX(entry->offset) + atop(start -
1057a8b0f100SAlan Cox 			    entry->start);
1058a8b0f100SAlan Cox 			m_next = vm_page_find_least(first_object, pstart);
1059a8b0f100SAlan Cox 			pend = OFF_TO_IDX(entry->offset) + atop(end -
1060a8b0f100SAlan Cox 			    entry->start);
1061a8b0f100SAlan Cox 			while ((m = m_next) != NULL && m->pindex < pend) {
1062a8b0f100SAlan Cox 				m_next = TAILQ_NEXT(m, listq);
1063a8b0f100SAlan Cox 				if (m->valid != VM_PAGE_BITS_ALL ||
1064a8b0f100SAlan Cox 				    vm_page_busied(m))
106513458803SAlan Cox 					continue;
1066d8015db3SAlan Cox 
1067d8015db3SAlan Cox 				/*
1068d8015db3SAlan Cox 				 * Don't clear PGA_REFERENCED, since it would
1069d8015db3SAlan Cox 				 * likely represent a reference by a different
1070d8015db3SAlan Cox 				 * process.
1071d8015db3SAlan Cox 				 *
1072d8015db3SAlan Cox 				 * Typically, at this point, prefetched pages
1073d8015db3SAlan Cox 				 * are still in the inactive queue.  Only
1074d8015db3SAlan Cox 				 * pages that triggered page faults are in the
1075d8015db3SAlan Cox 				 * active queue.
1076d8015db3SAlan Cox 				 */
107713458803SAlan Cox 				vm_page_lock(m);
1078d8015db3SAlan Cox 				vm_page_deactivate(m);
107913458803SAlan Cox 				vm_page_unlock(m);
108013458803SAlan Cox 			}
108113458803SAlan Cox 		}
1082a8b0f100SAlan Cox 	}
108313458803SAlan Cox 	if (first_object != object)
1084b5ab20c0SAlan Cox 		VM_OBJECT_WUNLOCK(first_object);
108513458803SAlan Cox }
108613458803SAlan Cox 
108713458803SAlan Cox /*
1088566526a9SAlan Cox  * vm_fault_prefault provides a quick way of clustering
1089566526a9SAlan Cox  * pagefaults into a processes address space.  It is a "cousin"
1090566526a9SAlan Cox  * of vm_map_pmap_enter, except it runs at page fault time instead
1091566526a9SAlan Cox  * of mmap time.
1092566526a9SAlan Cox  */
1093566526a9SAlan Cox static void
109463281952SAlan Cox vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
1095b0cd2017SGleb Smirnoff     int backward, int forward)
1096566526a9SAlan Cox {
109763281952SAlan Cox 	pmap_t pmap;
109863281952SAlan Cox 	vm_map_entry_t entry;
109963281952SAlan Cox 	vm_object_t backing_object, lobject;
1100566526a9SAlan Cox 	vm_offset_t addr, starta;
1101566526a9SAlan Cox 	vm_pindex_t pindex;
11022053c127SStephan Uphoff 	vm_page_t m;
1103b0cd2017SGleb Smirnoff 	int i;
1104566526a9SAlan Cox 
110563281952SAlan Cox 	pmap = fs->map->pmap;
1106950d5f7aSAlan Cox 	if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1107566526a9SAlan Cox 		return;
1108566526a9SAlan Cox 
110963281952SAlan Cox 	entry = fs->entry;
1110566526a9SAlan Cox 
111163281952SAlan Cox 	starta = addra - backward * PAGE_SIZE;
1112566526a9SAlan Cox 	if (starta < entry->start) {
1113566526a9SAlan Cox 		starta = entry->start;
1114566526a9SAlan Cox 	} else if (starta > addra) {
1115566526a9SAlan Cox 		starta = 0;
1116566526a9SAlan Cox 	}
1117566526a9SAlan Cox 
111863281952SAlan Cox 	/*
111963281952SAlan Cox 	 * Generate the sequence of virtual addresses that are candidates for
112063281952SAlan Cox 	 * prefaulting in an outward spiral from the faulting virtual address,
112163281952SAlan Cox 	 * "addra".  Specifically, the sequence is "addra - PAGE_SIZE", "addra
112263281952SAlan Cox 	 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
112363281952SAlan Cox 	 * If the candidate address doesn't have a backing physical page, then
112463281952SAlan Cox 	 * the loop immediately terminates.
112563281952SAlan Cox 	 */
112663281952SAlan Cox 	for (i = 0; i < 2 * imax(backward, forward); i++) {
112763281952SAlan Cox 		addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
112863281952SAlan Cox 		    PAGE_SIZE);
112963281952SAlan Cox 		if (addr > addra + forward * PAGE_SIZE)
1130566526a9SAlan Cox 			addr = 0;
1131566526a9SAlan Cox 
1132566526a9SAlan Cox 		if (addr < starta || addr >= entry->end)
1133566526a9SAlan Cox 			continue;
1134566526a9SAlan Cox 
1135566526a9SAlan Cox 		if (!pmap_is_prefaultable(pmap, addr))
1136566526a9SAlan Cox 			continue;
1137566526a9SAlan Cox 
1138566526a9SAlan Cox 		pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
113963281952SAlan Cox 		lobject = entry->object.vm_object;
1140c141ae7fSAlan Cox 		VM_OBJECT_RLOCK(lobject);
1141566526a9SAlan Cox 		while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1142566526a9SAlan Cox 		    lobject->type == OBJT_DEFAULT &&
1143566526a9SAlan Cox 		    (backing_object = lobject->backing_object) != NULL) {
114436930fc9SAlan Cox 			KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
114536930fc9SAlan Cox 			    0, ("vm_fault_prefault: unaligned object offset"));
1146566526a9SAlan Cox 			pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1147c141ae7fSAlan Cox 			VM_OBJECT_RLOCK(backing_object);
1148c141ae7fSAlan Cox 			VM_OBJECT_RUNLOCK(lobject);
1149566526a9SAlan Cox 			lobject = backing_object;
1150566526a9SAlan Cox 		}
1151cbfbaad8SAlan Cox 		if (m == NULL) {
1152c141ae7fSAlan Cox 			VM_OBJECT_RUNLOCK(lobject);
1153566526a9SAlan Cox 			break;
1154cbfbaad8SAlan Cox 		}
11550a2e596aSAlan Cox 		if (m->valid == VM_PAGE_BITS_ALL &&
11563c4a2440SAlan Cox 		    (m->flags & PG_FICTITIOUS) == 0)
11577bfda801SAlan Cox 			pmap_enter_quick(pmap, addr, m, entry->protection);
1158c141ae7fSAlan Cox 		VM_OBJECT_RUNLOCK(lobject);
1159566526a9SAlan Cox 	}
1160566526a9SAlan Cox }
1161566526a9SAlan Cox 
1162566526a9SAlan Cox /*
116382de724fSAlan Cox  * Hold each of the physical pages that are mapped by the specified range of
116482de724fSAlan Cox  * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
116582de724fSAlan Cox  * and allow the specified types of access, "prot".  If all of the implied
116682de724fSAlan Cox  * pages are successfully held, then the number of held pages is returned
116782de724fSAlan Cox  * together with pointers to those pages in the array "ma".  However, if any
116882de724fSAlan Cox  * of the pages cannot be held, -1 is returned.
116982de724fSAlan Cox  */
117082de724fSAlan Cox int
117182de724fSAlan Cox vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
117282de724fSAlan Cox     vm_prot_t prot, vm_page_t *ma, int max_count)
117382de724fSAlan Cox {
117482de724fSAlan Cox 	vm_offset_t end, va;
117582de724fSAlan Cox 	vm_page_t *mp;
11767e14088dSKonstantin Belousov 	int count;
117782de724fSAlan Cox 	boolean_t pmap_failed;
117882de724fSAlan Cox 
1179af32c419SKonstantin Belousov 	if (len == 0)
1180af32c419SKonstantin Belousov 		return (0);
118182de724fSAlan Cox 	end = round_page(addr + len);
118282de724fSAlan Cox 	addr = trunc_page(addr);
118382de724fSAlan Cox 
118482de724fSAlan Cox 	/*
118582de724fSAlan Cox 	 * Check for illegal addresses.
118682de724fSAlan Cox 	 */
118782de724fSAlan Cox 	if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
118882de724fSAlan Cox 		return (-1);
118982de724fSAlan Cox 
11907e14088dSKonstantin Belousov 	if (atop(end - addr) > max_count)
119182de724fSAlan Cox 		panic("vm_fault_quick_hold_pages: count > max_count");
11927e14088dSKonstantin Belousov 	count = atop(end - addr);
119382de724fSAlan Cox 
119482de724fSAlan Cox 	/*
119582de724fSAlan Cox 	 * Most likely, the physical pages are resident in the pmap, so it is
119682de724fSAlan Cox 	 * faster to try pmap_extract_and_hold() first.
119782de724fSAlan Cox 	 */
119882de724fSAlan Cox 	pmap_failed = FALSE;
119982de724fSAlan Cox 	for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
120082de724fSAlan Cox 		*mp = pmap_extract_and_hold(map->pmap, va, prot);
120182de724fSAlan Cox 		if (*mp == NULL)
120282de724fSAlan Cox 			pmap_failed = TRUE;
120382de724fSAlan Cox 		else if ((prot & VM_PROT_WRITE) != 0 &&
1204a5dbab54SAlan Cox 		    (*mp)->dirty != VM_PAGE_BITS_ALL) {
120582de724fSAlan Cox 			/*
120682de724fSAlan Cox 			 * Explicitly dirty the physical page.  Otherwise, the
120782de724fSAlan Cox 			 * caller's changes may go unnoticed because they are
120882de724fSAlan Cox 			 * performed through an unmanaged mapping or by a DMA
120982de724fSAlan Cox 			 * operation.
12103c76db4cSAlan Cox 			 *
1211abb9b935SKonstantin Belousov 			 * The object lock is not held here.
1212abb9b935SKonstantin Belousov 			 * See vm_page_clear_dirty_mask().
121382de724fSAlan Cox 			 */
12143c76db4cSAlan Cox 			vm_page_dirty(*mp);
121582de724fSAlan Cox 		}
121682de724fSAlan Cox 	}
121782de724fSAlan Cox 	if (pmap_failed) {
121882de724fSAlan Cox 		/*
121982de724fSAlan Cox 		 * One or more pages could not be held by the pmap.  Either no
122082de724fSAlan Cox 		 * page was mapped at the specified virtual address or that
122182de724fSAlan Cox 		 * mapping had insufficient permissions.  Attempt to fault in
122282de724fSAlan Cox 		 * and hold these pages.
122382de724fSAlan Cox 		 */
122482de724fSAlan Cox 		for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1225be996836SAttilio Rao 			if (*mp == NULL && vm_fault_hold(map, va, prot,
122682de724fSAlan Cox 			    VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
122782de724fSAlan Cox 				goto error;
122882de724fSAlan Cox 	}
122982de724fSAlan Cox 	return (count);
123082de724fSAlan Cox error:
123182de724fSAlan Cox 	for (mp = ma; mp < ma + count; mp++)
123282de724fSAlan Cox 		if (*mp != NULL) {
123382de724fSAlan Cox 			vm_page_lock(*mp);
123482de724fSAlan Cox 			vm_page_unhold(*mp);
123582de724fSAlan Cox 			vm_page_unlock(*mp);
123682de724fSAlan Cox 		}
123782de724fSAlan Cox 	return (-1);
123882de724fSAlan Cox }
123982de724fSAlan Cox 
124082de724fSAlan Cox /*
1241df8bae1dSRodney W. Grimes  *	Routine:
1242df8bae1dSRodney W. Grimes  *		vm_fault_copy_entry
1243df8bae1dSRodney W. Grimes  *	Function:
1244210a6886SKonstantin Belousov  *		Create new shadow object backing dst_entry with private copy of
1245210a6886SKonstantin Belousov  *		all underlying pages. When src_entry is equal to dst_entry,
1246210a6886SKonstantin Belousov  *		function implements COW for wired-down map entry. Otherwise,
1247210a6886SKonstantin Belousov  *		it forks wired entry into dst_map.
1248df8bae1dSRodney W. Grimes  *
1249df8bae1dSRodney W. Grimes  *	In/out conditions:
1250df8bae1dSRodney W. Grimes  *		The source and destination maps must be locked for write.
1251df8bae1dSRodney W. Grimes  *		The source map entry must be wired down (or be a sharing map
1252df8bae1dSRodney W. Grimes  *		entry corresponding to a main map entry that is wired down).
1253df8bae1dSRodney W. Grimes  */
125426f9a767SRodney W. Grimes void
1255121fd461SKonstantin Belousov vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1256121fd461SKonstantin Belousov     vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1257121fd461SKonstantin Belousov     vm_ooffset_t *fork_charge)
1258df8bae1dSRodney W. Grimes {
1259210a6886SKonstantin Belousov 	vm_object_t backing_object, dst_object, object, src_object;
12607afab86cSAlan Cox 	vm_pindex_t dst_pindex, pindex, src_pindex;
1261210a6886SKonstantin Belousov 	vm_prot_t access, prot;
1262df8bae1dSRodney W. Grimes 	vm_offset_t vaddr;
1263df8bae1dSRodney W. Grimes 	vm_page_t dst_m;
1264df8bae1dSRodney W. Grimes 	vm_page_t src_m;
12654c74acf7SKonstantin Belousov 	boolean_t upgrade;
1266df8bae1dSRodney W. Grimes 
1267df8bae1dSRodney W. Grimes #ifdef	lint
1268df8bae1dSRodney W. Grimes 	src_map++;
12690d94caffSDavid Greenman #endif	/* lint */
1270df8bae1dSRodney W. Grimes 
1271210a6886SKonstantin Belousov 	upgrade = src_entry == dst_entry;
12720973283dSKonstantin Belousov 	access = prot = dst_entry->protection;
1273210a6886SKonstantin Belousov 
1274df8bae1dSRodney W. Grimes 	src_object = src_entry->object.vm_object;
12757afab86cSAlan Cox 	src_pindex = OFF_TO_IDX(src_entry->offset);
1276df8bae1dSRodney W. Grimes 
12770973283dSKonstantin Belousov 	if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
12780973283dSKonstantin Belousov 		dst_object = src_object;
12790973283dSKonstantin Belousov 		vm_object_reference(dst_object);
12800973283dSKonstantin Belousov 	} else {
1281df8bae1dSRodney W. Grimes 		/*
12820d94caffSDavid Greenman 		 * Create the top-level object for the destination entry. (Doesn't
12830d94caffSDavid Greenman 		 * actually shadow anything - we copy the pages directly.)
1284df8bae1dSRodney W. Grimes 		 */
128524a1cce3SDavid Greenman 		dst_object = vm_object_allocate(OBJT_DEFAULT,
128657b5187bSAlan Cox 		    OFF_TO_IDX(dst_entry->end - dst_entry->start));
1287f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1288f8a47341SAlan Cox 		dst_object->flags |= OBJ_COLORED;
1289f8a47341SAlan Cox 		dst_object->pg_color = atop(dst_entry->start);
1290f8a47341SAlan Cox #endif
12910973283dSKonstantin Belousov 	}
1292df8bae1dSRodney W. Grimes 
129389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(dst_object);
1294210a6886SKonstantin Belousov 	KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1295121fd461SKonstantin Belousov 	    ("vm_fault_copy_entry: vm_object not NULL"));
12960973283dSKonstantin Belousov 	if (src_object != dst_object) {
1297df8bae1dSRodney W. Grimes 		dst_entry->object.vm_object = dst_object;
1298df8bae1dSRodney W. Grimes 		dst_entry->offset = 0;
12993364c323SKonstantin Belousov 		dst_object->charge = dst_entry->end - dst_entry->start;
13000973283dSKonstantin Belousov 	}
1301210a6886SKonstantin Belousov 	if (fork_charge != NULL) {
1302ef694c1aSEdward Tomasz Napierala 		KASSERT(dst_entry->cred == NULL,
1303121fd461SKonstantin Belousov 		    ("vm_fault_copy_entry: leaked swp charge"));
1304ef694c1aSEdward Tomasz Napierala 		dst_object->cred = curthread->td_ucred;
1305ef694c1aSEdward Tomasz Napierala 		crhold(dst_object->cred);
1306121fd461SKonstantin Belousov 		*fork_charge += dst_object->charge;
13070973283dSKonstantin Belousov 	} else if (dst_object->cred == NULL) {
13080973283dSKonstantin Belousov 		KASSERT(dst_entry->cred != NULL, ("no cred for entry %p",
13090973283dSKonstantin Belousov 		    dst_entry));
1310ef694c1aSEdward Tomasz Napierala 		dst_object->cred = dst_entry->cred;
1311ef694c1aSEdward Tomasz Napierala 		dst_entry->cred = NULL;
1312210a6886SKonstantin Belousov 	}
13130973283dSKonstantin Belousov 
1314210a6886SKonstantin Belousov 	/*
1315210a6886SKonstantin Belousov 	 * If not an upgrade, then enter the mappings in the pmap as
1316210a6886SKonstantin Belousov 	 * read and/or execute accesses.  Otherwise, enter them as
1317210a6886SKonstantin Belousov 	 * write accesses.
1318210a6886SKonstantin Belousov 	 *
1319210a6886SKonstantin Belousov 	 * A writeable large page mapping is only created if all of
1320210a6886SKonstantin Belousov 	 * the constituent small page mappings are modified. Marking
1321210a6886SKonstantin Belousov 	 * PTEs as modified on inception allows promotion to happen
1322210a6886SKonstantin Belousov 	 * without taking potentially large number of soft faults.
1323210a6886SKonstantin Belousov 	 */
1324210a6886SKonstantin Belousov 	if (!upgrade)
1325210a6886SKonstantin Belousov 		access &= ~VM_PROT_WRITE;
1326df8bae1dSRodney W. Grimes 
1327df8bae1dSRodney W. Grimes 	/*
1328ef45823eSKonstantin Belousov 	 * Loop through all of the virtual pages within the entry's
1329ef45823eSKonstantin Belousov 	 * range, copying each page from the source object to the
1330ef45823eSKonstantin Belousov 	 * destination object.  Since the source is wired, those pages
1331ef45823eSKonstantin Belousov 	 * must exist.  In contrast, the destination is pageable.
1332ef45823eSKonstantin Belousov 	 * Since the destination object does share any backing storage
1333ef45823eSKonstantin Belousov 	 * with the source object, all of its pages must be dirtied,
1334ef45823eSKonstantin Belousov 	 * regardless of whether they can be written.
1335df8bae1dSRodney W. Grimes 	 */
13367afab86cSAlan Cox 	for (vaddr = dst_entry->start, dst_pindex = 0;
1337df8bae1dSRodney W. Grimes 	    vaddr < dst_entry->end;
13387afab86cSAlan Cox 	    vaddr += PAGE_SIZE, dst_pindex++) {
13390973283dSKonstantin Belousov again:
1340df8bae1dSRodney W. Grimes 		/*
1341df8bae1dSRodney W. Grimes 		 * Find the page in the source object, and copy it in.
13424c74acf7SKonstantin Belousov 		 * Because the source is wired down, the page will be
13434c74acf7SKonstantin Belousov 		 * in memory.
1344df8bae1dSRodney W. Grimes 		 */
13450973283dSKonstantin Belousov 		if (src_object != dst_object)
134683b375eaSAttilio Rao 			VM_OBJECT_RLOCK(src_object);
1347c5b65a67SAlan Cox 		object = src_object;
13487afab86cSAlan Cox 		pindex = src_pindex + dst_pindex;
13497afab86cSAlan Cox 		while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1350c5b65a67SAlan Cox 		    (backing_object = object->backing_object) != NULL) {
1351c5b65a67SAlan Cox 			/*
13524c74acf7SKonstantin Belousov 			 * Unless the source mapping is read-only or
13534c74acf7SKonstantin Belousov 			 * it is presently being upgraded from
13544c74acf7SKonstantin Belousov 			 * read-only, the first object in the shadow
13554c74acf7SKonstantin Belousov 			 * chain should provide all of the pages.  In
13564c74acf7SKonstantin Belousov 			 * other words, this loop body should never be
13574c74acf7SKonstantin Belousov 			 * executed when the source mapping is already
13584c74acf7SKonstantin Belousov 			 * read/write.
1359c5b65a67SAlan Cox 			 */
13604c74acf7SKonstantin Belousov 			KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 ||
13614c74acf7SKonstantin Belousov 			    upgrade,
13624c74acf7SKonstantin Belousov 			    ("vm_fault_copy_entry: main object missing page"));
13634c74acf7SKonstantin Belousov 
136483b375eaSAttilio Rao 			VM_OBJECT_RLOCK(backing_object);
1365c5b65a67SAlan Cox 			pindex += OFF_TO_IDX(object->backing_object_offset);
13660973283dSKonstantin Belousov 			if (object != dst_object)
136783b375eaSAttilio Rao 				VM_OBJECT_RUNLOCK(object);
1368c5b65a67SAlan Cox 			object = backing_object;
1369c5b65a67SAlan Cox 		}
13704c74acf7SKonstantin Belousov 		KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing"));
13710973283dSKonstantin Belousov 
13720973283dSKonstantin Belousov 		if (object != dst_object) {
13730973283dSKonstantin Belousov 			/*
13740973283dSKonstantin Belousov 			 * Allocate a page in the destination object.
13750973283dSKonstantin Belousov 			 */
13762602a2eaSKonstantin Belousov 			dst_m = vm_page_alloc(dst_object, (src_object ==
13772602a2eaSKonstantin Belousov 			    dst_object ? src_pindex : 0) + dst_pindex,
13782602a2eaSKonstantin Belousov 			    VM_ALLOC_NORMAL);
13790973283dSKonstantin Belousov 			if (dst_m == NULL) {
13800973283dSKonstantin Belousov 				VM_OBJECT_WUNLOCK(dst_object);
13810973283dSKonstantin Belousov 				VM_OBJECT_RUNLOCK(object);
13820973283dSKonstantin Belousov 				VM_WAIT;
1383c8f780e3SKonstantin Belousov 				VM_OBJECT_WLOCK(dst_object);
13840973283dSKonstantin Belousov 				goto again;
13850973283dSKonstantin Belousov 			}
1386669890eaSAlan Cox 			pmap_copy_page(src_m, dst_m);
138783b375eaSAttilio Rao 			VM_OBJECT_RUNLOCK(object);
1388669890eaSAlan Cox 			dst_m->valid = VM_PAGE_BITS_ALL;
1389bc79b37fSKonstantin Belousov 			dst_m->dirty = VM_PAGE_BITS_ALL;
13900973283dSKonstantin Belousov 		} else {
13910973283dSKonstantin Belousov 			dst_m = src_m;
13920973283dSKonstantin Belousov 			if (vm_page_sleep_if_busy(dst_m, "fltupg"))
13930973283dSKonstantin Belousov 				goto again;
13940973283dSKonstantin Belousov 			vm_page_xbusy(dst_m);
13950973283dSKonstantin Belousov 			KASSERT(dst_m->valid == VM_PAGE_BITS_ALL,
13960973283dSKonstantin Belousov 			    ("invalid dst page %p", dst_m));
13970973283dSKonstantin Belousov 		}
139889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(dst_object);
1399df8bae1dSRodney W. Grimes 
1400df8bae1dSRodney W. Grimes 		/*
1401210a6886SKonstantin Belousov 		 * Enter it in the pmap. If a wired, copy-on-write
1402210a6886SKonstantin Belousov 		 * mapping is being replaced by a write-enabled
1403210a6886SKonstantin Belousov 		 * mapping, then wire that new mapping.
1404df8bae1dSRodney W. Grimes 		 */
140539ffa8c1SKonstantin Belousov 		pmap_enter(dst_map->pmap, vaddr, dst_m, prot,
140639ffa8c1SKonstantin Belousov 		    access | (upgrade ? PMAP_ENTER_WIRED : 0), 0);
1407df8bae1dSRodney W. Grimes 
1408df8bae1dSRodney W. Grimes 		/*
1409df8bae1dSRodney W. Grimes 		 * Mark it no longer busy, and put it on the active list.
1410df8bae1dSRodney W. Grimes 		 */
141189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(dst_object);
14122965a453SKip Macy 
1413210a6886SKonstantin Belousov 		if (upgrade) {
14140973283dSKonstantin Belousov 			if (src_m != dst_m) {
14152965a453SKip Macy 				vm_page_lock(src_m);
14163ae10f74SAttilio Rao 				vm_page_unwire(src_m, PQ_INACTIVE);
1417e20e8c15SKonstantin Belousov 				vm_page_unlock(src_m);
14182965a453SKip Macy 				vm_page_lock(dst_m);
1419210a6886SKonstantin Belousov 				vm_page_wire(dst_m);
1420e20e8c15SKonstantin Belousov 				vm_page_unlock(dst_m);
14212965a453SKip Macy 			} else {
14220973283dSKonstantin Belousov 				KASSERT(dst_m->wire_count > 0,
14230973283dSKonstantin Belousov 				    ("dst_m %p is not wired", dst_m));
14240973283dSKonstantin Belousov 			}
14250973283dSKonstantin Belousov 		} else {
14262965a453SKip Macy 			vm_page_lock(dst_m);
1427df8bae1dSRodney W. Grimes 			vm_page_activate(dst_m);
1428e20e8c15SKonstantin Belousov 			vm_page_unlock(dst_m);
14292965a453SKip Macy 		}
1430c7aebda8SAttilio Rao 		vm_page_xunbusy(dst_m);
1431df8bae1dSRodney W. Grimes 	}
143289f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(dst_object);
1433210a6886SKonstantin Belousov 	if (upgrade) {
1434210a6886SKonstantin Belousov 		dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1435210a6886SKonstantin Belousov 		vm_object_deallocate(src_object);
1436210a6886SKonstantin Belousov 	}
1437df8bae1dSRodney W. Grimes }
143826f9a767SRodney W. Grimes 
14395730afc9SAlan Cox /*
14405730afc9SAlan Cox  * Block entry into the machine-independent layer's page fault handler by
14415730afc9SAlan Cox  * the calling thread.  Subsequent calls to vm_fault() by that thread will
14425730afc9SAlan Cox  * return KERN_PROTECTION_FAILURE.  Enable machine-dependent handling of
14435730afc9SAlan Cox  * spurious page faults.
14445730afc9SAlan Cox  */
14452801687dSKonstantin Belousov int
14462801687dSKonstantin Belousov vm_fault_disable_pagefaults(void)
14472801687dSKonstantin Belousov {
14482801687dSKonstantin Belousov 
14495730afc9SAlan Cox 	return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
14502801687dSKonstantin Belousov }
14512801687dSKonstantin Belousov 
14522801687dSKonstantin Belousov void
14532801687dSKonstantin Belousov vm_fault_enable_pagefaults(int save)
14542801687dSKonstantin Belousov {
14552801687dSKonstantin Belousov 
14562801687dSKonstantin Belousov 	curthread_pflags_restore(save);
14572801687dSKonstantin Belousov }
1458