xref: /freebsd/sys/vm/vm_fault.c (revision 230afe0be6aa2c556ca3c87ec9d4bddb53df826b)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
426f9a767SRodney W. Grimes  * Copyright (c) 1994 John S. Dyson
526f9a767SRodney W. Grimes  * All rights reserved.
626f9a767SRodney W. Grimes  * Copyright (c) 1994 David Greenman
726f9a767SRodney W. Grimes  * All rights reserved.
826f9a767SRodney W. Grimes  *
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
11df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
12df8bae1dSRodney W. Grimes  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
225929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
23df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
24df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
25df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
26df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
27df8bae1dSRodney W. Grimes  *    without specific prior written permission.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
40df8bae1dSRodney W. Grimes  *
413c4dd356SDavid Greenman  *	from: @(#)vm_fault.c	8.4 (Berkeley) 1/12/94
42df8bae1dSRodney W. Grimes  *
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
45df8bae1dSRodney W. Grimes  * All rights reserved.
46df8bae1dSRodney W. Grimes  *
47df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
48df8bae1dSRodney W. Grimes  *
49df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
50df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
51df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
52df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
53df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
54df8bae1dSRodney W. Grimes  *
55df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
57df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58df8bae1dSRodney W. Grimes  *
59df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
60df8bae1dSRodney W. Grimes  *
61df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
62df8bae1dSRodney W. Grimes  *  School of Computer Science
63df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
64df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
65df8bae1dSRodney W. Grimes  *
66df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
67df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
68df8bae1dSRodney W. Grimes  */
69df8bae1dSRodney W. Grimes 
70df8bae1dSRodney W. Grimes /*
71df8bae1dSRodney W. Grimes  *	Page fault handling module.
72df8bae1dSRodney W. Grimes  */
73874651b1SDavid E. O'Brien 
74874651b1SDavid E. O'Brien #include <sys/cdefs.h>
75874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
76874651b1SDavid E. O'Brien 
7735818d2eSJohn Baldwin #include "opt_ktrace.h"
78f8a47341SAlan Cox #include "opt_vm.h"
79f8a47341SAlan Cox 
80df8bae1dSRodney W. Grimes #include <sys/param.h>
81df8bae1dSRodney W. Grimes #include <sys/systm.h>
824edf4a58SJohn Baldwin #include <sys/kernel.h>
83fb919e4dSMark Murray #include <sys/lock.h>
84a8b0f100SAlan Cox #include <sys/mman.h>
8526f9a767SRodney W. Grimes #include <sys/proc.h>
86ae34b6ffSEdward Tomasz Napierala #include <sys/racct.h>
8726f9a767SRodney W. Grimes #include <sys/resourcevar.h>
8889f6b863SAttilio Rao #include <sys/rwlock.h>
8923955314SAlfred Perlstein #include <sys/sysctl.h>
904edf4a58SJohn Baldwin #include <sys/vmmeter.h>
914edf4a58SJohn Baldwin #include <sys/vnode.h>
9235818d2eSJohn Baldwin #ifdef KTRACE
9335818d2eSJohn Baldwin #include <sys/ktrace.h>
9435818d2eSJohn Baldwin #endif
95df8bae1dSRodney W. Grimes 
96df8bae1dSRodney W. Grimes #include <vm/vm.h>
97efeaf95aSDavid Greenman #include <vm/vm_param.h>
98efeaf95aSDavid Greenman #include <vm/pmap.h>
99efeaf95aSDavid Greenman #include <vm/vm_map.h>
100efeaf95aSDavid Greenman #include <vm/vm_object.h>
101df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
102df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
103a83c285cSDavid Greenman #include <vm/vm_kern.h>
10424a1cce3SDavid Greenman #include <vm/vm_pager.h>
105efeaf95aSDavid Greenman #include <vm/vm_extern.h>
106dfdf9abdSAlan Cox #include <vm/vm_reserv.h>
107df8bae1dSRodney W. Grimes 
108566526a9SAlan Cox #define PFBAK 4
109566526a9SAlan Cox #define PFFOR 4
110566526a9SAlan Cox 
1115268042bSAlan Cox #define	VM_FAULT_READ_DEFAULT	(1 + VM_FAULT_READ_AHEAD_INIT)
11213458803SAlan Cox #define	VM_FAULT_READ_MAX	(1 + VM_FAULT_READ_AHEAD_MAX)
113a8b0f100SAlan Cox 
114a8b0f100SAlan Cox #define	VM_FAULT_DONTNEED_MIN	1048576
11526f9a767SRodney W. Grimes 
1164866e085SJohn Dyson struct faultstate {
1174866e085SJohn Dyson 	vm_page_t m;
1184866e085SJohn Dyson 	vm_object_t object;
1194866e085SJohn Dyson 	vm_pindex_t pindex;
1204866e085SJohn Dyson 	vm_page_t first_m;
1214866e085SJohn Dyson 	vm_object_t	first_object;
1224866e085SJohn Dyson 	vm_pindex_t first_pindex;
1234866e085SJohn Dyson 	vm_map_t map;
1244866e085SJohn Dyson 	vm_map_entry_t entry;
12525adb370SBrian Feldman 	int lookup_still_valid;
1264866e085SJohn Dyson 	struct vnode *vp;
1274866e085SJohn Dyson };
1284866e085SJohn Dyson 
129a8b0f100SAlan Cox static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr,
130a8b0f100SAlan Cox 	    int ahead);
13163281952SAlan Cox static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
132b0cd2017SGleb Smirnoff 	    int backward, int forward);
13313458803SAlan Cox 
13462a59e8fSWarner Losh static inline void
1354866e085SJohn Dyson release_page(struct faultstate *fs)
1364866e085SJohn Dyson {
1370d0be82aSKonstantin Belousov 
138c7aebda8SAttilio Rao 	vm_page_xunbusy(fs->m);
1392965a453SKip Macy 	vm_page_lock(fs->m);
1404866e085SJohn Dyson 	vm_page_deactivate(fs->m);
1412965a453SKip Macy 	vm_page_unlock(fs->m);
1424866e085SJohn Dyson 	fs->m = NULL;
1434866e085SJohn Dyson }
1444866e085SJohn Dyson 
14562a59e8fSWarner Losh static inline void
1464866e085SJohn Dyson unlock_map(struct faultstate *fs)
1474866e085SJohn Dyson {
1480d0be82aSKonstantin Belousov 
14925adb370SBrian Feldman 	if (fs->lookup_still_valid) {
1504866e085SJohn Dyson 		vm_map_lookup_done(fs->map, fs->entry);
15125adb370SBrian Feldman 		fs->lookup_still_valid = FALSE;
1524866e085SJohn Dyson 	}
1534866e085SJohn Dyson }
1544866e085SJohn Dyson 
1554866e085SJohn Dyson static void
156a51b0840SAlan Cox unlock_and_deallocate(struct faultstate *fs)
1574866e085SJohn Dyson {
158f29ba63eSAlan Cox 
1594866e085SJohn Dyson 	vm_object_pip_wakeup(fs->object);
16089f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(fs->object);
1614866e085SJohn Dyson 	if (fs->object != fs->first_object) {
16289f6b863SAttilio Rao 		VM_OBJECT_WLOCK(fs->first_object);
1632965a453SKip Macy 		vm_page_lock(fs->first_m);
1644866e085SJohn Dyson 		vm_page_free(fs->first_m);
1652965a453SKip Macy 		vm_page_unlock(fs->first_m);
1664866e085SJohn Dyson 		vm_object_pip_wakeup(fs->first_object);
16789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(fs->first_object);
1684866e085SJohn Dyson 		fs->first_m = NULL;
1694866e085SJohn Dyson 	}
1704866e085SJohn Dyson 	vm_object_deallocate(fs->first_object);
1714866e085SJohn Dyson 	unlock_map(fs);
1724866e085SJohn Dyson 	if (fs->vp != NULL) {
1730cddd8f0SMatthew Dillon 		vput(fs->vp);
1744866e085SJohn Dyson 		fs->vp = NULL;
1754866e085SJohn Dyson 	}
1764866e085SJohn Dyson }
1774866e085SJohn Dyson 
178a36f5532SKonstantin Belousov static void
179a36f5532SKonstantin Belousov vm_fault_dirty(vm_map_entry_t entry, vm_page_t m, vm_prot_t prot,
180a36f5532SKonstantin Belousov     vm_prot_t fault_type, int fault_flags, boolean_t set_wd)
181a36f5532SKonstantin Belousov {
182a36f5532SKonstantin Belousov 	boolean_t need_dirty;
183a36f5532SKonstantin Belousov 
184a36f5532SKonstantin Belousov 	if (((prot & VM_PROT_WRITE) == 0 &&
185a36f5532SKonstantin Belousov 	    (fault_flags & VM_FAULT_DIRTY) == 0) ||
186a36f5532SKonstantin Belousov 	    (m->oflags & VPO_UNMANAGED) != 0)
187a36f5532SKonstantin Belousov 		return;
188a36f5532SKonstantin Belousov 
189a36f5532SKonstantin Belousov 	VM_OBJECT_ASSERT_LOCKED(m->object);
190a36f5532SKonstantin Belousov 
191a36f5532SKonstantin Belousov 	need_dirty = ((fault_type & VM_PROT_WRITE) != 0 &&
1926a875bf9SKonstantin Belousov 	    (fault_flags & VM_FAULT_WIRE) == 0) ||
193a36f5532SKonstantin Belousov 	    (fault_flags & VM_FAULT_DIRTY) != 0;
194a36f5532SKonstantin Belousov 
195a36f5532SKonstantin Belousov 	if (set_wd)
196a36f5532SKonstantin Belousov 		vm_object_set_writeable_dirty(m->object);
197a36f5532SKonstantin Belousov 	else
198a36f5532SKonstantin Belousov 		/*
199a36f5532SKonstantin Belousov 		 * If two callers of vm_fault_dirty() with set_wd ==
200a36f5532SKonstantin Belousov 		 * FALSE, one for the map entry with MAP_ENTRY_NOSYNC
201a36f5532SKonstantin Belousov 		 * flag set, other with flag clear, race, it is
202a36f5532SKonstantin Belousov 		 * possible for the no-NOSYNC thread to see m->dirty
203a36f5532SKonstantin Belousov 		 * != 0 and not clear VPO_NOSYNC.  Take vm_page lock
204a36f5532SKonstantin Belousov 		 * around manipulation of VPO_NOSYNC and
205a36f5532SKonstantin Belousov 		 * vm_page_dirty() call, to avoid the race and keep
206a36f5532SKonstantin Belousov 		 * m->oflags consistent.
207a36f5532SKonstantin Belousov 		 */
208a36f5532SKonstantin Belousov 		vm_page_lock(m);
209a36f5532SKonstantin Belousov 
210a36f5532SKonstantin Belousov 	/*
211a36f5532SKonstantin Belousov 	 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
212a36f5532SKonstantin Belousov 	 * if the page is already dirty to prevent data written with
213a36f5532SKonstantin Belousov 	 * the expectation of being synced from not being synced.
214a36f5532SKonstantin Belousov 	 * Likewise if this entry does not request NOSYNC then make
215a36f5532SKonstantin Belousov 	 * sure the page isn't marked NOSYNC.  Applications sharing
216a36f5532SKonstantin Belousov 	 * data should use the same flags to avoid ping ponging.
217a36f5532SKonstantin Belousov 	 */
218a36f5532SKonstantin Belousov 	if ((entry->eflags & MAP_ENTRY_NOSYNC) != 0) {
219a36f5532SKonstantin Belousov 		if (m->dirty == 0) {
220a36f5532SKonstantin Belousov 			m->oflags |= VPO_NOSYNC;
221a36f5532SKonstantin Belousov 		}
222a36f5532SKonstantin Belousov 	} else {
223a36f5532SKonstantin Belousov 		m->oflags &= ~VPO_NOSYNC;
224a36f5532SKonstantin Belousov 	}
225a36f5532SKonstantin Belousov 
226a36f5532SKonstantin Belousov 	/*
227a36f5532SKonstantin Belousov 	 * If the fault is a write, we know that this page is being
228a36f5532SKonstantin Belousov 	 * written NOW so dirty it explicitly to save on
229a36f5532SKonstantin Belousov 	 * pmap_is_modified() calls later.
230a36f5532SKonstantin Belousov 	 *
231a36f5532SKonstantin Belousov 	 * Also tell the backing pager, if any, that it should remove
232a36f5532SKonstantin Belousov 	 * any swap backing since the page is now dirty.
233a36f5532SKonstantin Belousov 	 */
234a36f5532SKonstantin Belousov 	if (need_dirty)
235a36f5532SKonstantin Belousov 		vm_page_dirty(m);
236a36f5532SKonstantin Belousov 	if (!set_wd)
237a36f5532SKonstantin Belousov 		vm_page_unlock(m);
238a36f5532SKonstantin Belousov 	if (need_dirty)
239a36f5532SKonstantin Belousov 		vm_pager_page_unswapped(m);
240a36f5532SKonstantin Belousov }
241a36f5532SKonstantin Belousov 
242df8bae1dSRodney W. Grimes /*
243df8bae1dSRodney W. Grimes  *	vm_fault:
244df8bae1dSRodney W. Grimes  *
245956f3135SPhilippe Charnier  *	Handle a page fault occurring at the given address,
246df8bae1dSRodney W. Grimes  *	requiring the given permissions, in the map specified.
247df8bae1dSRodney W. Grimes  *	If successful, the page is inserted into the
248df8bae1dSRodney W. Grimes  *	associated physical map.
249df8bae1dSRodney W. Grimes  *
250df8bae1dSRodney W. Grimes  *	NOTE: the given address should be truncated to the
251df8bae1dSRodney W. Grimes  *	proper page address.
252df8bae1dSRodney W. Grimes  *
253df8bae1dSRodney W. Grimes  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
254df8bae1dSRodney W. Grimes  *	a standard error specifying why the fault is fatal is returned.
255df8bae1dSRodney W. Grimes  *
256df8bae1dSRodney W. Grimes  *	The map in question must be referenced, and remains so.
2570cddd8f0SMatthew Dillon  *	Caller may hold no locks.
258df8bae1dSRodney W. Grimes  */
259df8bae1dSRodney W. Grimes int
26023955314SAlfred Perlstein vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
26123955314SAlfred Perlstein     int fault_flags)
26223955314SAlfred Perlstein {
26335818d2eSJohn Baldwin 	struct thread *td;
26435818d2eSJohn Baldwin 	int result;
265acd11c74SAlan Cox 
26635818d2eSJohn Baldwin 	td = curthread;
26735818d2eSJohn Baldwin 	if ((td->td_pflags & TDP_NOFAULTING) != 0)
2682801687dSKonstantin Belousov 		return (KERN_PROTECTION_FAILURE);
26935818d2eSJohn Baldwin #ifdef KTRACE
27035818d2eSJohn Baldwin 	if (map != kernel_map && KTRPOINT(td, KTR_FAULT))
27135818d2eSJohn Baldwin 		ktrfault(vaddr, fault_type);
27235818d2eSJohn Baldwin #endif
273be996836SAttilio Rao 	result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags,
274be996836SAttilio Rao 	    NULL);
27535818d2eSJohn Baldwin #ifdef KTRACE
27635818d2eSJohn Baldwin 	if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND))
27735818d2eSJohn Baldwin 		ktrfaultend(result);
27835818d2eSJohn Baldwin #endif
27935818d2eSJohn Baldwin 	return (result);
280acd11c74SAlan Cox }
281acd11c74SAlan Cox 
282acd11c74SAlan Cox int
283be996836SAttilio Rao vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
284acd11c74SAlan Cox     int fault_flags, vm_page_t *m_hold)
285acd11c74SAlan Cox {
286df8bae1dSRodney W. Grimes 	vm_prot_t prot;
287b0cd2017SGleb Smirnoff 	int alloc_req, era, faultcount, nera, result;
2883f1c66b8SKonstantin Belousov 	boolean_t dead, growstack, is_first_object_locked, wired;
2892d8acc0fSJohn Dyson 	int map_generation;
290df8bae1dSRodney W. Grimes 	vm_object_t next_object;
2914866e085SJohn Dyson 	int hardfault;
2924866e085SJohn Dyson 	struct faultstate fs;
293d2bf64c3SKonstantin Belousov 	struct vnode *vp;
2940c3a4893SAlan Cox 	vm_offset_t e_end, e_start;
295afe55ca3SKonstantin Belousov 	vm_page_t m;
2960c3a4893SAlan Cox 	int ahead, behind, cluster_offset, error, locked, rv;
2970c3a4893SAlan Cox 	u_char behavior;
298df8bae1dSRodney W. Grimes 
2994866e085SJohn Dyson 	hardfault = 0;
3006139043bSAlan Cox 	growstack = TRUE;
30167596082SAttilio Rao 	PCPU_INC(cnt.v_vm_faults);
302d2bf64c3SKonstantin Belousov 	fs.vp = NULL;
303b0cd2017SGleb Smirnoff 	faultcount = 0;
3040c3a4893SAlan Cox 	nera = -1;
305df8bae1dSRodney W. Grimes 
306df8bae1dSRodney W. Grimes RetryFault:;
307df8bae1dSRodney W. Grimes 
308df8bae1dSRodney W. Grimes 	/*
3090d94caffSDavid Greenman 	 * Find the backing store object and offset into it to begin the
3100d94caffSDavid Greenman 	 * search.
311df8bae1dSRodney W. Grimes 	 */
31240360b1bSMatthew Dillon 	fs.map = map;
31392de35b0SAlan Cox 	result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry,
31492de35b0SAlan Cox 	    &fs.first_object, &fs.first_pindex, &prot, &wired);
31592de35b0SAlan Cox 	if (result != KERN_SUCCESS) {
3166139043bSAlan Cox 		if (growstack && result == KERN_INVALID_ADDRESS &&
3172db65ab4SAlan Cox 		    map != kernel_map) {
3186139043bSAlan Cox 			result = vm_map_growstack(curproc, vaddr);
319a976eb5eSAlan Cox 			if (result != KERN_SUCCESS)
3206139043bSAlan Cox 				return (KERN_FAILURE);
3216139043bSAlan Cox 			growstack = FALSE;
3226139043bSAlan Cox 			goto RetryFault;
3236139043bSAlan Cox 		}
324eb17fb15SMark Johnston 		if (fs.vp != NULL)
325eb17fb15SMark Johnston 			vput(fs.vp);
32692de35b0SAlan Cox 		return (result);
32709e0c6ccSJohn Dyson 	}
32809e0c6ccSJohn Dyson 
3294866e085SJohn Dyson 	map_generation = fs.map->timestamp;
3302d8acc0fSJohn Dyson 
3314866e085SJohn Dyson 	if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
33247221757SJohn Dyson 		panic("vm_fault: fault on nofault entry, addr: %lx",
33392c4c4ebSBruce Evans 		    (u_long)vaddr);
3347aaaa4fdSJohn Dyson 	}
3357aaaa4fdSJohn Dyson 
3364f9c9114SKonstantin Belousov 	if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
3374f9c9114SKonstantin Belousov 	    fs.entry->wiring_thread != curthread) {
3384f9c9114SKonstantin Belousov 		vm_map_unlock_read(fs.map);
3394f9c9114SKonstantin Belousov 		vm_map_lock(fs.map);
3404f9c9114SKonstantin Belousov 		if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
3414f9c9114SKonstantin Belousov 		    (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
34285af31a4SKonstantin Belousov 			if (fs.vp != NULL) {
34385af31a4SKonstantin Belousov 				vput(fs.vp);
34485af31a4SKonstantin Belousov 				fs.vp = NULL;
34585af31a4SKonstantin Belousov 			}
3464f9c9114SKonstantin Belousov 			fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
3474f9c9114SKonstantin Belousov 			vm_map_unlock_and_wait(fs.map, 0);
3484f9c9114SKonstantin Belousov 		} else
3494f9c9114SKonstantin Belousov 			vm_map_unlock(fs.map);
3504f9c9114SKonstantin Belousov 		goto RetryFault;
3514f9c9114SKonstantin Belousov 	}
3524f9c9114SKonstantin Belousov 
353afe55ca3SKonstantin Belousov 	if (wired)
354afe55ca3SKonstantin Belousov 		fault_type = prot | (fault_type & VM_PROT_COPY);
3556a875bf9SKonstantin Belousov 	else
3566a875bf9SKonstantin Belousov 		KASSERT((fault_flags & VM_FAULT_WIRE) == 0,
3576a875bf9SKonstantin Belousov 		    ("!wired && VM_FAULT_WIRE"));
358afe55ca3SKonstantin Belousov 
3598d67b8c8SAlan Cox 	/*
3608d67b8c8SAlan Cox 	 * Try to avoid lock contention on the top-level object through
3618d67b8c8SAlan Cox 	 * special-case handling of some types of page faults, specifically,
3628d67b8c8SAlan Cox 	 * those that are both (1) mapping an existing page from the top-
3638d67b8c8SAlan Cox 	 * level object and (2) not having to mark that object as containing
3648d67b8c8SAlan Cox 	 * dirty pages.  Under these conditions, a read lock on the top-level
3658d67b8c8SAlan Cox 	 * object suffices, allowing multiple page faults of a similar type to
3668d67b8c8SAlan Cox 	 * run in parallel on the same top-level object.
3678d67b8c8SAlan Cox 	 */
368afe55ca3SKonstantin Belousov 	if (fs.vp == NULL /* avoid locked vnode leak */ &&
3696a875bf9SKonstantin Belousov 	    (fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0 &&
370afe55ca3SKonstantin Belousov 	    /* avoid calling vm_object_set_writeable_dirty() */
371afe55ca3SKonstantin Belousov 	    ((prot & VM_PROT_WRITE) == 0 ||
372f40cb1c6SKonstantin Belousov 	    (fs.first_object->type != OBJT_VNODE &&
373f40cb1c6SKonstantin Belousov 	    (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
374afe55ca3SKonstantin Belousov 	    (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0)) {
375afe55ca3SKonstantin Belousov 		VM_OBJECT_RLOCK(fs.first_object);
376afe55ca3SKonstantin Belousov 		if ((prot & VM_PROT_WRITE) != 0 &&
377f40cb1c6SKonstantin Belousov 		    (fs.first_object->type == OBJT_VNODE ||
378f40cb1c6SKonstantin Belousov 		    (fs.first_object->flags & OBJ_TMPFS_NODE) != 0) &&
379afe55ca3SKonstantin Belousov 		    (fs.first_object->flags & OBJ_MIGHTBEDIRTY) == 0)
380afe55ca3SKonstantin Belousov 			goto fast_failed;
381afe55ca3SKonstantin Belousov 		m = vm_page_lookup(fs.first_object, fs.first_pindex);
382b9ce8cc2SAlan Cox 		/* A busy page can be mapped for read|execute access. */
383b9ce8cc2SAlan Cox 		if (m == NULL || ((prot & VM_PROT_WRITE) != 0 &&
384b9ce8cc2SAlan Cox 		    vm_page_busied(m)) || m->valid != VM_PAGE_BITS_ALL)
385afe55ca3SKonstantin Belousov 			goto fast_failed;
386afe55ca3SKonstantin Belousov 		result = pmap_enter(fs.map->pmap, vaddr, m, prot,
387afe55ca3SKonstantin Belousov 		   fault_type | PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED :
388afe55ca3SKonstantin Belousov 		   0), 0);
389afe55ca3SKonstantin Belousov 		if (result != KERN_SUCCESS)
390afe55ca3SKonstantin Belousov 			goto fast_failed;
391afe55ca3SKonstantin Belousov 		if (m_hold != NULL) {
392afe55ca3SKonstantin Belousov 			*m_hold = m;
393afe55ca3SKonstantin Belousov 			vm_page_lock(m);
394afe55ca3SKonstantin Belousov 			vm_page_hold(m);
395afe55ca3SKonstantin Belousov 			vm_page_unlock(m);
396afe55ca3SKonstantin Belousov 		}
397a36f5532SKonstantin Belousov 		vm_fault_dirty(fs.entry, m, prot, fault_type, fault_flags,
398a36f5532SKonstantin Belousov 		    FALSE);
399afe55ca3SKonstantin Belousov 		VM_OBJECT_RUNLOCK(fs.first_object);
400afe55ca3SKonstantin Belousov 		if (!wired)
401b0cd2017SGleb Smirnoff 			vm_fault_prefault(&fs, vaddr, PFBAK, PFFOR);
402afe55ca3SKonstantin Belousov 		vm_map_lookup_done(fs.map, fs.entry);
403afe55ca3SKonstantin Belousov 		curthread->td_ru.ru_minflt++;
404afe55ca3SKonstantin Belousov 		return (KERN_SUCCESS);
405afe55ca3SKonstantin Belousov fast_failed:
406afe55ca3SKonstantin Belousov 		if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) {
407afe55ca3SKonstantin Belousov 			VM_OBJECT_RUNLOCK(fs.first_object);
408afe55ca3SKonstantin Belousov 			VM_OBJECT_WLOCK(fs.first_object);
409afe55ca3SKonstantin Belousov 		}
410afe55ca3SKonstantin Belousov 	} else {
411afe55ca3SKonstantin Belousov 		VM_OBJECT_WLOCK(fs.first_object);
412afe55ca3SKonstantin Belousov 	}
413afe55ca3SKonstantin Belousov 
41495e5e988SJohn Dyson 	/*
41595e5e988SJohn Dyson 	 * Make a reference to this object to prevent its disposal while we
41695e5e988SJohn Dyson 	 * are messing with it.  Once we have the reference, the map is free
41795e5e988SJohn Dyson 	 * to be diddled.  Since objects reference their shadows (and copies),
41895e5e988SJohn Dyson 	 * they will stay around as well.
419fe8e0238SMatthew Dillon 	 *
420fe8e0238SMatthew Dillon 	 * Bump the paging-in-progress count to prevent size changes (e.g.
421fe8e0238SMatthew Dillon 	 * truncation operations) during I/O.  This must be done after
422fe8e0238SMatthew Dillon 	 * obtaining the vnode lock in order to avoid possible deadlocks.
42395e5e988SJohn Dyson 	 */
424a976eb5eSAlan Cox 	vm_object_reference_locked(fs.first_object);
425d474eaaaSDoug Rabson 	vm_object_pip_add(fs.first_object, 1);
42695e5e988SJohn Dyson 
42725adb370SBrian Feldman 	fs.lookup_still_valid = TRUE;
428df8bae1dSRodney W. Grimes 
4294866e085SJohn Dyson 	fs.first_m = NULL;
430df8bae1dSRodney W. Grimes 
431df8bae1dSRodney W. Grimes 	/*
432df8bae1dSRodney W. Grimes 	 * Search for the page at object/offset.
433df8bae1dSRodney W. Grimes 	 */
4344866e085SJohn Dyson 	fs.object = fs.first_object;
4354866e085SJohn Dyson 	fs.pindex = fs.first_pindex;
436df8bae1dSRodney W. Grimes 	while (TRUE) {
4371c7c3c6aSMatthew Dillon 		/*
438725441f6SKonstantin Belousov 		 * If the object is marked for imminent termination,
439725441f6SKonstantin Belousov 		 * we retry here, since the collapse pass has raced
440725441f6SKonstantin Belousov 		 * with us.  Otherwise, if we see terminally dead
441725441f6SKonstantin Belousov 		 * object, return fail.
4421c7c3c6aSMatthew Dillon 		 */
443725441f6SKonstantin Belousov 		if ((fs.object->flags & OBJ_DEAD) != 0) {
444725441f6SKonstantin Belousov 			dead = fs.object->type == OBJT_DEAD;
4454866e085SJohn Dyson 			unlock_and_deallocate(&fs);
446725441f6SKonstantin Belousov 			if (dead)
44747221757SJohn Dyson 				return (KERN_PROTECTION_FAILURE);
448725441f6SKonstantin Belousov 			pause("vmf_de", 1);
449725441f6SKonstantin Belousov 			goto RetryFault;
45047221757SJohn Dyson 		}
45147221757SJohn Dyson 
4521c7c3c6aSMatthew Dillon 		/*
4531c7c3c6aSMatthew Dillon 		 * See if page is resident
4541c7c3c6aSMatthew Dillon 		 */
4554866e085SJohn Dyson 		fs.m = vm_page_lookup(fs.object, fs.pindex);
4564866e085SJohn Dyson 		if (fs.m != NULL) {
45798cb733cSKenneth D. Merry 			/*
4581c7c3c6aSMatthew Dillon 			 * Wait/Retry if the page is busy.  We have to do this
459c7aebda8SAttilio Rao 			 * if the page is either exclusive or shared busy
460c7aebda8SAttilio Rao 			 * because the vm_pager may be using read busy for
461c7aebda8SAttilio Rao 			 * pageouts (and even pageins if it is the vnode
462c7aebda8SAttilio Rao 			 * pager), and we could end up trying to pagein and
463c7aebda8SAttilio Rao 			 * pageout the same page simultaneously.
4641c7c3c6aSMatthew Dillon 			 *
4651c7c3c6aSMatthew Dillon 			 * We can theoretically allow the busy case on a read
4661c7c3c6aSMatthew Dillon 			 * fault if the page is marked valid, but since such
4671c7c3c6aSMatthew Dillon 			 * pages are typically already pmap'd, putting that
4681c7c3c6aSMatthew Dillon 			 * special case in might be more effort then it is
4691c7c3c6aSMatthew Dillon 			 * worth.  We cannot under any circumstances mess
470c7aebda8SAttilio Rao 			 * around with a shared busied page except, perhaps,
4711c7c3c6aSMatthew Dillon 			 * to pmap it.
472df8bae1dSRodney W. Grimes 			 */
473c7aebda8SAttilio Rao 			if (vm_page_busied(fs.m)) {
474b88b6c9dSAlan Cox 				/*
475b88b6c9dSAlan Cox 				 * Reference the page before unlocking and
476b88b6c9dSAlan Cox 				 * sleeping so that the page daemon is less
477b88b6c9dSAlan Cox 				 * likely to reclaim it.
478b88b6c9dSAlan Cox 				 */
4793407fefeSKonstantin Belousov 				vm_page_aflag_set(fs.m, PGA_REFERENCED);
480a51b0840SAlan Cox 				if (fs.object != fs.first_object) {
48189f6b863SAttilio Rao 					if (!VM_OBJECT_TRYWLOCK(
482a6e38685SKonstantin Belousov 					    fs.first_object)) {
48389f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(fs.object);
48489f6b863SAttilio Rao 						VM_OBJECT_WLOCK(fs.first_object);
48589f6b863SAttilio Rao 						VM_OBJECT_WLOCK(fs.object);
486a6e38685SKonstantin Belousov 					}
4872965a453SKip Macy 					vm_page_lock(fs.first_m);
488a51b0840SAlan Cox 					vm_page_free(fs.first_m);
4892965a453SKip Macy 					vm_page_unlock(fs.first_m);
490a51b0840SAlan Cox 					vm_object_pip_wakeup(fs.first_object);
49189f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(fs.first_object);
492a51b0840SAlan Cox 					fs.first_m = NULL;
493a51b0840SAlan Cox 				}
494a51b0840SAlan Cox 				unlock_map(&fs);
495a51b0840SAlan Cox 				if (fs.m == vm_page_lookup(fs.object,
496a51b0840SAlan Cox 				    fs.pindex)) {
497c7aebda8SAttilio Rao 					vm_page_sleep_if_busy(fs.m, "vmpfw");
498a51b0840SAlan Cox 				}
499a51b0840SAlan Cox 				vm_object_pip_wakeup(fs.object);
50089f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(fs.object);
50167596082SAttilio Rao 				PCPU_INC(cnt.v_intrans);
5024866e085SJohn Dyson 				vm_object_deallocate(fs.first_object);
503df8bae1dSRodney W. Grimes 				goto RetryFault;
504df8bae1dSRodney W. Grimes 			}
5053846a822SKonstantin Belousov 			vm_page_lock(fs.m);
5068d220203SAlan Cox 			vm_page_remque(fs.m);
5072965a453SKip Macy 			vm_page_unlock(fs.m);
5087615edaaSMatthew Dillon 
5091c7c3c6aSMatthew Dillon 			/*
5101c7c3c6aSMatthew Dillon 			 * Mark page busy for other processes, and the
5111c7c3c6aSMatthew Dillon 			 * pagedaemon.  If it still isn't completely valid
5121c7c3c6aSMatthew Dillon 			 * (readable), jump to readrest, else break-out ( we
5131c7c3c6aSMatthew Dillon 			 * found the page ).
5141c7c3c6aSMatthew Dillon 			 */
515c7aebda8SAttilio Rao 			vm_page_xbusy(fs.m);
516ff5958e7SAlan Cox 			if (fs.m->valid != VM_PAGE_BITS_ALL)
5170d94caffSDavid Greenman 				goto readrest;
518df8bae1dSRodney W. Grimes 			break;
519df8bae1dSRodney W. Grimes 		}
52010b4196bSAlan Cox 		KASSERT(fs.m == NULL, ("fs.m should be NULL, not %p", fs.m));
5211c7c3c6aSMatthew Dillon 
5221c7c3c6aSMatthew Dillon 		/*
52310b4196bSAlan Cox 		 * Page is not resident.  If the pager might contain the page
52410b4196bSAlan Cox 		 * or this is the beginning of the search, allocate a new
52510b4196bSAlan Cox 		 * page.  (Default objects are zero-fill, so there is no real
52610b4196bSAlan Cox 		 * pager for them.)
5271c7c3c6aSMatthew Dillon 		 */
5286a875bf9SKonstantin Belousov 		if (fs.object->type != OBJT_DEFAULT ||
5296a875bf9SKonstantin Belousov 		    fs.object == fs.first_object) {
5304866e085SJohn Dyson 			if (fs.pindex >= fs.object->size) {
5314866e085SJohn Dyson 				unlock_and_deallocate(&fs);
5325f55e841SDavid Greenman 				return (KERN_PROTECTION_FAILURE);
5335f55e841SDavid Greenman 			}
53422ba64e8SJohn Dyson 
535df8bae1dSRodney W. Grimes 			/*
5360d94caffSDavid Greenman 			 * Allocate a new page for this object/offset pair.
5373f1c4c4fSKonstantin Belousov 			 *
5383f1c4c4fSKonstantin Belousov 			 * Unlocked read of the p_flag is harmless. At
5393f1c4c4fSKonstantin Belousov 			 * worst, the P_KILLED might be not observed
5403f1c4c4fSKonstantin Belousov 			 * there, and allocation can fail, causing
5413f1c4c4fSKonstantin Belousov 			 * restart and new reading of the p_flag.
542df8bae1dSRodney W. Grimes 			 */
5433f1c4c4fSKonstantin Belousov 			if (!vm_page_count_severe() || P_KILLED(curproc)) {
544f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
5453d653db0SAlan Cox 				vm_object_color(fs.object, atop(vaddr) -
5463d653db0SAlan Cox 				    fs.pindex);
547f8a47341SAlan Cox #endif
5483f1c4c4fSKonstantin Belousov 				alloc_req = P_KILLED(curproc) ?
5493f1c4c4fSKonstantin Belousov 				    VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
5503f1c4c4fSKonstantin Belousov 				if (fs.object->type != OBJT_VNODE &&
5513f1c4c4fSKonstantin Belousov 				    fs.object->backing_object == NULL)
5523f1c4c4fSKonstantin Belousov 					alloc_req |= VM_ALLOC_ZERO;
5534866e085SJohn Dyson 				fs.m = vm_page_alloc(fs.object, fs.pindex,
5543f1c4c4fSKonstantin Belousov 				    alloc_req);
55540360b1bSMatthew Dillon 			}
5564866e085SJohn Dyson 			if (fs.m == NULL) {
5574866e085SJohn Dyson 				unlock_and_deallocate(&fs);
558ef6020d1SMike Silbersack 				VM_WAITPFAULT;
559df8bae1dSRodney W. Grimes 				goto RetryFault;
5600a2e596aSAlan Cox 			} else if (fs.m->valid == VM_PAGE_BITS_ALL)
5614ab8ab92SKonstantin Belousov 				break;
562df8bae1dSRodney W. Grimes 		}
56347221757SJohn Dyson 
5640d94caffSDavid Greenman readrest:
5651c7c3c6aSMatthew Dillon 		/*
5660c3a4893SAlan Cox 		 * If the pager for the current object might have the page,
5670c3a4893SAlan Cox 		 * then determine the number of additional pages to read and
5680c3a4893SAlan Cox 		 * potentially reprioritize previously read pages for earlier
5690c3a4893SAlan Cox 		 * reclamation.  These operations should only be performed
5700c3a4893SAlan Cox 		 * once per page fault.  Even if the current pager doesn't
5710c3a4893SAlan Cox 		 * have the page, the number of additional pages to read will
5720c3a4893SAlan Cox 		 * apply to subsequent objects in the shadow chain.
5731c7c3c6aSMatthew Dillon 		 */
5740c3a4893SAlan Cox 		if (fs.object->type != OBJT_DEFAULT && nera == -1 &&
5750c3a4893SAlan Cox 		    !P_KILLED(curproc)) {
5760c3a4893SAlan Cox 			KASSERT(fs.lookup_still_valid, ("map unlocked"));
5775268042bSAlan Cox 			era = fs.entry->read_ahead;
5780c3a4893SAlan Cox 			behavior = vm_map_entry_behavior(fs.entry);
5790c3a4893SAlan Cox 			if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
5805268042bSAlan Cox 				nera = 0;
58113458803SAlan Cox 			} else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
5825268042bSAlan Cox 				nera = VM_FAULT_READ_AHEAD_MAX;
583381b7242SAlan Cox 				if (vaddr == fs.entry->next_read)
5840c3a4893SAlan Cox 					vm_fault_dontneed(&fs, vaddr, nera);
585381b7242SAlan Cox 			} else if (vaddr == fs.entry->next_read) {
58613458803SAlan Cox 				/*
5875268042bSAlan Cox 				 * This is a sequential fault.  Arithmetically
5885268042bSAlan Cox 				 * increase the requested number of pages in
5895268042bSAlan Cox 				 * the read-ahead window.  The requested
5905268042bSAlan Cox 				 * number of pages is "# of sequential faults
5915268042bSAlan Cox 				 * x (read ahead min + 1) + read ahead min"
59213458803SAlan Cox 				 */
5935268042bSAlan Cox 				nera = VM_FAULT_READ_AHEAD_MIN;
5945268042bSAlan Cox 				if (era > 0) {
5955268042bSAlan Cox 					nera += era + 1;
59613458803SAlan Cox 					if (nera > VM_FAULT_READ_AHEAD_MAX)
59713458803SAlan Cox 						nera = VM_FAULT_READ_AHEAD_MAX;
5985268042bSAlan Cox 				}
59913458803SAlan Cox 				if (era == VM_FAULT_READ_AHEAD_MAX)
6000c3a4893SAlan Cox 					vm_fault_dontneed(&fs, vaddr, nera);
6015268042bSAlan Cox 			} else {
6025268042bSAlan Cox 				/*
6030c3a4893SAlan Cox 				 * This is a non-sequential fault.
6045268042bSAlan Cox 				 */
6055268042bSAlan Cox 				nera = 0;
606867a482dSJohn Dyson 			}
6070c3a4893SAlan Cox 			if (era != nera) {
6080c3a4893SAlan Cox 				/*
6090c3a4893SAlan Cox 				 * A read lock on the map suffices to update
6100c3a4893SAlan Cox 				 * the read ahead count safely.
6110c3a4893SAlan Cox 				 */
6125268042bSAlan Cox 				fs.entry->read_ahead = nera;
6130c3a4893SAlan Cox 			}
614d2bf64c3SKonstantin Belousov 
615d2bf64c3SKonstantin Belousov 			/*
6160c3a4893SAlan Cox 			 * Prepare for unlocking the map.  Save the map
6170c3a4893SAlan Cox 			 * entry's start and end addresses, which are used to
6180c3a4893SAlan Cox 			 * optimize the size of the pager operation below.
6190c3a4893SAlan Cox 			 * Even if the map entry's addresses change after
6200c3a4893SAlan Cox 			 * unlocking the map, using the saved addresses is
6210c3a4893SAlan Cox 			 * safe.
6220c3a4893SAlan Cox 			 */
6230c3a4893SAlan Cox 			e_start = fs.entry->start;
6240c3a4893SAlan Cox 			e_end = fs.entry->end;
6250c3a4893SAlan Cox 		}
6260c3a4893SAlan Cox 
6270c3a4893SAlan Cox 		/*
6280c3a4893SAlan Cox 		 * Call the pager to retrieve the page if there is a chance
6290c3a4893SAlan Cox 		 * that the pager has it, and potentially retrieve additional
6300c3a4893SAlan Cox 		 * pages at the same time.
6310c3a4893SAlan Cox 		 */
6320c3a4893SAlan Cox 		if (fs.object->type != OBJT_DEFAULT) {
6330c3a4893SAlan Cox 			/*
6340c3a4893SAlan Cox 			 * We have either allocated a new page or found an
6350c3a4893SAlan Cox 			 * existing page that is only partially valid.  We
6360c3a4893SAlan Cox 			 * hold a reference on fs.object and the page is
6370c3a4893SAlan Cox 			 * exclusive busied.
638d2bf64c3SKonstantin Belousov 			 */
639d2bf64c3SKonstantin Belousov 			unlock_map(&fs);
640d2bf64c3SKonstantin Belousov 
641d2bf64c3SKonstantin Belousov 			if (fs.object->type == OBJT_VNODE) {
642d2bf64c3SKonstantin Belousov 				vp = fs.object->handle;
643d2bf64c3SKonstantin Belousov 				if (vp == fs.vp)
644d2bf64c3SKonstantin Belousov 					goto vnode_locked;
645d2bf64c3SKonstantin Belousov 				else if (fs.vp != NULL) {
646d2bf64c3SKonstantin Belousov 					vput(fs.vp);
647d2bf64c3SKonstantin Belousov 					fs.vp = NULL;
648d2bf64c3SKonstantin Belousov 				}
649d2bf64c3SKonstantin Belousov 				locked = VOP_ISLOCKED(vp);
650d2bf64c3SKonstantin Belousov 
651d2bf64c3SKonstantin Belousov 				if (locked != LK_EXCLUSIVE)
652d2bf64c3SKonstantin Belousov 					locked = LK_SHARED;
653d2bf64c3SKonstantin Belousov 				/* Do not sleep for vnode lock while fs.m is busy */
654d2bf64c3SKonstantin Belousov 				error = vget(vp, locked | LK_CANRECURSE |
655d2bf64c3SKonstantin Belousov 				    LK_NOWAIT, curthread);
656d2bf64c3SKonstantin Belousov 				if (error != 0) {
657d2bf64c3SKonstantin Belousov 					vhold(vp);
658d2bf64c3SKonstantin Belousov 					release_page(&fs);
659d2bf64c3SKonstantin Belousov 					unlock_and_deallocate(&fs);
660d2bf64c3SKonstantin Belousov 					error = vget(vp, locked | LK_RETRY |
661d2bf64c3SKonstantin Belousov 					    LK_CANRECURSE, curthread);
662d2bf64c3SKonstantin Belousov 					vdrop(vp);
663d2bf64c3SKonstantin Belousov 					fs.vp = vp;
664d2bf64c3SKonstantin Belousov 					KASSERT(error == 0,
665d2bf64c3SKonstantin Belousov 					    ("vm_fault: vget failed"));
666d2bf64c3SKonstantin Belousov 					goto RetryFault;
667d2bf64c3SKonstantin Belousov 				}
668d2bf64c3SKonstantin Belousov 				fs.vp = vp;
669d2bf64c3SKonstantin Belousov 			}
670d2bf64c3SKonstantin Belousov vnode_locked:
671d2bf64c3SKonstantin Belousov 			KASSERT(fs.vp == NULL || !fs.map->system_map,
672d2bf64c3SKonstantin Belousov 			    ("vm_fault: vnode-backed object mapped by system map"));
673d2bf64c3SKonstantin Belousov 
674df8bae1dSRodney W. Grimes 			/*
675b0cd2017SGleb Smirnoff 			 * Page in the requested page and hint the pager,
676b0cd2017SGleb Smirnoff 			 * that it may bring up surrounding pages.
67726f9a767SRodney W. Grimes 			 */
6780c3a4893SAlan Cox 			if (nera == -1 || behavior == MAP_ENTRY_BEHAV_RANDOM ||
6790c3a4893SAlan Cox 			    P_KILLED(curproc)) {
6800c3a4893SAlan Cox 				behind = 0;
6810c3a4893SAlan Cox 				ahead = 0;
6820c3a4893SAlan Cox 			} else {
6830c3a4893SAlan Cox 				/* Is this a sequential fault? */
6840c3a4893SAlan Cox 				if (nera > 0) {
6850c3a4893SAlan Cox 					behind = 0;
6860c3a4893SAlan Cox 					ahead = nera;
6870c3a4893SAlan Cox 				} else {
6880c3a4893SAlan Cox 					/*
6890c3a4893SAlan Cox 					 * Request a cluster of pages that is
6900c3a4893SAlan Cox 					 * aligned to a VM_FAULT_READ_DEFAULT
6910c3a4893SAlan Cox 					 * page offset boundary within the
6920c3a4893SAlan Cox 					 * object.  Alignment to a page offset
6930c3a4893SAlan Cox 					 * boundary is more likely to coincide
6940c3a4893SAlan Cox 					 * with the underlying file system
6950c3a4893SAlan Cox 					 * block than alignment to a virtual
6960c3a4893SAlan Cox 					 * address boundary.
6970c3a4893SAlan Cox 					 */
6980c3a4893SAlan Cox 					cluster_offset = fs.pindex %
6990c3a4893SAlan Cox 					    VM_FAULT_READ_DEFAULT;
7000c3a4893SAlan Cox 					behind = ulmin(cluster_offset,
7010c3a4893SAlan Cox 					    atop(vaddr - e_start));
7020c3a4893SAlan Cox 					ahead = VM_FAULT_READ_DEFAULT - 1 -
7030c3a4893SAlan Cox 					    cluster_offset;
7040c3a4893SAlan Cox 				}
7050c3a4893SAlan Cox 				ahead = ulmin(ahead, atop(e_end - vaddr) - 1);
7060c3a4893SAlan Cox 			}
707b0cd2017SGleb Smirnoff 			rv = vm_pager_get_pages(fs.object, &fs.m, 1,
708b0cd2017SGleb Smirnoff 			    &behind, &ahead);
70926f9a767SRodney W. Grimes 			if (rv == VM_PAGER_OK) {
710b0cd2017SGleb Smirnoff 				faultcount = behind + 1 + ahead;
71126f9a767SRodney W. Grimes 				hardfault++;
7121c7c3c6aSMatthew Dillon 				break; /* break to PAGE HAS BEEN FOUND */
713df8bae1dSRodney W. Grimes 			}
714a83c285cSDavid Greenman 			if (rv == VM_PAGER_ERROR)
715f3679e35SDavid Greenman 				printf("vm_fault: pager read error, pid %d (%s)\n",
716f3679e35SDavid Greenman 				    curproc->p_pid, curproc->p_comm);
717521ddf39SAlan Cox 
71826f9a767SRodney W. Grimes 			/*
719521ddf39SAlan Cox 			 * If an I/O error occurred or the requested page was
720521ddf39SAlan Cox 			 * outside the range of the pager, clean up and return
721521ddf39SAlan Cox 			 * an error.
72226f9a767SRodney W. Grimes 			 */
723521ddf39SAlan Cox 			if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) {
7242965a453SKip Macy 				vm_page_lock(fs.m);
725*230afe0bSKonstantin Belousov 				if (fs.m->wire_count == 0)
7264866e085SJohn Dyson 					vm_page_free(fs.m);
727*230afe0bSKonstantin Belousov 				else
728*230afe0bSKonstantin Belousov 					vm_page_xunbusy_maybelocked(fs.m);
7292965a453SKip Macy 				vm_page_unlock(fs.m);
7304866e085SJohn Dyson 				fs.m = NULL;
7314866e085SJohn Dyson 				unlock_and_deallocate(&fs);
732521ddf39SAlan Cox 				return (rv == VM_PAGER_ERROR ? KERN_FAILURE :
733521ddf39SAlan Cox 				    KERN_PROTECTION_FAILURE);
73426f9a767SRodney W. Grimes 			}
735521ddf39SAlan Cox 
736521ddf39SAlan Cox 			/*
737521ddf39SAlan Cox 			 * The requested page does not exist at this object/
738521ddf39SAlan Cox 			 * offset.  Remove the invalid page from the object,
739521ddf39SAlan Cox 			 * waking up anyone waiting for it, and continue on to
740521ddf39SAlan Cox 			 * the next object.  However, if this is the top-level
741521ddf39SAlan Cox 			 * object, we must leave the busy page in place to
742521ddf39SAlan Cox 			 * prevent another process from rushing past us, and
743521ddf39SAlan Cox 			 * inserting the page in that object at the same time
744521ddf39SAlan Cox 			 * that we are.
745521ddf39SAlan Cox 			 */
7464866e085SJohn Dyson 			if (fs.object != fs.first_object) {
7472965a453SKip Macy 				vm_page_lock(fs.m);
748*230afe0bSKonstantin Belousov 				if (fs.m->wire_count == 0)
7494866e085SJohn Dyson 					vm_page_free(fs.m);
750*230afe0bSKonstantin Belousov 				else
751*230afe0bSKonstantin Belousov 					vm_page_xunbusy_maybelocked(fs.m);
7522965a453SKip Macy 				vm_page_unlock(fs.m);
7534866e085SJohn Dyson 				fs.m = NULL;
754df8bae1dSRodney W. Grimes 			}
755df8bae1dSRodney W. Grimes 		}
75640360b1bSMatthew Dillon 
757df8bae1dSRodney W. Grimes 		/*
7581c7c3c6aSMatthew Dillon 		 * We get here if the object has default pager (or unwiring)
7591c7c3c6aSMatthew Dillon 		 * or the pager doesn't have the page.
760df8bae1dSRodney W. Grimes 		 */
7614866e085SJohn Dyson 		if (fs.object == fs.first_object)
7624866e085SJohn Dyson 			fs.first_m = fs.m;
763df8bae1dSRodney W. Grimes 
764df8bae1dSRodney W. Grimes 		/*
7650d94caffSDavid Greenman 		 * Move on to the next object.  Lock the next object before
7660d94caffSDavid Greenman 		 * unlocking the current one.
767df8bae1dSRodney W. Grimes 		 */
7684866e085SJohn Dyson 		next_object = fs.object->backing_object;
769df8bae1dSRodney W. Grimes 		if (next_object == NULL) {
770df8bae1dSRodney W. Grimes 			/*
7710d94caffSDavid Greenman 			 * If there's no object left, fill the page in the top
7720d94caffSDavid Greenman 			 * object with zeros.
773df8bae1dSRodney W. Grimes 			 */
7744866e085SJohn Dyson 			if (fs.object != fs.first_object) {
7754866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
77689f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(fs.object);
777df8bae1dSRodney W. Grimes 
7784866e085SJohn Dyson 				fs.object = fs.first_object;
7794866e085SJohn Dyson 				fs.pindex = fs.first_pindex;
7804866e085SJohn Dyson 				fs.m = fs.first_m;
78189f6b863SAttilio Rao 				VM_OBJECT_WLOCK(fs.object);
782df8bae1dSRodney W. Grimes 			}
7834866e085SJohn Dyson 			fs.first_m = NULL;
784df8bae1dSRodney W. Grimes 
7854221e284SAlan Cox 			/*
7864221e284SAlan Cox 			 * Zero the page if necessary and mark it valid.
7874221e284SAlan Cox 			 */
7884866e085SJohn Dyson 			if ((fs.m->flags & PG_ZERO) == 0) {
789fff6062aSAlan Cox 				pmap_zero_page(fs.m);
7904221e284SAlan Cox 			} else {
79167596082SAttilio Rao 				PCPU_INC(cnt.v_ozfod);
7924221e284SAlan Cox 			}
79367596082SAttilio Rao 			PCPU_INC(cnt.v_zfod);
7944221e284SAlan Cox 			fs.m->valid = VM_PAGE_BITS_ALL;
7957b9b301cSAlan Cox 			/* Don't try to prefault neighboring pages. */
7967b9b301cSAlan Cox 			faultcount = 1;
7971c7c3c6aSMatthew Dillon 			break;	/* break to PAGE HAS BEEN FOUND */
7980d94caffSDavid Greenman 		} else {
799c8567c3aSAlan Cox 			KASSERT(fs.object != next_object,
800c8567c3aSAlan Cox 			    ("object loop %p", next_object));
80189f6b863SAttilio Rao 			VM_OBJECT_WLOCK(next_object);
802c8567c3aSAlan Cox 			vm_object_pip_add(next_object, 1);
803c8567c3aSAlan Cox 			if (fs.object != fs.first_object)
8044866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
8056753423cSAlan Cox 			fs.pindex +=
8066753423cSAlan Cox 			    OFF_TO_IDX(fs.object->backing_object_offset);
80789f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(fs.object);
8084866e085SJohn Dyson 			fs.object = next_object;
809df8bae1dSRodney W. Grimes 		}
810df8bae1dSRodney W. Grimes 	}
8111c7c3c6aSMatthew Dillon 
812c7aebda8SAttilio Rao 	vm_page_assert_xbusied(fs.m);
813df8bae1dSRodney W. Grimes 
814df8bae1dSRodney W. Grimes 	/*
8150d94caffSDavid Greenman 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
816df8bae1dSRodney W. Grimes 	 * is held.]
817df8bae1dSRodney W. Grimes 	 */
818df8bae1dSRodney W. Grimes 
819df8bae1dSRodney W. Grimes 	/*
8200d94caffSDavid Greenman 	 * If the page is being written, but isn't already owned by the
8210d94caffSDavid Greenman 	 * top-level object, we have to copy it into a new page owned by the
8220d94caffSDavid Greenman 	 * top-level object.
823df8bae1dSRodney W. Grimes 	 */
8244866e085SJohn Dyson 	if (fs.object != fs.first_object) {
825df8bae1dSRodney W. Grimes 		/*
8260d94caffSDavid Greenman 		 * We only really need to copy if we want to write it.
827df8bae1dSRodney W. Grimes 		 */
828a6d42a0dSAlan Cox 		if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
829df8bae1dSRodney W. Grimes 			/*
8301c7c3c6aSMatthew Dillon 			 * This allows pages to be virtually copied from a
8311c7c3c6aSMatthew Dillon 			 * backing_object into the first_object, where the
8321c7c3c6aSMatthew Dillon 			 * backing object has no other refs to it, and cannot
8331c7c3c6aSMatthew Dillon 			 * gain any more refs.  Instead of a bcopy, we just
8341c7c3c6aSMatthew Dillon 			 * move the page from the backing object to the
8351c7c3c6aSMatthew Dillon 			 * first object.  Note that we must mark the page
8361c7c3c6aSMatthew Dillon 			 * dirty in the first object so that it will go out
8371c7c3c6aSMatthew Dillon 			 * to swap when needed.
838df8bae1dSRodney W. Grimes 			 */
839ebf75125SAlan Cox 			is_first_object_locked = FALSE;
840e50346b5SAlan Cox 			if (
841de5f6a77SJohn Dyson 				/*
842de5f6a77SJohn Dyson 				 * Only one shadow object
843de5f6a77SJohn Dyson 				 */
8444866e085SJohn Dyson 				(fs.object->shadow_count == 1) &&
845de5f6a77SJohn Dyson 				/*
846de5f6a77SJohn Dyson 				 * No COW refs, except us
847de5f6a77SJohn Dyson 				 */
8484866e085SJohn Dyson 				(fs.object->ref_count == 1) &&
849de5f6a77SJohn Dyson 				/*
8505929bcfaSPhilippe Charnier 				 * No one else can look this object up
851de5f6a77SJohn Dyson 				 */
8524866e085SJohn Dyson 				(fs.object->handle == NULL) &&
853de5f6a77SJohn Dyson 				/*
854de5f6a77SJohn Dyson 				 * No other ways to look the object up
855de5f6a77SJohn Dyson 				 */
8564866e085SJohn Dyson 				((fs.object->type == OBJT_DEFAULT) ||
8574866e085SJohn Dyson 				 (fs.object->type == OBJT_SWAP)) &&
85889f6b863SAttilio Rao 			    (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
859de5f6a77SJohn Dyson 				/*
860de5f6a77SJohn Dyson 				 * We don't chase down the shadow chain
861de5f6a77SJohn Dyson 				 */
862e50346b5SAlan Cox 			    fs.object == fs.first_object->backing_object) {
863bccdea45SAlan Cox 				vm_page_lock(fs.m);
864bccdea45SAlan Cox 				vm_page_remove(fs.m);
865bccdea45SAlan Cox 				vm_page_unlock(fs.m);
866eb00b276SAlan Cox 				vm_page_lock(fs.first_m);
867bccdea45SAlan Cox 				vm_page_replace_checked(fs.m, fs.first_object,
868bccdea45SAlan Cox 				    fs.first_pindex, fs.first_m);
8696fee422eSConrad Meyer 				vm_page_free(fs.first_m);
8706fee422eSConrad Meyer 				vm_page_unlock(fs.first_m);
871bccdea45SAlan Cox 				vm_page_dirty(fs.m);
872dfdf9abdSAlan Cox #if VM_NRESERVLEVEL > 0
873dfdf9abdSAlan Cox 				/*
874dfdf9abdSAlan Cox 				 * Rename the reservation.
875dfdf9abdSAlan Cox 				 */
876dfdf9abdSAlan Cox 				vm_reserv_rename(fs.m, fs.first_object,
877dfdf9abdSAlan Cox 				    fs.object, OFF_TO_IDX(
878dfdf9abdSAlan Cox 				    fs.first_object->backing_object_offset));
879dfdf9abdSAlan Cox #endif
880bccdea45SAlan Cox 				/*
881bccdea45SAlan Cox 				 * Removing the page from the backing object
882bccdea45SAlan Cox 				 * unbusied it.
883bccdea45SAlan Cox 				 */
884c7aebda8SAttilio Rao 				vm_page_xbusy(fs.m);
885d98ddc46SAlan Cox 				fs.first_m = fs.m;
8864866e085SJohn Dyson 				fs.m = NULL;
88767596082SAttilio Rao 				PCPU_INC(cnt.v_cow_optim);
888de5f6a77SJohn Dyson 			} else {
889de5f6a77SJohn Dyson 				/*
890de5f6a77SJohn Dyson 				 * Oh, well, lets copy it.
891de5f6a77SJohn Dyson 				 */
892669890eaSAlan Cox 				pmap_copy_page(fs.m, fs.first_m);
893669890eaSAlan Cox 				fs.first_m->valid = VM_PAGE_BITS_ALL;
894d8778512SAlan Cox 				if (wired && (fault_flags &
8956a875bf9SKonstantin Belousov 				    VM_FAULT_WIRE) == 0) {
8962965a453SKip Macy 					vm_page_lock(fs.first_m);
897d8778512SAlan Cox 					vm_page_wire(fs.first_m);
8982965a453SKip Macy 					vm_page_unlock(fs.first_m);
8992965a453SKip Macy 
9002965a453SKip Macy 					vm_page_lock(fs.m);
9013ae10f74SAttilio Rao 					vm_page_unwire(fs.m, PQ_INACTIVE);
9022965a453SKip Macy 					vm_page_unlock(fs.m);
903de5f6a77SJohn Dyson 				}
904df8bae1dSRodney W. Grimes 				/*
905df8bae1dSRodney W. Grimes 				 * We no longer need the old page or object.
906df8bae1dSRodney W. Grimes 				 */
9074866e085SJohn Dyson 				release_page(&fs);
908de5f6a77SJohn Dyson 			}
9091c7c3c6aSMatthew Dillon 			/*
9101c7c3c6aSMatthew Dillon 			 * fs.object != fs.first_object due to above
9111c7c3c6aSMatthew Dillon 			 * conditional
9121c7c3c6aSMatthew Dillon 			 */
9134866e085SJohn Dyson 			vm_object_pip_wakeup(fs.object);
91489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(fs.object);
915df8bae1dSRodney W. Grimes 			/*
916df8bae1dSRodney W. Grimes 			 * Only use the new page below...
917df8bae1dSRodney W. Grimes 			 */
9184866e085SJohn Dyson 			fs.object = fs.first_object;
9194866e085SJohn Dyson 			fs.pindex = fs.first_pindex;
920d98ddc46SAlan Cox 			fs.m = fs.first_m;
921f29ba63eSAlan Cox 			if (!is_first_object_locked)
92289f6b863SAttilio Rao 				VM_OBJECT_WLOCK(fs.object);
92367596082SAttilio Rao 			PCPU_INC(cnt.v_cow_faults);
9244d34e019SKonstantin Belousov 			curthread->td_cow++;
9250d94caffSDavid Greenman 		} else {
926df8bae1dSRodney W. Grimes 			prot &= ~VM_PROT_WRITE;
927df8bae1dSRodney W. Grimes 		}
928df8bae1dSRodney W. Grimes 	}
929df8bae1dSRodney W. Grimes 
930df8bae1dSRodney W. Grimes 	/*
9310d94caffSDavid Greenman 	 * We must verify that the maps have not changed since our last
9320d94caffSDavid Greenman 	 * lookup.
933df8bae1dSRodney W. Grimes 	 */
93419dc5607STor Egge 	if (!fs.lookup_still_valid) {
935df8bae1dSRodney W. Grimes 		vm_object_t retry_object;
936a316d390SJohn Dyson 		vm_pindex_t retry_pindex;
937df8bae1dSRodney W. Grimes 		vm_prot_t retry_prot;
938df8bae1dSRodney W. Grimes 
93919dc5607STor Egge 		if (!vm_map_trylock_read(fs.map)) {
940b823bbd6SMatthew Dillon 			release_page(&fs);
941b823bbd6SMatthew Dillon 			unlock_and_deallocate(&fs);
942b823bbd6SMatthew Dillon 			goto RetryFault;
943b823bbd6SMatthew Dillon 		}
94419dc5607STor Egge 		fs.lookup_still_valid = TRUE;
94519dc5607STor Egge 		if (fs.map->timestamp != map_generation) {
94619dc5607STor Egge 			result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
9474866e085SJohn Dyson 			    &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
948df8bae1dSRodney W. Grimes 
949df8bae1dSRodney W. Grimes 			/*
95044ed3417STor Egge 			 * If we don't need the page any longer, put it on the inactive
9510d94caffSDavid Greenman 			 * list (the easiest thing to do here).  If no one needs it,
9520d94caffSDavid Greenman 			 * pageout will grab it eventually.
953df8bae1dSRodney W. Grimes 			 */
954df8bae1dSRodney W. Grimes 			if (result != KERN_SUCCESS) {
9554866e085SJohn Dyson 				release_page(&fs);
9564866e085SJohn Dyson 				unlock_and_deallocate(&fs);
95719dc5607STor Egge 
95819dc5607STor Egge 				/*
95919dc5607STor Egge 				 * If retry of map lookup would have blocked then
96019dc5607STor Egge 				 * retry fault from start.
96119dc5607STor Egge 				 */
96219dc5607STor Egge 				if (result == KERN_FAILURE)
96319dc5607STor Egge 					goto RetryFault;
964df8bae1dSRodney W. Grimes 				return (result);
965df8bae1dSRodney W. Grimes 			}
9664866e085SJohn Dyson 			if ((retry_object != fs.first_object) ||
9674866e085SJohn Dyson 			    (retry_pindex != fs.first_pindex)) {
9684866e085SJohn Dyson 				release_page(&fs);
9694866e085SJohn Dyson 				unlock_and_deallocate(&fs);
970df8bae1dSRodney W. Grimes 				goto RetryFault;
971df8bae1dSRodney W. Grimes 			}
97219dc5607STor Egge 
973df8bae1dSRodney W. Grimes 			/*
9740d94caffSDavid Greenman 			 * Check whether the protection has changed or the object has
9750d94caffSDavid Greenman 			 * been copied while we left the map unlocked. Changing from
9760d94caffSDavid Greenman 			 * read to write permission is OK - we leave the page
9770d94caffSDavid Greenman 			 * write-protected, and catch the write fault. Changing from
9780d94caffSDavid Greenman 			 * write to read permission means that we can't mark the page
9790d94caffSDavid Greenman 			 * write-enabled after all.
980df8bae1dSRodney W. Grimes 			 */
981df8bae1dSRodney W. Grimes 			prot &= retry_prot;
982df8bae1dSRodney W. Grimes 		}
98319dc5607STor Egge 	}
984381b7242SAlan Cox 
985d2bf64c3SKonstantin Belousov 	/*
986381b7242SAlan Cox 	 * If the page was filled by a pager, save the virtual address that
987381b7242SAlan Cox 	 * should be faulted on next under a sequential access pattern to the
988381b7242SAlan Cox 	 * map entry.  A read lock on the map suffices to update this address
989381b7242SAlan Cox 	 * safely.
990d2bf64c3SKonstantin Belousov 	 */
9915758fe71SAlan Cox 	if (hardfault)
992381b7242SAlan Cox 		fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE;
993d2bf64c3SKonstantin Belousov 
994a36f5532SKonstantin Belousov 	vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags, TRUE);
995c7aebda8SAttilio Rao 	vm_page_assert_xbusied(fs.m);
996c7aebda8SAttilio Rao 
9974221e284SAlan Cox 	/*
99878cfe1f7SAlan Cox 	 * Page must be completely valid or it is not fit to
9994221e284SAlan Cox 	 * map into user space.  vm_pager_get_pages() ensures this.
10004221e284SAlan Cox 	 */
100178cfe1f7SAlan Cox 	KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
100278cfe1f7SAlan Cox 	    ("vm_fault: page %p partially invalid", fs.m));
100389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(fs.object);
1004cbfbaad8SAlan Cox 
100586735996SAlan Cox 	/*
100686735996SAlan Cox 	 * Put this page into the physical map.  We had to do the unlock above
100786735996SAlan Cox 	 * because pmap_enter() may sleep.  We don't put the page
100886735996SAlan Cox 	 * back on the active queue until later so that the pageout daemon
100986735996SAlan Cox 	 * won't find it (yet).
101086735996SAlan Cox 	 */
101139ffa8c1SKonstantin Belousov 	pmap_enter(fs.map->pmap, vaddr, fs.m, prot,
101239ffa8c1SKonstantin Belousov 	    fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0);
10136a875bf9SKonstantin Belousov 	if (faultcount != 1 && (fault_flags & VM_FAULT_WIRE) == 0 &&
10147b9b301cSAlan Cox 	    wired == 0)
1015b0cd2017SGleb Smirnoff 		vm_fault_prefault(&fs, vaddr,
1016b0cd2017SGleb Smirnoff 		    faultcount > 0 ? behind : PFBAK,
1017b0cd2017SGleb Smirnoff 		    faultcount > 0 ? ahead : PFFOR);
101889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(fs.object);
10192965a453SKip Macy 	vm_page_lock(fs.m);
1020ff97964aSJohn Dyson 
1021df8bae1dSRodney W. Grimes 	/*
10220d94caffSDavid Greenman 	 * If the page is not wired down, then put it where the pageout daemon
10230d94caffSDavid Greenman 	 * can find it.
1024df8bae1dSRodney W. Grimes 	 */
10256a875bf9SKonstantin Belousov 	if ((fault_flags & VM_FAULT_WIRE) != 0) {
10266a875bf9SKonstantin Belousov 		KASSERT(wired, ("VM_FAULT_WIRE && !wired"));
10274866e085SJohn Dyson 		vm_page_wire(fs.m);
102803679e23SAlan Cox 	} else
10294866e085SJohn Dyson 		vm_page_activate(fs.m);
1030acd11c74SAlan Cox 	if (m_hold != NULL) {
1031acd11c74SAlan Cox 		*m_hold = fs.m;
1032acd11c74SAlan Cox 		vm_page_hold(fs.m);
1033acd11c74SAlan Cox 	}
10342965a453SKip Macy 	vm_page_unlock(fs.m);
1035c7aebda8SAttilio Rao 	vm_page_xunbusy(fs.m);
1036eeec6babSJohn Baldwin 
1037eebf3286SAlan Cox 	/*
1038eebf3286SAlan Cox 	 * Unlock everything, and return
1039eebf3286SAlan Cox 	 */
1040eebf3286SAlan Cox 	unlock_and_deallocate(&fs);
1041b3a01bdfSAndrey Zonov 	if (hardfault) {
1042b3a01bdfSAndrey Zonov 		PCPU_INC(cnt.v_io_faults);
10431c4bcd05SJeff Roberson 		curthread->td_ru.ru_majflt++;
1044ae34b6ffSEdward Tomasz Napierala #ifdef RACCT
1045ae34b6ffSEdward Tomasz Napierala 		if (racct_enable && fs.object->type == OBJT_VNODE) {
1046ae34b6ffSEdward Tomasz Napierala 			PROC_LOCK(curproc);
1047ae34b6ffSEdward Tomasz Napierala 			if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1048ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_WRITEBPS,
1049ae34b6ffSEdward Tomasz Napierala 				    PAGE_SIZE + behind * PAGE_SIZE);
1050ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_WRITEIOPS, 1);
1051ae34b6ffSEdward Tomasz Napierala 			} else {
1052ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_READBPS,
1053ae34b6ffSEdward Tomasz Napierala 				    PAGE_SIZE + ahead * PAGE_SIZE);
1054ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_READIOPS, 1);
1055ae34b6ffSEdward Tomasz Napierala 			}
1056ae34b6ffSEdward Tomasz Napierala 			PROC_UNLOCK(curproc);
1057ae34b6ffSEdward Tomasz Napierala 		}
1058ae34b6ffSEdward Tomasz Napierala #endif
1059b3a01bdfSAndrey Zonov 	} else
10601c4bcd05SJeff Roberson 		curthread->td_ru.ru_minflt++;
1061df8bae1dSRodney W. Grimes 
1062df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1063df8bae1dSRodney W. Grimes }
1064df8bae1dSRodney W. Grimes 
1065df8bae1dSRodney W. Grimes /*
1066a8b0f100SAlan Cox  * Speed up the reclamation of pages that precede the faulting pindex within
1067a8b0f100SAlan Cox  * the first object of the shadow chain.  Essentially, perform the equivalent
1068a8b0f100SAlan Cox  * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes
1069a8b0f100SAlan Cox  * the faulting pindex by the cluster size when the pages read by vm_fault()
1070a8b0f100SAlan Cox  * cross a cluster-size boundary.  The cluster size is the greater of the
1071a8b0f100SAlan Cox  * smallest superpage size and VM_FAULT_DONTNEED_MIN.
1072a8b0f100SAlan Cox  *
1073a8b0f100SAlan Cox  * When "fs->first_object" is a shadow object, the pages in the backing object
1074a8b0f100SAlan Cox  * that precede the faulting pindex are deactivated by vm_fault().  So, this
1075a8b0f100SAlan Cox  * function must only be concerned with pages in the first object.
107613458803SAlan Cox  */
107713458803SAlan Cox static void
1078a8b0f100SAlan Cox vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead)
107913458803SAlan Cox {
1080a8b0f100SAlan Cox 	vm_map_entry_t entry;
108113458803SAlan Cox 	vm_object_t first_object, object;
1082a8b0f100SAlan Cox 	vm_offset_t end, start;
1083a8b0f100SAlan Cox 	vm_page_t m, m_next;
1084a8b0f100SAlan Cox 	vm_pindex_t pend, pstart;
1085a8b0f100SAlan Cox 	vm_size_t size;
108613458803SAlan Cox 
108713458803SAlan Cox 	object = fs->object;
108889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
108913458803SAlan Cox 	first_object = fs->first_object;
109013458803SAlan Cox 	if (first_object != object) {
1091b5ab20c0SAlan Cox 		if (!VM_OBJECT_TRYWLOCK(first_object)) {
109289f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
1093b5ab20c0SAlan Cox 			VM_OBJECT_WLOCK(first_object);
109489f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
109513458803SAlan Cox 		}
109613458803SAlan Cox 	}
1097a8b0f100SAlan Cox 	/* Neither fictitious nor unmanaged pages can be reclaimed. */
109828634820SAlan Cox 	if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
1099a8b0f100SAlan Cox 		size = VM_FAULT_DONTNEED_MIN;
1100a8b0f100SAlan Cox 		if (MAXPAGESIZES > 1 && size < pagesizes[1])
1101a8b0f100SAlan Cox 			size = pagesizes[1];
1102a8b0f100SAlan Cox 		end = rounddown2(vaddr, size);
1103a8b0f100SAlan Cox 		if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) &&
1104a8b0f100SAlan Cox 		    (entry = fs->entry)->start < end) {
1105a8b0f100SAlan Cox 			if (end - entry->start < size)
1106a8b0f100SAlan Cox 				start = entry->start;
110713458803SAlan Cox 			else
1108a8b0f100SAlan Cox 				start = end - size;
1109a8b0f100SAlan Cox 			pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED);
1110a8b0f100SAlan Cox 			pstart = OFF_TO_IDX(entry->offset) + atop(start -
1111a8b0f100SAlan Cox 			    entry->start);
1112a8b0f100SAlan Cox 			m_next = vm_page_find_least(first_object, pstart);
1113a8b0f100SAlan Cox 			pend = OFF_TO_IDX(entry->offset) + atop(end -
1114a8b0f100SAlan Cox 			    entry->start);
1115a8b0f100SAlan Cox 			while ((m = m_next) != NULL && m->pindex < pend) {
1116a8b0f100SAlan Cox 				m_next = TAILQ_NEXT(m, listq);
1117a8b0f100SAlan Cox 				if (m->valid != VM_PAGE_BITS_ALL ||
1118a8b0f100SAlan Cox 				    vm_page_busied(m))
111913458803SAlan Cox 					continue;
1120d8015db3SAlan Cox 
1121d8015db3SAlan Cox 				/*
1122d8015db3SAlan Cox 				 * Don't clear PGA_REFERENCED, since it would
1123d8015db3SAlan Cox 				 * likely represent a reference by a different
1124d8015db3SAlan Cox 				 * process.
1125d8015db3SAlan Cox 				 *
1126d8015db3SAlan Cox 				 * Typically, at this point, prefetched pages
1127d8015db3SAlan Cox 				 * are still in the inactive queue.  Only
1128d8015db3SAlan Cox 				 * pages that triggered page faults are in the
1129d8015db3SAlan Cox 				 * active queue.
1130d8015db3SAlan Cox 				 */
113113458803SAlan Cox 				vm_page_lock(m);
1132d8015db3SAlan Cox 				vm_page_deactivate(m);
113313458803SAlan Cox 				vm_page_unlock(m);
113413458803SAlan Cox 			}
113513458803SAlan Cox 		}
1136a8b0f100SAlan Cox 	}
113713458803SAlan Cox 	if (first_object != object)
1138b5ab20c0SAlan Cox 		VM_OBJECT_WUNLOCK(first_object);
113913458803SAlan Cox }
114013458803SAlan Cox 
114113458803SAlan Cox /*
1142566526a9SAlan Cox  * vm_fault_prefault provides a quick way of clustering
1143566526a9SAlan Cox  * pagefaults into a processes address space.  It is a "cousin"
1144566526a9SAlan Cox  * of vm_map_pmap_enter, except it runs at page fault time instead
1145566526a9SAlan Cox  * of mmap time.
1146566526a9SAlan Cox  */
1147566526a9SAlan Cox static void
114863281952SAlan Cox vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
1149b0cd2017SGleb Smirnoff     int backward, int forward)
1150566526a9SAlan Cox {
115163281952SAlan Cox 	pmap_t pmap;
115263281952SAlan Cox 	vm_map_entry_t entry;
115363281952SAlan Cox 	vm_object_t backing_object, lobject;
1154566526a9SAlan Cox 	vm_offset_t addr, starta;
1155566526a9SAlan Cox 	vm_pindex_t pindex;
11562053c127SStephan Uphoff 	vm_page_t m;
1157b0cd2017SGleb Smirnoff 	int i;
1158566526a9SAlan Cox 
115963281952SAlan Cox 	pmap = fs->map->pmap;
1160950d5f7aSAlan Cox 	if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1161566526a9SAlan Cox 		return;
1162566526a9SAlan Cox 
116363281952SAlan Cox 	entry = fs->entry;
1164566526a9SAlan Cox 
116563281952SAlan Cox 	starta = addra - backward * PAGE_SIZE;
1166566526a9SAlan Cox 	if (starta < entry->start) {
1167566526a9SAlan Cox 		starta = entry->start;
1168566526a9SAlan Cox 	} else if (starta > addra) {
1169566526a9SAlan Cox 		starta = 0;
1170566526a9SAlan Cox 	}
1171566526a9SAlan Cox 
117263281952SAlan Cox 	/*
117363281952SAlan Cox 	 * Generate the sequence of virtual addresses that are candidates for
117463281952SAlan Cox 	 * prefaulting in an outward spiral from the faulting virtual address,
117563281952SAlan Cox 	 * "addra".  Specifically, the sequence is "addra - PAGE_SIZE", "addra
117663281952SAlan Cox 	 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
117763281952SAlan Cox 	 * If the candidate address doesn't have a backing physical page, then
117863281952SAlan Cox 	 * the loop immediately terminates.
117963281952SAlan Cox 	 */
118063281952SAlan Cox 	for (i = 0; i < 2 * imax(backward, forward); i++) {
118163281952SAlan Cox 		addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
118263281952SAlan Cox 		    PAGE_SIZE);
118363281952SAlan Cox 		if (addr > addra + forward * PAGE_SIZE)
1184566526a9SAlan Cox 			addr = 0;
1185566526a9SAlan Cox 
1186566526a9SAlan Cox 		if (addr < starta || addr >= entry->end)
1187566526a9SAlan Cox 			continue;
1188566526a9SAlan Cox 
1189566526a9SAlan Cox 		if (!pmap_is_prefaultable(pmap, addr))
1190566526a9SAlan Cox 			continue;
1191566526a9SAlan Cox 
1192566526a9SAlan Cox 		pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
119363281952SAlan Cox 		lobject = entry->object.vm_object;
1194c141ae7fSAlan Cox 		VM_OBJECT_RLOCK(lobject);
1195566526a9SAlan Cox 		while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1196566526a9SAlan Cox 		    lobject->type == OBJT_DEFAULT &&
1197566526a9SAlan Cox 		    (backing_object = lobject->backing_object) != NULL) {
119836930fc9SAlan Cox 			KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
119936930fc9SAlan Cox 			    0, ("vm_fault_prefault: unaligned object offset"));
1200566526a9SAlan Cox 			pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1201c141ae7fSAlan Cox 			VM_OBJECT_RLOCK(backing_object);
1202c141ae7fSAlan Cox 			VM_OBJECT_RUNLOCK(lobject);
1203566526a9SAlan Cox 			lobject = backing_object;
1204566526a9SAlan Cox 		}
1205cbfbaad8SAlan Cox 		if (m == NULL) {
1206c141ae7fSAlan Cox 			VM_OBJECT_RUNLOCK(lobject);
1207566526a9SAlan Cox 			break;
1208cbfbaad8SAlan Cox 		}
12090a2e596aSAlan Cox 		if (m->valid == VM_PAGE_BITS_ALL &&
12103c4a2440SAlan Cox 		    (m->flags & PG_FICTITIOUS) == 0)
12117bfda801SAlan Cox 			pmap_enter_quick(pmap, addr, m, entry->protection);
1212c141ae7fSAlan Cox 		VM_OBJECT_RUNLOCK(lobject);
1213566526a9SAlan Cox 	}
1214566526a9SAlan Cox }
1215566526a9SAlan Cox 
1216566526a9SAlan Cox /*
121782de724fSAlan Cox  * Hold each of the physical pages that are mapped by the specified range of
121882de724fSAlan Cox  * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
121982de724fSAlan Cox  * and allow the specified types of access, "prot".  If all of the implied
122082de724fSAlan Cox  * pages are successfully held, then the number of held pages is returned
122182de724fSAlan Cox  * together with pointers to those pages in the array "ma".  However, if any
122282de724fSAlan Cox  * of the pages cannot be held, -1 is returned.
122382de724fSAlan Cox  */
122482de724fSAlan Cox int
122582de724fSAlan Cox vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
122682de724fSAlan Cox     vm_prot_t prot, vm_page_t *ma, int max_count)
122782de724fSAlan Cox {
122882de724fSAlan Cox 	vm_offset_t end, va;
122982de724fSAlan Cox 	vm_page_t *mp;
12307e14088dSKonstantin Belousov 	int count;
123182de724fSAlan Cox 	boolean_t pmap_failed;
123282de724fSAlan Cox 
1233af32c419SKonstantin Belousov 	if (len == 0)
1234af32c419SKonstantin Belousov 		return (0);
123582de724fSAlan Cox 	end = round_page(addr + len);
123682de724fSAlan Cox 	addr = trunc_page(addr);
123782de724fSAlan Cox 
123882de724fSAlan Cox 	/*
123982de724fSAlan Cox 	 * Check for illegal addresses.
124082de724fSAlan Cox 	 */
124182de724fSAlan Cox 	if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
124282de724fSAlan Cox 		return (-1);
124382de724fSAlan Cox 
12447e14088dSKonstantin Belousov 	if (atop(end - addr) > max_count)
124582de724fSAlan Cox 		panic("vm_fault_quick_hold_pages: count > max_count");
12467e14088dSKonstantin Belousov 	count = atop(end - addr);
124782de724fSAlan Cox 
124882de724fSAlan Cox 	/*
124982de724fSAlan Cox 	 * Most likely, the physical pages are resident in the pmap, so it is
125082de724fSAlan Cox 	 * faster to try pmap_extract_and_hold() first.
125182de724fSAlan Cox 	 */
125282de724fSAlan Cox 	pmap_failed = FALSE;
125382de724fSAlan Cox 	for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
125482de724fSAlan Cox 		*mp = pmap_extract_and_hold(map->pmap, va, prot);
125582de724fSAlan Cox 		if (*mp == NULL)
125682de724fSAlan Cox 			pmap_failed = TRUE;
125782de724fSAlan Cox 		else if ((prot & VM_PROT_WRITE) != 0 &&
1258a5dbab54SAlan Cox 		    (*mp)->dirty != VM_PAGE_BITS_ALL) {
125982de724fSAlan Cox 			/*
126082de724fSAlan Cox 			 * Explicitly dirty the physical page.  Otherwise, the
126182de724fSAlan Cox 			 * caller's changes may go unnoticed because they are
126282de724fSAlan Cox 			 * performed through an unmanaged mapping or by a DMA
126382de724fSAlan Cox 			 * operation.
12643c76db4cSAlan Cox 			 *
1265abb9b935SKonstantin Belousov 			 * The object lock is not held here.
1266abb9b935SKonstantin Belousov 			 * See vm_page_clear_dirty_mask().
126782de724fSAlan Cox 			 */
12683c76db4cSAlan Cox 			vm_page_dirty(*mp);
126982de724fSAlan Cox 		}
127082de724fSAlan Cox 	}
127182de724fSAlan Cox 	if (pmap_failed) {
127282de724fSAlan Cox 		/*
127382de724fSAlan Cox 		 * One or more pages could not be held by the pmap.  Either no
127482de724fSAlan Cox 		 * page was mapped at the specified virtual address or that
127582de724fSAlan Cox 		 * mapping had insufficient permissions.  Attempt to fault in
127682de724fSAlan Cox 		 * and hold these pages.
127782de724fSAlan Cox 		 */
127882de724fSAlan Cox 		for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1279be996836SAttilio Rao 			if (*mp == NULL && vm_fault_hold(map, va, prot,
128082de724fSAlan Cox 			    VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
128182de724fSAlan Cox 				goto error;
128282de724fSAlan Cox 	}
128382de724fSAlan Cox 	return (count);
128482de724fSAlan Cox error:
128582de724fSAlan Cox 	for (mp = ma; mp < ma + count; mp++)
128682de724fSAlan Cox 		if (*mp != NULL) {
128782de724fSAlan Cox 			vm_page_lock(*mp);
128882de724fSAlan Cox 			vm_page_unhold(*mp);
128982de724fSAlan Cox 			vm_page_unlock(*mp);
129082de724fSAlan Cox 		}
129182de724fSAlan Cox 	return (-1);
129282de724fSAlan Cox }
129382de724fSAlan Cox 
129482de724fSAlan Cox /*
1295df8bae1dSRodney W. Grimes  *	Routine:
1296df8bae1dSRodney W. Grimes  *		vm_fault_copy_entry
1297df8bae1dSRodney W. Grimes  *	Function:
1298210a6886SKonstantin Belousov  *		Create new shadow object backing dst_entry with private copy of
1299210a6886SKonstantin Belousov  *		all underlying pages. When src_entry is equal to dst_entry,
1300210a6886SKonstantin Belousov  *		function implements COW for wired-down map entry. Otherwise,
1301210a6886SKonstantin Belousov  *		it forks wired entry into dst_map.
1302df8bae1dSRodney W. Grimes  *
1303df8bae1dSRodney W. Grimes  *	In/out conditions:
1304df8bae1dSRodney W. Grimes  *		The source and destination maps must be locked for write.
1305df8bae1dSRodney W. Grimes  *		The source map entry must be wired down (or be a sharing map
1306df8bae1dSRodney W. Grimes  *		entry corresponding to a main map entry that is wired down).
1307df8bae1dSRodney W. Grimes  */
130826f9a767SRodney W. Grimes void
1309121fd461SKonstantin Belousov vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1310121fd461SKonstantin Belousov     vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1311121fd461SKonstantin Belousov     vm_ooffset_t *fork_charge)
1312df8bae1dSRodney W. Grimes {
1313210a6886SKonstantin Belousov 	vm_object_t backing_object, dst_object, object, src_object;
13147afab86cSAlan Cox 	vm_pindex_t dst_pindex, pindex, src_pindex;
1315210a6886SKonstantin Belousov 	vm_prot_t access, prot;
1316df8bae1dSRodney W. Grimes 	vm_offset_t vaddr;
1317df8bae1dSRodney W. Grimes 	vm_page_t dst_m;
1318df8bae1dSRodney W. Grimes 	vm_page_t src_m;
13194c74acf7SKonstantin Belousov 	boolean_t upgrade;
1320df8bae1dSRodney W. Grimes 
1321df8bae1dSRodney W. Grimes #ifdef	lint
1322df8bae1dSRodney W. Grimes 	src_map++;
13230d94caffSDavid Greenman #endif	/* lint */
1324df8bae1dSRodney W. Grimes 
1325210a6886SKonstantin Belousov 	upgrade = src_entry == dst_entry;
13260973283dSKonstantin Belousov 	access = prot = dst_entry->protection;
1327210a6886SKonstantin Belousov 
1328df8bae1dSRodney W. Grimes 	src_object = src_entry->object.vm_object;
13297afab86cSAlan Cox 	src_pindex = OFF_TO_IDX(src_entry->offset);
1330df8bae1dSRodney W. Grimes 
13310973283dSKonstantin Belousov 	if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
13320973283dSKonstantin Belousov 		dst_object = src_object;
13330973283dSKonstantin Belousov 		vm_object_reference(dst_object);
13340973283dSKonstantin Belousov 	} else {
1335df8bae1dSRodney W. Grimes 		/*
13360d94caffSDavid Greenman 		 * Create the top-level object for the destination entry. (Doesn't
13370d94caffSDavid Greenman 		 * actually shadow anything - we copy the pages directly.)
1338df8bae1dSRodney W. Grimes 		 */
133924a1cce3SDavid Greenman 		dst_object = vm_object_allocate(OBJT_DEFAULT,
134057b5187bSAlan Cox 		    OFF_TO_IDX(dst_entry->end - dst_entry->start));
1341f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1342f8a47341SAlan Cox 		dst_object->flags |= OBJ_COLORED;
1343f8a47341SAlan Cox 		dst_object->pg_color = atop(dst_entry->start);
1344f8a47341SAlan Cox #endif
13450973283dSKonstantin Belousov 	}
1346df8bae1dSRodney W. Grimes 
134789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(dst_object);
1348210a6886SKonstantin Belousov 	KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1349121fd461SKonstantin Belousov 	    ("vm_fault_copy_entry: vm_object not NULL"));
13500973283dSKonstantin Belousov 	if (src_object != dst_object) {
1351df8bae1dSRodney W. Grimes 		dst_entry->object.vm_object = dst_object;
1352df8bae1dSRodney W. Grimes 		dst_entry->offset = 0;
13533364c323SKonstantin Belousov 		dst_object->charge = dst_entry->end - dst_entry->start;
13540973283dSKonstantin Belousov 	}
1355210a6886SKonstantin Belousov 	if (fork_charge != NULL) {
1356ef694c1aSEdward Tomasz Napierala 		KASSERT(dst_entry->cred == NULL,
1357121fd461SKonstantin Belousov 		    ("vm_fault_copy_entry: leaked swp charge"));
1358ef694c1aSEdward Tomasz Napierala 		dst_object->cred = curthread->td_ucred;
1359ef694c1aSEdward Tomasz Napierala 		crhold(dst_object->cred);
1360121fd461SKonstantin Belousov 		*fork_charge += dst_object->charge;
13610973283dSKonstantin Belousov 	} else if (dst_object->cred == NULL) {
13620973283dSKonstantin Belousov 		KASSERT(dst_entry->cred != NULL, ("no cred for entry %p",
13630973283dSKonstantin Belousov 		    dst_entry));
1364ef694c1aSEdward Tomasz Napierala 		dst_object->cred = dst_entry->cred;
1365ef694c1aSEdward Tomasz Napierala 		dst_entry->cred = NULL;
1366210a6886SKonstantin Belousov 	}
13670973283dSKonstantin Belousov 
1368210a6886SKonstantin Belousov 	/*
1369210a6886SKonstantin Belousov 	 * If not an upgrade, then enter the mappings in the pmap as
1370210a6886SKonstantin Belousov 	 * read and/or execute accesses.  Otherwise, enter them as
1371210a6886SKonstantin Belousov 	 * write accesses.
1372210a6886SKonstantin Belousov 	 *
1373210a6886SKonstantin Belousov 	 * A writeable large page mapping is only created if all of
1374210a6886SKonstantin Belousov 	 * the constituent small page mappings are modified. Marking
1375210a6886SKonstantin Belousov 	 * PTEs as modified on inception allows promotion to happen
1376210a6886SKonstantin Belousov 	 * without taking potentially large number of soft faults.
1377210a6886SKonstantin Belousov 	 */
1378210a6886SKonstantin Belousov 	if (!upgrade)
1379210a6886SKonstantin Belousov 		access &= ~VM_PROT_WRITE;
1380df8bae1dSRodney W. Grimes 
1381df8bae1dSRodney W. Grimes 	/*
1382ef45823eSKonstantin Belousov 	 * Loop through all of the virtual pages within the entry's
1383ef45823eSKonstantin Belousov 	 * range, copying each page from the source object to the
1384ef45823eSKonstantin Belousov 	 * destination object.  Since the source is wired, those pages
1385ef45823eSKonstantin Belousov 	 * must exist.  In contrast, the destination is pageable.
1386ef45823eSKonstantin Belousov 	 * Since the destination object does share any backing storage
1387ef45823eSKonstantin Belousov 	 * with the source object, all of its pages must be dirtied,
1388ef45823eSKonstantin Belousov 	 * regardless of whether they can be written.
1389df8bae1dSRodney W. Grimes 	 */
13907afab86cSAlan Cox 	for (vaddr = dst_entry->start, dst_pindex = 0;
1391df8bae1dSRodney W. Grimes 	    vaddr < dst_entry->end;
13927afab86cSAlan Cox 	    vaddr += PAGE_SIZE, dst_pindex++) {
13930973283dSKonstantin Belousov again:
1394df8bae1dSRodney W. Grimes 		/*
1395df8bae1dSRodney W. Grimes 		 * Find the page in the source object, and copy it in.
13964c74acf7SKonstantin Belousov 		 * Because the source is wired down, the page will be
13974c74acf7SKonstantin Belousov 		 * in memory.
1398df8bae1dSRodney W. Grimes 		 */
13990973283dSKonstantin Belousov 		if (src_object != dst_object)
140083b375eaSAttilio Rao 			VM_OBJECT_RLOCK(src_object);
1401c5b65a67SAlan Cox 		object = src_object;
14027afab86cSAlan Cox 		pindex = src_pindex + dst_pindex;
14037afab86cSAlan Cox 		while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1404c5b65a67SAlan Cox 		    (backing_object = object->backing_object) != NULL) {
1405c5b65a67SAlan Cox 			/*
14064c74acf7SKonstantin Belousov 			 * Unless the source mapping is read-only or
14074c74acf7SKonstantin Belousov 			 * it is presently being upgraded from
14084c74acf7SKonstantin Belousov 			 * read-only, the first object in the shadow
14094c74acf7SKonstantin Belousov 			 * chain should provide all of the pages.  In
14104c74acf7SKonstantin Belousov 			 * other words, this loop body should never be
14114c74acf7SKonstantin Belousov 			 * executed when the source mapping is already
14124c74acf7SKonstantin Belousov 			 * read/write.
1413c5b65a67SAlan Cox 			 */
14144c74acf7SKonstantin Belousov 			KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 ||
14154c74acf7SKonstantin Belousov 			    upgrade,
14164c74acf7SKonstantin Belousov 			    ("vm_fault_copy_entry: main object missing page"));
14174c74acf7SKonstantin Belousov 
141883b375eaSAttilio Rao 			VM_OBJECT_RLOCK(backing_object);
1419c5b65a67SAlan Cox 			pindex += OFF_TO_IDX(object->backing_object_offset);
14200973283dSKonstantin Belousov 			if (object != dst_object)
142183b375eaSAttilio Rao 				VM_OBJECT_RUNLOCK(object);
1422c5b65a67SAlan Cox 			object = backing_object;
1423c5b65a67SAlan Cox 		}
14244c74acf7SKonstantin Belousov 		KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing"));
14250973283dSKonstantin Belousov 
14260973283dSKonstantin Belousov 		if (object != dst_object) {
14270973283dSKonstantin Belousov 			/*
14280973283dSKonstantin Belousov 			 * Allocate a page in the destination object.
14290973283dSKonstantin Belousov 			 */
14302602a2eaSKonstantin Belousov 			dst_m = vm_page_alloc(dst_object, (src_object ==
14312602a2eaSKonstantin Belousov 			    dst_object ? src_pindex : 0) + dst_pindex,
14322602a2eaSKonstantin Belousov 			    VM_ALLOC_NORMAL);
14330973283dSKonstantin Belousov 			if (dst_m == NULL) {
14340973283dSKonstantin Belousov 				VM_OBJECT_WUNLOCK(dst_object);
14350973283dSKonstantin Belousov 				VM_OBJECT_RUNLOCK(object);
14360973283dSKonstantin Belousov 				VM_WAIT;
1437c8f780e3SKonstantin Belousov 				VM_OBJECT_WLOCK(dst_object);
14380973283dSKonstantin Belousov 				goto again;
14390973283dSKonstantin Belousov 			}
1440669890eaSAlan Cox 			pmap_copy_page(src_m, dst_m);
144183b375eaSAttilio Rao 			VM_OBJECT_RUNLOCK(object);
1442669890eaSAlan Cox 			dst_m->valid = VM_PAGE_BITS_ALL;
1443bc79b37fSKonstantin Belousov 			dst_m->dirty = VM_PAGE_BITS_ALL;
14440973283dSKonstantin Belousov 		} else {
14450973283dSKonstantin Belousov 			dst_m = src_m;
14460973283dSKonstantin Belousov 			if (vm_page_sleep_if_busy(dst_m, "fltupg"))
14470973283dSKonstantin Belousov 				goto again;
14480973283dSKonstantin Belousov 			vm_page_xbusy(dst_m);
14490973283dSKonstantin Belousov 			KASSERT(dst_m->valid == VM_PAGE_BITS_ALL,
14500973283dSKonstantin Belousov 			    ("invalid dst page %p", dst_m));
14510973283dSKonstantin Belousov 		}
145289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(dst_object);
1453df8bae1dSRodney W. Grimes 
1454df8bae1dSRodney W. Grimes 		/*
1455210a6886SKonstantin Belousov 		 * Enter it in the pmap. If a wired, copy-on-write
1456210a6886SKonstantin Belousov 		 * mapping is being replaced by a write-enabled
1457210a6886SKonstantin Belousov 		 * mapping, then wire that new mapping.
1458df8bae1dSRodney W. Grimes 		 */
145939ffa8c1SKonstantin Belousov 		pmap_enter(dst_map->pmap, vaddr, dst_m, prot,
146039ffa8c1SKonstantin Belousov 		    access | (upgrade ? PMAP_ENTER_WIRED : 0), 0);
1461df8bae1dSRodney W. Grimes 
1462df8bae1dSRodney W. Grimes 		/*
1463df8bae1dSRodney W. Grimes 		 * Mark it no longer busy, and put it on the active list.
1464df8bae1dSRodney W. Grimes 		 */
146589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(dst_object);
14662965a453SKip Macy 
1467210a6886SKonstantin Belousov 		if (upgrade) {
14680973283dSKonstantin Belousov 			if (src_m != dst_m) {
14692965a453SKip Macy 				vm_page_lock(src_m);
14703ae10f74SAttilio Rao 				vm_page_unwire(src_m, PQ_INACTIVE);
1471e20e8c15SKonstantin Belousov 				vm_page_unlock(src_m);
14722965a453SKip Macy 				vm_page_lock(dst_m);
1473210a6886SKonstantin Belousov 				vm_page_wire(dst_m);
1474e20e8c15SKonstantin Belousov 				vm_page_unlock(dst_m);
14752965a453SKip Macy 			} else {
14760973283dSKonstantin Belousov 				KASSERT(dst_m->wire_count > 0,
14770973283dSKonstantin Belousov 				    ("dst_m %p is not wired", dst_m));
14780973283dSKonstantin Belousov 			}
14790973283dSKonstantin Belousov 		} else {
14802965a453SKip Macy 			vm_page_lock(dst_m);
1481df8bae1dSRodney W. Grimes 			vm_page_activate(dst_m);
1482e20e8c15SKonstantin Belousov 			vm_page_unlock(dst_m);
14832965a453SKip Macy 		}
1484c7aebda8SAttilio Rao 		vm_page_xunbusy(dst_m);
1485df8bae1dSRodney W. Grimes 	}
148689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(dst_object);
1487210a6886SKonstantin Belousov 	if (upgrade) {
1488210a6886SKonstantin Belousov 		dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1489210a6886SKonstantin Belousov 		vm_object_deallocate(src_object);
1490210a6886SKonstantin Belousov 	}
1491df8bae1dSRodney W. Grimes }
149226f9a767SRodney W. Grimes 
14935730afc9SAlan Cox /*
14945730afc9SAlan Cox  * Block entry into the machine-independent layer's page fault handler by
14955730afc9SAlan Cox  * the calling thread.  Subsequent calls to vm_fault() by that thread will
14965730afc9SAlan Cox  * return KERN_PROTECTION_FAILURE.  Enable machine-dependent handling of
14975730afc9SAlan Cox  * spurious page faults.
14985730afc9SAlan Cox  */
14992801687dSKonstantin Belousov int
15002801687dSKonstantin Belousov vm_fault_disable_pagefaults(void)
15012801687dSKonstantin Belousov {
15022801687dSKonstantin Belousov 
15035730afc9SAlan Cox 	return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
15042801687dSKonstantin Belousov }
15052801687dSKonstantin Belousov 
15062801687dSKonstantin Belousov void
15072801687dSKonstantin Belousov vm_fault_enable_pagefaults(int save)
15082801687dSKonstantin Belousov {
15092801687dSKonstantin Belousov 
15102801687dSKonstantin Belousov 	curthread_pflags_restore(save);
15112801687dSKonstantin Belousov }
1512