xref: /freebsd/sys/vm/vm_fault.c (revision d842aa511475b4e03afa78dde7ba44ca4504b5c0)
160727d8bSWarner Losh /*-
2796df753SPedro F. Giffuni  * SPDX-License-Identifier: (BSD-4-Clause AND MIT-CMU)
3df57947fSPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
626f9a767SRodney W. Grimes  * Copyright (c) 1994 John S. Dyson
726f9a767SRodney W. Grimes  * All rights reserved.
826f9a767SRodney W. Grimes  * Copyright (c) 1994 David Greenman
926f9a767SRodney W. Grimes  * All rights reserved.
1026f9a767SRodney W. Grimes  *
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
13df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
14df8bae1dSRodney W. Grimes  *
15df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
16df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
17df8bae1dSRodney W. Grimes  * are met:
18df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
20df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
21df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
22df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
23df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
245929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
25df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
26df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
27df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
28df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
29df8bae1dSRodney W. Grimes  *    without specific prior written permission.
30df8bae1dSRodney W. Grimes  *
31df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
42df8bae1dSRodney W. Grimes  *
433c4dd356SDavid Greenman  *	from: @(#)vm_fault.c	8.4 (Berkeley) 1/12/94
44df8bae1dSRodney W. Grimes  *
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
47df8bae1dSRodney W. Grimes  * All rights reserved.
48df8bae1dSRodney W. Grimes  *
49df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
50df8bae1dSRodney W. Grimes  *
51df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
52df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
53df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
54df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
55df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
56df8bae1dSRodney W. Grimes  *
57df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
58df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
59df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
60df8bae1dSRodney W. Grimes  *
61df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
62df8bae1dSRodney W. Grimes  *
63df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
64df8bae1dSRodney W. Grimes  *  School of Computer Science
65df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
66df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
67df8bae1dSRodney W. Grimes  *
68df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
69df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
70df8bae1dSRodney W. Grimes  */
71df8bae1dSRodney W. Grimes 
72df8bae1dSRodney W. Grimes /*
73df8bae1dSRodney W. Grimes  *	Page fault handling module.
74df8bae1dSRodney W. Grimes  */
75874651b1SDavid E. O'Brien 
76874651b1SDavid E. O'Brien #include <sys/cdefs.h>
77874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
78874651b1SDavid E. O'Brien 
7935818d2eSJohn Baldwin #include "opt_ktrace.h"
80f8a47341SAlan Cox #include "opt_vm.h"
81f8a47341SAlan Cox 
82df8bae1dSRodney W. Grimes #include <sys/param.h>
83df8bae1dSRodney W. Grimes #include <sys/systm.h>
844edf4a58SJohn Baldwin #include <sys/kernel.h>
85fb919e4dSMark Murray #include <sys/lock.h>
86a8b0f100SAlan Cox #include <sys/mman.h>
8726f9a767SRodney W. Grimes #include <sys/proc.h>
88ae34b6ffSEdward Tomasz Napierala #include <sys/racct.h>
8926f9a767SRodney W. Grimes #include <sys/resourcevar.h>
9089f6b863SAttilio Rao #include <sys/rwlock.h>
9123955314SAlfred Perlstein #include <sys/sysctl.h>
924edf4a58SJohn Baldwin #include <sys/vmmeter.h>
934edf4a58SJohn Baldwin #include <sys/vnode.h>
9435818d2eSJohn Baldwin #ifdef KTRACE
9535818d2eSJohn Baldwin #include <sys/ktrace.h>
9635818d2eSJohn Baldwin #endif
97df8bae1dSRodney W. Grimes 
98df8bae1dSRodney W. Grimes #include <vm/vm.h>
99efeaf95aSDavid Greenman #include <vm/vm_param.h>
100efeaf95aSDavid Greenman #include <vm/pmap.h>
101efeaf95aSDavid Greenman #include <vm/vm_map.h>
102efeaf95aSDavid Greenman #include <vm/vm_object.h>
103df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
104df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
105a83c285cSDavid Greenman #include <vm/vm_kern.h>
10624a1cce3SDavid Greenman #include <vm/vm_pager.h>
107efeaf95aSDavid Greenman #include <vm/vm_extern.h>
108dfdf9abdSAlan Cox #include <vm/vm_reserv.h>
109df8bae1dSRodney W. Grimes 
110566526a9SAlan Cox #define PFBAK 4
111566526a9SAlan Cox #define PFFOR 4
112566526a9SAlan Cox 
1135268042bSAlan Cox #define	VM_FAULT_READ_DEFAULT	(1 + VM_FAULT_READ_AHEAD_INIT)
11413458803SAlan Cox #define	VM_FAULT_READ_MAX	(1 + VM_FAULT_READ_AHEAD_MAX)
115a8b0f100SAlan Cox 
116a8b0f100SAlan Cox #define	VM_FAULT_DONTNEED_MIN	1048576
11726f9a767SRodney W. Grimes 
1184866e085SJohn Dyson struct faultstate {
1194866e085SJohn Dyson 	vm_page_t m;
1204866e085SJohn Dyson 	vm_object_t object;
1214866e085SJohn Dyson 	vm_pindex_t pindex;
1224866e085SJohn Dyson 	vm_page_t first_m;
1234866e085SJohn Dyson 	vm_object_t	first_object;
1244866e085SJohn Dyson 	vm_pindex_t first_pindex;
1254866e085SJohn Dyson 	vm_map_t map;
1264866e085SJohn Dyson 	vm_map_entry_t entry;
127dc5401d2SKonstantin Belousov 	int map_generation;
128cd8a6fe8SAlan Cox 	bool lookup_still_valid;
1294866e085SJohn Dyson 	struct vnode *vp;
1304866e085SJohn Dyson };
1314866e085SJohn Dyson 
132a8b0f100SAlan Cox static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr,
133a8b0f100SAlan Cox 	    int ahead);
13463281952SAlan Cox static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
135a7163bb9SKonstantin Belousov 	    int backward, int forward, bool obj_locked);
13613458803SAlan Cox 
13762a59e8fSWarner Losh static inline void
1384866e085SJohn Dyson release_page(struct faultstate *fs)
1394866e085SJohn Dyson {
1400d0be82aSKonstantin Belousov 
141c7aebda8SAttilio Rao 	vm_page_xunbusy(fs->m);
1422965a453SKip Macy 	vm_page_lock(fs->m);
1434866e085SJohn Dyson 	vm_page_deactivate(fs->m);
1442965a453SKip Macy 	vm_page_unlock(fs->m);
1454866e085SJohn Dyson 	fs->m = NULL;
1464866e085SJohn Dyson }
1474866e085SJohn Dyson 
14862a59e8fSWarner Losh static inline void
1494866e085SJohn Dyson unlock_map(struct faultstate *fs)
1504866e085SJohn Dyson {
1510d0be82aSKonstantin Belousov 
15225adb370SBrian Feldman 	if (fs->lookup_still_valid) {
1534866e085SJohn Dyson 		vm_map_lookup_done(fs->map, fs->entry);
154cd8a6fe8SAlan Cox 		fs->lookup_still_valid = false;
1554866e085SJohn Dyson 	}
1564866e085SJohn Dyson }
1574866e085SJohn Dyson 
1584866e085SJohn Dyson static void
159cfabea3dSKonstantin Belousov unlock_vp(struct faultstate *fs)
160cfabea3dSKonstantin Belousov {
161cfabea3dSKonstantin Belousov 
162cfabea3dSKonstantin Belousov 	if (fs->vp != NULL) {
163cfabea3dSKonstantin Belousov 		vput(fs->vp);
164cfabea3dSKonstantin Belousov 		fs->vp = NULL;
165cfabea3dSKonstantin Belousov 	}
166cfabea3dSKonstantin Belousov }
167cfabea3dSKonstantin Belousov 
168cfabea3dSKonstantin Belousov static void
169a51b0840SAlan Cox unlock_and_deallocate(struct faultstate *fs)
1704866e085SJohn Dyson {
171f29ba63eSAlan Cox 
1724866e085SJohn Dyson 	vm_object_pip_wakeup(fs->object);
17389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(fs->object);
1744866e085SJohn Dyson 	if (fs->object != fs->first_object) {
17589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(fs->first_object);
1762965a453SKip Macy 		vm_page_lock(fs->first_m);
1774866e085SJohn Dyson 		vm_page_free(fs->first_m);
1782965a453SKip Macy 		vm_page_unlock(fs->first_m);
1794866e085SJohn Dyson 		vm_object_pip_wakeup(fs->first_object);
18089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(fs->first_object);
1814866e085SJohn Dyson 		fs->first_m = NULL;
1824866e085SJohn Dyson 	}
1834866e085SJohn Dyson 	vm_object_deallocate(fs->first_object);
1844866e085SJohn Dyson 	unlock_map(fs);
185cfabea3dSKonstantin Belousov 	unlock_vp(fs);
1864866e085SJohn Dyson }
1874866e085SJohn Dyson 
188a36f5532SKonstantin Belousov static void
189a36f5532SKonstantin Belousov vm_fault_dirty(vm_map_entry_t entry, vm_page_t m, vm_prot_t prot,
190e26236e9SKonstantin Belousov     vm_prot_t fault_type, int fault_flags, bool set_wd)
191a36f5532SKonstantin Belousov {
192e26236e9SKonstantin Belousov 	bool need_dirty;
193a36f5532SKonstantin Belousov 
194a36f5532SKonstantin Belousov 	if (((prot & VM_PROT_WRITE) == 0 &&
195a36f5532SKonstantin Belousov 	    (fault_flags & VM_FAULT_DIRTY) == 0) ||
196a36f5532SKonstantin Belousov 	    (m->oflags & VPO_UNMANAGED) != 0)
197a36f5532SKonstantin Belousov 		return;
198a36f5532SKonstantin Belousov 
199a36f5532SKonstantin Belousov 	VM_OBJECT_ASSERT_LOCKED(m->object);
200a36f5532SKonstantin Belousov 
201a36f5532SKonstantin Belousov 	need_dirty = ((fault_type & VM_PROT_WRITE) != 0 &&
2026a875bf9SKonstantin Belousov 	    (fault_flags & VM_FAULT_WIRE) == 0) ||
203a36f5532SKonstantin Belousov 	    (fault_flags & VM_FAULT_DIRTY) != 0;
204a36f5532SKonstantin Belousov 
205a36f5532SKonstantin Belousov 	if (set_wd)
206a36f5532SKonstantin Belousov 		vm_object_set_writeable_dirty(m->object);
207a36f5532SKonstantin Belousov 	else
208a36f5532SKonstantin Belousov 		/*
209a36f5532SKonstantin Belousov 		 * If two callers of vm_fault_dirty() with set_wd ==
210a36f5532SKonstantin Belousov 		 * FALSE, one for the map entry with MAP_ENTRY_NOSYNC
211a36f5532SKonstantin Belousov 		 * flag set, other with flag clear, race, it is
212a36f5532SKonstantin Belousov 		 * possible for the no-NOSYNC thread to see m->dirty
213a36f5532SKonstantin Belousov 		 * != 0 and not clear VPO_NOSYNC.  Take vm_page lock
214a36f5532SKonstantin Belousov 		 * around manipulation of VPO_NOSYNC and
215a36f5532SKonstantin Belousov 		 * vm_page_dirty() call, to avoid the race and keep
216a36f5532SKonstantin Belousov 		 * m->oflags consistent.
217a36f5532SKonstantin Belousov 		 */
218a36f5532SKonstantin Belousov 		vm_page_lock(m);
219a36f5532SKonstantin Belousov 
220a36f5532SKonstantin Belousov 	/*
221a36f5532SKonstantin Belousov 	 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
222a36f5532SKonstantin Belousov 	 * if the page is already dirty to prevent data written with
223a36f5532SKonstantin Belousov 	 * the expectation of being synced from not being synced.
224a36f5532SKonstantin Belousov 	 * Likewise if this entry does not request NOSYNC then make
225a36f5532SKonstantin Belousov 	 * sure the page isn't marked NOSYNC.  Applications sharing
226a36f5532SKonstantin Belousov 	 * data should use the same flags to avoid ping ponging.
227a36f5532SKonstantin Belousov 	 */
228a36f5532SKonstantin Belousov 	if ((entry->eflags & MAP_ENTRY_NOSYNC) != 0) {
229a36f5532SKonstantin Belousov 		if (m->dirty == 0) {
230a36f5532SKonstantin Belousov 			m->oflags |= VPO_NOSYNC;
231a36f5532SKonstantin Belousov 		}
232a36f5532SKonstantin Belousov 	} else {
233a36f5532SKonstantin Belousov 		m->oflags &= ~VPO_NOSYNC;
234a36f5532SKonstantin Belousov 	}
235a36f5532SKonstantin Belousov 
236a36f5532SKonstantin Belousov 	/*
237a36f5532SKonstantin Belousov 	 * If the fault is a write, we know that this page is being
238a36f5532SKonstantin Belousov 	 * written NOW so dirty it explicitly to save on
239a36f5532SKonstantin Belousov 	 * pmap_is_modified() calls later.
240a36f5532SKonstantin Belousov 	 *
241d5efa0a4SAlan Cox 	 * Also, since the page is now dirty, we can possibly tell
242d5efa0a4SAlan Cox 	 * the pager to release any swap backing the page.  Calling
243d5efa0a4SAlan Cox 	 * the pager requires a write lock on the object.
244a36f5532SKonstantin Belousov 	 */
245a36f5532SKonstantin Belousov 	if (need_dirty)
246a36f5532SKonstantin Belousov 		vm_page_dirty(m);
247a36f5532SKonstantin Belousov 	if (!set_wd)
248a36f5532SKonstantin Belousov 		vm_page_unlock(m);
249d5efa0a4SAlan Cox 	else if (need_dirty)
250a36f5532SKonstantin Belousov 		vm_pager_page_unswapped(m);
251a36f5532SKonstantin Belousov }
252a36f5532SKonstantin Belousov 
25341ddec83SKonstantin Belousov static void
25441ddec83SKonstantin Belousov vm_fault_fill_hold(vm_page_t *m_hold, vm_page_t m)
25541ddec83SKonstantin Belousov {
25641ddec83SKonstantin Belousov 
25741ddec83SKonstantin Belousov 	if (m_hold != NULL) {
25841ddec83SKonstantin Belousov 		*m_hold = m;
25941ddec83SKonstantin Belousov 		vm_page_lock(m);
26041ddec83SKonstantin Belousov 		vm_page_hold(m);
26141ddec83SKonstantin Belousov 		vm_page_unlock(m);
26241ddec83SKonstantin Belousov 	}
26341ddec83SKonstantin Belousov }
26441ddec83SKonstantin Belousov 
26541ddec83SKonstantin Belousov /*
26641ddec83SKonstantin Belousov  * Unlocks fs.first_object and fs.map on success.
26741ddec83SKonstantin Belousov  */
26841ddec83SKonstantin Belousov static int
26941ddec83SKonstantin Belousov vm_fault_soft_fast(struct faultstate *fs, vm_offset_t vaddr, vm_prot_t prot,
27041ddec83SKonstantin Belousov     int fault_type, int fault_flags, boolean_t wired, vm_page_t *m_hold)
27141ddec83SKonstantin Belousov {
2728b5e1472SAlan Cox 	vm_page_t m, m_map;
2732bf8cb38SAlan Cox #if (defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
274f6893f09SMark Johnston     __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)) && \
275f6893f09SMark Johnston     VM_NRESERVLEVEL > 0
2768b5e1472SAlan Cox 	vm_page_t m_super;
27790ea34bfSAlan Cox 	int flags;
2788b5e1472SAlan Cox #endif
27990ea34bfSAlan Cox 	int psind, rv;
28041ddec83SKonstantin Belousov 
28141ddec83SKonstantin Belousov 	MPASS(fs->vp == NULL);
28241ddec83SKonstantin Belousov 	m = vm_page_lookup(fs->first_object, fs->first_pindex);
28341ddec83SKonstantin Belousov 	/* A busy page can be mapped for read|execute access. */
28441ddec83SKonstantin Belousov 	if (m == NULL || ((prot & VM_PROT_WRITE) != 0 &&
28541ddec83SKonstantin Belousov 	    vm_page_busied(m)) || m->valid != VM_PAGE_BITS_ALL)
28641ddec83SKonstantin Belousov 		return (KERN_FAILURE);
2878b5e1472SAlan Cox 	m_map = m;
2888b5e1472SAlan Cox 	psind = 0;
2892bf8cb38SAlan Cox #if (defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
290f6893f09SMark Johnston     __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)) && \
291f6893f09SMark Johnston     VM_NRESERVLEVEL > 0
2928b5e1472SAlan Cox 	if ((m->flags & PG_FICTITIOUS) == 0 &&
2938b5e1472SAlan Cox 	    (m_super = vm_reserv_to_superpage(m)) != NULL &&
2948b5e1472SAlan Cox 	    rounddown2(vaddr, pagesizes[m_super->psind]) >= fs->entry->start &&
2958b5e1472SAlan Cox 	    roundup2(vaddr + 1, pagesizes[m_super->psind]) <= fs->entry->end &&
2968b5e1472SAlan Cox 	    (vaddr & (pagesizes[m_super->psind] - 1)) == (VM_PAGE_TO_PHYS(m) &
29764087fd7SMark Johnston 	    (pagesizes[m_super->psind] - 1)) && !wired &&
2988b5e1472SAlan Cox 	    pmap_ps_enabled(fs->map->pmap)) {
2998b5e1472SAlan Cox 		flags = PS_ALL_VALID;
3008b5e1472SAlan Cox 		if ((prot & VM_PROT_WRITE) != 0) {
3018b5e1472SAlan Cox 			/*
3028b5e1472SAlan Cox 			 * Create a superpage mapping allowing write access
3038b5e1472SAlan Cox 			 * only if none of the constituent pages are busy and
3048b5e1472SAlan Cox 			 * all of them are already dirty (except possibly for
3058b5e1472SAlan Cox 			 * the page that was faulted on).
3068b5e1472SAlan Cox 			 */
3078b5e1472SAlan Cox 			flags |= PS_NONE_BUSY;
3088b5e1472SAlan Cox 			if ((fs->first_object->flags & OBJ_UNMANAGED) == 0)
3098b5e1472SAlan Cox 				flags |= PS_ALL_DIRTY;
3108b5e1472SAlan Cox 		}
3118b5e1472SAlan Cox 		if (vm_page_ps_test(m_super, flags, m)) {
3128b5e1472SAlan Cox 			m_map = m_super;
3138b5e1472SAlan Cox 			psind = m_super->psind;
3148b5e1472SAlan Cox 			vaddr = rounddown2(vaddr, pagesizes[psind]);
3158b5e1472SAlan Cox 			/* Preset the modified bit for dirty superpages. */
3168b5e1472SAlan Cox 			if ((flags & PS_ALL_DIRTY) != 0)
3178b5e1472SAlan Cox 				fault_type |= VM_PROT_WRITE;
3188b5e1472SAlan Cox 		}
3198b5e1472SAlan Cox 	}
3208b5e1472SAlan Cox #endif
3218b5e1472SAlan Cox 	rv = pmap_enter(fs->map->pmap, vaddr, m_map, prot, fault_type |
3228b5e1472SAlan Cox 	    PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED : 0), psind);
32341ddec83SKonstantin Belousov 	if (rv != KERN_SUCCESS)
32441ddec83SKonstantin Belousov 		return (rv);
32541ddec83SKonstantin Belousov 	vm_fault_fill_hold(m_hold, m);
32641ddec83SKonstantin Belousov 	vm_fault_dirty(fs->entry, m, prot, fault_type, fault_flags, false);
3278b5e1472SAlan Cox 	if (psind == 0 && !wired)
328a7163bb9SKonstantin Belousov 		vm_fault_prefault(fs, vaddr, PFBAK, PFFOR, true);
329a7163bb9SKonstantin Belousov 	VM_OBJECT_RUNLOCK(fs->first_object);
33041ddec83SKonstantin Belousov 	vm_map_lookup_done(fs->map, fs->entry);
33141ddec83SKonstantin Belousov 	curthread->td_ru.ru_minflt++;
33241ddec83SKonstantin Belousov 	return (KERN_SUCCESS);
33341ddec83SKonstantin Belousov }
33441ddec83SKonstantin Belousov 
335c42b43a0SKonstantin Belousov static void
336c42b43a0SKonstantin Belousov vm_fault_restore_map_lock(struct faultstate *fs)
337c42b43a0SKonstantin Belousov {
338c42b43a0SKonstantin Belousov 
339c42b43a0SKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
340c42b43a0SKonstantin Belousov 	MPASS(fs->first_object->paging_in_progress > 0);
341c42b43a0SKonstantin Belousov 
342c42b43a0SKonstantin Belousov 	if (!vm_map_trylock_read(fs->map)) {
343c42b43a0SKonstantin Belousov 		VM_OBJECT_WUNLOCK(fs->first_object);
344c42b43a0SKonstantin Belousov 		vm_map_lock_read(fs->map);
345c42b43a0SKonstantin Belousov 		VM_OBJECT_WLOCK(fs->first_object);
346c42b43a0SKonstantin Belousov 	}
347c42b43a0SKonstantin Belousov 	fs->lookup_still_valid = true;
348c42b43a0SKonstantin Belousov }
349c42b43a0SKonstantin Belousov 
3507a432b84SKonstantin Belousov static void
3517a432b84SKonstantin Belousov vm_fault_populate_check_page(vm_page_t m)
3527a432b84SKonstantin Belousov {
3537a432b84SKonstantin Belousov 
3547a432b84SKonstantin Belousov 	/*
3557a432b84SKonstantin Belousov 	 * Check each page to ensure that the pager is obeying the
3567a432b84SKonstantin Belousov 	 * interface: the page must be installed in the object, fully
3577a432b84SKonstantin Belousov 	 * valid, and exclusively busied.
3587a432b84SKonstantin Belousov 	 */
3597a432b84SKonstantin Belousov 	MPASS(m != NULL);
3607a432b84SKonstantin Belousov 	MPASS(m->valid == VM_PAGE_BITS_ALL);
3617a432b84SKonstantin Belousov 	MPASS(vm_page_xbusied(m));
3627a432b84SKonstantin Belousov }
3637a432b84SKonstantin Belousov 
3647a432b84SKonstantin Belousov static void
3657a432b84SKonstantin Belousov vm_fault_populate_cleanup(vm_object_t object, vm_pindex_t first,
3667a432b84SKonstantin Belousov     vm_pindex_t last)
3677a432b84SKonstantin Belousov {
3687a432b84SKonstantin Belousov 	vm_page_t m;
3697a432b84SKonstantin Belousov 	vm_pindex_t pidx;
3707a432b84SKonstantin Belousov 
3717a432b84SKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(object);
3727a432b84SKonstantin Belousov 	MPASS(first <= last);
3737a432b84SKonstantin Belousov 	for (pidx = first, m = vm_page_lookup(object, pidx);
3747a432b84SKonstantin Belousov 	    pidx <= last; pidx++, m = vm_page_next(m)) {
3757a432b84SKonstantin Belousov 		vm_fault_populate_check_page(m);
3767a432b84SKonstantin Belousov 		vm_page_lock(m);
3777a432b84SKonstantin Belousov 		vm_page_deactivate(m);
3787a432b84SKonstantin Belousov 		vm_page_unlock(m);
3797a432b84SKonstantin Belousov 		vm_page_xunbusy(m);
3807a432b84SKonstantin Belousov 	}
3817a432b84SKonstantin Belousov }
382c42b43a0SKonstantin Belousov 
383c42b43a0SKonstantin Belousov static int
384d3f8534eSAlan Cox vm_fault_populate(struct faultstate *fs, vm_prot_t prot, int fault_type,
385d3f8534eSAlan Cox     int fault_flags, boolean_t wired, vm_page_t *m_hold)
386c42b43a0SKonstantin Belousov {
38770183daaSAlan Cox 	struct mtx *m_mtx;
38870183daaSAlan Cox 	vm_offset_t vaddr;
389c42b43a0SKonstantin Belousov 	vm_page_t m;
3907a432b84SKonstantin Belousov 	vm_pindex_t map_first, map_last, pager_first, pager_last, pidx;
39170183daaSAlan Cox 	int i, npages, psind, rv;
392c42b43a0SKonstantin Belousov 
393c42b43a0SKonstantin Belousov 	MPASS(fs->object == fs->first_object);
394c42b43a0SKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
395c42b43a0SKonstantin Belousov 	MPASS(fs->first_object->paging_in_progress > 0);
396c42b43a0SKonstantin Belousov 	MPASS(fs->first_object->backing_object == NULL);
397c42b43a0SKonstantin Belousov 	MPASS(fs->lookup_still_valid);
398c42b43a0SKonstantin Belousov 
3997a432b84SKonstantin Belousov 	pager_first = OFF_TO_IDX(fs->entry->offset);
40089564188SAlan Cox 	pager_last = pager_first + atop(fs->entry->end - fs->entry->start) - 1;
401c42b43a0SKonstantin Belousov 	unlock_map(fs);
402c42b43a0SKonstantin Belousov 	unlock_vp(fs);
403c42b43a0SKonstantin Belousov 
404c42b43a0SKonstantin Belousov 	/*
405c42b43a0SKonstantin Belousov 	 * Call the pager (driver) populate() method.
406c42b43a0SKonstantin Belousov 	 *
407c42b43a0SKonstantin Belousov 	 * There is no guarantee that the method will be called again
408c42b43a0SKonstantin Belousov 	 * if the current fault is for read, and a future fault is
409c42b43a0SKonstantin Belousov 	 * for write.  Report the entry's maximum allowed protection
410c42b43a0SKonstantin Belousov 	 * to the driver.
411c42b43a0SKonstantin Belousov 	 */
412c42b43a0SKonstantin Belousov 	rv = vm_pager_populate(fs->first_object, fs->first_pindex,
4137a432b84SKonstantin Belousov 	    fault_type, fs->entry->max_protection, &pager_first, &pager_last);
414c42b43a0SKonstantin Belousov 
415c42b43a0SKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
416c42b43a0SKonstantin Belousov 	if (rv == VM_PAGER_BAD) {
417c42b43a0SKonstantin Belousov 		/*
418c42b43a0SKonstantin Belousov 		 * VM_PAGER_BAD is the backdoor for a pager to request
419c42b43a0SKonstantin Belousov 		 * normal fault handling.
420c42b43a0SKonstantin Belousov 		 */
421c42b43a0SKonstantin Belousov 		vm_fault_restore_map_lock(fs);
422c42b43a0SKonstantin Belousov 		if (fs->map->timestamp != fs->map_generation)
423c42b43a0SKonstantin Belousov 			return (KERN_RESOURCE_SHORTAGE); /* RetryFault */
424c42b43a0SKonstantin Belousov 		return (KERN_NOT_RECEIVER);
425c42b43a0SKonstantin Belousov 	}
426c42b43a0SKonstantin Belousov 	if (rv != VM_PAGER_OK)
427c42b43a0SKonstantin Belousov 		return (KERN_FAILURE); /* AKA SIGSEGV */
428c42b43a0SKonstantin Belousov 
429c42b43a0SKonstantin Belousov 	/* Ensure that the driver is obeying the interface. */
4307a432b84SKonstantin Belousov 	MPASS(pager_first <= pager_last);
4317a432b84SKonstantin Belousov 	MPASS(fs->first_pindex <= pager_last);
4327a432b84SKonstantin Belousov 	MPASS(fs->first_pindex >= pager_first);
4337a432b84SKonstantin Belousov 	MPASS(pager_last < fs->first_object->size);
434c42b43a0SKonstantin Belousov 
435c42b43a0SKonstantin Belousov 	vm_fault_restore_map_lock(fs);
4367a432b84SKonstantin Belousov 	if (fs->map->timestamp != fs->map_generation) {
4377a432b84SKonstantin Belousov 		vm_fault_populate_cleanup(fs->first_object, pager_first,
4387a432b84SKonstantin Belousov 		    pager_last);
439c42b43a0SKonstantin Belousov 		return (KERN_RESOURCE_SHORTAGE); /* RetryFault */
4407a432b84SKonstantin Belousov 	}
441c42b43a0SKonstantin Belousov 
442c42b43a0SKonstantin Belousov 	/*
4437a432b84SKonstantin Belousov 	 * The map is unchanged after our last unlock.  Process the fault.
4447a432b84SKonstantin Belousov 	 *
4457a432b84SKonstantin Belousov 	 * The range [pager_first, pager_last] that is given to the
4467a432b84SKonstantin Belousov 	 * pager is only a hint.  The pager may populate any range
4477a432b84SKonstantin Belousov 	 * within the object that includes the requested page index.
4487a432b84SKonstantin Belousov 	 * In case the pager expanded the range, clip it to fit into
4497a432b84SKonstantin Belousov 	 * the map entry.
450c42b43a0SKonstantin Belousov 	 */
45189564188SAlan Cox 	map_first = OFF_TO_IDX(fs->entry->offset);
45289564188SAlan Cox 	if (map_first > pager_first) {
4537a432b84SKonstantin Belousov 		vm_fault_populate_cleanup(fs->first_object, pager_first,
4547a432b84SKonstantin Belousov 		    map_first - 1);
45589564188SAlan Cox 		pager_first = map_first;
45689564188SAlan Cox 	}
45789564188SAlan Cox 	map_last = map_first + atop(fs->entry->end - fs->entry->start) - 1;
45889564188SAlan Cox 	if (map_last < pager_last) {
4597a432b84SKonstantin Belousov 		vm_fault_populate_cleanup(fs->first_object, map_last + 1,
4607a432b84SKonstantin Belousov 		    pager_last);
46189564188SAlan Cox 		pager_last = map_last;
46289564188SAlan Cox 	}
46389564188SAlan Cox 	for (pidx = pager_first, m = vm_page_lookup(fs->first_object, pidx);
46470183daaSAlan Cox 	    pidx <= pager_last;
46570183daaSAlan Cox 	    pidx += npages, m = vm_page_next(&m[npages - 1])) {
46670183daaSAlan Cox 		vaddr = fs->entry->start + IDX_TO_OFF(pidx) - fs->entry->offset;
4672bf8cb38SAlan Cox #if defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
468f6893f09SMark Johnston     __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)
46970183daaSAlan Cox 		psind = m->psind;
47070183daaSAlan Cox 		if (psind > 0 && ((vaddr & (pagesizes[psind] - 1)) != 0 ||
47170183daaSAlan Cox 		    pidx + OFF_TO_IDX(pagesizes[psind]) - 1 > pager_last ||
47264087fd7SMark Johnston 		    !pmap_ps_enabled(fs->map->pmap) || wired))
47370183daaSAlan Cox 			psind = 0;
47470183daaSAlan Cox #else
47570183daaSAlan Cox 		psind = 0;
47670183daaSAlan Cox #endif
47770183daaSAlan Cox 		npages = atop(pagesizes[psind]);
47870183daaSAlan Cox 		for (i = 0; i < npages; i++) {
47970183daaSAlan Cox 			vm_fault_populate_check_page(&m[i]);
48070183daaSAlan Cox 			vm_fault_dirty(fs->entry, &m[i], prot, fault_type,
48170183daaSAlan Cox 			    fault_flags, true);
48270183daaSAlan Cox 		}
483c42b43a0SKonstantin Belousov 		VM_OBJECT_WUNLOCK(fs->first_object);
484e7a9df16SKonstantin Belousov 		rv = pmap_enter(fs->map->pmap, vaddr, m, prot, fault_type |
485e7a9df16SKonstantin Belousov 		    (wired ? PMAP_ENTER_WIRED : 0), psind);
486e7a9df16SKonstantin Belousov #if defined(__amd64__)
487e7a9df16SKonstantin Belousov 		if (psind > 0 && rv == KERN_FAILURE) {
488e7a9df16SKonstantin Belousov 			for (i = 0; i < npages; i++) {
489e7a9df16SKonstantin Belousov 				rv = pmap_enter(fs->map->pmap, vaddr + ptoa(i),
490e7a9df16SKonstantin Belousov 				    &m[i], prot, fault_type |
491e7a9df16SKonstantin Belousov 				    (wired ? PMAP_ENTER_WIRED : 0), 0);
492e7a9df16SKonstantin Belousov 				MPASS(rv == KERN_SUCCESS);
493e7a9df16SKonstantin Belousov 			}
494e7a9df16SKonstantin Belousov 		}
495e7a9df16SKonstantin Belousov #else
496e7a9df16SKonstantin Belousov 		MPASS(rv == KERN_SUCCESS);
497e7a9df16SKonstantin Belousov #endif
498c42b43a0SKonstantin Belousov 		VM_OBJECT_WLOCK(fs->first_object);
49970183daaSAlan Cox 		m_mtx = NULL;
50070183daaSAlan Cox 		for (i = 0; i < npages; i++) {
50170183daaSAlan Cox 			vm_page_change_lock(&m[i], &m_mtx);
502fccdefa1SAlan Cox 			if ((fault_flags & VM_FAULT_WIRE) != 0)
50370183daaSAlan Cox 				vm_page_wire(&m[i]);
504fccdefa1SAlan Cox 			else
50570183daaSAlan Cox 				vm_page_activate(&m[i]);
50670183daaSAlan Cox 			if (m_hold != NULL && m[i].pindex == fs->first_pindex) {
50770183daaSAlan Cox 				*m_hold = &m[i];
50870183daaSAlan Cox 				vm_page_hold(&m[i]);
509c42b43a0SKonstantin Belousov 			}
5106e1e759cSAlan Cox 			vm_page_xunbusy_maybelocked(&m[i]);
51170183daaSAlan Cox 		}
51270183daaSAlan Cox 		if (m_mtx != NULL)
51370183daaSAlan Cox 			mtx_unlock(m_mtx);
514c42b43a0SKonstantin Belousov 	}
515c42b43a0SKonstantin Belousov 	curthread->td_ru.ru_majflt++;
516c42b43a0SKonstantin Belousov 	return (KERN_SUCCESS);
517c42b43a0SKonstantin Belousov }
518c42b43a0SKonstantin Belousov 
519df8bae1dSRodney W. Grimes /*
520df8bae1dSRodney W. Grimes  *	vm_fault:
521df8bae1dSRodney W. Grimes  *
522956f3135SPhilippe Charnier  *	Handle a page fault occurring at the given address,
523df8bae1dSRodney W. Grimes  *	requiring the given permissions, in the map specified.
524df8bae1dSRodney W. Grimes  *	If successful, the page is inserted into the
525df8bae1dSRodney W. Grimes  *	associated physical map.
526df8bae1dSRodney W. Grimes  *
527df8bae1dSRodney W. Grimes  *	NOTE: the given address should be truncated to the
528df8bae1dSRodney W. Grimes  *	proper page address.
529df8bae1dSRodney W. Grimes  *
530df8bae1dSRodney W. Grimes  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
531df8bae1dSRodney W. Grimes  *	a standard error specifying why the fault is fatal is returned.
532df8bae1dSRodney W. Grimes  *
533df8bae1dSRodney W. Grimes  *	The map in question must be referenced, and remains so.
5340cddd8f0SMatthew Dillon  *	Caller may hold no locks.
535df8bae1dSRodney W. Grimes  */
536df8bae1dSRodney W. Grimes int
53723955314SAlfred Perlstein vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
53823955314SAlfred Perlstein     int fault_flags)
53923955314SAlfred Perlstein {
54035818d2eSJohn Baldwin 	struct thread *td;
54135818d2eSJohn Baldwin 	int result;
542acd11c74SAlan Cox 
54335818d2eSJohn Baldwin 	td = curthread;
54435818d2eSJohn Baldwin 	if ((td->td_pflags & TDP_NOFAULTING) != 0)
5452801687dSKonstantin Belousov 		return (KERN_PROTECTION_FAILURE);
54635818d2eSJohn Baldwin #ifdef KTRACE
54735818d2eSJohn Baldwin 	if (map != kernel_map && KTRPOINT(td, KTR_FAULT))
54835818d2eSJohn Baldwin 		ktrfault(vaddr, fault_type);
54935818d2eSJohn Baldwin #endif
550be996836SAttilio Rao 	result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags,
551be996836SAttilio Rao 	    NULL);
55235818d2eSJohn Baldwin #ifdef KTRACE
55335818d2eSJohn Baldwin 	if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND))
55435818d2eSJohn Baldwin 		ktrfaultend(result);
55535818d2eSJohn Baldwin #endif
55635818d2eSJohn Baldwin 	return (result);
557acd11c74SAlan Cox }
558acd11c74SAlan Cox 
559acd11c74SAlan Cox int
560be996836SAttilio Rao vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
561acd11c74SAlan Cox     int fault_flags, vm_page_t *m_hold)
562acd11c74SAlan Cox {
5634866e085SJohn Dyson 	struct faultstate fs;
564d2bf64c3SKonstantin Belousov 	struct vnode *vp;
56523984ce5SMark Johnston 	struct domainset *dset;
566ebcddc72SAlan Cox 	vm_object_t next_object, retry_object;
5670c3a4893SAlan Cox 	vm_offset_t e_end, e_start;
568ebcddc72SAlan Cox 	vm_pindex_t retry_pindex;
569ebcddc72SAlan Cox 	vm_prot_t prot, retry_prot;
570f994b207SAlan Cox 	int ahead, alloc_req, behind, cluster_offset, error, era, faultcount;
571dc5401d2SKonstantin Belousov 	int locked, nera, result, rv;
5720c3a4893SAlan Cox 	u_char behavior;
573cd8a6fe8SAlan Cox 	boolean_t wired;	/* Passed by reference. */
57419bd0d9cSKonstantin Belousov 	bool dead, hardfault, is_first_object_locked;
575df8bae1dSRodney W. Grimes 
57683c9dea1SGleb Smirnoff 	VM_CNT_INC(v_vm_faults);
577d2bf64c3SKonstantin Belousov 	fs.vp = NULL;
578b0cd2017SGleb Smirnoff 	faultcount = 0;
5790c3a4893SAlan Cox 	nera = -1;
580320023e2SAlan Cox 	hardfault = false;
581df8bae1dSRodney W. Grimes 
582df8bae1dSRodney W. Grimes RetryFault:;
583df8bae1dSRodney W. Grimes 
584df8bae1dSRodney W. Grimes 	/*
5850d94caffSDavid Greenman 	 * Find the backing store object and offset into it to begin the
5860d94caffSDavid Greenman 	 * search.
587df8bae1dSRodney W. Grimes 	 */
58840360b1bSMatthew Dillon 	fs.map = map;
58919bd0d9cSKonstantin Belousov 	result = vm_map_lookup(&fs.map, vaddr, fault_type |
59019bd0d9cSKonstantin Belousov 	    VM_PROT_FAULT_LOOKUP, &fs.entry, &fs.first_object,
59119bd0d9cSKonstantin Belousov 	    &fs.first_pindex, &prot, &wired);
59292de35b0SAlan Cox 	if (result != KERN_SUCCESS) {
593a9ee028dSMark Johnston 		unlock_vp(&fs);
59492de35b0SAlan Cox 		return (result);
59509e0c6ccSJohn Dyson 	}
59609e0c6ccSJohn Dyson 
597dc5401d2SKonstantin Belousov 	fs.map_generation = fs.map->timestamp;
5982d8acc0fSJohn Dyson 
5994866e085SJohn Dyson 	if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
60005d58177SBjoern A. Zeeb 		panic("%s: fault on nofault entry, addr: %#lx",
60105d58177SBjoern A. Zeeb 		    __func__, (u_long)vaddr);
6027aaaa4fdSJohn Dyson 	}
6037aaaa4fdSJohn Dyson 
6044f9c9114SKonstantin Belousov 	if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
6054f9c9114SKonstantin Belousov 	    fs.entry->wiring_thread != curthread) {
6064f9c9114SKonstantin Belousov 		vm_map_unlock_read(fs.map);
6074f9c9114SKonstantin Belousov 		vm_map_lock(fs.map);
6084f9c9114SKonstantin Belousov 		if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
6094f9c9114SKonstantin Belousov 		    (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
610cfabea3dSKonstantin Belousov 			unlock_vp(&fs);
6114f9c9114SKonstantin Belousov 			fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
6124f9c9114SKonstantin Belousov 			vm_map_unlock_and_wait(fs.map, 0);
6134f9c9114SKonstantin Belousov 		} else
6144f9c9114SKonstantin Belousov 			vm_map_unlock(fs.map);
6154f9c9114SKonstantin Belousov 		goto RetryFault;
6164f9c9114SKonstantin Belousov 	}
6174f9c9114SKonstantin Belousov 
61819bd0d9cSKonstantin Belousov 	MPASS((fs.entry->eflags & MAP_ENTRY_GUARD) == 0);
61919bd0d9cSKonstantin Belousov 
620afe55ca3SKonstantin Belousov 	if (wired)
621afe55ca3SKonstantin Belousov 		fault_type = prot | (fault_type & VM_PROT_COPY);
6226a875bf9SKonstantin Belousov 	else
6236a875bf9SKonstantin Belousov 		KASSERT((fault_flags & VM_FAULT_WIRE) == 0,
6246a875bf9SKonstantin Belousov 		    ("!wired && VM_FAULT_WIRE"));
625afe55ca3SKonstantin Belousov 
6268d67b8c8SAlan Cox 	/*
6278d67b8c8SAlan Cox 	 * Try to avoid lock contention on the top-level object through
6288d67b8c8SAlan Cox 	 * special-case handling of some types of page faults, specifically,
6298d67b8c8SAlan Cox 	 * those that are both (1) mapping an existing page from the top-
6308d67b8c8SAlan Cox 	 * level object and (2) not having to mark that object as containing
6318d67b8c8SAlan Cox 	 * dirty pages.  Under these conditions, a read lock on the top-level
6328d67b8c8SAlan Cox 	 * object suffices, allowing multiple page faults of a similar type to
6338d67b8c8SAlan Cox 	 * run in parallel on the same top-level object.
6348d67b8c8SAlan Cox 	 */
635afe55ca3SKonstantin Belousov 	if (fs.vp == NULL /* avoid locked vnode leak */ &&
6366a875bf9SKonstantin Belousov 	    (fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0 &&
637afe55ca3SKonstantin Belousov 	    /* avoid calling vm_object_set_writeable_dirty() */
638afe55ca3SKonstantin Belousov 	    ((prot & VM_PROT_WRITE) == 0 ||
639f40cb1c6SKonstantin Belousov 	    (fs.first_object->type != OBJT_VNODE &&
640f40cb1c6SKonstantin Belousov 	    (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
641afe55ca3SKonstantin Belousov 	    (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0)) {
642afe55ca3SKonstantin Belousov 		VM_OBJECT_RLOCK(fs.first_object);
64341ddec83SKonstantin Belousov 		if ((prot & VM_PROT_WRITE) == 0 ||
64441ddec83SKonstantin Belousov 		    (fs.first_object->type != OBJT_VNODE &&
64541ddec83SKonstantin Belousov 		    (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
64641ddec83SKonstantin Belousov 		    (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0) {
64741ddec83SKonstantin Belousov 			rv = vm_fault_soft_fast(&fs, vaddr, prot, fault_type,
64841ddec83SKonstantin Belousov 			    fault_flags, wired, m_hold);
64941ddec83SKonstantin Belousov 			if (rv == KERN_SUCCESS)
65041ddec83SKonstantin Belousov 				return (rv);
651afe55ca3SKonstantin Belousov 		}
652afe55ca3SKonstantin Belousov 		if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) {
653afe55ca3SKonstantin Belousov 			VM_OBJECT_RUNLOCK(fs.first_object);
654afe55ca3SKonstantin Belousov 			VM_OBJECT_WLOCK(fs.first_object);
655afe55ca3SKonstantin Belousov 		}
656afe55ca3SKonstantin Belousov 	} else {
657afe55ca3SKonstantin Belousov 		VM_OBJECT_WLOCK(fs.first_object);
658afe55ca3SKonstantin Belousov 	}
659afe55ca3SKonstantin Belousov 
66095e5e988SJohn Dyson 	/*
66195e5e988SJohn Dyson 	 * Make a reference to this object to prevent its disposal while we
66295e5e988SJohn Dyson 	 * are messing with it.  Once we have the reference, the map is free
66395e5e988SJohn Dyson 	 * to be diddled.  Since objects reference their shadows (and copies),
66495e5e988SJohn Dyson 	 * they will stay around as well.
665fe8e0238SMatthew Dillon 	 *
666fe8e0238SMatthew Dillon 	 * Bump the paging-in-progress count to prevent size changes (e.g.
667dda4d369SAlan Cox 	 * truncation operations) during I/O.
66895e5e988SJohn Dyson 	 */
669a976eb5eSAlan Cox 	vm_object_reference_locked(fs.first_object);
670d474eaaaSDoug Rabson 	vm_object_pip_add(fs.first_object, 1);
67195e5e988SJohn Dyson 
672cd8a6fe8SAlan Cox 	fs.lookup_still_valid = true;
673df8bae1dSRodney W. Grimes 
6744866e085SJohn Dyson 	fs.first_m = NULL;
675df8bae1dSRodney W. Grimes 
676df8bae1dSRodney W. Grimes 	/*
677df8bae1dSRodney W. Grimes 	 * Search for the page at object/offset.
678df8bae1dSRodney W. Grimes 	 */
6794866e085SJohn Dyson 	fs.object = fs.first_object;
6804866e085SJohn Dyson 	fs.pindex = fs.first_pindex;
681df8bae1dSRodney W. Grimes 	while (TRUE) {
6821c7c3c6aSMatthew Dillon 		/*
683725441f6SKonstantin Belousov 		 * If the object is marked for imminent termination,
684725441f6SKonstantin Belousov 		 * we retry here, since the collapse pass has raced
685725441f6SKonstantin Belousov 		 * with us.  Otherwise, if we see terminally dead
686725441f6SKonstantin Belousov 		 * object, return fail.
6871c7c3c6aSMatthew Dillon 		 */
688725441f6SKonstantin Belousov 		if ((fs.object->flags & OBJ_DEAD) != 0) {
689725441f6SKonstantin Belousov 			dead = fs.object->type == OBJT_DEAD;
6904866e085SJohn Dyson 			unlock_and_deallocate(&fs);
691725441f6SKonstantin Belousov 			if (dead)
69247221757SJohn Dyson 				return (KERN_PROTECTION_FAILURE);
693725441f6SKonstantin Belousov 			pause("vmf_de", 1);
694725441f6SKonstantin Belousov 			goto RetryFault;
69547221757SJohn Dyson 		}
69647221757SJohn Dyson 
6971c7c3c6aSMatthew Dillon 		/*
6981c7c3c6aSMatthew Dillon 		 * See if page is resident
6991c7c3c6aSMatthew Dillon 		 */
7004866e085SJohn Dyson 		fs.m = vm_page_lookup(fs.object, fs.pindex);
7014866e085SJohn Dyson 		if (fs.m != NULL) {
70298cb733cSKenneth D. Merry 			/*
7031c7c3c6aSMatthew Dillon 			 * Wait/Retry if the page is busy.  We have to do this
704c7aebda8SAttilio Rao 			 * if the page is either exclusive or shared busy
705c7aebda8SAttilio Rao 			 * because the vm_pager may be using read busy for
706c7aebda8SAttilio Rao 			 * pageouts (and even pageins if it is the vnode
707c7aebda8SAttilio Rao 			 * pager), and we could end up trying to pagein and
708c7aebda8SAttilio Rao 			 * pageout the same page simultaneously.
7091c7c3c6aSMatthew Dillon 			 *
7101c7c3c6aSMatthew Dillon 			 * We can theoretically allow the busy case on a read
7111c7c3c6aSMatthew Dillon 			 * fault if the page is marked valid, but since such
7121c7c3c6aSMatthew Dillon 			 * pages are typically already pmap'd, putting that
7131c7c3c6aSMatthew Dillon 			 * special case in might be more effort then it is
7141c7c3c6aSMatthew Dillon 			 * worth.  We cannot under any circumstances mess
715c7aebda8SAttilio Rao 			 * around with a shared busied page except, perhaps,
7161c7c3c6aSMatthew Dillon 			 * to pmap it.
717df8bae1dSRodney W. Grimes 			 */
718c7aebda8SAttilio Rao 			if (vm_page_busied(fs.m)) {
719b88b6c9dSAlan Cox 				/*
720b88b6c9dSAlan Cox 				 * Reference the page before unlocking and
721b88b6c9dSAlan Cox 				 * sleeping so that the page daemon is less
722b88b6c9dSAlan Cox 				 * likely to reclaim it.
723b88b6c9dSAlan Cox 				 */
7243407fefeSKonstantin Belousov 				vm_page_aflag_set(fs.m, PGA_REFERENCED);
725a51b0840SAlan Cox 				if (fs.object != fs.first_object) {
72689f6b863SAttilio Rao 					if (!VM_OBJECT_TRYWLOCK(
727a6e38685SKonstantin Belousov 					    fs.first_object)) {
72889f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(fs.object);
72989f6b863SAttilio Rao 						VM_OBJECT_WLOCK(fs.first_object);
73089f6b863SAttilio Rao 						VM_OBJECT_WLOCK(fs.object);
731a6e38685SKonstantin Belousov 					}
7322965a453SKip Macy 					vm_page_lock(fs.first_m);
733a51b0840SAlan Cox 					vm_page_free(fs.first_m);
7342965a453SKip Macy 					vm_page_unlock(fs.first_m);
735a51b0840SAlan Cox 					vm_object_pip_wakeup(fs.first_object);
73689f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(fs.first_object);
737a51b0840SAlan Cox 					fs.first_m = NULL;
738a51b0840SAlan Cox 				}
739a51b0840SAlan Cox 				unlock_map(&fs);
740a51b0840SAlan Cox 				if (fs.m == vm_page_lookup(fs.object,
741a51b0840SAlan Cox 				    fs.pindex)) {
742c7aebda8SAttilio Rao 					vm_page_sleep_if_busy(fs.m, "vmpfw");
743a51b0840SAlan Cox 				}
744a51b0840SAlan Cox 				vm_object_pip_wakeup(fs.object);
74589f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(fs.object);
74683c9dea1SGleb Smirnoff 				VM_CNT_INC(v_intrans);
7474866e085SJohn Dyson 				vm_object_deallocate(fs.first_object);
748df8bae1dSRodney W. Grimes 				goto RetryFault;
749df8bae1dSRodney W. Grimes 			}
7507615edaaSMatthew Dillon 
7511c7c3c6aSMatthew Dillon 			/*
7521c7c3c6aSMatthew Dillon 			 * Mark page busy for other processes, and the
7531c7c3c6aSMatthew Dillon 			 * pagedaemon.  If it still isn't completely valid
7541c7c3c6aSMatthew Dillon 			 * (readable), jump to readrest, else break-out ( we
7551c7c3c6aSMatthew Dillon 			 * found the page ).
7561c7c3c6aSMatthew Dillon 			 */
757c7aebda8SAttilio Rao 			vm_page_xbusy(fs.m);
758ff5958e7SAlan Cox 			if (fs.m->valid != VM_PAGE_BITS_ALL)
7590d94caffSDavid Greenman 				goto readrest;
760c6a70eaeSMark Johnston 			break; /* break to PAGE HAS BEEN FOUND */
761df8bae1dSRodney W. Grimes 		}
76210b4196bSAlan Cox 		KASSERT(fs.m == NULL, ("fs.m should be NULL, not %p", fs.m));
7631c7c3c6aSMatthew Dillon 
7641c7c3c6aSMatthew Dillon 		/*
76510b4196bSAlan Cox 		 * Page is not resident.  If the pager might contain the page
76610b4196bSAlan Cox 		 * or this is the beginning of the search, allocate a new
76710b4196bSAlan Cox 		 * page.  (Default objects are zero-fill, so there is no real
76810b4196bSAlan Cox 		 * pager for them.)
7691c7c3c6aSMatthew Dillon 		 */
7706a875bf9SKonstantin Belousov 		if (fs.object->type != OBJT_DEFAULT ||
7716a875bf9SKonstantin Belousov 		    fs.object == fs.first_object) {
7724866e085SJohn Dyson 			if (fs.pindex >= fs.object->size) {
7734866e085SJohn Dyson 				unlock_and_deallocate(&fs);
7745f55e841SDavid Greenman 				return (KERN_PROTECTION_FAILURE);
7755f55e841SDavid Greenman 			}
77622ba64e8SJohn Dyson 
777c42b43a0SKonstantin Belousov 			if (fs.object == fs.first_object &&
778c42b43a0SKonstantin Belousov 			    (fs.first_object->flags & OBJ_POPULATE) != 0 &&
779c42b43a0SKonstantin Belousov 			    fs.first_object->shadow_count == 0) {
780d3f8534eSAlan Cox 				rv = vm_fault_populate(&fs, prot, fault_type,
781d3f8534eSAlan Cox 				    fault_flags, wired, m_hold);
782c42b43a0SKonstantin Belousov 				switch (rv) {
783c42b43a0SKonstantin Belousov 				case KERN_SUCCESS:
784c42b43a0SKonstantin Belousov 				case KERN_FAILURE:
785c42b43a0SKonstantin Belousov 					unlock_and_deallocate(&fs);
786c42b43a0SKonstantin Belousov 					return (rv);
787c42b43a0SKonstantin Belousov 				case KERN_RESOURCE_SHORTAGE:
788c42b43a0SKonstantin Belousov 					unlock_and_deallocate(&fs);
789c42b43a0SKonstantin Belousov 					goto RetryFault;
790c42b43a0SKonstantin Belousov 				case KERN_NOT_RECEIVER:
791c42b43a0SKonstantin Belousov 					/*
792c42b43a0SKonstantin Belousov 					 * Pager's populate() method
793c42b43a0SKonstantin Belousov 					 * returned VM_PAGER_BAD.
794c42b43a0SKonstantin Belousov 					 */
795c42b43a0SKonstantin Belousov 					break;
796c42b43a0SKonstantin Belousov 				default:
797c42b43a0SKonstantin Belousov 					panic("inconsistent return codes");
798c42b43a0SKonstantin Belousov 				}
799c42b43a0SKonstantin Belousov 			}
800c42b43a0SKonstantin Belousov 
801df8bae1dSRodney W. Grimes 			/*
8020d94caffSDavid Greenman 			 * Allocate a new page for this object/offset pair.
8033f1c4c4fSKonstantin Belousov 			 *
8043f1c4c4fSKonstantin Belousov 			 * Unlocked read of the p_flag is harmless. At
8053f1c4c4fSKonstantin Belousov 			 * worst, the P_KILLED might be not observed
8063f1c4c4fSKonstantin Belousov 			 * there, and allocation can fail, causing
8073f1c4c4fSKonstantin Belousov 			 * restart and new reading of the p_flag.
808df8bae1dSRodney W. Grimes 			 */
80923984ce5SMark Johnston 			dset = fs.object->domain.dr_policy;
81023984ce5SMark Johnston 			if (dset == NULL)
81123984ce5SMark Johnston 				dset = curthread->td_domain.dr_policy;
81223984ce5SMark Johnston 			if (!vm_page_count_severe_set(&dset->ds_mask) ||
81323984ce5SMark Johnston 			    P_KILLED(curproc)) {
814f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
8153d653db0SAlan Cox 				vm_object_color(fs.object, atop(vaddr) -
8163d653db0SAlan Cox 				    fs.pindex);
817f8a47341SAlan Cox #endif
8183f1c4c4fSKonstantin Belousov 				alloc_req = P_KILLED(curproc) ?
8193f1c4c4fSKonstantin Belousov 				    VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
8203f1c4c4fSKonstantin Belousov 				if (fs.object->type != OBJT_VNODE &&
8213f1c4c4fSKonstantin Belousov 				    fs.object->backing_object == NULL)
8223f1c4c4fSKonstantin Belousov 					alloc_req |= VM_ALLOC_ZERO;
8234866e085SJohn Dyson 				fs.m = vm_page_alloc(fs.object, fs.pindex,
8243f1c4c4fSKonstantin Belousov 				    alloc_req);
82540360b1bSMatthew Dillon 			}
8264866e085SJohn Dyson 			if (fs.m == NULL) {
8274866e085SJohn Dyson 				unlock_and_deallocate(&fs);
82823984ce5SMark Johnston 				vm_waitpfault(dset);
829df8bae1dSRodney W. Grimes 				goto RetryFault;
8307667839aSAlan Cox 			}
831df8bae1dSRodney W. Grimes 		}
83247221757SJohn Dyson 
8330d94caffSDavid Greenman readrest:
8341c7c3c6aSMatthew Dillon 		/*
83585702505SAlan Cox 		 * At this point, we have either allocated a new page or found
83685702505SAlan Cox 		 * an existing page that is only partially valid.
83785702505SAlan Cox 		 *
83885702505SAlan Cox 		 * We hold a reference on the current object and the page is
83985702505SAlan Cox 		 * exclusive busied.
84085702505SAlan Cox 		 */
84185702505SAlan Cox 
84285702505SAlan Cox 		/*
8430c3a4893SAlan Cox 		 * If the pager for the current object might have the page,
8440c3a4893SAlan Cox 		 * then determine the number of additional pages to read and
8450c3a4893SAlan Cox 		 * potentially reprioritize previously read pages for earlier
8460c3a4893SAlan Cox 		 * reclamation.  These operations should only be performed
8470c3a4893SAlan Cox 		 * once per page fault.  Even if the current pager doesn't
8480c3a4893SAlan Cox 		 * have the page, the number of additional pages to read will
8490c3a4893SAlan Cox 		 * apply to subsequent objects in the shadow chain.
8501c7c3c6aSMatthew Dillon 		 */
8510c3a4893SAlan Cox 		if (fs.object->type != OBJT_DEFAULT && nera == -1 &&
8520c3a4893SAlan Cox 		    !P_KILLED(curproc)) {
8530c3a4893SAlan Cox 			KASSERT(fs.lookup_still_valid, ("map unlocked"));
8545268042bSAlan Cox 			era = fs.entry->read_ahead;
8550c3a4893SAlan Cox 			behavior = vm_map_entry_behavior(fs.entry);
8560c3a4893SAlan Cox 			if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
8575268042bSAlan Cox 				nera = 0;
85813458803SAlan Cox 			} else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
8595268042bSAlan Cox 				nera = VM_FAULT_READ_AHEAD_MAX;
860381b7242SAlan Cox 				if (vaddr == fs.entry->next_read)
8610c3a4893SAlan Cox 					vm_fault_dontneed(&fs, vaddr, nera);
862381b7242SAlan Cox 			} else if (vaddr == fs.entry->next_read) {
86313458803SAlan Cox 				/*
8645268042bSAlan Cox 				 * This is a sequential fault.  Arithmetically
8655268042bSAlan Cox 				 * increase the requested number of pages in
8665268042bSAlan Cox 				 * the read-ahead window.  The requested
8675268042bSAlan Cox 				 * number of pages is "# of sequential faults
8685268042bSAlan Cox 				 * x (read ahead min + 1) + read ahead min"
86913458803SAlan Cox 				 */
8705268042bSAlan Cox 				nera = VM_FAULT_READ_AHEAD_MIN;
8715268042bSAlan Cox 				if (era > 0) {
8725268042bSAlan Cox 					nera += era + 1;
87313458803SAlan Cox 					if (nera > VM_FAULT_READ_AHEAD_MAX)
87413458803SAlan Cox 						nera = VM_FAULT_READ_AHEAD_MAX;
8755268042bSAlan Cox 				}
87613458803SAlan Cox 				if (era == VM_FAULT_READ_AHEAD_MAX)
8770c3a4893SAlan Cox 					vm_fault_dontneed(&fs, vaddr, nera);
8785268042bSAlan Cox 			} else {
8795268042bSAlan Cox 				/*
8800c3a4893SAlan Cox 				 * This is a non-sequential fault.
8815268042bSAlan Cox 				 */
8825268042bSAlan Cox 				nera = 0;
883867a482dSJohn Dyson 			}
8840c3a4893SAlan Cox 			if (era != nera) {
8850c3a4893SAlan Cox 				/*
8860c3a4893SAlan Cox 				 * A read lock on the map suffices to update
8870c3a4893SAlan Cox 				 * the read ahead count safely.
8880c3a4893SAlan Cox 				 */
8895268042bSAlan Cox 				fs.entry->read_ahead = nera;
8900c3a4893SAlan Cox 			}
891d2bf64c3SKonstantin Belousov 
892d2bf64c3SKonstantin Belousov 			/*
8930c3a4893SAlan Cox 			 * Prepare for unlocking the map.  Save the map
8940c3a4893SAlan Cox 			 * entry's start and end addresses, which are used to
8950c3a4893SAlan Cox 			 * optimize the size of the pager operation below.
8960c3a4893SAlan Cox 			 * Even if the map entry's addresses change after
8970c3a4893SAlan Cox 			 * unlocking the map, using the saved addresses is
8980c3a4893SAlan Cox 			 * safe.
8990c3a4893SAlan Cox 			 */
9000c3a4893SAlan Cox 			e_start = fs.entry->start;
9010c3a4893SAlan Cox 			e_end = fs.entry->end;
9020c3a4893SAlan Cox 		}
9030c3a4893SAlan Cox 
9040c3a4893SAlan Cox 		/*
9050c3a4893SAlan Cox 		 * Call the pager to retrieve the page if there is a chance
9060c3a4893SAlan Cox 		 * that the pager has it, and potentially retrieve additional
9070c3a4893SAlan Cox 		 * pages at the same time.
9080c3a4893SAlan Cox 		 */
9090c3a4893SAlan Cox 		if (fs.object->type != OBJT_DEFAULT) {
9100c3a4893SAlan Cox 			/*
91185702505SAlan Cox 			 * Release the map lock before locking the vnode or
91285702505SAlan Cox 			 * sleeping in the pager.  (If the current object has
91385702505SAlan Cox 			 * a shadow, then an earlier iteration of this loop
91485702505SAlan Cox 			 * may have already unlocked the map.)
915d2bf64c3SKonstantin Belousov 			 */
916d2bf64c3SKonstantin Belousov 			unlock_map(&fs);
917d2bf64c3SKonstantin Belousov 
918022dfd69SKonstantin Belousov 			if (fs.object->type == OBJT_VNODE &&
919022dfd69SKonstantin Belousov 			    (vp = fs.object->handle) != fs.vp) {
92085702505SAlan Cox 				/*
92185702505SAlan Cox 				 * Perform an unlock in case the desired vnode
92285702505SAlan Cox 				 * changed while the map was unlocked during a
92385702505SAlan Cox 				 * retry.
92485702505SAlan Cox 				 */
925cfabea3dSKonstantin Belousov 				unlock_vp(&fs);
926d2bf64c3SKonstantin Belousov 
92785702505SAlan Cox 				locked = VOP_ISLOCKED(vp);
928d2bf64c3SKonstantin Belousov 				if (locked != LK_EXCLUSIVE)
929d2bf64c3SKonstantin Belousov 					locked = LK_SHARED;
930dda4d369SAlan Cox 
931dda4d369SAlan Cox 				/*
932dda4d369SAlan Cox 				 * We must not sleep acquiring the vnode lock
933dda4d369SAlan Cox 				 * while we have the page exclusive busied or
934dda4d369SAlan Cox 				 * the object's paging-in-progress count
935dda4d369SAlan Cox 				 * incremented.  Otherwise, we could deadlock.
936dda4d369SAlan Cox 				 */
937d2bf64c3SKonstantin Belousov 				error = vget(vp, locked | LK_CANRECURSE |
938d2bf64c3SKonstantin Belousov 				    LK_NOWAIT, curthread);
939d2bf64c3SKonstantin Belousov 				if (error != 0) {
940d2bf64c3SKonstantin Belousov 					vhold(vp);
941d2bf64c3SKonstantin Belousov 					release_page(&fs);
942d2bf64c3SKonstantin Belousov 					unlock_and_deallocate(&fs);
943d2bf64c3SKonstantin Belousov 					error = vget(vp, locked | LK_RETRY |
944d2bf64c3SKonstantin Belousov 					    LK_CANRECURSE, curthread);
945d2bf64c3SKonstantin Belousov 					vdrop(vp);
946d2bf64c3SKonstantin Belousov 					fs.vp = vp;
947d2bf64c3SKonstantin Belousov 					KASSERT(error == 0,
948d2bf64c3SKonstantin Belousov 					    ("vm_fault: vget failed"));
949d2bf64c3SKonstantin Belousov 					goto RetryFault;
950d2bf64c3SKonstantin Belousov 				}
951d2bf64c3SKonstantin Belousov 				fs.vp = vp;
952d2bf64c3SKonstantin Belousov 			}
953d2bf64c3SKonstantin Belousov 			KASSERT(fs.vp == NULL || !fs.map->system_map,
954d2bf64c3SKonstantin Belousov 			    ("vm_fault: vnode-backed object mapped by system map"));
955d2bf64c3SKonstantin Belousov 
956df8bae1dSRodney W. Grimes 			/*
957b0cd2017SGleb Smirnoff 			 * Page in the requested page and hint the pager,
958b0cd2017SGleb Smirnoff 			 * that it may bring up surrounding pages.
95926f9a767SRodney W. Grimes 			 */
9600c3a4893SAlan Cox 			if (nera == -1 || behavior == MAP_ENTRY_BEHAV_RANDOM ||
9610c3a4893SAlan Cox 			    P_KILLED(curproc)) {
9620c3a4893SAlan Cox 				behind = 0;
9630c3a4893SAlan Cox 				ahead = 0;
9640c3a4893SAlan Cox 			} else {
9650c3a4893SAlan Cox 				/* Is this a sequential fault? */
9660c3a4893SAlan Cox 				if (nera > 0) {
9670c3a4893SAlan Cox 					behind = 0;
9680c3a4893SAlan Cox 					ahead = nera;
9690c3a4893SAlan Cox 				} else {
9700c3a4893SAlan Cox 					/*
9710c3a4893SAlan Cox 					 * Request a cluster of pages that is
9720c3a4893SAlan Cox 					 * aligned to a VM_FAULT_READ_DEFAULT
9730c3a4893SAlan Cox 					 * page offset boundary within the
9740c3a4893SAlan Cox 					 * object.  Alignment to a page offset
9750c3a4893SAlan Cox 					 * boundary is more likely to coincide
9760c3a4893SAlan Cox 					 * with the underlying file system
9770c3a4893SAlan Cox 					 * block than alignment to a virtual
9780c3a4893SAlan Cox 					 * address boundary.
9790c3a4893SAlan Cox 					 */
9800c3a4893SAlan Cox 					cluster_offset = fs.pindex %
9810c3a4893SAlan Cox 					    VM_FAULT_READ_DEFAULT;
9820c3a4893SAlan Cox 					behind = ulmin(cluster_offset,
9830c3a4893SAlan Cox 					    atop(vaddr - e_start));
9840c3a4893SAlan Cox 					ahead = VM_FAULT_READ_DEFAULT - 1 -
9850c3a4893SAlan Cox 					    cluster_offset;
9860c3a4893SAlan Cox 				}
9870c3a4893SAlan Cox 				ahead = ulmin(ahead, atop(e_end - vaddr) - 1);
9880c3a4893SAlan Cox 			}
989b0cd2017SGleb Smirnoff 			rv = vm_pager_get_pages(fs.object, &fs.m, 1,
990b0cd2017SGleb Smirnoff 			    &behind, &ahead);
99126f9a767SRodney W. Grimes 			if (rv == VM_PAGER_OK) {
992b0cd2017SGleb Smirnoff 				faultcount = behind + 1 + ahead;
993320023e2SAlan Cox 				hardfault = true;
9941c7c3c6aSMatthew Dillon 				break; /* break to PAGE HAS BEEN FOUND */
995df8bae1dSRodney W. Grimes 			}
996a83c285cSDavid Greenman 			if (rv == VM_PAGER_ERROR)
997f3679e35SDavid Greenman 				printf("vm_fault: pager read error, pid %d (%s)\n",
998f3679e35SDavid Greenman 				    curproc->p_pid, curproc->p_comm);
999521ddf39SAlan Cox 
100026f9a767SRodney W. Grimes 			/*
1001521ddf39SAlan Cox 			 * If an I/O error occurred or the requested page was
1002521ddf39SAlan Cox 			 * outside the range of the pager, clean up and return
1003521ddf39SAlan Cox 			 * an error.
100426f9a767SRodney W. Grimes 			 */
1005521ddf39SAlan Cox 			if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) {
10062965a453SKip Macy 				vm_page_lock(fs.m);
1007*d842aa51SMark Johnston 				if (!vm_page_wired(fs.m))
10084866e085SJohn Dyson 					vm_page_free(fs.m);
1009230afe0bSKonstantin Belousov 				else
1010230afe0bSKonstantin Belousov 					vm_page_xunbusy_maybelocked(fs.m);
10112965a453SKip Macy 				vm_page_unlock(fs.m);
10124866e085SJohn Dyson 				fs.m = NULL;
10134866e085SJohn Dyson 				unlock_and_deallocate(&fs);
1014521ddf39SAlan Cox 				return (rv == VM_PAGER_ERROR ? KERN_FAILURE :
1015521ddf39SAlan Cox 				    KERN_PROTECTION_FAILURE);
101626f9a767SRodney W. Grimes 			}
1017521ddf39SAlan Cox 
1018521ddf39SAlan Cox 			/*
1019521ddf39SAlan Cox 			 * The requested page does not exist at this object/
1020521ddf39SAlan Cox 			 * offset.  Remove the invalid page from the object,
1021521ddf39SAlan Cox 			 * waking up anyone waiting for it, and continue on to
1022521ddf39SAlan Cox 			 * the next object.  However, if this is the top-level
1023521ddf39SAlan Cox 			 * object, we must leave the busy page in place to
1024521ddf39SAlan Cox 			 * prevent another process from rushing past us, and
1025521ddf39SAlan Cox 			 * inserting the page in that object at the same time
1026521ddf39SAlan Cox 			 * that we are.
1027521ddf39SAlan Cox 			 */
10284866e085SJohn Dyson 			if (fs.object != fs.first_object) {
10292965a453SKip Macy 				vm_page_lock(fs.m);
1030*d842aa51SMark Johnston 				if (!vm_page_wired(fs.m))
10314866e085SJohn Dyson 					vm_page_free(fs.m);
1032230afe0bSKonstantin Belousov 				else
1033230afe0bSKonstantin Belousov 					vm_page_xunbusy_maybelocked(fs.m);
10342965a453SKip Macy 				vm_page_unlock(fs.m);
10354866e085SJohn Dyson 				fs.m = NULL;
1036df8bae1dSRodney W. Grimes 			}
1037df8bae1dSRodney W. Grimes 		}
103840360b1bSMatthew Dillon 
1039df8bae1dSRodney W. Grimes 		/*
10401c7c3c6aSMatthew Dillon 		 * We get here if the object has default pager (or unwiring)
10411c7c3c6aSMatthew Dillon 		 * or the pager doesn't have the page.
1042df8bae1dSRodney W. Grimes 		 */
10434866e085SJohn Dyson 		if (fs.object == fs.first_object)
10444866e085SJohn Dyson 			fs.first_m = fs.m;
1045df8bae1dSRodney W. Grimes 
1046df8bae1dSRodney W. Grimes 		/*
10470d94caffSDavid Greenman 		 * Move on to the next object.  Lock the next object before
10480d94caffSDavid Greenman 		 * unlocking the current one.
1049df8bae1dSRodney W. Grimes 		 */
10504866e085SJohn Dyson 		next_object = fs.object->backing_object;
1051df8bae1dSRodney W. Grimes 		if (next_object == NULL) {
1052df8bae1dSRodney W. Grimes 			/*
10530d94caffSDavid Greenman 			 * If there's no object left, fill the page in the top
10540d94caffSDavid Greenman 			 * object with zeros.
1055df8bae1dSRodney W. Grimes 			 */
10564866e085SJohn Dyson 			if (fs.object != fs.first_object) {
10574866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
105889f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(fs.object);
1059df8bae1dSRodney W. Grimes 
10604866e085SJohn Dyson 				fs.object = fs.first_object;
10614866e085SJohn Dyson 				fs.pindex = fs.first_pindex;
10624866e085SJohn Dyson 				fs.m = fs.first_m;
106389f6b863SAttilio Rao 				VM_OBJECT_WLOCK(fs.object);
1064df8bae1dSRodney W. Grimes 			}
10654866e085SJohn Dyson 			fs.first_m = NULL;
1066df8bae1dSRodney W. Grimes 
10674221e284SAlan Cox 			/*
10684221e284SAlan Cox 			 * Zero the page if necessary and mark it valid.
10694221e284SAlan Cox 			 */
10704866e085SJohn Dyson 			if ((fs.m->flags & PG_ZERO) == 0) {
1071fff6062aSAlan Cox 				pmap_zero_page(fs.m);
10724221e284SAlan Cox 			} else {
107383c9dea1SGleb Smirnoff 				VM_CNT_INC(v_ozfod);
10744221e284SAlan Cox 			}
107583c9dea1SGleb Smirnoff 			VM_CNT_INC(v_zfod);
10764221e284SAlan Cox 			fs.m->valid = VM_PAGE_BITS_ALL;
10777b9b301cSAlan Cox 			/* Don't try to prefault neighboring pages. */
10787b9b301cSAlan Cox 			faultcount = 1;
10791c7c3c6aSMatthew Dillon 			break;	/* break to PAGE HAS BEEN FOUND */
10800d94caffSDavid Greenman 		} else {
1081c8567c3aSAlan Cox 			KASSERT(fs.object != next_object,
1082c8567c3aSAlan Cox 			    ("object loop %p", next_object));
108389f6b863SAttilio Rao 			VM_OBJECT_WLOCK(next_object);
1084c8567c3aSAlan Cox 			vm_object_pip_add(next_object, 1);
1085c8567c3aSAlan Cox 			if (fs.object != fs.first_object)
10864866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
10876753423cSAlan Cox 			fs.pindex +=
10886753423cSAlan Cox 			    OFF_TO_IDX(fs.object->backing_object_offset);
108989f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(fs.object);
10904866e085SJohn Dyson 			fs.object = next_object;
1091df8bae1dSRodney W. Grimes 		}
1092df8bae1dSRodney W. Grimes 	}
10931c7c3c6aSMatthew Dillon 
1094c7aebda8SAttilio Rao 	vm_page_assert_xbusied(fs.m);
1095df8bae1dSRodney W. Grimes 
1096df8bae1dSRodney W. Grimes 	/*
10970d94caffSDavid Greenman 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1098df8bae1dSRodney W. Grimes 	 * is held.]
1099df8bae1dSRodney W. Grimes 	 */
1100df8bae1dSRodney W. Grimes 
1101df8bae1dSRodney W. Grimes 	/*
11020d94caffSDavid Greenman 	 * If the page is being written, but isn't already owned by the
11030d94caffSDavid Greenman 	 * top-level object, we have to copy it into a new page owned by the
11040d94caffSDavid Greenman 	 * top-level object.
1105df8bae1dSRodney W. Grimes 	 */
11064866e085SJohn Dyson 	if (fs.object != fs.first_object) {
1107df8bae1dSRodney W. Grimes 		/*
11080d94caffSDavid Greenman 		 * We only really need to copy if we want to write it.
1109df8bae1dSRodney W. Grimes 		 */
1110a6d42a0dSAlan Cox 		if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1111df8bae1dSRodney W. Grimes 			/*
11121c7c3c6aSMatthew Dillon 			 * This allows pages to be virtually copied from a
11131c7c3c6aSMatthew Dillon 			 * backing_object into the first_object, where the
11141c7c3c6aSMatthew Dillon 			 * backing object has no other refs to it, and cannot
11151c7c3c6aSMatthew Dillon 			 * gain any more refs.  Instead of a bcopy, we just
11161c7c3c6aSMatthew Dillon 			 * move the page from the backing object to the
11171c7c3c6aSMatthew Dillon 			 * first object.  Note that we must mark the page
11181c7c3c6aSMatthew Dillon 			 * dirty in the first object so that it will go out
11191c7c3c6aSMatthew Dillon 			 * to swap when needed.
1120df8bae1dSRodney W. Grimes 			 */
1121cd8a6fe8SAlan Cox 			is_first_object_locked = false;
1122e50346b5SAlan Cox 			if (
1123de5f6a77SJohn Dyson 				/*
1124de5f6a77SJohn Dyson 				 * Only one shadow object
1125de5f6a77SJohn Dyson 				 */
11264866e085SJohn Dyson 				(fs.object->shadow_count == 1) &&
1127de5f6a77SJohn Dyson 				/*
1128de5f6a77SJohn Dyson 				 * No COW refs, except us
1129de5f6a77SJohn Dyson 				 */
11304866e085SJohn Dyson 				(fs.object->ref_count == 1) &&
1131de5f6a77SJohn Dyson 				/*
11325929bcfaSPhilippe Charnier 				 * No one else can look this object up
1133de5f6a77SJohn Dyson 				 */
11344866e085SJohn Dyson 				(fs.object->handle == NULL) &&
1135de5f6a77SJohn Dyson 				/*
1136de5f6a77SJohn Dyson 				 * No other ways to look the object up
1137de5f6a77SJohn Dyson 				 */
11384866e085SJohn Dyson 				((fs.object->type == OBJT_DEFAULT) ||
11394866e085SJohn Dyson 				 (fs.object->type == OBJT_SWAP)) &&
114089f6b863SAttilio Rao 			    (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
1141de5f6a77SJohn Dyson 				/*
1142de5f6a77SJohn Dyson 				 * We don't chase down the shadow chain
1143de5f6a77SJohn Dyson 				 */
1144e50346b5SAlan Cox 			    fs.object == fs.first_object->backing_object) {
1145bccdea45SAlan Cox 				vm_page_lock(fs.m);
114621f01f45SMark Johnston 				vm_page_dequeue(fs.m);
1147bccdea45SAlan Cox 				vm_page_remove(fs.m);
1148bccdea45SAlan Cox 				vm_page_unlock(fs.m);
1149eb00b276SAlan Cox 				vm_page_lock(fs.first_m);
1150bccdea45SAlan Cox 				vm_page_replace_checked(fs.m, fs.first_object,
1151bccdea45SAlan Cox 				    fs.first_pindex, fs.first_m);
11526fee422eSConrad Meyer 				vm_page_free(fs.first_m);
11536fee422eSConrad Meyer 				vm_page_unlock(fs.first_m);
1154bccdea45SAlan Cox 				vm_page_dirty(fs.m);
1155dfdf9abdSAlan Cox #if VM_NRESERVLEVEL > 0
1156dfdf9abdSAlan Cox 				/*
1157dfdf9abdSAlan Cox 				 * Rename the reservation.
1158dfdf9abdSAlan Cox 				 */
1159dfdf9abdSAlan Cox 				vm_reserv_rename(fs.m, fs.first_object,
1160dfdf9abdSAlan Cox 				    fs.object, OFF_TO_IDX(
1161dfdf9abdSAlan Cox 				    fs.first_object->backing_object_offset));
1162dfdf9abdSAlan Cox #endif
1163bccdea45SAlan Cox 				/*
1164bccdea45SAlan Cox 				 * Removing the page from the backing object
1165bccdea45SAlan Cox 				 * unbusied it.
1166bccdea45SAlan Cox 				 */
1167c7aebda8SAttilio Rao 				vm_page_xbusy(fs.m);
1168d98ddc46SAlan Cox 				fs.first_m = fs.m;
11694866e085SJohn Dyson 				fs.m = NULL;
117083c9dea1SGleb Smirnoff 				VM_CNT_INC(v_cow_optim);
1171de5f6a77SJohn Dyson 			} else {
1172de5f6a77SJohn Dyson 				/*
1173de5f6a77SJohn Dyson 				 * Oh, well, lets copy it.
1174de5f6a77SJohn Dyson 				 */
1175669890eaSAlan Cox 				pmap_copy_page(fs.m, fs.first_m);
1176669890eaSAlan Cox 				fs.first_m->valid = VM_PAGE_BITS_ALL;
1177d8778512SAlan Cox 				if (wired && (fault_flags &
11786a875bf9SKonstantin Belousov 				    VM_FAULT_WIRE) == 0) {
11792965a453SKip Macy 					vm_page_lock(fs.first_m);
1180d8778512SAlan Cox 					vm_page_wire(fs.first_m);
11812965a453SKip Macy 					vm_page_unlock(fs.first_m);
11822965a453SKip Macy 
11832965a453SKip Macy 					vm_page_lock(fs.m);
11843ae10f74SAttilio Rao 					vm_page_unwire(fs.m, PQ_INACTIVE);
11852965a453SKip Macy 					vm_page_unlock(fs.m);
1186de5f6a77SJohn Dyson 				}
1187df8bae1dSRodney W. Grimes 				/*
1188df8bae1dSRodney W. Grimes 				 * We no longer need the old page or object.
1189df8bae1dSRodney W. Grimes 				 */
11904866e085SJohn Dyson 				release_page(&fs);
1191de5f6a77SJohn Dyson 			}
11921c7c3c6aSMatthew Dillon 			/*
11931c7c3c6aSMatthew Dillon 			 * fs.object != fs.first_object due to above
11941c7c3c6aSMatthew Dillon 			 * conditional
11951c7c3c6aSMatthew Dillon 			 */
11964866e085SJohn Dyson 			vm_object_pip_wakeup(fs.object);
119789f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(fs.object);
11989f1abe3dSAlan Cox 
11999f1abe3dSAlan Cox 			/*
12009f1abe3dSAlan Cox 			 * We only try to prefault read-only mappings to the
12019f1abe3dSAlan Cox 			 * neighboring pages when this copy-on-write fault is
12029f1abe3dSAlan Cox 			 * a hard fault.  In other cases, trying to prefault
12039f1abe3dSAlan Cox 			 * is typically wasted effort.
12049f1abe3dSAlan Cox 			 */
12059f1abe3dSAlan Cox 			if (faultcount == 0)
12069f1abe3dSAlan Cox 				faultcount = 1;
12079f1abe3dSAlan Cox 
1208df8bae1dSRodney W. Grimes 			/*
1209df8bae1dSRodney W. Grimes 			 * Only use the new page below...
1210df8bae1dSRodney W. Grimes 			 */
12114866e085SJohn Dyson 			fs.object = fs.first_object;
12124866e085SJohn Dyson 			fs.pindex = fs.first_pindex;
1213d98ddc46SAlan Cox 			fs.m = fs.first_m;
1214f29ba63eSAlan Cox 			if (!is_first_object_locked)
121589f6b863SAttilio Rao 				VM_OBJECT_WLOCK(fs.object);
121683c9dea1SGleb Smirnoff 			VM_CNT_INC(v_cow_faults);
12174d34e019SKonstantin Belousov 			curthread->td_cow++;
12180d94caffSDavid Greenman 		} else {
1219df8bae1dSRodney W. Grimes 			prot &= ~VM_PROT_WRITE;
1220df8bae1dSRodney W. Grimes 		}
1221df8bae1dSRodney W. Grimes 	}
1222df8bae1dSRodney W. Grimes 
1223df8bae1dSRodney W. Grimes 	/*
12240d94caffSDavid Greenman 	 * We must verify that the maps have not changed since our last
12250d94caffSDavid Greenman 	 * lookup.
1226df8bae1dSRodney W. Grimes 	 */
122719dc5607STor Egge 	if (!fs.lookup_still_valid) {
122819dc5607STor Egge 		if (!vm_map_trylock_read(fs.map)) {
1229b823bbd6SMatthew Dillon 			release_page(&fs);
1230b823bbd6SMatthew Dillon 			unlock_and_deallocate(&fs);
1231b823bbd6SMatthew Dillon 			goto RetryFault;
1232b823bbd6SMatthew Dillon 		}
1233cd8a6fe8SAlan Cox 		fs.lookup_still_valid = true;
1234dc5401d2SKonstantin Belousov 		if (fs.map->timestamp != fs.map_generation) {
123519dc5607STor Egge 			result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
12364866e085SJohn Dyson 			    &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
1237df8bae1dSRodney W. Grimes 
1238df8bae1dSRodney W. Grimes 			/*
123944ed3417STor Egge 			 * If we don't need the page any longer, put it on the inactive
12400d94caffSDavid Greenman 			 * list (the easiest thing to do here).  If no one needs it,
12410d94caffSDavid Greenman 			 * pageout will grab it eventually.
1242df8bae1dSRodney W. Grimes 			 */
1243df8bae1dSRodney W. Grimes 			if (result != KERN_SUCCESS) {
12444866e085SJohn Dyson 				release_page(&fs);
12454866e085SJohn Dyson 				unlock_and_deallocate(&fs);
124619dc5607STor Egge 
124719dc5607STor Egge 				/*
124819dc5607STor Egge 				 * If retry of map lookup would have blocked then
124919dc5607STor Egge 				 * retry fault from start.
125019dc5607STor Egge 				 */
125119dc5607STor Egge 				if (result == KERN_FAILURE)
125219dc5607STor Egge 					goto RetryFault;
1253df8bae1dSRodney W. Grimes 				return (result);
1254df8bae1dSRodney W. Grimes 			}
12554866e085SJohn Dyson 			if ((retry_object != fs.first_object) ||
12564866e085SJohn Dyson 			    (retry_pindex != fs.first_pindex)) {
12574866e085SJohn Dyson 				release_page(&fs);
12584866e085SJohn Dyson 				unlock_and_deallocate(&fs);
1259df8bae1dSRodney W. Grimes 				goto RetryFault;
1260df8bae1dSRodney W. Grimes 			}
126119dc5607STor Egge 
1262df8bae1dSRodney W. Grimes 			/*
12630d94caffSDavid Greenman 			 * Check whether the protection has changed or the object has
12640d94caffSDavid Greenman 			 * been copied while we left the map unlocked. Changing from
12650d94caffSDavid Greenman 			 * read to write permission is OK - we leave the page
12660d94caffSDavid Greenman 			 * write-protected, and catch the write fault. Changing from
12670d94caffSDavid Greenman 			 * write to read permission means that we can't mark the page
12680d94caffSDavid Greenman 			 * write-enabled after all.
1269df8bae1dSRodney W. Grimes 			 */
1270df8bae1dSRodney W. Grimes 			prot &= retry_prot;
1271607970bcSKonstantin Belousov 			fault_type &= retry_prot;
1272607970bcSKonstantin Belousov 			if (prot == 0) {
1273607970bcSKonstantin Belousov 				release_page(&fs);
1274607970bcSKonstantin Belousov 				unlock_and_deallocate(&fs);
1275607970bcSKonstantin Belousov 				goto RetryFault;
1276607970bcSKonstantin Belousov 			}
1277fccdefa1SAlan Cox 
1278fccdefa1SAlan Cox 			/* Reassert because wired may have changed. */
1279fccdefa1SAlan Cox 			KASSERT(wired || (fault_flags & VM_FAULT_WIRE) == 0,
1280fccdefa1SAlan Cox 			    ("!wired && VM_FAULT_WIRE"));
1281df8bae1dSRodney W. Grimes 		}
128219dc5607STor Egge 	}
1283381b7242SAlan Cox 
1284d2bf64c3SKonstantin Belousov 	/*
1285381b7242SAlan Cox 	 * If the page was filled by a pager, save the virtual address that
1286381b7242SAlan Cox 	 * should be faulted on next under a sequential access pattern to the
1287381b7242SAlan Cox 	 * map entry.  A read lock on the map suffices to update this address
1288381b7242SAlan Cox 	 * safely.
1289d2bf64c3SKonstantin Belousov 	 */
12905758fe71SAlan Cox 	if (hardfault)
1291381b7242SAlan Cox 		fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE;
1292d2bf64c3SKonstantin Belousov 
1293e26236e9SKonstantin Belousov 	vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags, true);
1294c7aebda8SAttilio Rao 	vm_page_assert_xbusied(fs.m);
1295c7aebda8SAttilio Rao 
12964221e284SAlan Cox 	/*
129778cfe1f7SAlan Cox 	 * Page must be completely valid or it is not fit to
12984221e284SAlan Cox 	 * map into user space.  vm_pager_get_pages() ensures this.
12994221e284SAlan Cox 	 */
130078cfe1f7SAlan Cox 	KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
130178cfe1f7SAlan Cox 	    ("vm_fault: page %p partially invalid", fs.m));
130289f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(fs.object);
1303cbfbaad8SAlan Cox 
130486735996SAlan Cox 	/*
130586735996SAlan Cox 	 * Put this page into the physical map.  We had to do the unlock above
130686735996SAlan Cox 	 * because pmap_enter() may sleep.  We don't put the page
130786735996SAlan Cox 	 * back on the active queue until later so that the pageout daemon
130886735996SAlan Cox 	 * won't find it (yet).
130986735996SAlan Cox 	 */
131039ffa8c1SKonstantin Belousov 	pmap_enter(fs.map->pmap, vaddr, fs.m, prot,
131139ffa8c1SKonstantin Belousov 	    fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0);
13126a875bf9SKonstantin Belousov 	if (faultcount != 1 && (fault_flags & VM_FAULT_WIRE) == 0 &&
13137b9b301cSAlan Cox 	    wired == 0)
1314b0cd2017SGleb Smirnoff 		vm_fault_prefault(&fs, vaddr,
1315b0cd2017SGleb Smirnoff 		    faultcount > 0 ? behind : PFBAK,
1316a7163bb9SKonstantin Belousov 		    faultcount > 0 ? ahead : PFFOR, false);
131789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(fs.object);
13182965a453SKip Macy 	vm_page_lock(fs.m);
1319ff97964aSJohn Dyson 
1320df8bae1dSRodney W. Grimes 	/*
13210d94caffSDavid Greenman 	 * If the page is not wired down, then put it where the pageout daemon
13220d94caffSDavid Greenman 	 * can find it.
1323df8bae1dSRodney W. Grimes 	 */
1324fccdefa1SAlan Cox 	if ((fault_flags & VM_FAULT_WIRE) != 0)
13254866e085SJohn Dyson 		vm_page_wire(fs.m);
1326fccdefa1SAlan Cox 	else
13274866e085SJohn Dyson 		vm_page_activate(fs.m);
1328acd11c74SAlan Cox 	if (m_hold != NULL) {
1329acd11c74SAlan Cox 		*m_hold = fs.m;
1330acd11c74SAlan Cox 		vm_page_hold(fs.m);
1331acd11c74SAlan Cox 	}
13322965a453SKip Macy 	vm_page_unlock(fs.m);
1333c7aebda8SAttilio Rao 	vm_page_xunbusy(fs.m);
1334eeec6babSJohn Baldwin 
1335eebf3286SAlan Cox 	/*
1336eebf3286SAlan Cox 	 * Unlock everything, and return
1337eebf3286SAlan Cox 	 */
1338eebf3286SAlan Cox 	unlock_and_deallocate(&fs);
1339b3a01bdfSAndrey Zonov 	if (hardfault) {
134083c9dea1SGleb Smirnoff 		VM_CNT_INC(v_io_faults);
13411c4bcd05SJeff Roberson 		curthread->td_ru.ru_majflt++;
1342ae34b6ffSEdward Tomasz Napierala #ifdef RACCT
1343ae34b6ffSEdward Tomasz Napierala 		if (racct_enable && fs.object->type == OBJT_VNODE) {
1344ae34b6ffSEdward Tomasz Napierala 			PROC_LOCK(curproc);
1345ae34b6ffSEdward Tomasz Napierala 			if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1346ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_WRITEBPS,
1347ae34b6ffSEdward Tomasz Napierala 				    PAGE_SIZE + behind * PAGE_SIZE);
1348ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_WRITEIOPS, 1);
1349ae34b6ffSEdward Tomasz Napierala 			} else {
1350ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_READBPS,
1351ae34b6ffSEdward Tomasz Napierala 				    PAGE_SIZE + ahead * PAGE_SIZE);
1352ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_READIOPS, 1);
1353ae34b6ffSEdward Tomasz Napierala 			}
1354ae34b6ffSEdward Tomasz Napierala 			PROC_UNLOCK(curproc);
1355ae34b6ffSEdward Tomasz Napierala 		}
1356ae34b6ffSEdward Tomasz Napierala #endif
1357b3a01bdfSAndrey Zonov 	} else
13581c4bcd05SJeff Roberson 		curthread->td_ru.ru_minflt++;
1359df8bae1dSRodney W. Grimes 
1360df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1361df8bae1dSRodney W. Grimes }
1362df8bae1dSRodney W. Grimes 
1363df8bae1dSRodney W. Grimes /*
1364a8b0f100SAlan Cox  * Speed up the reclamation of pages that precede the faulting pindex within
1365a8b0f100SAlan Cox  * the first object of the shadow chain.  Essentially, perform the equivalent
1366a8b0f100SAlan Cox  * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes
1367a8b0f100SAlan Cox  * the faulting pindex by the cluster size when the pages read by vm_fault()
1368a8b0f100SAlan Cox  * cross a cluster-size boundary.  The cluster size is the greater of the
1369a8b0f100SAlan Cox  * smallest superpage size and VM_FAULT_DONTNEED_MIN.
1370a8b0f100SAlan Cox  *
1371a8b0f100SAlan Cox  * When "fs->first_object" is a shadow object, the pages in the backing object
1372a8b0f100SAlan Cox  * that precede the faulting pindex are deactivated by vm_fault().  So, this
1373a8b0f100SAlan Cox  * function must only be concerned with pages in the first object.
137413458803SAlan Cox  */
137513458803SAlan Cox static void
1376a8b0f100SAlan Cox vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead)
137713458803SAlan Cox {
1378a8b0f100SAlan Cox 	vm_map_entry_t entry;
137913458803SAlan Cox 	vm_object_t first_object, object;
1380a8b0f100SAlan Cox 	vm_offset_t end, start;
1381a8b0f100SAlan Cox 	vm_page_t m, m_next;
1382a8b0f100SAlan Cox 	vm_pindex_t pend, pstart;
1383a8b0f100SAlan Cox 	vm_size_t size;
138413458803SAlan Cox 
138513458803SAlan Cox 	object = fs->object;
138689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
138713458803SAlan Cox 	first_object = fs->first_object;
138813458803SAlan Cox 	if (first_object != object) {
1389b5ab20c0SAlan Cox 		if (!VM_OBJECT_TRYWLOCK(first_object)) {
139089f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
1391b5ab20c0SAlan Cox 			VM_OBJECT_WLOCK(first_object);
139289f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
139313458803SAlan Cox 		}
139413458803SAlan Cox 	}
1395a8b0f100SAlan Cox 	/* Neither fictitious nor unmanaged pages can be reclaimed. */
139628634820SAlan Cox 	if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
1397a8b0f100SAlan Cox 		size = VM_FAULT_DONTNEED_MIN;
1398a8b0f100SAlan Cox 		if (MAXPAGESIZES > 1 && size < pagesizes[1])
1399a8b0f100SAlan Cox 			size = pagesizes[1];
1400a8b0f100SAlan Cox 		end = rounddown2(vaddr, size);
1401a8b0f100SAlan Cox 		if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) &&
1402a8b0f100SAlan Cox 		    (entry = fs->entry)->start < end) {
1403a8b0f100SAlan Cox 			if (end - entry->start < size)
1404a8b0f100SAlan Cox 				start = entry->start;
140513458803SAlan Cox 			else
1406a8b0f100SAlan Cox 				start = end - size;
1407a8b0f100SAlan Cox 			pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED);
1408a8b0f100SAlan Cox 			pstart = OFF_TO_IDX(entry->offset) + atop(start -
1409a8b0f100SAlan Cox 			    entry->start);
1410a8b0f100SAlan Cox 			m_next = vm_page_find_least(first_object, pstart);
1411a8b0f100SAlan Cox 			pend = OFF_TO_IDX(entry->offset) + atop(end -
1412a8b0f100SAlan Cox 			    entry->start);
1413a8b0f100SAlan Cox 			while ((m = m_next) != NULL && m->pindex < pend) {
1414a8b0f100SAlan Cox 				m_next = TAILQ_NEXT(m, listq);
1415a8b0f100SAlan Cox 				if (m->valid != VM_PAGE_BITS_ALL ||
1416a8b0f100SAlan Cox 				    vm_page_busied(m))
141713458803SAlan Cox 					continue;
1418d8015db3SAlan Cox 
1419d8015db3SAlan Cox 				/*
1420d8015db3SAlan Cox 				 * Don't clear PGA_REFERENCED, since it would
1421d8015db3SAlan Cox 				 * likely represent a reference by a different
1422d8015db3SAlan Cox 				 * process.
1423d8015db3SAlan Cox 				 *
1424d8015db3SAlan Cox 				 * Typically, at this point, prefetched pages
1425d8015db3SAlan Cox 				 * are still in the inactive queue.  Only
1426d8015db3SAlan Cox 				 * pages that triggered page faults are in the
1427d8015db3SAlan Cox 				 * active queue.
1428d8015db3SAlan Cox 				 */
142913458803SAlan Cox 				vm_page_lock(m);
14300eb50f9cSMark Johnston 				if (!vm_page_inactive(m))
1431d8015db3SAlan Cox 					vm_page_deactivate(m);
143213458803SAlan Cox 				vm_page_unlock(m);
143313458803SAlan Cox 			}
143413458803SAlan Cox 		}
1435a8b0f100SAlan Cox 	}
143613458803SAlan Cox 	if (first_object != object)
1437b5ab20c0SAlan Cox 		VM_OBJECT_WUNLOCK(first_object);
143813458803SAlan Cox }
143913458803SAlan Cox 
144013458803SAlan Cox /*
1441566526a9SAlan Cox  * vm_fault_prefault provides a quick way of clustering
1442566526a9SAlan Cox  * pagefaults into a processes address space.  It is a "cousin"
1443566526a9SAlan Cox  * of vm_map_pmap_enter, except it runs at page fault time instead
1444566526a9SAlan Cox  * of mmap time.
1445566526a9SAlan Cox  */
1446566526a9SAlan Cox static void
144763281952SAlan Cox vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
1448a7163bb9SKonstantin Belousov     int backward, int forward, bool obj_locked)
1449566526a9SAlan Cox {
145063281952SAlan Cox 	pmap_t pmap;
145163281952SAlan Cox 	vm_map_entry_t entry;
145263281952SAlan Cox 	vm_object_t backing_object, lobject;
1453566526a9SAlan Cox 	vm_offset_t addr, starta;
1454566526a9SAlan Cox 	vm_pindex_t pindex;
14552053c127SStephan Uphoff 	vm_page_t m;
1456b0cd2017SGleb Smirnoff 	int i;
1457566526a9SAlan Cox 
145863281952SAlan Cox 	pmap = fs->map->pmap;
1459950d5f7aSAlan Cox 	if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1460566526a9SAlan Cox 		return;
1461566526a9SAlan Cox 
146263281952SAlan Cox 	entry = fs->entry;
1463566526a9SAlan Cox 
146463cdcaaeSKonstantin Belousov 	if (addra < backward * PAGE_SIZE) {
1465566526a9SAlan Cox 		starta = entry->start;
146663cdcaaeSKonstantin Belousov 	} else {
146763cdcaaeSKonstantin Belousov 		starta = addra - backward * PAGE_SIZE;
146863cdcaaeSKonstantin Belousov 		if (starta < entry->start)
146963cdcaaeSKonstantin Belousov 			starta = entry->start;
1470566526a9SAlan Cox 	}
1471566526a9SAlan Cox 
147263281952SAlan Cox 	/*
147363281952SAlan Cox 	 * Generate the sequence of virtual addresses that are candidates for
147463281952SAlan Cox 	 * prefaulting in an outward spiral from the faulting virtual address,
147563281952SAlan Cox 	 * "addra".  Specifically, the sequence is "addra - PAGE_SIZE", "addra
147663281952SAlan Cox 	 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
147763281952SAlan Cox 	 * If the candidate address doesn't have a backing physical page, then
147863281952SAlan Cox 	 * the loop immediately terminates.
147963281952SAlan Cox 	 */
148063281952SAlan Cox 	for (i = 0; i < 2 * imax(backward, forward); i++) {
148163281952SAlan Cox 		addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
148263281952SAlan Cox 		    PAGE_SIZE);
148363281952SAlan Cox 		if (addr > addra + forward * PAGE_SIZE)
1484566526a9SAlan Cox 			addr = 0;
1485566526a9SAlan Cox 
1486566526a9SAlan Cox 		if (addr < starta || addr >= entry->end)
1487566526a9SAlan Cox 			continue;
1488566526a9SAlan Cox 
1489566526a9SAlan Cox 		if (!pmap_is_prefaultable(pmap, addr))
1490566526a9SAlan Cox 			continue;
1491566526a9SAlan Cox 
1492566526a9SAlan Cox 		pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
149363281952SAlan Cox 		lobject = entry->object.vm_object;
1494a7163bb9SKonstantin Belousov 		if (!obj_locked)
1495c141ae7fSAlan Cox 			VM_OBJECT_RLOCK(lobject);
1496566526a9SAlan Cox 		while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1497566526a9SAlan Cox 		    lobject->type == OBJT_DEFAULT &&
1498566526a9SAlan Cox 		    (backing_object = lobject->backing_object) != NULL) {
149936930fc9SAlan Cox 			KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
150036930fc9SAlan Cox 			    0, ("vm_fault_prefault: unaligned object offset"));
1501566526a9SAlan Cox 			pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1502c141ae7fSAlan Cox 			VM_OBJECT_RLOCK(backing_object);
1503a7163bb9SKonstantin Belousov 			if (!obj_locked || lobject != entry->object.vm_object)
1504c141ae7fSAlan Cox 				VM_OBJECT_RUNLOCK(lobject);
1505566526a9SAlan Cox 			lobject = backing_object;
1506566526a9SAlan Cox 		}
1507cbfbaad8SAlan Cox 		if (m == NULL) {
1508a7163bb9SKonstantin Belousov 			if (!obj_locked || lobject != entry->object.vm_object)
1509c141ae7fSAlan Cox 				VM_OBJECT_RUNLOCK(lobject);
1510566526a9SAlan Cox 			break;
1511cbfbaad8SAlan Cox 		}
15120a2e596aSAlan Cox 		if (m->valid == VM_PAGE_BITS_ALL &&
15133c4a2440SAlan Cox 		    (m->flags & PG_FICTITIOUS) == 0)
15147bfda801SAlan Cox 			pmap_enter_quick(pmap, addr, m, entry->protection);
1515a7163bb9SKonstantin Belousov 		if (!obj_locked || lobject != entry->object.vm_object)
1516c141ae7fSAlan Cox 			VM_OBJECT_RUNLOCK(lobject);
1517566526a9SAlan Cox 	}
1518566526a9SAlan Cox }
1519566526a9SAlan Cox 
1520566526a9SAlan Cox /*
152182de724fSAlan Cox  * Hold each of the physical pages that are mapped by the specified range of
152282de724fSAlan Cox  * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
152382de724fSAlan Cox  * and allow the specified types of access, "prot".  If all of the implied
152482de724fSAlan Cox  * pages are successfully held, then the number of held pages is returned
152582de724fSAlan Cox  * together with pointers to those pages in the array "ma".  However, if any
152682de724fSAlan Cox  * of the pages cannot be held, -1 is returned.
152782de724fSAlan Cox  */
152882de724fSAlan Cox int
152982de724fSAlan Cox vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
153082de724fSAlan Cox     vm_prot_t prot, vm_page_t *ma, int max_count)
153182de724fSAlan Cox {
153282de724fSAlan Cox 	vm_offset_t end, va;
153382de724fSAlan Cox 	vm_page_t *mp;
15347e14088dSKonstantin Belousov 	int count;
153582de724fSAlan Cox 	boolean_t pmap_failed;
153682de724fSAlan Cox 
1537af32c419SKonstantin Belousov 	if (len == 0)
1538af32c419SKonstantin Belousov 		return (0);
153982de724fSAlan Cox 	end = round_page(addr + len);
154082de724fSAlan Cox 	addr = trunc_page(addr);
154182de724fSAlan Cox 
154282de724fSAlan Cox 	/*
154382de724fSAlan Cox 	 * Check for illegal addresses.
154482de724fSAlan Cox 	 */
154582de724fSAlan Cox 	if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
154682de724fSAlan Cox 		return (-1);
154782de724fSAlan Cox 
15487e14088dSKonstantin Belousov 	if (atop(end - addr) > max_count)
154982de724fSAlan Cox 		panic("vm_fault_quick_hold_pages: count > max_count");
15507e14088dSKonstantin Belousov 	count = atop(end - addr);
155182de724fSAlan Cox 
155282de724fSAlan Cox 	/*
155382de724fSAlan Cox 	 * Most likely, the physical pages are resident in the pmap, so it is
155482de724fSAlan Cox 	 * faster to try pmap_extract_and_hold() first.
155582de724fSAlan Cox 	 */
155682de724fSAlan Cox 	pmap_failed = FALSE;
155782de724fSAlan Cox 	for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
155882de724fSAlan Cox 		*mp = pmap_extract_and_hold(map->pmap, va, prot);
155982de724fSAlan Cox 		if (*mp == NULL)
156082de724fSAlan Cox 			pmap_failed = TRUE;
156182de724fSAlan Cox 		else if ((prot & VM_PROT_WRITE) != 0 &&
1562a5dbab54SAlan Cox 		    (*mp)->dirty != VM_PAGE_BITS_ALL) {
156382de724fSAlan Cox 			/*
156482de724fSAlan Cox 			 * Explicitly dirty the physical page.  Otherwise, the
156582de724fSAlan Cox 			 * caller's changes may go unnoticed because they are
156682de724fSAlan Cox 			 * performed through an unmanaged mapping or by a DMA
156782de724fSAlan Cox 			 * operation.
15683c76db4cSAlan Cox 			 *
1569abb9b935SKonstantin Belousov 			 * The object lock is not held here.
1570abb9b935SKonstantin Belousov 			 * See vm_page_clear_dirty_mask().
157182de724fSAlan Cox 			 */
15723c76db4cSAlan Cox 			vm_page_dirty(*mp);
157382de724fSAlan Cox 		}
157482de724fSAlan Cox 	}
157582de724fSAlan Cox 	if (pmap_failed) {
157682de724fSAlan Cox 		/*
157782de724fSAlan Cox 		 * One or more pages could not be held by the pmap.  Either no
157882de724fSAlan Cox 		 * page was mapped at the specified virtual address or that
157982de724fSAlan Cox 		 * mapping had insufficient permissions.  Attempt to fault in
158082de724fSAlan Cox 		 * and hold these pages.
15818ec533d3SKonstantin Belousov 		 *
15828ec533d3SKonstantin Belousov 		 * If vm_fault_disable_pagefaults() was called,
15838ec533d3SKonstantin Belousov 		 * i.e., TDP_NOFAULTING is set, we must not sleep nor
15848ec533d3SKonstantin Belousov 		 * acquire MD VM locks, which means we must not call
15858ec533d3SKonstantin Belousov 		 * vm_fault_hold().  Some (out of tree) callers mark
15868ec533d3SKonstantin Belousov 		 * too wide a code area with vm_fault_disable_pagefaults()
15878ec533d3SKonstantin Belousov 		 * already, use the VM_PROT_QUICK_NOFAULT flag to request
15888ec533d3SKonstantin Belousov 		 * the proper behaviour explicitly.
158982de724fSAlan Cox 		 */
15908ec533d3SKonstantin Belousov 		if ((prot & VM_PROT_QUICK_NOFAULT) != 0 &&
15918ec533d3SKonstantin Belousov 		    (curthread->td_pflags & TDP_NOFAULTING) != 0)
15928ec533d3SKonstantin Belousov 			goto error;
159382de724fSAlan Cox 		for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1594be996836SAttilio Rao 			if (*mp == NULL && vm_fault_hold(map, va, prot,
159582de724fSAlan Cox 			    VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
159682de724fSAlan Cox 				goto error;
159782de724fSAlan Cox 	}
159882de724fSAlan Cox 	return (count);
159982de724fSAlan Cox error:
160082de724fSAlan Cox 	for (mp = ma; mp < ma + count; mp++)
160182de724fSAlan Cox 		if (*mp != NULL) {
160282de724fSAlan Cox 			vm_page_lock(*mp);
160382de724fSAlan Cox 			vm_page_unhold(*mp);
160482de724fSAlan Cox 			vm_page_unlock(*mp);
160582de724fSAlan Cox 		}
160682de724fSAlan Cox 	return (-1);
160782de724fSAlan Cox }
160882de724fSAlan Cox 
160982de724fSAlan Cox /*
1610df8bae1dSRodney W. Grimes  *	Routine:
1611df8bae1dSRodney W. Grimes  *		vm_fault_copy_entry
1612df8bae1dSRodney W. Grimes  *	Function:
1613210a6886SKonstantin Belousov  *		Create new shadow object backing dst_entry with private copy of
1614210a6886SKonstantin Belousov  *		all underlying pages. When src_entry is equal to dst_entry,
1615210a6886SKonstantin Belousov  *		function implements COW for wired-down map entry. Otherwise,
1616210a6886SKonstantin Belousov  *		it forks wired entry into dst_map.
1617df8bae1dSRodney W. Grimes  *
1618df8bae1dSRodney W. Grimes  *	In/out conditions:
1619df8bae1dSRodney W. Grimes  *		The source and destination maps must be locked for write.
1620df8bae1dSRodney W. Grimes  *		The source map entry must be wired down (or be a sharing map
1621df8bae1dSRodney W. Grimes  *		entry corresponding to a main map entry that is wired down).
1622df8bae1dSRodney W. Grimes  */
162326f9a767SRodney W. Grimes void
1624121fd461SKonstantin Belousov vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1625121fd461SKonstantin Belousov     vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1626121fd461SKonstantin Belousov     vm_ooffset_t *fork_charge)
1627df8bae1dSRodney W. Grimes {
1628210a6886SKonstantin Belousov 	vm_object_t backing_object, dst_object, object, src_object;
16297afab86cSAlan Cox 	vm_pindex_t dst_pindex, pindex, src_pindex;
1630210a6886SKonstantin Belousov 	vm_prot_t access, prot;
1631df8bae1dSRodney W. Grimes 	vm_offset_t vaddr;
1632df8bae1dSRodney W. Grimes 	vm_page_t dst_m;
1633df8bae1dSRodney W. Grimes 	vm_page_t src_m;
16344c74acf7SKonstantin Belousov 	boolean_t upgrade;
1635df8bae1dSRodney W. Grimes 
1636df8bae1dSRodney W. Grimes #ifdef	lint
1637df8bae1dSRodney W. Grimes 	src_map++;
16380d94caffSDavid Greenman #endif	/* lint */
1639df8bae1dSRodney W. Grimes 
1640210a6886SKonstantin Belousov 	upgrade = src_entry == dst_entry;
16410973283dSKonstantin Belousov 	access = prot = dst_entry->protection;
1642210a6886SKonstantin Belousov 
1643df8bae1dSRodney W. Grimes 	src_object = src_entry->object.vm_object;
16447afab86cSAlan Cox 	src_pindex = OFF_TO_IDX(src_entry->offset);
1645df8bae1dSRodney W. Grimes 
16460973283dSKonstantin Belousov 	if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
16470973283dSKonstantin Belousov 		dst_object = src_object;
16480973283dSKonstantin Belousov 		vm_object_reference(dst_object);
16490973283dSKonstantin Belousov 	} else {
1650df8bae1dSRodney W. Grimes 		/*
16510d94caffSDavid Greenman 		 * Create the top-level object for the destination entry. (Doesn't
16520d94caffSDavid Greenman 		 * actually shadow anything - we copy the pages directly.)
1653df8bae1dSRodney W. Grimes 		 */
165424a1cce3SDavid Greenman 		dst_object = vm_object_allocate(OBJT_DEFAULT,
1655d1780e8dSKonstantin Belousov 		    atop(dst_entry->end - dst_entry->start));
1656f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1657f8a47341SAlan Cox 		dst_object->flags |= OBJ_COLORED;
1658f8a47341SAlan Cox 		dst_object->pg_color = atop(dst_entry->start);
1659f8a47341SAlan Cox #endif
1660a60d3db1SKonstantin Belousov 		dst_object->domain = src_object->domain;
1661a60d3db1SKonstantin Belousov 		dst_object->charge = dst_entry->end - dst_entry->start;
16620973283dSKonstantin Belousov 	}
1663df8bae1dSRodney W. Grimes 
166489f6b863SAttilio Rao 	VM_OBJECT_WLOCK(dst_object);
1665210a6886SKonstantin Belousov 	KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1666121fd461SKonstantin Belousov 	    ("vm_fault_copy_entry: vm_object not NULL"));
16670973283dSKonstantin Belousov 	if (src_object != dst_object) {
1668df8bae1dSRodney W. Grimes 		dst_entry->object.vm_object = dst_object;
1669df8bae1dSRodney W. Grimes 		dst_entry->offset = 0;
167078022527SKonstantin Belousov 		dst_entry->eflags &= ~MAP_ENTRY_VN_EXEC;
16710973283dSKonstantin Belousov 	}
1672210a6886SKonstantin Belousov 	if (fork_charge != NULL) {
1673ef694c1aSEdward Tomasz Napierala 		KASSERT(dst_entry->cred == NULL,
1674121fd461SKonstantin Belousov 		    ("vm_fault_copy_entry: leaked swp charge"));
1675ef694c1aSEdward Tomasz Napierala 		dst_object->cred = curthread->td_ucred;
1676ef694c1aSEdward Tomasz Napierala 		crhold(dst_object->cred);
1677121fd461SKonstantin Belousov 		*fork_charge += dst_object->charge;
16789f25ab83SKonstantin Belousov 	} else if ((dst_object->type == OBJT_DEFAULT ||
16799f25ab83SKonstantin Belousov 	    dst_object->type == OBJT_SWAP) &&
16809f25ab83SKonstantin Belousov 	    dst_object->cred == NULL) {
16810973283dSKonstantin Belousov 		KASSERT(dst_entry->cred != NULL, ("no cred for entry %p",
16820973283dSKonstantin Belousov 		    dst_entry));
1683ef694c1aSEdward Tomasz Napierala 		dst_object->cred = dst_entry->cred;
1684ef694c1aSEdward Tomasz Napierala 		dst_entry->cred = NULL;
1685210a6886SKonstantin Belousov 	}
16860973283dSKonstantin Belousov 
1687210a6886SKonstantin Belousov 	/*
1688210a6886SKonstantin Belousov 	 * If not an upgrade, then enter the mappings in the pmap as
1689210a6886SKonstantin Belousov 	 * read and/or execute accesses.  Otherwise, enter them as
1690210a6886SKonstantin Belousov 	 * write accesses.
1691210a6886SKonstantin Belousov 	 *
1692210a6886SKonstantin Belousov 	 * A writeable large page mapping is only created if all of
1693210a6886SKonstantin Belousov 	 * the constituent small page mappings are modified. Marking
1694210a6886SKonstantin Belousov 	 * PTEs as modified on inception allows promotion to happen
1695210a6886SKonstantin Belousov 	 * without taking potentially large number of soft faults.
1696210a6886SKonstantin Belousov 	 */
1697210a6886SKonstantin Belousov 	if (!upgrade)
1698210a6886SKonstantin Belousov 		access &= ~VM_PROT_WRITE;
1699df8bae1dSRodney W. Grimes 
1700df8bae1dSRodney W. Grimes 	/*
1701ef45823eSKonstantin Belousov 	 * Loop through all of the virtual pages within the entry's
1702ef45823eSKonstantin Belousov 	 * range, copying each page from the source object to the
1703ef45823eSKonstantin Belousov 	 * destination object.  Since the source is wired, those pages
1704ef45823eSKonstantin Belousov 	 * must exist.  In contrast, the destination is pageable.
17056939b4d3SMark Johnston 	 * Since the destination object doesn't share any backing storage
1706ef45823eSKonstantin Belousov 	 * with the source object, all of its pages must be dirtied,
1707ef45823eSKonstantin Belousov 	 * regardless of whether they can be written.
1708df8bae1dSRodney W. Grimes 	 */
17097afab86cSAlan Cox 	for (vaddr = dst_entry->start, dst_pindex = 0;
1710df8bae1dSRodney W. Grimes 	    vaddr < dst_entry->end;
17117afab86cSAlan Cox 	    vaddr += PAGE_SIZE, dst_pindex++) {
17120973283dSKonstantin Belousov again:
1713df8bae1dSRodney W. Grimes 		/*
1714df8bae1dSRodney W. Grimes 		 * Find the page in the source object, and copy it in.
17154c74acf7SKonstantin Belousov 		 * Because the source is wired down, the page will be
17164c74acf7SKonstantin Belousov 		 * in memory.
1717df8bae1dSRodney W. Grimes 		 */
17180973283dSKonstantin Belousov 		if (src_object != dst_object)
171983b375eaSAttilio Rao 			VM_OBJECT_RLOCK(src_object);
1720c5b65a67SAlan Cox 		object = src_object;
17217afab86cSAlan Cox 		pindex = src_pindex + dst_pindex;
17227afab86cSAlan Cox 		while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1723c5b65a67SAlan Cox 		    (backing_object = object->backing_object) != NULL) {
1724c5b65a67SAlan Cox 			/*
17254c74acf7SKonstantin Belousov 			 * Unless the source mapping is read-only or
17264c74acf7SKonstantin Belousov 			 * it is presently being upgraded from
17274c74acf7SKonstantin Belousov 			 * read-only, the first object in the shadow
17284c74acf7SKonstantin Belousov 			 * chain should provide all of the pages.  In
17294c74acf7SKonstantin Belousov 			 * other words, this loop body should never be
17304c74acf7SKonstantin Belousov 			 * executed when the source mapping is already
17314c74acf7SKonstantin Belousov 			 * read/write.
1732c5b65a67SAlan Cox 			 */
17334c74acf7SKonstantin Belousov 			KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 ||
17344c74acf7SKonstantin Belousov 			    upgrade,
17354c74acf7SKonstantin Belousov 			    ("vm_fault_copy_entry: main object missing page"));
17364c74acf7SKonstantin Belousov 
173783b375eaSAttilio Rao 			VM_OBJECT_RLOCK(backing_object);
1738c5b65a67SAlan Cox 			pindex += OFF_TO_IDX(object->backing_object_offset);
17390973283dSKonstantin Belousov 			if (object != dst_object)
174083b375eaSAttilio Rao 				VM_OBJECT_RUNLOCK(object);
1741c5b65a67SAlan Cox 			object = backing_object;
1742c5b65a67SAlan Cox 		}
17434c74acf7SKonstantin Belousov 		KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing"));
17440973283dSKonstantin Belousov 
17450973283dSKonstantin Belousov 		if (object != dst_object) {
17460973283dSKonstantin Belousov 			/*
17470973283dSKonstantin Belousov 			 * Allocate a page in the destination object.
17480973283dSKonstantin Belousov 			 */
17492602a2eaSKonstantin Belousov 			dst_m = vm_page_alloc(dst_object, (src_object ==
17502602a2eaSKonstantin Belousov 			    dst_object ? src_pindex : 0) + dst_pindex,
17512602a2eaSKonstantin Belousov 			    VM_ALLOC_NORMAL);
17520973283dSKonstantin Belousov 			if (dst_m == NULL) {
17530973283dSKonstantin Belousov 				VM_OBJECT_WUNLOCK(dst_object);
17540973283dSKonstantin Belousov 				VM_OBJECT_RUNLOCK(object);
17552c0f13aaSKonstantin Belousov 				vm_wait(dst_object);
1756c8f780e3SKonstantin Belousov 				VM_OBJECT_WLOCK(dst_object);
17570973283dSKonstantin Belousov 				goto again;
17580973283dSKonstantin Belousov 			}
1759669890eaSAlan Cox 			pmap_copy_page(src_m, dst_m);
176083b375eaSAttilio Rao 			VM_OBJECT_RUNLOCK(object);
176145d72c7dSKonstantin Belousov 			dst_m->dirty = dst_m->valid = src_m->valid;
17620973283dSKonstantin Belousov 		} else {
17630973283dSKonstantin Belousov 			dst_m = src_m;
17640973283dSKonstantin Belousov 			if (vm_page_sleep_if_busy(dst_m, "fltupg"))
17650973283dSKonstantin Belousov 				goto again;
1766c62637d6SKonstantin Belousov 			if (dst_m->pindex >= dst_object->size)
1767c62637d6SKonstantin Belousov 				/*
1768c62637d6SKonstantin Belousov 				 * We are upgrading.  Index can occur
1769c62637d6SKonstantin Belousov 				 * out of bounds if the object type is
1770c62637d6SKonstantin Belousov 				 * vnode and the file was truncated.
1771c62637d6SKonstantin Belousov 				 */
1772c62637d6SKonstantin Belousov 				break;
17730973283dSKonstantin Belousov 			vm_page_xbusy(dst_m);
17740973283dSKonstantin Belousov 		}
177589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(dst_object);
1776df8bae1dSRodney W. Grimes 
1777df8bae1dSRodney W. Grimes 		/*
1778210a6886SKonstantin Belousov 		 * Enter it in the pmap. If a wired, copy-on-write
1779210a6886SKonstantin Belousov 		 * mapping is being replaced by a write-enabled
1780210a6886SKonstantin Belousov 		 * mapping, then wire that new mapping.
178145d72c7dSKonstantin Belousov 		 *
178245d72c7dSKonstantin Belousov 		 * The page can be invalid if the user called
178345d72c7dSKonstantin Belousov 		 * msync(MS_INVALIDATE) or truncated the backing vnode
178445d72c7dSKonstantin Belousov 		 * or shared memory object.  In this case, do not
178545d72c7dSKonstantin Belousov 		 * insert it into pmap, but still do the copy so that
178645d72c7dSKonstantin Belousov 		 * all copies of the wired map entry have similar
178745d72c7dSKonstantin Belousov 		 * backing pages.
1788df8bae1dSRodney W. Grimes 		 */
178945d72c7dSKonstantin Belousov 		if (dst_m->valid == VM_PAGE_BITS_ALL) {
179039ffa8c1SKonstantin Belousov 			pmap_enter(dst_map->pmap, vaddr, dst_m, prot,
179139ffa8c1SKonstantin Belousov 			    access | (upgrade ? PMAP_ENTER_WIRED : 0), 0);
179245d72c7dSKonstantin Belousov 		}
1793df8bae1dSRodney W. Grimes 
1794df8bae1dSRodney W. Grimes 		/*
1795df8bae1dSRodney W. Grimes 		 * Mark it no longer busy, and put it on the active list.
1796df8bae1dSRodney W. Grimes 		 */
179789f6b863SAttilio Rao 		VM_OBJECT_WLOCK(dst_object);
17982965a453SKip Macy 
1799210a6886SKonstantin Belousov 		if (upgrade) {
18000973283dSKonstantin Belousov 			if (src_m != dst_m) {
18012965a453SKip Macy 				vm_page_lock(src_m);
18023ae10f74SAttilio Rao 				vm_page_unwire(src_m, PQ_INACTIVE);
1803e20e8c15SKonstantin Belousov 				vm_page_unlock(src_m);
18042965a453SKip Macy 				vm_page_lock(dst_m);
1805210a6886SKonstantin Belousov 				vm_page_wire(dst_m);
1806e20e8c15SKonstantin Belousov 				vm_page_unlock(dst_m);
18072965a453SKip Macy 			} else {
1808*d842aa51SMark Johnston 				KASSERT(vm_page_wired(dst_m),
18090973283dSKonstantin Belousov 				    ("dst_m %p is not wired", dst_m));
18100973283dSKonstantin Belousov 			}
18110973283dSKonstantin Belousov 		} else {
18122965a453SKip Macy 			vm_page_lock(dst_m);
1813df8bae1dSRodney W. Grimes 			vm_page_activate(dst_m);
1814e20e8c15SKonstantin Belousov 			vm_page_unlock(dst_m);
18152965a453SKip Macy 		}
1816c7aebda8SAttilio Rao 		vm_page_xunbusy(dst_m);
1817df8bae1dSRodney W. Grimes 	}
181889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(dst_object);
1819210a6886SKonstantin Belousov 	if (upgrade) {
1820210a6886SKonstantin Belousov 		dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1821210a6886SKonstantin Belousov 		vm_object_deallocate(src_object);
1822210a6886SKonstantin Belousov 	}
1823df8bae1dSRodney W. Grimes }
182426f9a767SRodney W. Grimes 
18255730afc9SAlan Cox /*
18265730afc9SAlan Cox  * Block entry into the machine-independent layer's page fault handler by
18275730afc9SAlan Cox  * the calling thread.  Subsequent calls to vm_fault() by that thread will
18285730afc9SAlan Cox  * return KERN_PROTECTION_FAILURE.  Enable machine-dependent handling of
18295730afc9SAlan Cox  * spurious page faults.
18305730afc9SAlan Cox  */
18312801687dSKonstantin Belousov int
18322801687dSKonstantin Belousov vm_fault_disable_pagefaults(void)
18332801687dSKonstantin Belousov {
18342801687dSKonstantin Belousov 
18355730afc9SAlan Cox 	return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
18362801687dSKonstantin Belousov }
18372801687dSKonstantin Belousov 
18382801687dSKonstantin Belousov void
18392801687dSKonstantin Belousov vm_fault_enable_pagefaults(int save)
18402801687dSKonstantin Belousov {
18412801687dSKonstantin Belousov 
18422801687dSKonstantin Belousov 	curthread_pflags_restore(save);
18432801687dSKonstantin Belousov }
1844