xref: /freebsd/sys/vm/vm_fault.c (revision ef747607ead43552cb3ebb093fadd2c39c56a36d)
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>
87eeacb3b0SMark Johnston #include <sys/mutex.h>
885d32157dSMark Johnston #include <sys/pctrie.h>
8926f9a767SRodney W. Grimes #include <sys/proc.h>
90ae34b6ffSEdward Tomasz Napierala #include <sys/racct.h>
9111b57401SHans Petter Selasky #include <sys/refcount.h>
9226f9a767SRodney W. Grimes #include <sys/resourcevar.h>
9389f6b863SAttilio Rao #include <sys/rwlock.h>
94df08823dSKonstantin Belousov #include <sys/signalvar.h>
9523955314SAlfred Perlstein #include <sys/sysctl.h>
96df08823dSKonstantin Belousov #include <sys/sysent.h>
974edf4a58SJohn Baldwin #include <sys/vmmeter.h>
984edf4a58SJohn Baldwin #include <sys/vnode.h>
9935818d2eSJohn Baldwin #ifdef KTRACE
10035818d2eSJohn Baldwin #include <sys/ktrace.h>
10135818d2eSJohn Baldwin #endif
102df8bae1dSRodney W. Grimes 
103df8bae1dSRodney W. Grimes #include <vm/vm.h>
104efeaf95aSDavid Greenman #include <vm/vm_param.h>
105efeaf95aSDavid Greenman #include <vm/pmap.h>
106efeaf95aSDavid Greenman #include <vm/vm_map.h>
107efeaf95aSDavid Greenman #include <vm/vm_object.h>
108df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
109df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
110a83c285cSDavid Greenman #include <vm/vm_kern.h>
11124a1cce3SDavid Greenman #include <vm/vm_pager.h>
112efeaf95aSDavid Greenman #include <vm/vm_extern.h>
113dfdf9abdSAlan Cox #include <vm/vm_reserv.h>
114df8bae1dSRodney W. Grimes 
115566526a9SAlan Cox #define PFBAK 4
116566526a9SAlan Cox #define PFFOR 4
117566526a9SAlan Cox 
1185268042bSAlan Cox #define	VM_FAULT_READ_DEFAULT	(1 + VM_FAULT_READ_AHEAD_INIT)
119a8b0f100SAlan Cox 
120a8b0f100SAlan Cox #define	VM_FAULT_DONTNEED_MIN	1048576
12126f9a767SRodney W. Grimes 
1224866e085SJohn Dyson struct faultstate {
1232c2f4413SJeff Roberson 	/* Fault parameters. */
1245949b1caSJeff Roberson 	vm_offset_t	vaddr;
1252c2f4413SJeff Roberson 	vm_page_t	*m_hold;
1262c2f4413SJeff Roberson 	vm_prot_t	fault_type;
1272c2f4413SJeff Roberson 	vm_prot_t	prot;
1282c2f4413SJeff Roberson 	int		fault_flags;
12945c09a74SMark Johnston 	boolean_t	wired;
13045c09a74SMark Johnston 
13145c09a74SMark Johnston 	/* Control state. */
132174aad04SKonstantin Belousov 	struct timeval	oom_start_time;
133174aad04SKonstantin Belousov 	bool		oom_started;
13445c09a74SMark Johnston 	int		nera;
135fdb1dbb1SMateusz Guzik 	bool		can_read_lock;
1362c2f4413SJeff Roberson 
1372c2f4413SJeff Roberson 	/* Page reference for cow. */
13858447749SJeff Roberson 	vm_page_t m_cow;
1392c2f4413SJeff Roberson 
1402c2f4413SJeff Roberson 	/* Current object. */
1414866e085SJohn Dyson 	vm_object_t	object;
1424866e085SJohn Dyson 	vm_pindex_t	pindex;
1432c2f4413SJeff Roberson 	vm_page_t	m;
1442c2f4413SJeff Roberson 
1452c2f4413SJeff Roberson 	/* Top-level map object. */
1464866e085SJohn Dyson 	vm_object_t	first_object;
1474866e085SJohn Dyson 	vm_pindex_t	first_pindex;
1482c2f4413SJeff Roberson 	vm_page_t	first_m;
1492c2f4413SJeff Roberson 
1502c2f4413SJeff Roberson 	/* Map state. */
1514866e085SJohn Dyson 	vm_map_t	map;
1524866e085SJohn Dyson 	vm_map_entry_t	entry;
153dc5401d2SKonstantin Belousov 	int		map_generation;
154cd8a6fe8SAlan Cox 	bool		lookup_still_valid;
1552c2f4413SJeff Roberson 
1562c2f4413SJeff Roberson 	/* Vnode if locked. */
1574866e085SJohn Dyson 	struct vnode	*vp;
1584866e085SJohn Dyson };
1594866e085SJohn Dyson 
160f1b642c2SMark Johnston /*
161f1b642c2SMark Johnston  * Return codes for internal fault routines.
162f1b642c2SMark Johnston  */
163f1b642c2SMark Johnston enum fault_status {
164*ef747607SKonstantin Belousov 	FAULT_SUCCESS = 10000,	/* Return success to user. */
165f1b642c2SMark Johnston 	FAULT_FAILURE,		/* Return failure to user. */
166f1b642c2SMark Johnston 	FAULT_CONTINUE,		/* Continue faulting. */
167f1b642c2SMark Johnston 	FAULT_RESTART,		/* Restart fault. */
168f1b642c2SMark Johnston 	FAULT_OUT_OF_BOUNDS,	/* Invalid address for pager. */
169f1b642c2SMark Johnston 	FAULT_HARD,		/* Performed I/O. */
170f1b642c2SMark Johnston 	FAULT_SOFT,		/* Found valid page. */
171d47d3a94SMark Johnston 	FAULT_PROTECTION_FAILURE, /* Invalid access. */
172f1b642c2SMark Johnston };
173f1b642c2SMark Johnston 
174fdb1dbb1SMateusz Guzik enum fault_next_status {
175fdb1dbb1SMateusz Guzik 	FAULT_NEXT_GOTOBJ = 1,
176fdb1dbb1SMateusz Guzik 	FAULT_NEXT_NOOBJ,
177fdb1dbb1SMateusz Guzik 	FAULT_NEXT_RESTART,
178fdb1dbb1SMateusz Guzik };
179fdb1dbb1SMateusz Guzik 
180a8b0f100SAlan Cox static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr,
181a8b0f100SAlan Cox 	    int ahead);
18263281952SAlan Cox static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
183a7163bb9SKonstantin Belousov 	    int backward, int forward, bool obj_locked);
18413458803SAlan Cox 
185245139c6SKonstantin Belousov static int vm_pfault_oom_attempts = 3;
186245139c6SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, pfault_oom_attempts, CTLFLAG_RWTUN,
187245139c6SKonstantin Belousov     &vm_pfault_oom_attempts, 0,
188245139c6SKonstantin Belousov     "Number of page allocation attempts in page fault handler before it "
189245139c6SKonstantin Belousov     "triggers OOM handling");
190245139c6SKonstantin Belousov 
191245139c6SKonstantin Belousov static int vm_pfault_oom_wait = 10;
192245139c6SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, pfault_oom_wait, CTLFLAG_RWTUN,
193245139c6SKonstantin Belousov     &vm_pfault_oom_wait, 0,
194245139c6SKonstantin Belousov     "Number of seconds to wait for free pages before retrying "
195245139c6SKonstantin Belousov     "the page fault handler");
196245139c6SKonstantin Belousov 
19762a59e8fSWarner Losh static inline void
1980a310c94SMateusz Guzik vm_fault_page_release(vm_page_t *mp)
1994866e085SJohn Dyson {
2004bf95d00SJeff Roberson 	vm_page_t m;
2010d0be82aSKonstantin Belousov 
2024bf95d00SJeff Roberson 	m = *mp;
2034bf95d00SJeff Roberson 	if (m != NULL) {
204be801aaaSMark Johnston 		/*
2054bf95d00SJeff Roberson 		 * We are likely to loop around again and attempt to busy
2064bf95d00SJeff Roberson 		 * this page.  Deactivating it leaves it available for
2074bf95d00SJeff Roberson 		 * pageout while optimizing fault restarts.
208be801aaaSMark Johnston 		 */
2094bf95d00SJeff Roberson 		vm_page_deactivate(m);
2104bf95d00SJeff Roberson 		vm_page_xunbusy(m);
2114bf95d00SJeff Roberson 		*mp = NULL;
2124bf95d00SJeff Roberson 	}
2134bf95d00SJeff Roberson }
2144bf95d00SJeff Roberson 
2154bf95d00SJeff Roberson static inline void
2160a310c94SMateusz Guzik vm_fault_page_free(vm_page_t *mp)
2174bf95d00SJeff Roberson {
2184bf95d00SJeff Roberson 	vm_page_t m;
2194bf95d00SJeff Roberson 
2204bf95d00SJeff Roberson 	m = *mp;
2214bf95d00SJeff Roberson 	if (m != NULL) {
2224bf95d00SJeff Roberson 		VM_OBJECT_ASSERT_WLOCKED(m->object);
2234bf95d00SJeff Roberson 		if (!vm_page_wired(m))
2244bf95d00SJeff Roberson 			vm_page_free(m);
225419f0b1fSJeff Roberson 		else
226419f0b1fSJeff Roberson 			vm_page_xunbusy(m);
2274bf95d00SJeff Roberson 		*mp = NULL;
2284866e085SJohn Dyson 	}
229be2c5610SMark Johnston }
2304866e085SJohn Dyson 
2315d32157dSMark Johnston /*
2325d32157dSMark Johnston  * Return true if a vm_pager_get_pages() call is needed in order to check
2335d32157dSMark Johnston  * whether the pager might have a particular page, false if it can be determined
2345d32157dSMark Johnston  * immediately that the pager can not have a copy.  For swap objects, this can
2355d32157dSMark Johnston  * be checked quickly.
2365d32157dSMark Johnston  */
2375d32157dSMark Johnston static inline bool
2380a310c94SMateusz Guzik vm_fault_object_needs_getpages(vm_object_t object)
2395d32157dSMark Johnston {
2405d32157dSMark Johnston 	VM_OBJECT_ASSERT_LOCKED(object);
2415d32157dSMark Johnston 
2425d32157dSMark Johnston 	return ((object->flags & OBJ_SWAP) == 0 ||
2435d32157dSMark Johnston 	    !pctrie_is_empty(&object->un_pager.swp.swp_blks));
2445d32157dSMark Johnston }
2455d32157dSMark Johnston 
24662a59e8fSWarner Losh static inline void
2470a310c94SMateusz Guzik vm_fault_unlock_map(struct faultstate *fs)
2484866e085SJohn Dyson {
2490d0be82aSKonstantin Belousov 
25025adb370SBrian Feldman 	if (fs->lookup_still_valid) {
2514866e085SJohn Dyson 		vm_map_lookup_done(fs->map, fs->entry);
252cd8a6fe8SAlan Cox 		fs->lookup_still_valid = false;
2534866e085SJohn Dyson 	}
2544866e085SJohn Dyson }
2554866e085SJohn Dyson 
2564866e085SJohn Dyson static void
2570a310c94SMateusz Guzik vm_fault_unlock_vp(struct faultstate *fs)
258cfabea3dSKonstantin Belousov {
259cfabea3dSKonstantin Belousov 
260cfabea3dSKonstantin Belousov 	if (fs->vp != NULL) {
261cfabea3dSKonstantin Belousov 		vput(fs->vp);
262cfabea3dSKonstantin Belousov 		fs->vp = NULL;
263cfabea3dSKonstantin Belousov 	}
264cfabea3dSKonstantin Belousov }
265cfabea3dSKonstantin Belousov 
266cfabea3dSKonstantin Belousov static void
2670a310c94SMateusz Guzik vm_fault_deallocate(struct faultstate *fs)
2684866e085SJohn Dyson {
269f29ba63eSAlan Cox 
2700a310c94SMateusz Guzik 	vm_fault_page_release(&fs->m_cow);
2710a310c94SMateusz Guzik 	vm_fault_page_release(&fs->m);
2724866e085SJohn Dyson 	vm_object_pip_wakeup(fs->object);
2734866e085SJohn Dyson 	if (fs->object != fs->first_object) {
27489f6b863SAttilio Rao 		VM_OBJECT_WLOCK(fs->first_object);
2750a310c94SMateusz Guzik 		vm_fault_page_free(&fs->first_m);
27689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(fs->first_object);
2774bf95d00SJeff Roberson 		vm_object_pip_wakeup(fs->first_object);
2784866e085SJohn Dyson 	}
2794866e085SJohn Dyson 	vm_object_deallocate(fs->first_object);
2800a310c94SMateusz Guzik 	vm_fault_unlock_map(fs);
2810a310c94SMateusz Guzik 	vm_fault_unlock_vp(fs);
2824866e085SJohn Dyson }
2834866e085SJohn Dyson 
284a36f5532SKonstantin Belousov static void
2850a310c94SMateusz Guzik vm_fault_unlock_and_deallocate(struct faultstate *fs)
2864b3e0665SJeff Roberson {
2874b3e0665SJeff Roberson 
288fdb1dbb1SMateusz Guzik 	VM_OBJECT_UNLOCK(fs->object);
2890a310c94SMateusz Guzik 	vm_fault_deallocate(fs);
2904b3e0665SJeff Roberson }
2914b3e0665SJeff Roberson 
2924b3e0665SJeff Roberson static void
2932c2f4413SJeff Roberson vm_fault_dirty(struct faultstate *fs, vm_page_t m)
294a36f5532SKonstantin Belousov {
295e26236e9SKonstantin Belousov 	bool need_dirty;
296a36f5532SKonstantin Belousov 
2972c2f4413SJeff Roberson 	if (((fs->prot & VM_PROT_WRITE) == 0 &&
2982c2f4413SJeff Roberson 	    (fs->fault_flags & VM_FAULT_DIRTY) == 0) ||
299a36f5532SKonstantin Belousov 	    (m->oflags & VPO_UNMANAGED) != 0)
300a36f5532SKonstantin Belousov 		return;
301a36f5532SKonstantin Belousov 
3020012f373SJeff Roberson 	VM_PAGE_OBJECT_BUSY_ASSERT(m);
303a36f5532SKonstantin Belousov 
3042c2f4413SJeff Roberson 	need_dirty = ((fs->fault_type & VM_PROT_WRITE) != 0 &&
3052c2f4413SJeff Roberson 	    (fs->fault_flags & VM_FAULT_WIRE) == 0) ||
3062c2f4413SJeff Roberson 	    (fs->fault_flags & VM_FAULT_DIRTY) != 0;
307a36f5532SKonstantin Belousov 
308a36f5532SKonstantin Belousov 	vm_object_set_writeable_dirty(m->object);
30967d0e293SJeff Roberson 
310a36f5532SKonstantin Belousov 	/*
311a8081778SJeff Roberson 	 * If the fault is a write, we know that this page is being
312a8081778SJeff Roberson 	 * written NOW so dirty it explicitly to save on
313a8081778SJeff Roberson 	 * pmap_is_modified() calls later.
314a8081778SJeff Roberson 	 *
315a8081778SJeff Roberson 	 * Also, since the page is now dirty, we can possibly tell
316a8081778SJeff Roberson 	 * the pager to release any swap backing the page.
317a36f5532SKonstantin Belousov 	 */
318a8081778SJeff Roberson 	if (need_dirty && vm_page_set_dirty(m) == 0) {
319a36f5532SKonstantin Belousov 		/*
320fff5403fSJeff Roberson 		 * If this is a NOSYNC mmap we do not want to set PGA_NOSYNC
321a36f5532SKonstantin Belousov 		 * if the page is already dirty to prevent data written with
322a36f5532SKonstantin Belousov 		 * the expectation of being synced from not being synced.
323a36f5532SKonstantin Belousov 		 * Likewise if this entry does not request NOSYNC then make
324a36f5532SKonstantin Belousov 		 * sure the page isn't marked NOSYNC.  Applications sharing
325a36f5532SKonstantin Belousov 		 * data should use the same flags to avoid ping ponging.
326a36f5532SKonstantin Belousov 		 */
3272c2f4413SJeff Roberson 		if ((fs->entry->eflags & MAP_ENTRY_NOSYNC) != 0)
328fff5403fSJeff Roberson 			vm_page_aflag_set(m, PGA_NOSYNC);
329a8081778SJeff Roberson 		else
330fff5403fSJeff Roberson 			vm_page_aflag_clear(m, PGA_NOSYNC);
331a36f5532SKonstantin Belousov 	}
332a36f5532SKonstantin Belousov 
333a36f5532SKonstantin Belousov }
334a36f5532SKonstantin Belousov 
33541ddec83SKonstantin Belousov /*
33641ddec83SKonstantin Belousov  * Unlocks fs.first_object and fs.map on success.
33741ddec83SKonstantin Belousov  */
338f1b642c2SMark Johnston static enum fault_status
3392c2f4413SJeff Roberson vm_fault_soft_fast(struct faultstate *fs)
34041ddec83SKonstantin Belousov {
3418b5e1472SAlan Cox 	vm_page_t m, m_map;
342fe0dcc40SKonstantin Belousov #if VM_NRESERVLEVEL > 0
3438b5e1472SAlan Cox 	vm_page_t m_super;
34490ea34bfSAlan Cox 	int flags;
3458b5e1472SAlan Cox #endif
346f1b642c2SMark Johnston 	int psind;
3472c2f4413SJeff Roberson 	vm_offset_t vaddr;
34841ddec83SKonstantin Belousov 
34941ddec83SKonstantin Belousov 	MPASS(fs->vp == NULL);
350f1b642c2SMark Johnston 
3510e71f4f7SMateusz Guzik 	/*
3520e71f4f7SMateusz Guzik 	 * If we fail, vast majority of the time it is because the page is not
3530e71f4f7SMateusz Guzik 	 * there to begin with. Opportunistically perform the lookup and
3540e71f4f7SMateusz Guzik 	 * subsequent checks without the object lock, revalidate later.
3550e71f4f7SMateusz Guzik 	 *
3560e71f4f7SMateusz Guzik 	 * Note: a busy page can be mapped for read|execute access.
3570e71f4f7SMateusz Guzik 	 */
3580e71f4f7SMateusz Guzik 	m = vm_page_lookup_unlocked(fs->first_object, fs->first_pindex);
3590e71f4f7SMateusz Guzik 	if (m == NULL || !vm_page_all_valid(m) ||
3600e71f4f7SMateusz Guzik 	    ((fs->prot & VM_PROT_WRITE) != 0 && vm_page_busied(m))) {
3610e71f4f7SMateusz Guzik 		VM_OBJECT_WLOCK(fs->first_object);
3620e71f4f7SMateusz Guzik 		return (FAULT_FAILURE);
3630e71f4f7SMateusz Guzik 	}
3640e71f4f7SMateusz Guzik 
3652c2f4413SJeff Roberson 	vaddr = fs->vaddr;
3660e71f4f7SMateusz Guzik 
3670e71f4f7SMateusz Guzik 	VM_OBJECT_RLOCK(fs->first_object);
3680e71f4f7SMateusz Guzik 
3690e71f4f7SMateusz Guzik 	/*
3700e71f4f7SMateusz Guzik 	 * Now that we stabilized the state, revalidate the page is in the shape
3710e71f4f7SMateusz Guzik 	 * we encountered above.
3720e71f4f7SMateusz Guzik 	 */
3730e71f4f7SMateusz Guzik 
3740e71f4f7SMateusz Guzik 	if (m->object != fs->first_object || m->pindex != fs->first_pindex)
375d0991948SMark Johnston 		goto fail;
3760e71f4f7SMateusz Guzik 
3770e71f4f7SMateusz Guzik 	vm_object_busy(fs->first_object);
3780e71f4f7SMateusz Guzik 
3790e71f4f7SMateusz Guzik 	if (!vm_page_all_valid(m) ||
3800e71f4f7SMateusz Guzik 	    ((fs->prot & VM_PROT_WRITE) != 0 && vm_page_busied(m)))
3810e71f4f7SMateusz Guzik 		goto fail_busy;
3820e71f4f7SMateusz Guzik 
3838b5e1472SAlan Cox 	m_map = m;
3848b5e1472SAlan Cox 	psind = 0;
385fe0dcc40SKonstantin Belousov #if VM_NRESERVLEVEL > 0
3868b5e1472SAlan Cox 	if ((m->flags & PG_FICTITIOUS) == 0 &&
3878b5e1472SAlan Cox 	    (m_super = vm_reserv_to_superpage(m)) != NULL &&
3888b5e1472SAlan Cox 	    rounddown2(vaddr, pagesizes[m_super->psind]) >= fs->entry->start &&
3898b5e1472SAlan Cox 	    roundup2(vaddr + 1, pagesizes[m_super->psind]) <= fs->entry->end &&
3908b5e1472SAlan Cox 	    (vaddr & (pagesizes[m_super->psind] - 1)) == (VM_PAGE_TO_PHYS(m) &
3912c2f4413SJeff Roberson 	    (pagesizes[m_super->psind] - 1)) && !fs->wired &&
3928b5e1472SAlan Cox 	    pmap_ps_enabled(fs->map->pmap)) {
3938b5e1472SAlan Cox 		flags = PS_ALL_VALID;
3942c2f4413SJeff Roberson 		if ((fs->prot & VM_PROT_WRITE) != 0) {
3958b5e1472SAlan Cox 			/*
3968b5e1472SAlan Cox 			 * Create a superpage mapping allowing write access
3978b5e1472SAlan Cox 			 * only if none of the constituent pages are busy and
3988b5e1472SAlan Cox 			 * all of them are already dirty (except possibly for
3998b5e1472SAlan Cox 			 * the page that was faulted on).
4008b5e1472SAlan Cox 			 */
4018b5e1472SAlan Cox 			flags |= PS_NONE_BUSY;
4028b5e1472SAlan Cox 			if ((fs->first_object->flags & OBJ_UNMANAGED) == 0)
4038b5e1472SAlan Cox 				flags |= PS_ALL_DIRTY;
4048b5e1472SAlan Cox 		}
4058b5e1472SAlan Cox 		if (vm_page_ps_test(m_super, flags, m)) {
4068b5e1472SAlan Cox 			m_map = m_super;
4078b5e1472SAlan Cox 			psind = m_super->psind;
4088b5e1472SAlan Cox 			vaddr = rounddown2(vaddr, pagesizes[psind]);
4098b5e1472SAlan Cox 			/* Preset the modified bit for dirty superpages. */
4108b5e1472SAlan Cox 			if ((flags & PS_ALL_DIRTY) != 0)
4112c2f4413SJeff Roberson 				fs->fault_type |= VM_PROT_WRITE;
4128b5e1472SAlan Cox 		}
4138b5e1472SAlan Cox 	}
4148b5e1472SAlan Cox #endif
415f1b642c2SMark Johnston 	if (pmap_enter(fs->map->pmap, vaddr, m_map, fs->prot, fs->fault_type |
416f1b642c2SMark Johnston 	    PMAP_ENTER_NOSLEEP | (fs->wired ? PMAP_ENTER_WIRED : 0), psind) !=
417d0991948SMark Johnston 	    KERN_SUCCESS)
4180e71f4f7SMateusz Guzik 		goto fail_busy;
4192c2f4413SJeff Roberson 	if (fs->m_hold != NULL) {
4202c2f4413SJeff Roberson 		(*fs->m_hold) = m;
421fee2a2faSMark Johnston 		vm_page_wire(m);
422fee2a2faSMark Johnston 	}
4232c2f4413SJeff Roberson 	if (psind == 0 && !fs->wired)
424a7163bb9SKonstantin Belousov 		vm_fault_prefault(fs, vaddr, PFBAK, PFFOR, true);
425a7163bb9SKonstantin Belousov 	VM_OBJECT_RUNLOCK(fs->first_object);
4262c2f4413SJeff Roberson 	vm_fault_dirty(fs, m);
427d0991948SMark Johnston 	vm_object_unbusy(fs->first_object);
42841ddec83SKonstantin Belousov 	vm_map_lookup_done(fs->map, fs->entry);
42941ddec83SKonstantin Belousov 	curthread->td_ru.ru_minflt++;
430d0991948SMark Johnston 	return (FAULT_SUCCESS);
4310e71f4f7SMateusz Guzik fail_busy:
432205be21dSJeff Roberson 	vm_object_unbusy(fs->first_object);
4330e71f4f7SMateusz Guzik fail:
4340e71f4f7SMateusz Guzik 	if (!VM_OBJECT_TRYUPGRADE(fs->first_object)) {
4350e71f4f7SMateusz Guzik 		VM_OBJECT_RUNLOCK(fs->first_object);
4360e71f4f7SMateusz Guzik 		VM_OBJECT_WLOCK(fs->first_object);
4370e71f4f7SMateusz Guzik 	}
438d0991948SMark Johnston 	return (FAULT_FAILURE);
43941ddec83SKonstantin Belousov }
44041ddec83SKonstantin Belousov 
441c42b43a0SKonstantin Belousov static void
442c42b43a0SKonstantin Belousov vm_fault_restore_map_lock(struct faultstate *fs)
443c42b43a0SKonstantin Belousov {
444c42b43a0SKonstantin Belousov 
445c42b43a0SKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
446c99d0c58SMark Johnston 	MPASS(blockcount_read(&fs->first_object->paging_in_progress) > 0);
447c42b43a0SKonstantin Belousov 
448c42b43a0SKonstantin Belousov 	if (!vm_map_trylock_read(fs->map)) {
449c42b43a0SKonstantin Belousov 		VM_OBJECT_WUNLOCK(fs->first_object);
450c42b43a0SKonstantin Belousov 		vm_map_lock_read(fs->map);
451c42b43a0SKonstantin Belousov 		VM_OBJECT_WLOCK(fs->first_object);
452c42b43a0SKonstantin Belousov 	}
453c42b43a0SKonstantin Belousov 	fs->lookup_still_valid = true;
454c42b43a0SKonstantin Belousov }
455c42b43a0SKonstantin Belousov 
4567a432b84SKonstantin Belousov static void
4577a432b84SKonstantin Belousov vm_fault_populate_check_page(vm_page_t m)
4587a432b84SKonstantin Belousov {
4597a432b84SKonstantin Belousov 
4607a432b84SKonstantin Belousov 	/*
4617a432b84SKonstantin Belousov 	 * Check each page to ensure that the pager is obeying the
4627a432b84SKonstantin Belousov 	 * interface: the page must be installed in the object, fully
4637a432b84SKonstantin Belousov 	 * valid, and exclusively busied.
4647a432b84SKonstantin Belousov 	 */
4657a432b84SKonstantin Belousov 	MPASS(m != NULL);
4660012f373SJeff Roberson 	MPASS(vm_page_all_valid(m));
4677a432b84SKonstantin Belousov 	MPASS(vm_page_xbusied(m));
4687a432b84SKonstantin Belousov }
4697a432b84SKonstantin Belousov 
4707a432b84SKonstantin Belousov static void
4717a432b84SKonstantin Belousov vm_fault_populate_cleanup(vm_object_t object, vm_pindex_t first,
4727a432b84SKonstantin Belousov     vm_pindex_t last)
4737a432b84SKonstantin Belousov {
4747a432b84SKonstantin Belousov 	vm_page_t m;
4757a432b84SKonstantin Belousov 	vm_pindex_t pidx;
4767a432b84SKonstantin Belousov 
4777a432b84SKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(object);
4787a432b84SKonstantin Belousov 	MPASS(first <= last);
4797a432b84SKonstantin Belousov 	for (pidx = first, m = vm_page_lookup(object, pidx);
4807a432b84SKonstantin Belousov 	    pidx <= last; pidx++, m = vm_page_next(m)) {
4817a432b84SKonstantin Belousov 		vm_fault_populate_check_page(m);
4827a432b84SKonstantin Belousov 		vm_page_deactivate(m);
4837a432b84SKonstantin Belousov 		vm_page_xunbusy(m);
4847a432b84SKonstantin Belousov 	}
4857a432b84SKonstantin Belousov }
486c42b43a0SKonstantin Belousov 
487f1b642c2SMark Johnston static enum fault_status
4882c2f4413SJeff Roberson vm_fault_populate(struct faultstate *fs)
489c42b43a0SKonstantin Belousov {
49070183daaSAlan Cox 	vm_offset_t vaddr;
491c42b43a0SKonstantin Belousov 	vm_page_t m;
4927a432b84SKonstantin Belousov 	vm_pindex_t map_first, map_last, pager_first, pager_last, pidx;
493d301b358SKonstantin Belousov 	int bdry_idx, i, npages, psind, rv;
494f1b642c2SMark Johnston 	enum fault_status res;
495c42b43a0SKonstantin Belousov 
496c42b43a0SKonstantin Belousov 	MPASS(fs->object == fs->first_object);
497c42b43a0SKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
498c99d0c58SMark Johnston 	MPASS(blockcount_read(&fs->first_object->paging_in_progress) > 0);
499c42b43a0SKonstantin Belousov 	MPASS(fs->first_object->backing_object == NULL);
500c42b43a0SKonstantin Belousov 	MPASS(fs->lookup_still_valid);
501c42b43a0SKonstantin Belousov 
5027a432b84SKonstantin Belousov 	pager_first = OFF_TO_IDX(fs->entry->offset);
50389564188SAlan Cox 	pager_last = pager_first + atop(fs->entry->end - fs->entry->start) - 1;
5040a310c94SMateusz Guzik 	vm_fault_unlock_map(fs);
5050a310c94SMateusz Guzik 	vm_fault_unlock_vp(fs);
506c42b43a0SKonstantin Belousov 
507f1b642c2SMark Johnston 	res = FAULT_SUCCESS;
508f1b642c2SMark Johnston 
509c42b43a0SKonstantin Belousov 	/*
510c42b43a0SKonstantin Belousov 	 * Call the pager (driver) populate() method.
511c42b43a0SKonstantin Belousov 	 *
512c42b43a0SKonstantin Belousov 	 * There is no guarantee that the method will be called again
513c42b43a0SKonstantin Belousov 	 * if the current fault is for read, and a future fault is
514c42b43a0SKonstantin Belousov 	 * for write.  Report the entry's maximum allowed protection
515c42b43a0SKonstantin Belousov 	 * to the driver.
516c42b43a0SKonstantin Belousov 	 */
517c42b43a0SKonstantin Belousov 	rv = vm_pager_populate(fs->first_object, fs->first_pindex,
518d301b358SKonstantin Belousov 	    fs->fault_type, fs->entry->max_protection, &pager_first,
519d301b358SKonstantin Belousov 	    &pager_last);
520c42b43a0SKonstantin Belousov 
521c42b43a0SKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
522c42b43a0SKonstantin Belousov 	if (rv == VM_PAGER_BAD) {
523c42b43a0SKonstantin Belousov 		/*
524c42b43a0SKonstantin Belousov 		 * VM_PAGER_BAD is the backdoor for a pager to request
525c42b43a0SKonstantin Belousov 		 * normal fault handling.
526c42b43a0SKonstantin Belousov 		 */
527c42b43a0SKonstantin Belousov 		vm_fault_restore_map_lock(fs);
528c42b43a0SKonstantin Belousov 		if (fs->map->timestamp != fs->map_generation)
529f1b642c2SMark Johnston 			return (FAULT_RESTART);
530f1b642c2SMark Johnston 		return (FAULT_CONTINUE);
531c42b43a0SKonstantin Belousov 	}
532c42b43a0SKonstantin Belousov 	if (rv != VM_PAGER_OK)
533f1b642c2SMark Johnston 		return (FAULT_FAILURE); /* AKA SIGSEGV */
534c42b43a0SKonstantin Belousov 
535c42b43a0SKonstantin Belousov 	/* Ensure that the driver is obeying the interface. */
5367a432b84SKonstantin Belousov 	MPASS(pager_first <= pager_last);
5377a432b84SKonstantin Belousov 	MPASS(fs->first_pindex <= pager_last);
5387a432b84SKonstantin Belousov 	MPASS(fs->first_pindex >= pager_first);
5397a432b84SKonstantin Belousov 	MPASS(pager_last < fs->first_object->size);
540c42b43a0SKonstantin Belousov 
541c42b43a0SKonstantin Belousov 	vm_fault_restore_map_lock(fs);
542d301b358SKonstantin Belousov 	bdry_idx = (fs->entry->eflags & MAP_ENTRY_SPLIT_BOUNDARY_MASK) >>
543d301b358SKonstantin Belousov 	    MAP_ENTRY_SPLIT_BOUNDARY_SHIFT;
5447a432b84SKonstantin Belousov 	if (fs->map->timestamp != fs->map_generation) {
545d301b358SKonstantin Belousov 		if (bdry_idx == 0) {
5467a432b84SKonstantin Belousov 			vm_fault_populate_cleanup(fs->first_object, pager_first,
5477a432b84SKonstantin Belousov 			    pager_last);
548d301b358SKonstantin Belousov 		} else {
549d301b358SKonstantin Belousov 			m = vm_page_lookup(fs->first_object, pager_first);
550d301b358SKonstantin Belousov 			if (m != fs->m)
551d301b358SKonstantin Belousov 				vm_page_xunbusy(m);
552d301b358SKonstantin Belousov 		}
553f1b642c2SMark Johnston 		return (FAULT_RESTART);
5547a432b84SKonstantin Belousov 	}
555c42b43a0SKonstantin Belousov 
556c42b43a0SKonstantin Belousov 	/*
5577a432b84SKonstantin Belousov 	 * The map is unchanged after our last unlock.  Process the fault.
5587a432b84SKonstantin Belousov 	 *
559d301b358SKonstantin Belousov 	 * First, the special case of largepage mappings, where
560d301b358SKonstantin Belousov 	 * populate only busies the first page in superpage run.
561d301b358SKonstantin Belousov 	 */
562d301b358SKonstantin Belousov 	if (bdry_idx != 0) {
56378257765SMark Johnston 		KASSERT(PMAP_HAS_LARGEPAGES,
56478257765SMark Johnston 		    ("missing pmap support for large pages"));
565d301b358SKonstantin Belousov 		m = vm_page_lookup(fs->first_object, pager_first);
566d301b358SKonstantin Belousov 		vm_fault_populate_check_page(m);
567d301b358SKonstantin Belousov 		VM_OBJECT_WUNLOCK(fs->first_object);
568d301b358SKonstantin Belousov 		vaddr = fs->entry->start + IDX_TO_OFF(pager_first) -
569d301b358SKonstantin Belousov 		    fs->entry->offset;
570d301b358SKonstantin Belousov 		/* assert alignment for entry */
571d301b358SKonstantin Belousov 		KASSERT((vaddr & (pagesizes[bdry_idx] - 1)) == 0,
572d301b358SKonstantin Belousov     ("unaligned superpage start %#jx pager_first %#jx offset %#jx vaddr %#jx",
573d301b358SKonstantin Belousov 		    (uintmax_t)fs->entry->start, (uintmax_t)pager_first,
574d301b358SKonstantin Belousov 		    (uintmax_t)fs->entry->offset, (uintmax_t)vaddr));
575d301b358SKonstantin Belousov 		KASSERT((VM_PAGE_TO_PHYS(m) & (pagesizes[bdry_idx] - 1)) == 0,
576d301b358SKonstantin Belousov 		    ("unaligned superpage m %p %#jx", m,
577d301b358SKonstantin Belousov 		    (uintmax_t)VM_PAGE_TO_PHYS(m)));
578d301b358SKonstantin Belousov 		rv = pmap_enter(fs->map->pmap, vaddr, m, fs->prot,
579d301b358SKonstantin Belousov 		    fs->fault_type | (fs->wired ? PMAP_ENTER_WIRED : 0) |
580d301b358SKonstantin Belousov 		    PMAP_ENTER_LARGEPAGE, bdry_idx);
581d301b358SKonstantin Belousov 		VM_OBJECT_WLOCK(fs->first_object);
582d301b358SKonstantin Belousov 		vm_page_xunbusy(m);
583f1b642c2SMark Johnston 		if (rv != KERN_SUCCESS) {
584f1b642c2SMark Johnston 			res = FAULT_FAILURE;
585c7b913aaSKonstantin Belousov 			goto out;
586f1b642c2SMark Johnston 		}
587d301b358SKonstantin Belousov 		if ((fs->fault_flags & VM_FAULT_WIRE) != 0) {
588d301b358SKonstantin Belousov 			for (i = 0; i < atop(pagesizes[bdry_idx]); i++)
589d301b358SKonstantin Belousov 				vm_page_wire(m + i);
590d301b358SKonstantin Belousov 		}
591d301b358SKonstantin Belousov 		if (fs->m_hold != NULL) {
592d301b358SKonstantin Belousov 			*fs->m_hold = m + (fs->first_pindex - pager_first);
593d301b358SKonstantin Belousov 			vm_page_wire(*fs->m_hold);
594d301b358SKonstantin Belousov 		}
595d301b358SKonstantin Belousov 		goto out;
596d301b358SKonstantin Belousov 	}
597d301b358SKonstantin Belousov 
598d301b358SKonstantin Belousov 	/*
5997a432b84SKonstantin Belousov 	 * The range [pager_first, pager_last] that is given to the
6007a432b84SKonstantin Belousov 	 * pager is only a hint.  The pager may populate any range
6017a432b84SKonstantin Belousov 	 * within the object that includes the requested page index.
6027a432b84SKonstantin Belousov 	 * In case the pager expanded the range, clip it to fit into
6037a432b84SKonstantin Belousov 	 * the map entry.
604c42b43a0SKonstantin Belousov 	 */
60589564188SAlan Cox 	map_first = OFF_TO_IDX(fs->entry->offset);
60689564188SAlan Cox 	if (map_first > pager_first) {
6077a432b84SKonstantin Belousov 		vm_fault_populate_cleanup(fs->first_object, pager_first,
6087a432b84SKonstantin Belousov 		    map_first - 1);
60989564188SAlan Cox 		pager_first = map_first;
61089564188SAlan Cox 	}
61189564188SAlan Cox 	map_last = map_first + atop(fs->entry->end - fs->entry->start) - 1;
61289564188SAlan Cox 	if (map_last < pager_last) {
6137a432b84SKonstantin Belousov 		vm_fault_populate_cleanup(fs->first_object, map_last + 1,
6147a432b84SKonstantin Belousov 		    pager_last);
61589564188SAlan Cox 		pager_last = map_last;
61689564188SAlan Cox 	}
61789564188SAlan Cox 	for (pidx = pager_first, m = vm_page_lookup(fs->first_object, pidx);
61870183daaSAlan Cox 	    pidx <= pager_last;
61970183daaSAlan Cox 	    pidx += npages, m = vm_page_next(&m[npages - 1])) {
62070183daaSAlan Cox 		vaddr = fs->entry->start + IDX_TO_OFF(pidx) - fs->entry->offset;
6218dc8feb5SJason A. Harmening 
62270183daaSAlan Cox 		psind = m->psind;
62370183daaSAlan Cox 		if (psind > 0 && ((vaddr & (pagesizes[psind] - 1)) != 0 ||
62470183daaSAlan Cox 		    pidx + OFF_TO_IDX(pagesizes[psind]) - 1 > pager_last ||
6252c2f4413SJeff Roberson 		    !pmap_ps_enabled(fs->map->pmap) || fs->wired))
62670183daaSAlan Cox 			psind = 0;
6278dc8feb5SJason A. Harmening 
62870183daaSAlan Cox 		npages = atop(pagesizes[psind]);
62970183daaSAlan Cox 		for (i = 0; i < npages; i++) {
63070183daaSAlan Cox 			vm_fault_populate_check_page(&m[i]);
6312c2f4413SJeff Roberson 			vm_fault_dirty(fs, &m[i]);
63270183daaSAlan Cox 		}
633c42b43a0SKonstantin Belousov 		VM_OBJECT_WUNLOCK(fs->first_object);
6342c2f4413SJeff Roberson 		rv = pmap_enter(fs->map->pmap, vaddr, m, fs->prot, fs->fault_type |
6352c2f4413SJeff Roberson 		    (fs->wired ? PMAP_ENTER_WIRED : 0), psind);
6368dc8feb5SJason A. Harmening 
6378dc8feb5SJason A. Harmening 		/*
6388dc8feb5SJason A. Harmening 		 * pmap_enter() may fail for a superpage mapping if additional
6398dc8feb5SJason A. Harmening 		 * protection policies prevent the full mapping.
6408dc8feb5SJason A. Harmening 		 * For example, this will happen on amd64 if the entire
6418dc8feb5SJason A. Harmening 		 * address range does not share the same userspace protection
6428dc8feb5SJason A. Harmening 		 * key.  Revert to single-page mappings if this happens.
6438dc8feb5SJason A. Harmening 		 */
6448dc8feb5SJason A. Harmening 		MPASS(rv == KERN_SUCCESS ||
6458dc8feb5SJason A. Harmening 		    (psind > 0 && rv == KERN_PROTECTION_FAILURE));
6468dc8feb5SJason A. Harmening 		if (__predict_false(psind > 0 &&
6478dc8feb5SJason A. Harmening 		    rv == KERN_PROTECTION_FAILURE)) {
64888642d97SMark Johnston 			MPASS(!fs->wired);
649e7a9df16SKonstantin Belousov 			for (i = 0; i < npages; i++) {
650e7a9df16SKonstantin Belousov 				rv = pmap_enter(fs->map->pmap, vaddr + ptoa(i),
65188642d97SMark Johnston 				    &m[i], fs->prot, fs->fault_type, 0);
652e7a9df16SKonstantin Belousov 				MPASS(rv == KERN_SUCCESS);
653e7a9df16SKonstantin Belousov 			}
654e7a9df16SKonstantin Belousov 		}
6558dc8feb5SJason A. Harmening 
656c42b43a0SKonstantin Belousov 		VM_OBJECT_WLOCK(fs->first_object);
65770183daaSAlan Cox 		for (i = 0; i < npages; i++) {
65888642d97SMark Johnston 			if ((fs->fault_flags & VM_FAULT_WIRE) != 0 &&
65988642d97SMark Johnston 			    m[i].pindex == fs->first_pindex)
66070183daaSAlan Cox 				vm_page_wire(&m[i]);
6619f5632e6SMark Johnston 			else
66270183daaSAlan Cox 				vm_page_activate(&m[i]);
66388642d97SMark Johnston 			if (fs->m_hold != NULL &&
66488642d97SMark Johnston 			    m[i].pindex == fs->first_pindex) {
6652c2f4413SJeff Roberson 				(*fs->m_hold) = &m[i];
666eeacb3b0SMark Johnston 				vm_page_wire(&m[i]);
667c42b43a0SKonstantin Belousov 			}
6684cdea4a8SJeff Roberson 			vm_page_xunbusy(&m[i]);
66970183daaSAlan Cox 		}
670c42b43a0SKonstantin Belousov 	}
671d301b358SKonstantin Belousov out:
672c42b43a0SKonstantin Belousov 	curthread->td_ru.ru_majflt++;
673f1b642c2SMark Johnston 	return (res);
674c42b43a0SKonstantin Belousov }
675c42b43a0SKonstantin Belousov 
676df08823dSKonstantin Belousov static int prot_fault_translation;
677df08823dSKonstantin Belousov SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RWTUN,
678df08823dSKonstantin Belousov     &prot_fault_translation, 0,
679df08823dSKonstantin Belousov     "Control signal to deliver on protection fault");
680df08823dSKonstantin Belousov 
681df08823dSKonstantin Belousov /* compat definition to keep common code for signal translation */
682df08823dSKonstantin Belousov #define	UCODE_PAGEFLT	12
683df08823dSKonstantin Belousov #ifdef T_PAGEFLT
684df08823dSKonstantin Belousov _Static_assert(UCODE_PAGEFLT == T_PAGEFLT, "T_PAGEFLT");
685df08823dSKonstantin Belousov #endif
686df08823dSKonstantin Belousov 
687df8bae1dSRodney W. Grimes /*
688df08823dSKonstantin Belousov  *	vm_fault_trap:
689df8bae1dSRodney W. Grimes  *
690956f3135SPhilippe Charnier  *	Handle a page fault occurring at the given address,
691df8bae1dSRodney W. Grimes  *	requiring the given permissions, in the map specified.
692df8bae1dSRodney W. Grimes  *	If successful, the page is inserted into the
693df8bae1dSRodney W. Grimes  *	associated physical map.
694df8bae1dSRodney W. Grimes  *
695df8bae1dSRodney W. Grimes  *	NOTE: the given address should be truncated to the
696df8bae1dSRodney W. Grimes  *	proper page address.
697df8bae1dSRodney W. Grimes  *
698df8bae1dSRodney W. Grimes  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
699df8bae1dSRodney W. Grimes  *	a standard error specifying why the fault is fatal is returned.
700df8bae1dSRodney W. Grimes  *
701df8bae1dSRodney W. Grimes  *	The map in question must be referenced, and remains so.
7020cddd8f0SMatthew Dillon  *	Caller may hold no locks.
703df8bae1dSRodney W. Grimes  */
704df8bae1dSRodney W. Grimes int
705df08823dSKonstantin Belousov vm_fault_trap(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
706df08823dSKonstantin Belousov     int fault_flags, int *signo, int *ucode)
70723955314SAlfred Perlstein {
70835818d2eSJohn Baldwin 	int result;
709acd11c74SAlan Cox 
710df08823dSKonstantin Belousov 	MPASS(signo == NULL || ucode != NULL);
71135818d2eSJohn Baldwin #ifdef KTRACE
712c31cec45SKonstantin Belousov 	if (map != kernel_map && KTRPOINT(curthread, KTR_FAULT))
71335818d2eSJohn Baldwin 		ktrfault(vaddr, fault_type);
71435818d2eSJohn Baldwin #endif
715df08823dSKonstantin Belousov 	result = vm_fault(map, trunc_page(vaddr), fault_type, fault_flags,
716be996836SAttilio Rao 	    NULL);
717df08823dSKonstantin Belousov 	KASSERT(result == KERN_SUCCESS || result == KERN_FAILURE ||
718df08823dSKonstantin Belousov 	    result == KERN_INVALID_ADDRESS ||
719df08823dSKonstantin Belousov 	    result == KERN_RESOURCE_SHORTAGE ||
720df08823dSKonstantin Belousov 	    result == KERN_PROTECTION_FAILURE ||
721df08823dSKonstantin Belousov 	    result == KERN_OUT_OF_BOUNDS,
722df08823dSKonstantin Belousov 	    ("Unexpected Mach error %d from vm_fault()", result));
72335818d2eSJohn Baldwin #ifdef KTRACE
724c31cec45SKonstantin Belousov 	if (map != kernel_map && KTRPOINT(curthread, KTR_FAULTEND))
72535818d2eSJohn Baldwin 		ktrfaultend(result);
72635818d2eSJohn Baldwin #endif
727df08823dSKonstantin Belousov 	if (result != KERN_SUCCESS && signo != NULL) {
728df08823dSKonstantin Belousov 		switch (result) {
729df08823dSKonstantin Belousov 		case KERN_FAILURE:
730df08823dSKonstantin Belousov 		case KERN_INVALID_ADDRESS:
731df08823dSKonstantin Belousov 			*signo = SIGSEGV;
732df08823dSKonstantin Belousov 			*ucode = SEGV_MAPERR;
733df08823dSKonstantin Belousov 			break;
734df08823dSKonstantin Belousov 		case KERN_RESOURCE_SHORTAGE:
735df08823dSKonstantin Belousov 			*signo = SIGBUS;
736df08823dSKonstantin Belousov 			*ucode = BUS_OOMERR;
737df08823dSKonstantin Belousov 			break;
738df08823dSKonstantin Belousov 		case KERN_OUT_OF_BOUNDS:
739df08823dSKonstantin Belousov 			*signo = SIGBUS;
740df08823dSKonstantin Belousov 			*ucode = BUS_OBJERR;
741df08823dSKonstantin Belousov 			break;
742df08823dSKonstantin Belousov 		case KERN_PROTECTION_FAILURE:
743df08823dSKonstantin Belousov 			if (prot_fault_translation == 0) {
744df08823dSKonstantin Belousov 				/*
745df08823dSKonstantin Belousov 				 * Autodetect.  This check also covers
746df08823dSKonstantin Belousov 				 * the images without the ABI-tag ELF
747df08823dSKonstantin Belousov 				 * note.
748df08823dSKonstantin Belousov 				 */
749df08823dSKonstantin Belousov 				if (SV_CURPROC_ABI() == SV_ABI_FREEBSD &&
750df08823dSKonstantin Belousov 				    curproc->p_osrel >= P_OSREL_SIGSEGV) {
751df08823dSKonstantin Belousov 					*signo = SIGSEGV;
752df08823dSKonstantin Belousov 					*ucode = SEGV_ACCERR;
753df08823dSKonstantin Belousov 				} else {
754df08823dSKonstantin Belousov 					*signo = SIGBUS;
755df08823dSKonstantin Belousov 					*ucode = UCODE_PAGEFLT;
756df08823dSKonstantin Belousov 				}
757df08823dSKonstantin Belousov 			} else if (prot_fault_translation == 1) {
758df08823dSKonstantin Belousov 				/* Always compat mode. */
759df08823dSKonstantin Belousov 				*signo = SIGBUS;
760df08823dSKonstantin Belousov 				*ucode = UCODE_PAGEFLT;
761df08823dSKonstantin Belousov 			} else {
762df08823dSKonstantin Belousov 				/* Always SIGSEGV mode. */
763df08823dSKonstantin Belousov 				*signo = SIGSEGV;
764df08823dSKonstantin Belousov 				*ucode = SEGV_ACCERR;
765df08823dSKonstantin Belousov 			}
766df08823dSKonstantin Belousov 			break;
767df08823dSKonstantin Belousov 		default:
768df08823dSKonstantin Belousov 			KASSERT(0, ("Unexpected Mach error %d from vm_fault()",
769df08823dSKonstantin Belousov 			    result));
770df08823dSKonstantin Belousov 			break;
771df08823dSKonstantin Belousov 		}
772df08823dSKonstantin Belousov 	}
77335818d2eSJohn Baldwin 	return (result);
774acd11c74SAlan Cox }
775acd11c74SAlan Cox 
776fdb1dbb1SMateusz Guzik static bool
777fdb1dbb1SMateusz Guzik vm_fault_object_ensure_wlocked(struct faultstate *fs)
778fdb1dbb1SMateusz Guzik {
779fdb1dbb1SMateusz Guzik 	if (fs->object == fs->first_object)
780fdb1dbb1SMateusz Guzik 		VM_OBJECT_ASSERT_WLOCKED(fs->object);
781fdb1dbb1SMateusz Guzik 
782fdb1dbb1SMateusz Guzik 	if (!fs->can_read_lock)  {
783fdb1dbb1SMateusz Guzik 		VM_OBJECT_ASSERT_WLOCKED(fs->object);
784fdb1dbb1SMateusz Guzik 		return (true);
785fdb1dbb1SMateusz Guzik 	}
786fdb1dbb1SMateusz Guzik 
787fdb1dbb1SMateusz Guzik 	if (VM_OBJECT_WOWNED(fs->object))
788fdb1dbb1SMateusz Guzik 		return (true);
789fdb1dbb1SMateusz Guzik 
790fdb1dbb1SMateusz Guzik 	if (VM_OBJECT_TRYUPGRADE(fs->object))
791fdb1dbb1SMateusz Guzik 		return (true);
792fdb1dbb1SMateusz Guzik 
793fdb1dbb1SMateusz Guzik 	return (false);
794fdb1dbb1SMateusz Guzik }
795fdb1dbb1SMateusz Guzik 
796f1b642c2SMark Johnston static enum fault_status
7971e40fe41SJeff Roberson vm_fault_lock_vnode(struct faultstate *fs, bool objlocked)
7980ddd3082SKonstantin Belousov {
7990ddd3082SKonstantin Belousov 	struct vnode *vp;
8000ddd3082SKonstantin Belousov 	int error, locked;
8010ddd3082SKonstantin Belousov 
8020ddd3082SKonstantin Belousov 	if (fs->object->type != OBJT_VNODE)
803f1b642c2SMark Johnston 		return (FAULT_CONTINUE);
8040ddd3082SKonstantin Belousov 	vp = fs->object->handle;
80516b0c092SKonstantin Belousov 	if (vp == fs->vp) {
80616b0c092SKonstantin Belousov 		ASSERT_VOP_LOCKED(vp, "saved vnode is not locked");
807f1b642c2SMark Johnston 		return (FAULT_CONTINUE);
80816b0c092SKonstantin Belousov 	}
8090ddd3082SKonstantin Belousov 
8100ddd3082SKonstantin Belousov 	/*
8110ddd3082SKonstantin Belousov 	 * Perform an unlock in case the desired vnode changed while
8120ddd3082SKonstantin Belousov 	 * the map was unlocked during a retry.
8130ddd3082SKonstantin Belousov 	 */
8140a310c94SMateusz Guzik 	vm_fault_unlock_vp(fs);
8150ddd3082SKonstantin Belousov 
8160ddd3082SKonstantin Belousov 	locked = VOP_ISLOCKED(vp);
8170ddd3082SKonstantin Belousov 	if (locked != LK_EXCLUSIVE)
8180ddd3082SKonstantin Belousov 		locked = LK_SHARED;
8190ddd3082SKonstantin Belousov 
8200ddd3082SKonstantin Belousov 	/*
8210ddd3082SKonstantin Belousov 	 * We must not sleep acquiring the vnode lock while we have
8220ddd3082SKonstantin Belousov 	 * the page exclusive busied or the object's
8230ddd3082SKonstantin Belousov 	 * paging-in-progress count incremented.  Otherwise, we could
8240ddd3082SKonstantin Belousov 	 * deadlock.
8250ddd3082SKonstantin Belousov 	 */
826a92a971bSMateusz Guzik 	error = vget(vp, locked | LK_CANRECURSE | LK_NOWAIT);
8270ddd3082SKonstantin Belousov 	if (error == 0) {
8280ddd3082SKonstantin Belousov 		fs->vp = vp;
829f1b642c2SMark Johnston 		return (FAULT_CONTINUE);
8300ddd3082SKonstantin Belousov 	}
8310ddd3082SKonstantin Belousov 
8320ddd3082SKonstantin Belousov 	vhold(vp);
8331e40fe41SJeff Roberson 	if (objlocked)
8340a310c94SMateusz Guzik 		vm_fault_unlock_and_deallocate(fs);
8351e40fe41SJeff Roberson 	else
8360a310c94SMateusz Guzik 		vm_fault_deallocate(fs);
837a92a971bSMateusz Guzik 	error = vget(vp, locked | LK_RETRY | LK_CANRECURSE);
8380ddd3082SKonstantin Belousov 	vdrop(vp);
8390ddd3082SKonstantin Belousov 	fs->vp = vp;
8400ddd3082SKonstantin Belousov 	KASSERT(error == 0, ("vm_fault: vget failed %d", error));
841f1b642c2SMark Johnston 	return (FAULT_RESTART);
8420ddd3082SKonstantin Belousov }
8430ddd3082SKonstantin Belousov 
844bef91632SJeff Roberson /*
8455949b1caSJeff Roberson  * Calculate the desired readahead.  Handle drop-behind.
8465949b1caSJeff Roberson  *
8475949b1caSJeff Roberson  * Returns the number of readahead blocks to pass to the pager.
8485949b1caSJeff Roberson  */
8495949b1caSJeff Roberson static int
8505949b1caSJeff Roberson vm_fault_readahead(struct faultstate *fs)
8515949b1caSJeff Roberson {
8525949b1caSJeff Roberson 	int era, nera;
8535949b1caSJeff Roberson 	u_char behavior;
8545949b1caSJeff Roberson 
8555949b1caSJeff Roberson 	KASSERT(fs->lookup_still_valid, ("map unlocked"));
8565949b1caSJeff Roberson 	era = fs->entry->read_ahead;
8575949b1caSJeff Roberson 	behavior = vm_map_entry_behavior(fs->entry);
8585949b1caSJeff Roberson 	if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
8595949b1caSJeff Roberson 		nera = 0;
8605949b1caSJeff Roberson 	} else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
8615949b1caSJeff Roberson 		nera = VM_FAULT_READ_AHEAD_MAX;
8625949b1caSJeff Roberson 		if (fs->vaddr == fs->entry->next_read)
8635949b1caSJeff Roberson 			vm_fault_dontneed(fs, fs->vaddr, nera);
8645949b1caSJeff Roberson 	} else if (fs->vaddr == fs->entry->next_read) {
8655949b1caSJeff Roberson 		/*
8665949b1caSJeff Roberson 		 * This is a sequential fault.  Arithmetically
8675949b1caSJeff Roberson 		 * increase the requested number of pages in
8685949b1caSJeff Roberson 		 * the read-ahead window.  The requested
8695949b1caSJeff Roberson 		 * number of pages is "# of sequential faults
8705949b1caSJeff Roberson 		 * x (read ahead min + 1) + read ahead min"
8715949b1caSJeff Roberson 		 */
8725949b1caSJeff Roberson 		nera = VM_FAULT_READ_AHEAD_MIN;
8735949b1caSJeff Roberson 		if (era > 0) {
8745949b1caSJeff Roberson 			nera += era + 1;
8755949b1caSJeff Roberson 			if (nera > VM_FAULT_READ_AHEAD_MAX)
8765949b1caSJeff Roberson 				nera = VM_FAULT_READ_AHEAD_MAX;
8775949b1caSJeff Roberson 		}
8785949b1caSJeff Roberson 		if (era == VM_FAULT_READ_AHEAD_MAX)
8795949b1caSJeff Roberson 			vm_fault_dontneed(fs, fs->vaddr, nera);
8805949b1caSJeff Roberson 	} else {
8815949b1caSJeff Roberson 		/*
8825949b1caSJeff Roberson 		 * This is a non-sequential fault.
8835949b1caSJeff Roberson 		 */
8845949b1caSJeff Roberson 		nera = 0;
8855949b1caSJeff Roberson 	}
8865949b1caSJeff Roberson 	if (era != nera) {
8875949b1caSJeff Roberson 		/*
8885949b1caSJeff Roberson 		 * A read lock on the map suffices to update
8895949b1caSJeff Roberson 		 * the read ahead count safely.
8905949b1caSJeff Roberson 		 */
8915949b1caSJeff Roberson 		fs->entry->read_ahead = nera;
8925949b1caSJeff Roberson 	}
8935949b1caSJeff Roberson 
8945949b1caSJeff Roberson 	return (nera);
8955949b1caSJeff Roberson }
8965949b1caSJeff Roberson 
897c308a3a6SJeff Roberson static int
898c308a3a6SJeff Roberson vm_fault_lookup(struct faultstate *fs)
899c308a3a6SJeff Roberson {
900c308a3a6SJeff Roberson 	int result;
901c308a3a6SJeff Roberson 
902c308a3a6SJeff Roberson 	KASSERT(!fs->lookup_still_valid,
903c308a3a6SJeff Roberson 	   ("vm_fault_lookup: Map already locked."));
904c308a3a6SJeff Roberson 	result = vm_map_lookup(&fs->map, fs->vaddr, fs->fault_type |
905c308a3a6SJeff Roberson 	    VM_PROT_FAULT_LOOKUP, &fs->entry, &fs->first_object,
906c308a3a6SJeff Roberson 	    &fs->first_pindex, &fs->prot, &fs->wired);
907c308a3a6SJeff Roberson 	if (result != KERN_SUCCESS) {
9080a310c94SMateusz Guzik 		vm_fault_unlock_vp(fs);
909c308a3a6SJeff Roberson 		return (result);
910c308a3a6SJeff Roberson 	}
911c308a3a6SJeff Roberson 
912c308a3a6SJeff Roberson 	fs->map_generation = fs->map->timestamp;
913c308a3a6SJeff Roberson 
914c308a3a6SJeff Roberson 	if (fs->entry->eflags & MAP_ENTRY_NOFAULT) {
915c308a3a6SJeff Roberson 		panic("%s: fault on nofault entry, addr: %#lx",
916c308a3a6SJeff Roberson 		    __func__, (u_long)fs->vaddr);
917c308a3a6SJeff Roberson 	}
918c308a3a6SJeff Roberson 
919c308a3a6SJeff Roberson 	if (fs->entry->eflags & MAP_ENTRY_IN_TRANSITION &&
920c308a3a6SJeff Roberson 	    fs->entry->wiring_thread != curthread) {
921c308a3a6SJeff Roberson 		vm_map_unlock_read(fs->map);
922c308a3a6SJeff Roberson 		vm_map_lock(fs->map);
923c308a3a6SJeff Roberson 		if (vm_map_lookup_entry(fs->map, fs->vaddr, &fs->entry) &&
924c308a3a6SJeff Roberson 		    (fs->entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
9250a310c94SMateusz Guzik 			vm_fault_unlock_vp(fs);
926c308a3a6SJeff Roberson 			fs->entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
927c308a3a6SJeff Roberson 			vm_map_unlock_and_wait(fs->map, 0);
928c308a3a6SJeff Roberson 		} else
929c308a3a6SJeff Roberson 			vm_map_unlock(fs->map);
930c308a3a6SJeff Roberson 		return (KERN_RESOURCE_SHORTAGE);
931c308a3a6SJeff Roberson 	}
932c308a3a6SJeff Roberson 
933c308a3a6SJeff Roberson 	MPASS((fs->entry->eflags & MAP_ENTRY_GUARD) == 0);
934c308a3a6SJeff Roberson 
935c308a3a6SJeff Roberson 	if (fs->wired)
936c308a3a6SJeff Roberson 		fs->fault_type = fs->prot | (fs->fault_type & VM_PROT_COPY);
937c308a3a6SJeff Roberson 	else
938c308a3a6SJeff Roberson 		KASSERT((fs->fault_flags & VM_FAULT_WIRE) == 0,
939c308a3a6SJeff Roberson 		    ("!fs->wired && VM_FAULT_WIRE"));
940c308a3a6SJeff Roberson 	fs->lookup_still_valid = true;
941c308a3a6SJeff Roberson 
942c308a3a6SJeff Roberson 	return (KERN_SUCCESS);
943c308a3a6SJeff Roberson }
944c308a3a6SJeff Roberson 
945fcb04758SJeff Roberson static int
946fcb04758SJeff Roberson vm_fault_relookup(struct faultstate *fs)
947fcb04758SJeff Roberson {
948fcb04758SJeff Roberson 	vm_object_t retry_object;
949fcb04758SJeff Roberson 	vm_pindex_t retry_pindex;
950fcb04758SJeff Roberson 	vm_prot_t retry_prot;
951fcb04758SJeff Roberson 	int result;
952fcb04758SJeff Roberson 
953fcb04758SJeff Roberson 	if (!vm_map_trylock_read(fs->map))
954fcb04758SJeff Roberson 		return (KERN_RESTART);
955fcb04758SJeff Roberson 
956fcb04758SJeff Roberson 	fs->lookup_still_valid = true;
957fcb04758SJeff Roberson 	if (fs->map->timestamp == fs->map_generation)
958fcb04758SJeff Roberson 		return (KERN_SUCCESS);
959fcb04758SJeff Roberson 
960fcb04758SJeff Roberson 	result = vm_map_lookup_locked(&fs->map, fs->vaddr, fs->fault_type,
961fcb04758SJeff Roberson 	    &fs->entry, &retry_object, &retry_pindex, &retry_prot,
962fcb04758SJeff Roberson 	    &fs->wired);
963fcb04758SJeff Roberson 	if (result != KERN_SUCCESS) {
964fcb04758SJeff Roberson 		/*
965fcb04758SJeff Roberson 		 * If retry of map lookup would have blocked then
966fcb04758SJeff Roberson 		 * retry fault from start.
967fcb04758SJeff Roberson 		 */
968fcb04758SJeff Roberson 		if (result == KERN_FAILURE)
969fcb04758SJeff Roberson 			return (KERN_RESTART);
970fcb04758SJeff Roberson 		return (result);
971fcb04758SJeff Roberson 	}
972fcb04758SJeff Roberson 	if (retry_object != fs->first_object ||
973fcb04758SJeff Roberson 	    retry_pindex != fs->first_pindex)
974fcb04758SJeff Roberson 		return (KERN_RESTART);
975fcb04758SJeff Roberson 
976fcb04758SJeff Roberson 	/*
977fcb04758SJeff Roberson 	 * Check whether the protection has changed or the object has
978fcb04758SJeff Roberson 	 * been copied while we left the map unlocked. Changing from
979fcb04758SJeff Roberson 	 * read to write permission is OK - we leave the page
980fcb04758SJeff Roberson 	 * write-protected, and catch the write fault. Changing from
981fcb04758SJeff Roberson 	 * write to read permission means that we can't mark the page
982fcb04758SJeff Roberson 	 * write-enabled after all.
983fcb04758SJeff Roberson 	 */
984fcb04758SJeff Roberson 	fs->prot &= retry_prot;
985fcb04758SJeff Roberson 	fs->fault_type &= retry_prot;
986fcb04758SJeff Roberson 	if (fs->prot == 0)
987fcb04758SJeff Roberson 		return (KERN_RESTART);
988fcb04758SJeff Roberson 
989fcb04758SJeff Roberson 	/* Reassert because wired may have changed. */
990fcb04758SJeff Roberson 	KASSERT(fs->wired || (fs->fault_flags & VM_FAULT_WIRE) == 0,
991fcb04758SJeff Roberson 	    ("!wired && VM_FAULT_WIRE"));
992fcb04758SJeff Roberson 
993fcb04758SJeff Roberson 	return (KERN_SUCCESS);
994fcb04758SJeff Roberson }
995fcb04758SJeff Roberson 
9965936b6a8SJeff Roberson static void
9975936b6a8SJeff Roberson vm_fault_cow(struct faultstate *fs)
9985936b6a8SJeff Roberson {
9995936b6a8SJeff Roberson 	bool is_first_object_locked;
10005936b6a8SJeff Roberson 
1001982693bbSMark Johnston 	KASSERT(fs->object != fs->first_object,
1002982693bbSMark Johnston 	    ("source and target COW objects are identical"));
1003982693bbSMark Johnston 
10045936b6a8SJeff Roberson 	/*
10055936b6a8SJeff Roberson 	 * This allows pages to be virtually copied from a backing_object
10065936b6a8SJeff Roberson 	 * into the first_object, where the backing object has no other
10075936b6a8SJeff Roberson 	 * refs to it, and cannot gain any more refs.  Instead of a bcopy,
10085936b6a8SJeff Roberson 	 * we just move the page from the backing object to the first
10095936b6a8SJeff Roberson 	 * object.  Note that we must mark the page dirty in the first
10105936b6a8SJeff Roberson 	 * object so that it will go out to swap when needed.
10115936b6a8SJeff Roberson 	 */
10125936b6a8SJeff Roberson 	is_first_object_locked = false;
10135936b6a8SJeff Roberson 	if (
10145936b6a8SJeff Roberson 	    /*
10155936b6a8SJeff Roberson 	     * Only one shadow object and no other refs.
10165936b6a8SJeff Roberson 	     */
10175936b6a8SJeff Roberson 	    fs->object->shadow_count == 1 && fs->object->ref_count == 1 &&
10185936b6a8SJeff Roberson 	    /*
10195936b6a8SJeff Roberson 	     * No other ways to look the object up
10205936b6a8SJeff Roberson 	     */
10215936b6a8SJeff Roberson 	    fs->object->handle == NULL && (fs->object->flags & OBJ_ANON) != 0 &&
10225936b6a8SJeff Roberson 	    /*
10235936b6a8SJeff Roberson 	     * We don't chase down the shadow chain and we can acquire locks.
10245936b6a8SJeff Roberson 	     */
10255936b6a8SJeff Roberson 	    (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs->first_object)) &&
10265936b6a8SJeff Roberson 	    fs->object == fs->first_object->backing_object &&
10275936b6a8SJeff Roberson 	    VM_OBJECT_TRYWLOCK(fs->object)) {
10285936b6a8SJeff Roberson 		/*
10295936b6a8SJeff Roberson 		 * Remove but keep xbusy for replace.  fs->m is moved into
10305936b6a8SJeff Roberson 		 * fs->first_object and left busy while fs->first_m is
10315936b6a8SJeff Roberson 		 * conditionally freed.
10325936b6a8SJeff Roberson 		 */
10335936b6a8SJeff Roberson 		vm_page_remove_xbusy(fs->m);
10345936b6a8SJeff Roberson 		vm_page_replace(fs->m, fs->first_object, fs->first_pindex,
10355936b6a8SJeff Roberson 		    fs->first_m);
10365936b6a8SJeff Roberson 		vm_page_dirty(fs->m);
10375936b6a8SJeff Roberson #if VM_NRESERVLEVEL > 0
10385936b6a8SJeff Roberson 		/*
10395936b6a8SJeff Roberson 		 * Rename the reservation.
10405936b6a8SJeff Roberson 		 */
10415936b6a8SJeff Roberson 		vm_reserv_rename(fs->m, fs->first_object, fs->object,
10425936b6a8SJeff Roberson 		    OFF_TO_IDX(fs->first_object->backing_object_offset));
10435936b6a8SJeff Roberson #endif
10445936b6a8SJeff Roberson 		VM_OBJECT_WUNLOCK(fs->object);
10455936b6a8SJeff Roberson 		VM_OBJECT_WUNLOCK(fs->first_object);
10465936b6a8SJeff Roberson 		fs->first_m = fs->m;
10475936b6a8SJeff Roberson 		fs->m = NULL;
10485936b6a8SJeff Roberson 		VM_CNT_INC(v_cow_optim);
10495936b6a8SJeff Roberson 	} else {
10505936b6a8SJeff Roberson 		if (is_first_object_locked)
10515936b6a8SJeff Roberson 			VM_OBJECT_WUNLOCK(fs->first_object);
10525936b6a8SJeff Roberson 		/*
10535936b6a8SJeff Roberson 		 * Oh, well, lets copy it.
10545936b6a8SJeff Roberson 		 */
10555936b6a8SJeff Roberson 		pmap_copy_page(fs->m, fs->first_m);
10565936b6a8SJeff Roberson 		vm_page_valid(fs->first_m);
10575936b6a8SJeff Roberson 		if (fs->wired && (fs->fault_flags & VM_FAULT_WIRE) == 0) {
10585936b6a8SJeff Roberson 			vm_page_wire(fs->first_m);
10595936b6a8SJeff Roberson 			vm_page_unwire(fs->m, PQ_INACTIVE);
10605936b6a8SJeff Roberson 		}
10615936b6a8SJeff Roberson 		/*
10625936b6a8SJeff Roberson 		 * Save the cow page to be released after
10635936b6a8SJeff Roberson 		 * pmap_enter is complete.
10645936b6a8SJeff Roberson 		 */
10655936b6a8SJeff Roberson 		fs->m_cow = fs->m;
10665936b6a8SJeff Roberson 		fs->m = NULL;
1067982693bbSMark Johnston 
10685936b6a8SJeff Roberson 		/*
1069982693bbSMark Johnston 		 * Typically, the shadow object is either private to this
1070982693bbSMark Johnston 		 * address space (OBJ_ONEMAPPING) or its pages are read only.
1071982693bbSMark Johnston 		 * In the highly unusual case where the pages of a shadow object
1072982693bbSMark Johnston 		 * are read/write shared between this and other address spaces,
1073982693bbSMark Johnston 		 * we need to ensure that any pmap-level mappings to the
1074982693bbSMark Johnston 		 * original, copy-on-write page from the backing object are
1075982693bbSMark Johnston 		 * removed from those other address spaces.
1076982693bbSMark Johnston 		 *
1077982693bbSMark Johnston 		 * The flag check is racy, but this is tolerable: if
1078982693bbSMark Johnston 		 * OBJ_ONEMAPPING is cleared after the check, the busy state
1079982693bbSMark Johnston 		 * ensures that new mappings of m_cow can't be created.
1080982693bbSMark Johnston 		 * pmap_enter() will replace an existing mapping in the current
1081982693bbSMark Johnston 		 * address space.  If OBJ_ONEMAPPING is set after the check,
1082982693bbSMark Johnston 		 * removing mappings will at worse trigger some unnecessary page
1083982693bbSMark Johnston 		 * faults.
10845936b6a8SJeff Roberson 		 */
1085982693bbSMark Johnston 		vm_page_assert_xbusied(fs->m_cow);
1086982693bbSMark Johnston 		if ((fs->first_object->flags & OBJ_ONEMAPPING) == 0)
1087982693bbSMark Johnston 			pmap_remove_all(fs->m_cow);
1088982693bbSMark Johnston 	}
1089982693bbSMark Johnston 
10905936b6a8SJeff Roberson 	vm_object_pip_wakeup(fs->object);
10915936b6a8SJeff Roberson 
10925936b6a8SJeff Roberson 	/*
10935936b6a8SJeff Roberson 	 * Only use the new page below...
10945936b6a8SJeff Roberson 	 */
10955936b6a8SJeff Roberson 	fs->object = fs->first_object;
10965936b6a8SJeff Roberson 	fs->pindex = fs->first_pindex;
10975936b6a8SJeff Roberson 	fs->m = fs->first_m;
10985936b6a8SJeff Roberson 	VM_CNT_INC(v_cow_faults);
10995936b6a8SJeff Roberson 	curthread->td_cow++;
11005936b6a8SJeff Roberson }
11015936b6a8SJeff Roberson 
1102fdb1dbb1SMateusz Guzik static enum fault_next_status
110391eb2e90SJeff Roberson vm_fault_next(struct faultstate *fs)
110491eb2e90SJeff Roberson {
110591eb2e90SJeff Roberson 	vm_object_t next_object;
110691eb2e90SJeff Roberson 
1107fdb1dbb1SMateusz Guzik 	if (fs->object == fs->first_object || !fs->can_read_lock)
110873b951cdSMateusz Guzik 		VM_OBJECT_ASSERT_WLOCKED(fs->object);
1109fdb1dbb1SMateusz Guzik 	else
1110fdb1dbb1SMateusz Guzik 		VM_OBJECT_ASSERT_LOCKED(fs->object);
111173b951cdSMateusz Guzik 
111291eb2e90SJeff Roberson 	/*
111391eb2e90SJeff Roberson 	 * The requested page does not exist at this object/
111491eb2e90SJeff Roberson 	 * offset.  Remove the invalid page from the object,
111591eb2e90SJeff Roberson 	 * waking up anyone waiting for it, and continue on to
111691eb2e90SJeff Roberson 	 * the next object.  However, if this is the top-level
111791eb2e90SJeff Roberson 	 * object, we must leave the busy page in place to
111891eb2e90SJeff Roberson 	 * prevent another process from rushing past us, and
111991eb2e90SJeff Roberson 	 * inserting the page in that object at the same time
112091eb2e90SJeff Roberson 	 * that we are.
112191eb2e90SJeff Roberson 	 */
112291eb2e90SJeff Roberson 	if (fs->object == fs->first_object) {
112391eb2e90SJeff Roberson 		fs->first_m = fs->m;
112491eb2e90SJeff Roberson 		fs->m = NULL;
11253c3a434fSMateusz Guzik 	} else if (fs->m != NULL) {
1126fdb1dbb1SMateusz Guzik 		if (!vm_fault_object_ensure_wlocked(fs)) {
1127fdb1dbb1SMateusz Guzik 			fs->can_read_lock = false;
11280a310c94SMateusz Guzik 			vm_fault_unlock_and_deallocate(fs);
1129fdb1dbb1SMateusz Guzik 			return (FAULT_NEXT_RESTART);
1130fdb1dbb1SMateusz Guzik 		}
11310a310c94SMateusz Guzik 		vm_fault_page_free(&fs->m);
1132fdb1dbb1SMateusz Guzik 	}
113391eb2e90SJeff Roberson 
113491eb2e90SJeff Roberson 	/*
113591eb2e90SJeff Roberson 	 * Move on to the next object.  Lock the next object before
113691eb2e90SJeff Roberson 	 * unlocking the current one.
113791eb2e90SJeff Roberson 	 */
113891eb2e90SJeff Roberson 	next_object = fs->object->backing_object;
1139fb4d37eaSJeff Roberson 	if (next_object == NULL)
1140fdb1dbb1SMateusz Guzik 		return (FAULT_NEXT_NOOBJ);
1141fb4d37eaSJeff Roberson 	MPASS(fs->first_m != NULL);
1142fb4d37eaSJeff Roberson 	KASSERT(fs->object != next_object, ("object loop %p", next_object));
1143fdb1dbb1SMateusz Guzik 	if (fs->can_read_lock)
1144fdb1dbb1SMateusz Guzik 		VM_OBJECT_RLOCK(next_object);
1145fdb1dbb1SMateusz Guzik 	else
1146fb4d37eaSJeff Roberson 		VM_OBJECT_WLOCK(next_object);
1147fb4d37eaSJeff Roberson 	vm_object_pip_add(next_object, 1);
1148fb4d37eaSJeff Roberson 	if (fs->object != fs->first_object)
1149fb4d37eaSJeff Roberson 		vm_object_pip_wakeup(fs->object);
1150fb4d37eaSJeff Roberson 	fs->pindex += OFF_TO_IDX(fs->object->backing_object_offset);
1151fdb1dbb1SMateusz Guzik 	VM_OBJECT_UNLOCK(fs->object);
1152fb4d37eaSJeff Roberson 	fs->object = next_object;
1153fb4d37eaSJeff Roberson 
1154fdb1dbb1SMateusz Guzik 	return (FAULT_NEXT_GOTOBJ);
1155fb4d37eaSJeff Roberson }
1156fb4d37eaSJeff Roberson 
1157fb4d37eaSJeff Roberson static void
1158fb4d37eaSJeff Roberson vm_fault_zerofill(struct faultstate *fs)
1159fb4d37eaSJeff Roberson {
1160fb4d37eaSJeff Roberson 
116191eb2e90SJeff Roberson 	/*
116291eb2e90SJeff Roberson 	 * If there's no object left, fill the page in the top
116391eb2e90SJeff Roberson 	 * object with zeros.
116491eb2e90SJeff Roberson 	 */
116591eb2e90SJeff Roberson 	if (fs->object != fs->first_object) {
116691eb2e90SJeff Roberson 		vm_object_pip_wakeup(fs->object);
116791eb2e90SJeff Roberson 		fs->object = fs->first_object;
116891eb2e90SJeff Roberson 		fs->pindex = fs->first_pindex;
116991eb2e90SJeff Roberson 	}
117091eb2e90SJeff Roberson 	MPASS(fs->first_m != NULL);
117191eb2e90SJeff Roberson 	MPASS(fs->m == NULL);
117291eb2e90SJeff Roberson 	fs->m = fs->first_m;
117391eb2e90SJeff Roberson 	fs->first_m = NULL;
117491eb2e90SJeff Roberson 
117591eb2e90SJeff Roberson 	/*
117691eb2e90SJeff Roberson 	 * Zero the page if necessary and mark it valid.
117791eb2e90SJeff Roberson 	 */
117891eb2e90SJeff Roberson 	if ((fs->m->flags & PG_ZERO) == 0) {
117991eb2e90SJeff Roberson 		pmap_zero_page(fs->m);
118091eb2e90SJeff Roberson 	} else {
118191eb2e90SJeff Roberson 		VM_CNT_INC(v_ozfod);
118291eb2e90SJeff Roberson 	}
118391eb2e90SJeff Roberson 	VM_CNT_INC(v_zfod);
118491eb2e90SJeff Roberson 	vm_page_valid(fs->m);
118591eb2e90SJeff Roberson }
118691eb2e90SJeff Roberson 
1187df794f5cSJeff Roberson /*
1188174aad04SKonstantin Belousov  * Initiate page fault after timeout.  Returns true if caller should
1189174aad04SKonstantin Belousov  * do vm_waitpfault() after the call.
1190174aad04SKonstantin Belousov  */
1191174aad04SKonstantin Belousov static bool
1192174aad04SKonstantin Belousov vm_fault_allocate_oom(struct faultstate *fs)
1193174aad04SKonstantin Belousov {
1194174aad04SKonstantin Belousov 	struct timeval now;
1195174aad04SKonstantin Belousov 
11960a310c94SMateusz Guzik 	vm_fault_unlock_and_deallocate(fs);
1197174aad04SKonstantin Belousov 	if (vm_pfault_oom_attempts < 0)
1198174aad04SKonstantin Belousov 		return (true);
1199174aad04SKonstantin Belousov 	if (!fs->oom_started) {
1200174aad04SKonstantin Belousov 		fs->oom_started = true;
1201174aad04SKonstantin Belousov 		getmicrotime(&fs->oom_start_time);
1202174aad04SKonstantin Belousov 		return (true);
1203174aad04SKonstantin Belousov 	}
1204174aad04SKonstantin Belousov 
1205174aad04SKonstantin Belousov 	getmicrotime(&now);
1206174aad04SKonstantin Belousov 	timevalsub(&now, &fs->oom_start_time);
1207174aad04SKonstantin Belousov 	if (now.tv_sec < vm_pfault_oom_attempts * vm_pfault_oom_wait)
1208174aad04SKonstantin Belousov 		return (true);
1209174aad04SKonstantin Belousov 
1210174aad04SKonstantin Belousov 	if (bootverbose)
1211174aad04SKonstantin Belousov 		printf(
1212174aad04SKonstantin Belousov 	    "proc %d (%s) failed to alloc page on fault, starting OOM\n",
1213174aad04SKonstantin Belousov 		    curproc->p_pid, curproc->p_comm);
1214174aad04SKonstantin Belousov 	vm_pageout_oom(VM_OOM_MEM_PF);
1215174aad04SKonstantin Belousov 	fs->oom_started = false;
1216174aad04SKonstantin Belousov 	return (false);
1217174aad04SKonstantin Belousov }
1218174aad04SKonstantin Belousov 
1219174aad04SKonstantin Belousov /*
1220df794f5cSJeff Roberson  * Allocate a page directly or via the object populate method.
1221df794f5cSJeff Roberson  */
1222f1b642c2SMark Johnston static enum fault_status
1223df794f5cSJeff Roberson vm_fault_allocate(struct faultstate *fs)
1224df794f5cSJeff Roberson {
1225df794f5cSJeff Roberson 	struct domainset *dset;
1226f1b642c2SMark Johnston 	enum fault_status res;
1227df794f5cSJeff Roberson 
1228df794f5cSJeff Roberson 	if ((fs->object->flags & OBJ_SIZEVNLOCK) != 0) {
1229f1b642c2SMark Johnston 		res = vm_fault_lock_vnode(fs, true);
1230f1b642c2SMark Johnston 		MPASS(res == FAULT_CONTINUE || res == FAULT_RESTART);
1231f1b642c2SMark Johnston 		if (res == FAULT_RESTART)
1232f1b642c2SMark Johnston 			return (res);
1233df794f5cSJeff Roberson 	}
1234df794f5cSJeff Roberson 
1235f1b642c2SMark Johnston 	if (fs->pindex >= fs->object->size) {
12360a310c94SMateusz Guzik 		vm_fault_unlock_and_deallocate(fs);
1237f1b642c2SMark Johnston 		return (FAULT_OUT_OF_BOUNDS);
1238f1b642c2SMark Johnston 	}
1239df794f5cSJeff Roberson 
1240df794f5cSJeff Roberson 	if (fs->object == fs->first_object &&
1241df794f5cSJeff Roberson 	    (fs->first_object->flags & OBJ_POPULATE) != 0 &&
1242df794f5cSJeff Roberson 	    fs->first_object->shadow_count == 0) {
1243f1b642c2SMark Johnston 		res = vm_fault_populate(fs);
1244f1b642c2SMark Johnston 		switch (res) {
1245f1b642c2SMark Johnston 		case FAULT_SUCCESS:
1246f1b642c2SMark Johnston 		case FAULT_FAILURE:
1247f1b642c2SMark Johnston 		case FAULT_RESTART:
12480a310c94SMateusz Guzik 			vm_fault_unlock_and_deallocate(fs);
1249f1b642c2SMark Johnston 			return (res);
1250f1b642c2SMark Johnston 		case FAULT_CONTINUE:
1251df794f5cSJeff Roberson 			/*
1252df794f5cSJeff Roberson 			 * Pager's populate() method
1253df794f5cSJeff Roberson 			 * returned VM_PAGER_BAD.
1254df794f5cSJeff Roberson 			 */
1255df794f5cSJeff Roberson 			break;
1256df794f5cSJeff Roberson 		default:
1257df794f5cSJeff Roberson 			panic("inconsistent return codes");
1258df794f5cSJeff Roberson 		}
1259df794f5cSJeff Roberson 	}
1260df794f5cSJeff Roberson 
1261df794f5cSJeff Roberson 	/*
1262df794f5cSJeff Roberson 	 * Allocate a new page for this object/offset pair.
1263df794f5cSJeff Roberson 	 *
1264b801c79dSMark Johnston 	 * If the process has a fatal signal pending, prioritize the allocation
1265b801c79dSMark Johnston 	 * with the expectation that the process will exit shortly and free some
1266b801c79dSMark Johnston 	 * pages.  In particular, the signal may have been posted by the page
1267b801c79dSMark Johnston 	 * daemon in an attempt to resolve an out-of-memory condition.
1268b801c79dSMark Johnston 	 *
1269b801c79dSMark Johnston 	 * The unlocked read of the p_flag is harmless.  At worst, the P_KILLED
1270b801c79dSMark Johnston 	 * might be not observed here, and allocation fails, causing a restart
1271b801c79dSMark Johnston 	 * and new reading of the p_flag.
1272df794f5cSJeff Roberson 	 */
1273df794f5cSJeff Roberson 	dset = fs->object->domain.dr_policy;
1274df794f5cSJeff Roberson 	if (dset == NULL)
1275df794f5cSJeff Roberson 		dset = curthread->td_domain.dr_policy;
1276df794f5cSJeff Roberson 	if (!vm_page_count_severe_set(&dset->ds_mask) || P_KILLED(curproc)) {
1277df794f5cSJeff Roberson #if VM_NRESERVLEVEL > 0
1278df794f5cSJeff Roberson 		vm_object_color(fs->object, atop(fs->vaddr) - fs->pindex);
1279df794f5cSJeff Roberson #endif
1280ec201dddSKonstantin Belousov 		if (!vm_pager_can_alloc_page(fs->object, fs->pindex)) {
12810a310c94SMateusz Guzik 			vm_fault_unlock_and_deallocate(fs);
1282ec201dddSKonstantin Belousov 			return (FAULT_FAILURE);
1283ec201dddSKonstantin Belousov 		}
1284b801c79dSMark Johnston 		fs->m = vm_page_alloc(fs->object, fs->pindex,
1285b801c79dSMark Johnston 		    P_KILLED(curproc) ? VM_ALLOC_SYSTEM : 0);
1286df794f5cSJeff Roberson 	}
1287df794f5cSJeff Roberson 	if (fs->m == NULL) {
1288174aad04SKonstantin Belousov 		if (vm_fault_allocate_oom(fs))
1289df794f5cSJeff Roberson 			vm_waitpfault(dset, vm_pfault_oom_wait * hz);
1290f1b642c2SMark Johnston 		return (FAULT_RESTART);
1291df794f5cSJeff Roberson 	}
1292174aad04SKonstantin Belousov 	fs->oom_started = false;
1293df794f5cSJeff Roberson 
1294f1b642c2SMark Johnston 	return (FAULT_CONTINUE);
1295df794f5cSJeff Roberson }
12965909dafeSJeff Roberson 
12975909dafeSJeff Roberson /*
12985909dafeSJeff Roberson  * Call the pager to retrieve the page if there is a chance
12995909dafeSJeff Roberson  * that the pager has it, and potentially retrieve additional
13005909dafeSJeff Roberson  * pages at the same time.
13015909dafeSJeff Roberson  */
1302f1b642c2SMark Johnston static enum fault_status
130345c09a74SMark Johnston vm_fault_getpages(struct faultstate *fs, int *behindp, int *aheadp)
13045909dafeSJeff Roberson {
13055909dafeSJeff Roberson 	vm_offset_t e_end, e_start;
13065909dafeSJeff Roberson 	int ahead, behind, cluster_offset, rv;
1307f1b642c2SMark Johnston 	enum fault_status status;
13085909dafeSJeff Roberson 	u_char behavior;
13095909dafeSJeff Roberson 
13105909dafeSJeff Roberson 	/*
13115909dafeSJeff Roberson 	 * Prepare for unlocking the map.  Save the map
13125909dafeSJeff Roberson 	 * entry's start and end addresses, which are used to
13135909dafeSJeff Roberson 	 * optimize the size of the pager operation below.
13145909dafeSJeff Roberson 	 * Even if the map entry's addresses change after
13155909dafeSJeff Roberson 	 * unlocking the map, using the saved addresses is
13165909dafeSJeff Roberson 	 * safe.
13175909dafeSJeff Roberson 	 */
13185909dafeSJeff Roberson 	e_start = fs->entry->start;
13195909dafeSJeff Roberson 	e_end = fs->entry->end;
13205909dafeSJeff Roberson 	behavior = vm_map_entry_behavior(fs->entry);
13215909dafeSJeff Roberson 
13225909dafeSJeff Roberson 	/*
132345c09a74SMark Johnston 	 * If the pager for the current object might have
132445c09a74SMark Johnston 	 * the page, then determine the number of additional
132545c09a74SMark Johnston 	 * pages to read and potentially reprioritize
132645c09a74SMark Johnston 	 * previously read pages for earlier reclamation.
132745c09a74SMark Johnston 	 * These operations should only be performed once per
132845c09a74SMark Johnston 	 * page fault.  Even if the current pager doesn't
132945c09a74SMark Johnston 	 * have the page, the number of additional pages to
133045c09a74SMark Johnston 	 * read will apply to subsequent objects in the
133145c09a74SMark Johnston 	 * shadow chain.
133245c09a74SMark Johnston 	 */
133345c09a74SMark Johnston 	if (fs->nera == -1 && !P_KILLED(curproc))
133445c09a74SMark Johnston 		fs->nera = vm_fault_readahead(fs);
133545c09a74SMark Johnston 
133645c09a74SMark Johnston 	/*
13375909dafeSJeff Roberson 	 * Release the map lock before locking the vnode or
13385909dafeSJeff Roberson 	 * sleeping in the pager.  (If the current object has
13395909dafeSJeff Roberson 	 * a shadow, then an earlier iteration of this loop
13405909dafeSJeff Roberson 	 * may have already unlocked the map.)
13415909dafeSJeff Roberson 	 */
13420a310c94SMateusz Guzik 	vm_fault_unlock_map(fs);
13435909dafeSJeff Roberson 
1344f1b642c2SMark Johnston 	status = vm_fault_lock_vnode(fs, false);
1345f1b642c2SMark Johnston 	MPASS(status == FAULT_CONTINUE || status == FAULT_RESTART);
1346f1b642c2SMark Johnston 	if (status == FAULT_RESTART)
1347f1b642c2SMark Johnston 		return (status);
13485909dafeSJeff Roberson 	KASSERT(fs->vp == NULL || !fs->map->system_map,
13495909dafeSJeff Roberson 	    ("vm_fault: vnode-backed object mapped by system map"));
13505909dafeSJeff Roberson 
13515909dafeSJeff Roberson 	/*
13525909dafeSJeff Roberson 	 * Page in the requested page and hint the pager,
13535909dafeSJeff Roberson 	 * that it may bring up surrounding pages.
13545909dafeSJeff Roberson 	 */
135545c09a74SMark Johnston 	if (fs->nera == -1 || behavior == MAP_ENTRY_BEHAV_RANDOM ||
13565909dafeSJeff Roberson 	    P_KILLED(curproc)) {
13575909dafeSJeff Roberson 		behind = 0;
13585909dafeSJeff Roberson 		ahead = 0;
13595909dafeSJeff Roberson 	} else {
13605909dafeSJeff Roberson 		/* Is this a sequential fault? */
136145c09a74SMark Johnston 		if (fs->nera > 0) {
13625909dafeSJeff Roberson 			behind = 0;
136345c09a74SMark Johnston 			ahead = fs->nera;
13645909dafeSJeff Roberson 		} else {
13655909dafeSJeff Roberson 			/*
13665909dafeSJeff Roberson 			 * Request a cluster of pages that is
13675909dafeSJeff Roberson 			 * aligned to a VM_FAULT_READ_DEFAULT
13685909dafeSJeff Roberson 			 * page offset boundary within the
13695909dafeSJeff Roberson 			 * object.  Alignment to a page offset
13705909dafeSJeff Roberson 			 * boundary is more likely to coincide
13715909dafeSJeff Roberson 			 * with the underlying file system
13725909dafeSJeff Roberson 			 * block than alignment to a virtual
13735909dafeSJeff Roberson 			 * address boundary.
13745909dafeSJeff Roberson 			 */
13755909dafeSJeff Roberson 			cluster_offset = fs->pindex % VM_FAULT_READ_DEFAULT;
13765909dafeSJeff Roberson 			behind = ulmin(cluster_offset,
13775909dafeSJeff Roberson 			    atop(fs->vaddr - e_start));
13785909dafeSJeff Roberson 			ahead = VM_FAULT_READ_DEFAULT - 1 - cluster_offset;
13795909dafeSJeff Roberson 		}
13805909dafeSJeff Roberson 		ahead = ulmin(ahead, atop(e_end - fs->vaddr) - 1);
13815909dafeSJeff Roberson 	}
13825909dafeSJeff Roberson 	*behindp = behind;
13835909dafeSJeff Roberson 	*aheadp = ahead;
13845909dafeSJeff Roberson 	rv = vm_pager_get_pages(fs->object, &fs->m, 1, behindp, aheadp);
13855909dafeSJeff Roberson 	if (rv == VM_PAGER_OK)
1386f1b642c2SMark Johnston 		return (FAULT_HARD);
13875909dafeSJeff Roberson 	if (rv == VM_PAGER_ERROR)
13885909dafeSJeff Roberson 		printf("vm_fault: pager read error, pid %d (%s)\n",
13895909dafeSJeff Roberson 		    curproc->p_pid, curproc->p_comm);
13905909dafeSJeff Roberson 	/*
13915909dafeSJeff Roberson 	 * If an I/O error occurred or the requested page was
13925909dafeSJeff Roberson 	 * outside the range of the pager, clean up and return
13935909dafeSJeff Roberson 	 * an error.
13945909dafeSJeff Roberson 	 */
139545c09a74SMark Johnston 	if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) {
139645c09a74SMark Johnston 		VM_OBJECT_WLOCK(fs->object);
13970a310c94SMateusz Guzik 		vm_fault_page_free(&fs->m);
13980a310c94SMateusz Guzik 		vm_fault_unlock_and_deallocate(fs);
1399f1b642c2SMark Johnston 		return (FAULT_OUT_OF_BOUNDS);
140045c09a74SMark Johnston 	}
140145c09a74SMark Johnston 	KASSERT(rv == VM_PAGER_FAIL,
14029a89977bSPeter Jeremy 	    ("%s: unexpected pager error %d", __func__, rv));
1403f1b642c2SMark Johnston 	return (FAULT_CONTINUE);
14045909dafeSJeff Roberson }
14055909dafeSJeff Roberson 
14065949b1caSJeff Roberson /*
1407bef91632SJeff Roberson  * Wait/Retry if the page is busy.  We have to do this if the page is
1408bef91632SJeff Roberson  * either exclusive or shared busy because the vm_pager may be using
1409bef91632SJeff Roberson  * read busy for pageouts (and even pageins if it is the vnode pager),
1410bef91632SJeff Roberson  * and we could end up trying to pagein and pageout the same page
1411bef91632SJeff Roberson  * simultaneously.
1412bef91632SJeff Roberson  *
1413bef91632SJeff Roberson  * We can theoretically allow the busy case on a read fault if the page
1414bef91632SJeff Roberson  * is marked valid, but since such pages are typically already pmap'd,
1415bef91632SJeff Roberson  * putting that special case in might be more effort then it is worth.
1416bef91632SJeff Roberson  * We cannot under any circumstances mess around with a shared busied
1417bef91632SJeff Roberson  * page except, perhaps, to pmap it.
1418bef91632SJeff Roberson  */
1419bef91632SJeff Roberson static void
1420bef91632SJeff Roberson vm_fault_busy_sleep(struct faultstate *fs)
1421bef91632SJeff Roberson {
1422bef91632SJeff Roberson 	/*
1423bef91632SJeff Roberson 	 * Reference the page before unlocking and
1424bef91632SJeff Roberson 	 * sleeping so that the page daemon is less
1425bef91632SJeff Roberson 	 * likely to reclaim it.
1426bef91632SJeff Roberson 	 */
1427bef91632SJeff Roberson 	vm_page_aflag_set(fs->m, PGA_REFERENCED);
1428bef91632SJeff Roberson 	if (fs->object != fs->first_object) {
14290a310c94SMateusz Guzik 		vm_fault_page_release(&fs->first_m);
1430bef91632SJeff Roberson 		vm_object_pip_wakeup(fs->first_object);
1431bef91632SJeff Roberson 	}
1432bef91632SJeff Roberson 	vm_object_pip_wakeup(fs->object);
14330a310c94SMateusz Guzik 	vm_fault_unlock_map(fs);
143487b64663SMark Johnston 	if (fs->m != vm_page_lookup(fs->object, fs->pindex) ||
143587b64663SMark Johnston 	    !vm_page_busy_sleep(fs->m, "vmpfw", 0))
1436fdb1dbb1SMateusz Guzik 		VM_OBJECT_UNLOCK(fs->object);
1437bef91632SJeff Roberson 	VM_CNT_INC(v_intrans);
1438bef91632SJeff Roberson 	vm_object_deallocate(fs->first_object);
1439bef91632SJeff Roberson }
1440bef91632SJeff Roberson 
1441d47d3a94SMark Johnston /*
1442d47d3a94SMark Johnston  * Handle page lookup, populate, allocate, page-in for the current
1443d47d3a94SMark Johnston  * object.
1444d47d3a94SMark Johnston  *
1445d47d3a94SMark Johnston  * The object is locked on entry and will remain locked with a return
1446d47d3a94SMark Johnston  * code of FAULT_CONTINUE so that fault may follow the shadow chain.
1447d47d3a94SMark Johnston  * Otherwise, the object will be unlocked upon return.
1448d47d3a94SMark Johnston  */
1449d47d3a94SMark Johnston static enum fault_status
1450d47d3a94SMark Johnston vm_fault_object(struct faultstate *fs, int *behindp, int *aheadp)
1451d47d3a94SMark Johnston {
1452d47d3a94SMark Johnston 	enum fault_status res;
1453d47d3a94SMark Johnston 	bool dead;
1454d47d3a94SMark Johnston 
1455fdb1dbb1SMateusz Guzik 	if (fs->object == fs->first_object || !fs->can_read_lock)
145673b951cdSMateusz Guzik 		VM_OBJECT_ASSERT_WLOCKED(fs->object);
1457fdb1dbb1SMateusz Guzik 	else
1458fdb1dbb1SMateusz Guzik 		VM_OBJECT_ASSERT_LOCKED(fs->object);
145973b951cdSMateusz Guzik 
1460d47d3a94SMark Johnston 	/*
1461d47d3a94SMark Johnston 	 * If the object is marked for imminent termination, we retry
1462d47d3a94SMark Johnston 	 * here, since the collapse pass has raced with us.  Otherwise,
1463d47d3a94SMark Johnston 	 * if we see terminally dead object, return fail.
1464d47d3a94SMark Johnston 	 */
1465d47d3a94SMark Johnston 	if ((fs->object->flags & OBJ_DEAD) != 0) {
1466d47d3a94SMark Johnston 		dead = fs->object->type == OBJT_DEAD;
14670a310c94SMateusz Guzik 		vm_fault_unlock_and_deallocate(fs);
1468d47d3a94SMark Johnston 		if (dead)
1469d47d3a94SMark Johnston 			return (FAULT_PROTECTION_FAILURE);
1470d47d3a94SMark Johnston 		pause("vmf_de", 1);
1471d47d3a94SMark Johnston 		return (FAULT_RESTART);
1472d47d3a94SMark Johnston 	}
1473d47d3a94SMark Johnston 
1474d47d3a94SMark Johnston 	/*
1475d47d3a94SMark Johnston 	 * See if the page is resident.
1476d47d3a94SMark Johnston 	 */
1477d47d3a94SMark Johnston 	fs->m = vm_page_lookup(fs->object, fs->pindex);
1478d47d3a94SMark Johnston 	if (fs->m != NULL) {
1479d47d3a94SMark Johnston 		if (!vm_page_tryxbusy(fs->m)) {
1480d47d3a94SMark Johnston 			vm_fault_busy_sleep(fs);
1481d47d3a94SMark Johnston 			return (FAULT_RESTART);
1482d47d3a94SMark Johnston 		}
1483d47d3a94SMark Johnston 
1484d47d3a94SMark Johnston 		/*
1485d47d3a94SMark Johnston 		 * The page is marked busy for other processes and the
1486d47d3a94SMark Johnston 		 * pagedaemon.  If it is still completely valid we are
1487d47d3a94SMark Johnston 		 * done.
1488d47d3a94SMark Johnston 		 */
1489d47d3a94SMark Johnston 		if (vm_page_all_valid(fs->m)) {
1490fdb1dbb1SMateusz Guzik 			VM_OBJECT_UNLOCK(fs->object);
1491d47d3a94SMark Johnston 			return (FAULT_SOFT);
1492d47d3a94SMark Johnston 		}
1493d47d3a94SMark Johnston 	}
1494d47d3a94SMark Johnston 
1495d47d3a94SMark Johnston 	/*
1496d47d3a94SMark Johnston 	 * Page is not resident.  If the pager might contain the page
1497d47d3a94SMark Johnston 	 * or this is the beginning of the search, allocate a new
14985d32157dSMark Johnston 	 * page.
1499d47d3a94SMark Johnston 	 */
15000a310c94SMateusz Guzik 	if (fs->m == NULL && (vm_fault_object_needs_getpages(fs->object) ||
1501d47d3a94SMark Johnston 	    fs->object == fs->first_object)) {
1502fdb1dbb1SMateusz Guzik 		if (!vm_fault_object_ensure_wlocked(fs)) {
1503fdb1dbb1SMateusz Guzik 			fs->can_read_lock = false;
15040a310c94SMateusz Guzik 			vm_fault_unlock_and_deallocate(fs);
1505fdb1dbb1SMateusz Guzik 			return (FAULT_RESTART);
1506fdb1dbb1SMateusz Guzik 		}
1507d47d3a94SMark Johnston 		res = vm_fault_allocate(fs);
1508d47d3a94SMark Johnston 		if (res != FAULT_CONTINUE)
1509d47d3a94SMark Johnston 			return (res);
1510d47d3a94SMark Johnston 	}
1511d47d3a94SMark Johnston 
1512d47d3a94SMark Johnston 	/*
1513e08302f6SMark Johnston 	 * Check to see if the pager can possibly satisfy this fault.
1514e08302f6SMark Johnston 	 * If not, skip to the next object without dropping the lock to
1515e08302f6SMark Johnston 	 * preserve atomicity of shadow faults.
1516d47d3a94SMark Johnston 	 */
15170a310c94SMateusz Guzik 	if (vm_fault_object_needs_getpages(fs->object)) {
1518d47d3a94SMark Johnston 		/*
1519d47d3a94SMark Johnston 		 * At this point, we have either allocated a new page
1520d47d3a94SMark Johnston 		 * or found an existing page that is only partially
1521d47d3a94SMark Johnston 		 * valid.
1522d47d3a94SMark Johnston 		 *
1523d47d3a94SMark Johnston 		 * We hold a reference on the current object and the
1524d47d3a94SMark Johnston 		 * page is exclusive busied.  The exclusive busy
1525d47d3a94SMark Johnston 		 * prevents simultaneous faults and collapses while
1526d47d3a94SMark Johnston 		 * the object lock is dropped.
1527d47d3a94SMark Johnston 		 */
1528fdb1dbb1SMateusz Guzik 		VM_OBJECT_UNLOCK(fs->object);
1529d47d3a94SMark Johnston 		res = vm_fault_getpages(fs, behindp, aheadp);
1530d47d3a94SMark Johnston 		if (res == FAULT_CONTINUE)
1531d47d3a94SMark Johnston 			VM_OBJECT_WLOCK(fs->object);
1532d47d3a94SMark Johnston 	} else {
1533d47d3a94SMark Johnston 		res = FAULT_CONTINUE;
1534d47d3a94SMark Johnston 	}
1535d47d3a94SMark Johnston 	return (res);
1536d47d3a94SMark Johnston }
1537d47d3a94SMark Johnston 
1538acd11c74SAlan Cox int
1539df08823dSKonstantin Belousov vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
1540acd11c74SAlan Cox     int fault_flags, vm_page_t *m_hold)
1541acd11c74SAlan Cox {
15424866e085SJohn Dyson 	struct faultstate fs;
1543f1b642c2SMark Johnston 	int ahead, behind, faultcount, rv;
1544f1b642c2SMark Johnston 	enum fault_status res;
1545fdb1dbb1SMateusz Guzik 	enum fault_next_status res_next;
1546d47d3a94SMark Johnston 	bool hardfault;
1547df8bae1dSRodney W. Grimes 
154883c9dea1SGleb Smirnoff 	VM_CNT_INC(v_vm_faults);
1549c31cec45SKonstantin Belousov 
1550c31cec45SKonstantin Belousov 	if ((curthread->td_pflags & TDP_NOFAULTING) != 0)
1551c31cec45SKonstantin Belousov 		return (KERN_PROTECTION_FAILURE);
1552c31cec45SKonstantin Belousov 
1553d2bf64c3SKonstantin Belousov 	fs.vp = NULL;
15545949b1caSJeff Roberson 	fs.vaddr = vaddr;
15552c2f4413SJeff Roberson 	fs.m_hold = m_hold;
15562c2f4413SJeff Roberson 	fs.fault_flags = fault_flags;
1557c308a3a6SJeff Roberson 	fs.map = map;
1558c308a3a6SJeff Roberson 	fs.lookup_still_valid = false;
1559174aad04SKonstantin Belousov 	fs.oom_started = false;
156045c09a74SMark Johnston 	fs.nera = -1;
1561fdb1dbb1SMateusz Guzik 	fs.can_read_lock = true;
1562b0cd2017SGleb Smirnoff 	faultcount = 0;
1563320023e2SAlan Cox 	hardfault = false;
1564df8bae1dSRodney W. Grimes 
1565245139c6SKonstantin Belousov RetryFault:
15662c2f4413SJeff Roberson 	fs.fault_type = fault_type;
1567df8bae1dSRodney W. Grimes 
1568df8bae1dSRodney W. Grimes 	/*
15690d94caffSDavid Greenman 	 * Find the backing store object and offset into it to begin the
15700d94caffSDavid Greenman 	 * search.
1571df8bae1dSRodney W. Grimes 	 */
1572f1b642c2SMark Johnston 	rv = vm_fault_lookup(&fs);
1573f1b642c2SMark Johnston 	if (rv != KERN_SUCCESS) {
1574f1b642c2SMark Johnston 		if (rv == KERN_RESOURCE_SHORTAGE)
1575c308a3a6SJeff Roberson 			goto RetryFault;
1576f1b642c2SMark Johnston 		return (rv);
157709e0c6ccSJohn Dyson 	}
157809e0c6ccSJohn Dyson 
15798d67b8c8SAlan Cox 	/*
15808d67b8c8SAlan Cox 	 * Try to avoid lock contention on the top-level object through
15818d67b8c8SAlan Cox 	 * special-case handling of some types of page faults, specifically,
158267d0e293SJeff Roberson 	 * those that are mapping an existing page from the top-level object.
158367d0e293SJeff Roberson 	 * Under this condition, a read lock on the object suffices, allowing
158467d0e293SJeff Roberson 	 * multiple page faults of a similar type to run in parallel.
15858d67b8c8SAlan Cox 	 */
1586afe55ca3SKonstantin Belousov 	if (fs.vp == NULL /* avoid locked vnode leak */ &&
1587d301b358SKonstantin Belousov 	    (fs.entry->eflags & MAP_ENTRY_SPLIT_BOUNDARY_MASK) == 0 &&
15882c2f4413SJeff Roberson 	    (fs.fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0) {
1589f1b642c2SMark Johnston 		res = vm_fault_soft_fast(&fs);
15900e71f4f7SMateusz Guzik 		if (res == FAULT_SUCCESS) {
15910e71f4f7SMateusz Guzik 			VM_OBJECT_ASSERT_UNLOCKED(fs.first_object);
1592f1b642c2SMark Johnston 			return (KERN_SUCCESS);
1593afe55ca3SKonstantin Belousov 		}
15940e71f4f7SMateusz Guzik 		VM_OBJECT_ASSERT_WLOCKED(fs.first_object);
1595afe55ca3SKonstantin Belousov 	} else {
1596afe55ca3SKonstantin Belousov 		VM_OBJECT_WLOCK(fs.first_object);
1597afe55ca3SKonstantin Belousov 	}
1598afe55ca3SKonstantin Belousov 
159995e5e988SJohn Dyson 	/*
160095e5e988SJohn Dyson 	 * Make a reference to this object to prevent its disposal while we
160195e5e988SJohn Dyson 	 * are messing with it.  Once we have the reference, the map is free
160295e5e988SJohn Dyson 	 * to be diddled.  Since objects reference their shadows (and copies),
160395e5e988SJohn Dyson 	 * they will stay around as well.
1604fe8e0238SMatthew Dillon 	 *
1605fe8e0238SMatthew Dillon 	 * Bump the paging-in-progress count to prevent size changes (e.g.
1606dda4d369SAlan Cox 	 * truncation operations) during I/O.
160795e5e988SJohn Dyson 	 */
1608a976eb5eSAlan Cox 	vm_object_reference_locked(fs.first_object);
1609d474eaaaSDoug Rabson 	vm_object_pip_add(fs.first_object, 1);
161095e5e988SJohn Dyson 
161158447749SJeff Roberson 	fs.m_cow = fs.m = fs.first_m = NULL;
1612df8bae1dSRodney W. Grimes 
1613df8bae1dSRodney W. Grimes 	/*
1614df8bae1dSRodney W. Grimes 	 * Search for the page at object/offset.
1615df8bae1dSRodney W. Grimes 	 */
16164866e085SJohn Dyson 	fs.object = fs.first_object;
16174866e085SJohn Dyson 	fs.pindex = fs.first_pindex;
1618d301b358SKonstantin Belousov 
1619d301b358SKonstantin Belousov 	if ((fs.entry->eflags & MAP_ENTRY_SPLIT_BOUNDARY_MASK) != 0) {
1620f1b642c2SMark Johnston 		res = vm_fault_allocate(&fs);
1621f1b642c2SMark Johnston 		switch (res) {
1622f1b642c2SMark Johnston 		case FAULT_RESTART:
1623d301b358SKonstantin Belousov 			goto RetryFault;
1624f1b642c2SMark Johnston 		case FAULT_SUCCESS:
1625f1b642c2SMark Johnston 			return (KERN_SUCCESS);
1626f1b642c2SMark Johnston 		case FAULT_FAILURE:
1627f1b642c2SMark Johnston 			return (KERN_FAILURE);
1628f1b642c2SMark Johnston 		case FAULT_OUT_OF_BOUNDS:
1629f1b642c2SMark Johnston 			return (KERN_OUT_OF_BOUNDS);
1630f1b642c2SMark Johnston 		case FAULT_CONTINUE:
1631d301b358SKonstantin Belousov 			break;
1632d301b358SKonstantin Belousov 		default:
1633f1b642c2SMark Johnston 			panic("vm_fault: Unhandled status %d", res);
1634d301b358SKonstantin Belousov 		}
1635d301b358SKonstantin Belousov 	}
1636d301b358SKonstantin Belousov 
1637df8bae1dSRodney W. Grimes 	while (TRUE) {
16384bf95d00SJeff Roberson 		KASSERT(fs.m == NULL,
16394bf95d00SJeff Roberson 		    ("page still set %p at loop start", fs.m));
164047221757SJohn Dyson 
1641d47d3a94SMark Johnston 		res = vm_fault_object(&fs, &behind, &ahead);
1642f1b642c2SMark Johnston 		switch (res) {
1643d47d3a94SMark Johnston 		case FAULT_SOFT:
1644d47d3a94SMark Johnston 			goto found;
1645d47d3a94SMark Johnston 		case FAULT_HARD:
1646d47d3a94SMark Johnston 			faultcount = behind + 1 + ahead;
1647d47d3a94SMark Johnston 			hardfault = true;
1648d47d3a94SMark Johnston 			goto found;
1649f1b642c2SMark Johnston 		case FAULT_RESTART:
1650df794f5cSJeff Roberson 			goto RetryFault;
1651f1b642c2SMark Johnston 		case FAULT_SUCCESS:
1652f1b642c2SMark Johnston 			return (KERN_SUCCESS);
1653f1b642c2SMark Johnston 		case FAULT_FAILURE:
1654f1b642c2SMark Johnston 			return (KERN_FAILURE);
1655f1b642c2SMark Johnston 		case FAULT_OUT_OF_BOUNDS:
1656f1b642c2SMark Johnston 			return (KERN_OUT_OF_BOUNDS);
1657d47d3a94SMark Johnston 		case FAULT_PROTECTION_FAILURE:
1658d47d3a94SMark Johnston 			return (KERN_PROTECTION_FAILURE);
1659f1b642c2SMark Johnston 		case FAULT_CONTINUE:
1660c42b43a0SKonstantin Belousov 			break;
1661c42b43a0SKonstantin Belousov 		default:
1662f1b642c2SMark Johnston 			panic("vm_fault: Unhandled status %d", res);
1663c42b43a0SKonstantin Belousov 		}
16644bf95d00SJeff Roberson 
1665521ddf39SAlan Cox 		/*
16665909dafeSJeff Roberson 		 * The page was not found in the current object.  Try to
16675909dafeSJeff Roberson 		 * traverse into a backing object or zero fill if none is
16685909dafeSJeff Roberson 		 * found.
1669521ddf39SAlan Cox 		 */
1670fdb1dbb1SMateusz Guzik 		res_next = vm_fault_next(&fs);
1671fdb1dbb1SMateusz Guzik 		if (res_next == FAULT_NEXT_RESTART)
1672fdb1dbb1SMateusz Guzik 			goto RetryFault;
1673fdb1dbb1SMateusz Guzik 		else if (res_next == FAULT_NEXT_GOTOBJ)
1674fb4d37eaSJeff Roberson 			continue;
1675fdb1dbb1SMateusz Guzik 		MPASS(res_next == FAULT_NEXT_NOOBJ);
1676f31695ccSMark Johnston 		if ((fs.fault_flags & VM_FAULT_NOFILL) != 0) {
1677f31695ccSMark Johnston 			if (fs.first_object == fs.object)
16780a310c94SMateusz Guzik 				vm_fault_page_free(&fs.first_m);
16790a310c94SMateusz Guzik 			vm_fault_unlock_and_deallocate(&fs);
1680f31695ccSMark Johnston 			return (KERN_OUT_OF_BOUNDS);
1681f31695ccSMark Johnston 		}
1682fdb1dbb1SMateusz Guzik 		VM_OBJECT_UNLOCK(fs.object);
1683fb4d37eaSJeff Roberson 		vm_fault_zerofill(&fs);
16847b9b301cSAlan Cox 		/* Don't try to prefault neighboring pages. */
16857b9b301cSAlan Cox 		faultcount = 1;
1686d47d3a94SMark Johnston 		break;
1687df8bae1dSRodney W. Grimes 	}
16881c7c3c6aSMatthew Dillon 
1689d47d3a94SMark Johnston found:
1690df8bae1dSRodney W. Grimes 	/*
1691d47d3a94SMark Johnston 	 * A valid page has been found and exclusively busied.  The
1692d47d3a94SMark Johnston 	 * object lock must no longer be held.
1693df8bae1dSRodney W. Grimes 	 */
16941e40fe41SJeff Roberson 	vm_page_assert_xbusied(fs.m);
16951e40fe41SJeff Roberson 	VM_OBJECT_ASSERT_UNLOCKED(fs.object);
1696df8bae1dSRodney W. Grimes 
1697df8bae1dSRodney W. Grimes 	/*
16980d94caffSDavid Greenman 	 * If the page is being written, but isn't already owned by the
16990d94caffSDavid Greenman 	 * top-level object, we have to copy it into a new page owned by the
17000d94caffSDavid Greenman 	 * top-level object.
1701df8bae1dSRodney W. Grimes 	 */
17024866e085SJohn Dyson 	if (fs.object != fs.first_object) {
1703df8bae1dSRodney W. Grimes 		/*
17040d94caffSDavid Greenman 		 * We only really need to copy if we want to write it.
1705df8bae1dSRodney W. Grimes 		 */
17062c2f4413SJeff Roberson 		if ((fs.fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
17075936b6a8SJeff Roberson 			vm_fault_cow(&fs);
17089f1abe3dSAlan Cox 			/*
17099f1abe3dSAlan Cox 			 * We only try to prefault read-only mappings to the
17109f1abe3dSAlan Cox 			 * neighboring pages when this copy-on-write fault is
17119f1abe3dSAlan Cox 			 * a hard fault.  In other cases, trying to prefault
17129f1abe3dSAlan Cox 			 * is typically wasted effort.
17139f1abe3dSAlan Cox 			 */
17149f1abe3dSAlan Cox 			if (faultcount == 0)
17159f1abe3dSAlan Cox 				faultcount = 1;
17169f1abe3dSAlan Cox 
17170d94caffSDavid Greenman 		} else {
17182c2f4413SJeff Roberson 			fs.prot &= ~VM_PROT_WRITE;
1719df8bae1dSRodney W. Grimes 		}
1720df8bae1dSRodney W. Grimes 	}
1721df8bae1dSRodney W. Grimes 
1722df8bae1dSRodney W. Grimes 	/*
17230d94caffSDavid Greenman 	 * We must verify that the maps have not changed since our last
17240d94caffSDavid Greenman 	 * lookup.
1725df8bae1dSRodney W. Grimes 	 */
172619dc5607STor Egge 	if (!fs.lookup_still_valid) {
1727f1b642c2SMark Johnston 		rv = vm_fault_relookup(&fs);
1728f1b642c2SMark Johnston 		if (rv != KERN_SUCCESS) {
17290a310c94SMateusz Guzik 			vm_fault_deallocate(&fs);
1730f1b642c2SMark Johnston 			if (rv == KERN_RESTART)
173119dc5607STor Egge 				goto RetryFault;
1732f1b642c2SMark Johnston 			return (rv);
1733df8bae1dSRodney W. Grimes 		}
173419dc5607STor Egge 	}
17351e40fe41SJeff Roberson 	VM_OBJECT_ASSERT_UNLOCKED(fs.object);
1736381b7242SAlan Cox 
1737d2bf64c3SKonstantin Belousov 	/*
1738381b7242SAlan Cox 	 * If the page was filled by a pager, save the virtual address that
1739381b7242SAlan Cox 	 * should be faulted on next under a sequential access pattern to the
1740381b7242SAlan Cox 	 * map entry.  A read lock on the map suffices to update this address
1741381b7242SAlan Cox 	 * safely.
1742d2bf64c3SKonstantin Belousov 	 */
17435758fe71SAlan Cox 	if (hardfault)
1744381b7242SAlan Cox 		fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE;
1745d2bf64c3SKonstantin Belousov 
17464221e284SAlan Cox 	/*
174778cfe1f7SAlan Cox 	 * Page must be completely valid or it is not fit to
17484221e284SAlan Cox 	 * map into user space.  vm_pager_get_pages() ensures this.
17494221e284SAlan Cox 	 */
17501e40fe41SJeff Roberson 	vm_page_assert_xbusied(fs.m);
17510012f373SJeff Roberson 	KASSERT(vm_page_all_valid(fs.m),
175278cfe1f7SAlan Cox 	    ("vm_fault: page %p partially invalid", fs.m));
17531e40fe41SJeff Roberson 
17542c2f4413SJeff Roberson 	vm_fault_dirty(&fs, fs.m);
1755cbfbaad8SAlan Cox 
175686735996SAlan Cox 	/*
175786735996SAlan Cox 	 * Put this page into the physical map.  We had to do the unlock above
175886735996SAlan Cox 	 * because pmap_enter() may sleep.  We don't put the page
175986735996SAlan Cox 	 * back on the active queue until later so that the pageout daemon
176086735996SAlan Cox 	 * won't find it (yet).
176186735996SAlan Cox 	 */
17622c2f4413SJeff Roberson 	pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot,
17632c2f4413SJeff Roberson 	    fs.fault_type | (fs.wired ? PMAP_ENTER_WIRED : 0), 0);
17642c2f4413SJeff Roberson 	if (faultcount != 1 && (fs.fault_flags & VM_FAULT_WIRE) == 0 &&
17652c2f4413SJeff Roberson 	    fs.wired == 0)
1766b0cd2017SGleb Smirnoff 		vm_fault_prefault(&fs, vaddr,
1767b0cd2017SGleb Smirnoff 		    faultcount > 0 ? behind : PFBAK,
1768a7163bb9SKonstantin Belousov 		    faultcount > 0 ? ahead : PFFOR, false);
1769ff97964aSJohn Dyson 
1770df8bae1dSRodney W. Grimes 	/*
17710d94caffSDavid Greenman 	 * If the page is not wired down, then put it where the pageout daemon
17720d94caffSDavid Greenman 	 * can find it.
1773df8bae1dSRodney W. Grimes 	 */
17742c2f4413SJeff Roberson 	if ((fs.fault_flags & VM_FAULT_WIRE) != 0)
17754866e085SJohn Dyson 		vm_page_wire(fs.m);
17769f5632e6SMark Johnston 	else
17774866e085SJohn Dyson 		vm_page_activate(fs.m);
17782c2f4413SJeff Roberson 	if (fs.m_hold != NULL) {
17792c2f4413SJeff Roberson 		(*fs.m_hold) = fs.m;
1780eeacb3b0SMark Johnston 		vm_page_wire(fs.m);
1781acd11c74SAlan Cox 	}
1782c7aebda8SAttilio Rao 	vm_page_xunbusy(fs.m);
17834bf95d00SJeff Roberson 	fs.m = NULL;
1784eeec6babSJohn Baldwin 
1785eebf3286SAlan Cox 	/*
1786eebf3286SAlan Cox 	 * Unlock everything, and return
1787eebf3286SAlan Cox 	 */
17880a310c94SMateusz Guzik 	vm_fault_deallocate(&fs);
1789b3a01bdfSAndrey Zonov 	if (hardfault) {
179083c9dea1SGleb Smirnoff 		VM_CNT_INC(v_io_faults);
17911c4bcd05SJeff Roberson 		curthread->td_ru.ru_majflt++;
1792ae34b6ffSEdward Tomasz Napierala #ifdef RACCT
1793ae34b6ffSEdward Tomasz Napierala 		if (racct_enable && fs.object->type == OBJT_VNODE) {
1794ae34b6ffSEdward Tomasz Napierala 			PROC_LOCK(curproc);
17952c2f4413SJeff Roberson 			if ((fs.fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1796ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_WRITEBPS,
1797ae34b6ffSEdward Tomasz Napierala 				    PAGE_SIZE + behind * PAGE_SIZE);
1798ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_WRITEIOPS, 1);
1799ae34b6ffSEdward Tomasz Napierala 			} else {
1800ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_READBPS,
1801ae34b6ffSEdward Tomasz Napierala 				    PAGE_SIZE + ahead * PAGE_SIZE);
1802ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_READIOPS, 1);
1803ae34b6ffSEdward Tomasz Napierala 			}
1804ae34b6ffSEdward Tomasz Napierala 			PROC_UNLOCK(curproc);
1805ae34b6ffSEdward Tomasz Napierala 		}
1806ae34b6ffSEdward Tomasz Napierala #endif
1807b3a01bdfSAndrey Zonov 	} else
18081c4bcd05SJeff Roberson 		curthread->td_ru.ru_minflt++;
1809df8bae1dSRodney W. Grimes 
1810df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1811df8bae1dSRodney W. Grimes }
1812df8bae1dSRodney W. Grimes 
1813df8bae1dSRodney W. Grimes /*
1814a8b0f100SAlan Cox  * Speed up the reclamation of pages that precede the faulting pindex within
1815a8b0f100SAlan Cox  * the first object of the shadow chain.  Essentially, perform the equivalent
1816a8b0f100SAlan Cox  * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes
1817a8b0f100SAlan Cox  * the faulting pindex by the cluster size when the pages read by vm_fault()
1818a8b0f100SAlan Cox  * cross a cluster-size boundary.  The cluster size is the greater of the
1819a8b0f100SAlan Cox  * smallest superpage size and VM_FAULT_DONTNEED_MIN.
1820a8b0f100SAlan Cox  *
1821a8b0f100SAlan Cox  * When "fs->first_object" is a shadow object, the pages in the backing object
1822a8b0f100SAlan Cox  * that precede the faulting pindex are deactivated by vm_fault().  So, this
1823a8b0f100SAlan Cox  * function must only be concerned with pages in the first object.
182413458803SAlan Cox  */
182513458803SAlan Cox static void
1826a8b0f100SAlan Cox vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead)
182713458803SAlan Cox {
1828a8b0f100SAlan Cox 	vm_map_entry_t entry;
182940cbcb99SJohn Baldwin 	vm_object_t first_object;
1830a8b0f100SAlan Cox 	vm_offset_t end, start;
1831a8b0f100SAlan Cox 	vm_page_t m, m_next;
1832a8b0f100SAlan Cox 	vm_pindex_t pend, pstart;
1833a8b0f100SAlan Cox 	vm_size_t size;
183413458803SAlan Cox 
183540cbcb99SJohn Baldwin 	VM_OBJECT_ASSERT_UNLOCKED(fs->object);
183613458803SAlan Cox 	first_object = fs->first_object;
1837a8b0f100SAlan Cox 	/* Neither fictitious nor unmanaged pages can be reclaimed. */
183828634820SAlan Cox 	if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
18391e40fe41SJeff Roberson 		VM_OBJECT_RLOCK(first_object);
1840a8b0f100SAlan Cox 		size = VM_FAULT_DONTNEED_MIN;
1841a8b0f100SAlan Cox 		if (MAXPAGESIZES > 1 && size < pagesizes[1])
1842a8b0f100SAlan Cox 			size = pagesizes[1];
1843a8b0f100SAlan Cox 		end = rounddown2(vaddr, size);
1844a8b0f100SAlan Cox 		if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) &&
1845a8b0f100SAlan Cox 		    (entry = fs->entry)->start < end) {
1846a8b0f100SAlan Cox 			if (end - entry->start < size)
1847a8b0f100SAlan Cox 				start = entry->start;
184813458803SAlan Cox 			else
1849a8b0f100SAlan Cox 				start = end - size;
1850a8b0f100SAlan Cox 			pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED);
1851a8b0f100SAlan Cox 			pstart = OFF_TO_IDX(entry->offset) + atop(start -
1852a8b0f100SAlan Cox 			    entry->start);
1853a8b0f100SAlan Cox 			m_next = vm_page_find_least(first_object, pstart);
1854a8b0f100SAlan Cox 			pend = OFF_TO_IDX(entry->offset) + atop(end -
1855a8b0f100SAlan Cox 			    entry->start);
1856a8b0f100SAlan Cox 			while ((m = m_next) != NULL && m->pindex < pend) {
1857a8b0f100SAlan Cox 				m_next = TAILQ_NEXT(m, listq);
18580012f373SJeff Roberson 				if (!vm_page_all_valid(m) ||
1859a8b0f100SAlan Cox 				    vm_page_busied(m))
186013458803SAlan Cox 					continue;
1861d8015db3SAlan Cox 
1862d8015db3SAlan Cox 				/*
1863d8015db3SAlan Cox 				 * Don't clear PGA_REFERENCED, since it would
1864d8015db3SAlan Cox 				 * likely represent a reference by a different
1865d8015db3SAlan Cox 				 * process.
1866d8015db3SAlan Cox 				 *
1867d8015db3SAlan Cox 				 * Typically, at this point, prefetched pages
1868d8015db3SAlan Cox 				 * are still in the inactive queue.  Only
1869d8015db3SAlan Cox 				 * pages that triggered page faults are in the
18709f5632e6SMark Johnston 				 * active queue.  The test for whether the page
18719f5632e6SMark Johnston 				 * is in the inactive queue is racy; in the
18729f5632e6SMark Johnston 				 * worst case we will requeue the page
18739f5632e6SMark Johnston 				 * unnecessarily.
1874d8015db3SAlan Cox 				 */
18750eb50f9cSMark Johnston 				if (!vm_page_inactive(m))
1876d8015db3SAlan Cox 					vm_page_deactivate(m);
187713458803SAlan Cox 			}
187813458803SAlan Cox 		}
18791e40fe41SJeff Roberson 		VM_OBJECT_RUNLOCK(first_object);
1880a8b0f100SAlan Cox 	}
188113458803SAlan Cox }
188213458803SAlan Cox 
188313458803SAlan Cox /*
1884566526a9SAlan Cox  * vm_fault_prefault provides a quick way of clustering
1885566526a9SAlan Cox  * pagefaults into a processes address space.  It is a "cousin"
1886566526a9SAlan Cox  * of vm_map_pmap_enter, except it runs at page fault time instead
1887566526a9SAlan Cox  * of mmap time.
1888566526a9SAlan Cox  */
1889566526a9SAlan Cox static void
189063281952SAlan Cox vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
1891a7163bb9SKonstantin Belousov     int backward, int forward, bool obj_locked)
1892566526a9SAlan Cox {
189363281952SAlan Cox 	pmap_t pmap;
189463281952SAlan Cox 	vm_map_entry_t entry;
189563281952SAlan Cox 	vm_object_t backing_object, lobject;
1896566526a9SAlan Cox 	vm_offset_t addr, starta;
1897566526a9SAlan Cox 	vm_pindex_t pindex;
18982053c127SStephan Uphoff 	vm_page_t m;
1899b0cd2017SGleb Smirnoff 	int i;
1900566526a9SAlan Cox 
190163281952SAlan Cox 	pmap = fs->map->pmap;
1902950d5f7aSAlan Cox 	if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1903566526a9SAlan Cox 		return;
1904566526a9SAlan Cox 
190563281952SAlan Cox 	entry = fs->entry;
1906566526a9SAlan Cox 
190763cdcaaeSKonstantin Belousov 	if (addra < backward * PAGE_SIZE) {
1908566526a9SAlan Cox 		starta = entry->start;
190963cdcaaeSKonstantin Belousov 	} else {
191063cdcaaeSKonstantin Belousov 		starta = addra - backward * PAGE_SIZE;
191163cdcaaeSKonstantin Belousov 		if (starta < entry->start)
191263cdcaaeSKonstantin Belousov 			starta = entry->start;
1913566526a9SAlan Cox 	}
1914566526a9SAlan Cox 
191563281952SAlan Cox 	/*
191663281952SAlan Cox 	 * Generate the sequence of virtual addresses that are candidates for
191763281952SAlan Cox 	 * prefaulting in an outward spiral from the faulting virtual address,
191863281952SAlan Cox 	 * "addra".  Specifically, the sequence is "addra - PAGE_SIZE", "addra
191963281952SAlan Cox 	 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
192063281952SAlan Cox 	 * If the candidate address doesn't have a backing physical page, then
192163281952SAlan Cox 	 * the loop immediately terminates.
192263281952SAlan Cox 	 */
192363281952SAlan Cox 	for (i = 0; i < 2 * imax(backward, forward); i++) {
192463281952SAlan Cox 		addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
192563281952SAlan Cox 		    PAGE_SIZE);
192663281952SAlan Cox 		if (addr > addra + forward * PAGE_SIZE)
1927566526a9SAlan Cox 			addr = 0;
1928566526a9SAlan Cox 
1929566526a9SAlan Cox 		if (addr < starta || addr >= entry->end)
1930566526a9SAlan Cox 			continue;
1931566526a9SAlan Cox 
1932566526a9SAlan Cox 		if (!pmap_is_prefaultable(pmap, addr))
1933566526a9SAlan Cox 			continue;
1934566526a9SAlan Cox 
1935566526a9SAlan Cox 		pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
193663281952SAlan Cox 		lobject = entry->object.vm_object;
1937a7163bb9SKonstantin Belousov 		if (!obj_locked)
1938c141ae7fSAlan Cox 			VM_OBJECT_RLOCK(lobject);
1939566526a9SAlan Cox 		while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
19400a310c94SMateusz Guzik 		    !vm_fault_object_needs_getpages(lobject) &&
1941566526a9SAlan Cox 		    (backing_object = lobject->backing_object) != NULL) {
194236930fc9SAlan Cox 			KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
194336930fc9SAlan Cox 			    0, ("vm_fault_prefault: unaligned object offset"));
1944566526a9SAlan Cox 			pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1945c141ae7fSAlan Cox 			VM_OBJECT_RLOCK(backing_object);
1946a7163bb9SKonstantin Belousov 			if (!obj_locked || lobject != entry->object.vm_object)
1947c141ae7fSAlan Cox 				VM_OBJECT_RUNLOCK(lobject);
1948566526a9SAlan Cox 			lobject = backing_object;
1949566526a9SAlan Cox 		}
1950cbfbaad8SAlan Cox 		if (m == NULL) {
1951a7163bb9SKonstantin Belousov 			if (!obj_locked || lobject != entry->object.vm_object)
1952c141ae7fSAlan Cox 				VM_OBJECT_RUNLOCK(lobject);
1953566526a9SAlan Cox 			break;
1954cbfbaad8SAlan Cox 		}
19550012f373SJeff Roberson 		if (vm_page_all_valid(m) &&
19563c4a2440SAlan Cox 		    (m->flags & PG_FICTITIOUS) == 0)
19577bfda801SAlan Cox 			pmap_enter_quick(pmap, addr, m, entry->protection);
1958a7163bb9SKonstantin Belousov 		if (!obj_locked || lobject != entry->object.vm_object)
1959c141ae7fSAlan Cox 			VM_OBJECT_RUNLOCK(lobject);
1960566526a9SAlan Cox 	}
1961566526a9SAlan Cox }
1962566526a9SAlan Cox 
1963566526a9SAlan Cox /*
196482de724fSAlan Cox  * Hold each of the physical pages that are mapped by the specified range of
196582de724fSAlan Cox  * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
196682de724fSAlan Cox  * and allow the specified types of access, "prot".  If all of the implied
196782de724fSAlan Cox  * pages are successfully held, then the number of held pages is returned
196882de724fSAlan Cox  * together with pointers to those pages in the array "ma".  However, if any
196982de724fSAlan Cox  * of the pages cannot be held, -1 is returned.
197082de724fSAlan Cox  */
197182de724fSAlan Cox int
197282de724fSAlan Cox vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
197382de724fSAlan Cox     vm_prot_t prot, vm_page_t *ma, int max_count)
197482de724fSAlan Cox {
197582de724fSAlan Cox 	vm_offset_t end, va;
197682de724fSAlan Cox 	vm_page_t *mp;
19777e14088dSKonstantin Belousov 	int count;
197882de724fSAlan Cox 	boolean_t pmap_failed;
197982de724fSAlan Cox 
1980af32c419SKonstantin Belousov 	if (len == 0)
1981af32c419SKonstantin Belousov 		return (0);
198282de724fSAlan Cox 	end = round_page(addr + len);
198382de724fSAlan Cox 	addr = trunc_page(addr);
198482de724fSAlan Cox 
19850f1e6ec5SMark Johnston 	if (!vm_map_range_valid(map, addr, end))
198682de724fSAlan Cox 		return (-1);
198782de724fSAlan Cox 
19887e14088dSKonstantin Belousov 	if (atop(end - addr) > max_count)
198982de724fSAlan Cox 		panic("vm_fault_quick_hold_pages: count > max_count");
19907e14088dSKonstantin Belousov 	count = atop(end - addr);
199182de724fSAlan Cox 
199282de724fSAlan Cox 	/*
199382de724fSAlan Cox 	 * Most likely, the physical pages are resident in the pmap, so it is
199482de724fSAlan Cox 	 * faster to try pmap_extract_and_hold() first.
199582de724fSAlan Cox 	 */
199682de724fSAlan Cox 	pmap_failed = FALSE;
199782de724fSAlan Cox 	for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
199882de724fSAlan Cox 		*mp = pmap_extract_and_hold(map->pmap, va, prot);
199982de724fSAlan Cox 		if (*mp == NULL)
200082de724fSAlan Cox 			pmap_failed = TRUE;
200182de724fSAlan Cox 		else if ((prot & VM_PROT_WRITE) != 0 &&
2002a5dbab54SAlan Cox 		    (*mp)->dirty != VM_PAGE_BITS_ALL) {
200382de724fSAlan Cox 			/*
200482de724fSAlan Cox 			 * Explicitly dirty the physical page.  Otherwise, the
200582de724fSAlan Cox 			 * caller's changes may go unnoticed because they are
200682de724fSAlan Cox 			 * performed through an unmanaged mapping or by a DMA
200782de724fSAlan Cox 			 * operation.
20083c76db4cSAlan Cox 			 *
2009abb9b935SKonstantin Belousov 			 * The object lock is not held here.
2010abb9b935SKonstantin Belousov 			 * See vm_page_clear_dirty_mask().
201182de724fSAlan Cox 			 */
20123c76db4cSAlan Cox 			vm_page_dirty(*mp);
201382de724fSAlan Cox 		}
201482de724fSAlan Cox 	}
201582de724fSAlan Cox 	if (pmap_failed) {
201682de724fSAlan Cox 		/*
201782de724fSAlan Cox 		 * One or more pages could not be held by the pmap.  Either no
201882de724fSAlan Cox 		 * page was mapped at the specified virtual address or that
201982de724fSAlan Cox 		 * mapping had insufficient permissions.  Attempt to fault in
202082de724fSAlan Cox 		 * and hold these pages.
20218ec533d3SKonstantin Belousov 		 *
20228ec533d3SKonstantin Belousov 		 * If vm_fault_disable_pagefaults() was called,
20238ec533d3SKonstantin Belousov 		 * i.e., TDP_NOFAULTING is set, we must not sleep nor
20248ec533d3SKonstantin Belousov 		 * acquire MD VM locks, which means we must not call
2025df08823dSKonstantin Belousov 		 * vm_fault().  Some (out of tree) callers mark
20268ec533d3SKonstantin Belousov 		 * too wide a code area with vm_fault_disable_pagefaults()
20278ec533d3SKonstantin Belousov 		 * already, use the VM_PROT_QUICK_NOFAULT flag to request
20288ec533d3SKonstantin Belousov 		 * the proper behaviour explicitly.
202982de724fSAlan Cox 		 */
20308ec533d3SKonstantin Belousov 		if ((prot & VM_PROT_QUICK_NOFAULT) != 0 &&
20318ec533d3SKonstantin Belousov 		    (curthread->td_pflags & TDP_NOFAULTING) != 0)
20328ec533d3SKonstantin Belousov 			goto error;
203382de724fSAlan Cox 		for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
2034df08823dSKonstantin Belousov 			if (*mp == NULL && vm_fault(map, va, prot,
203582de724fSAlan Cox 			    VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
203682de724fSAlan Cox 				goto error;
203782de724fSAlan Cox 	}
203882de724fSAlan Cox 	return (count);
203982de724fSAlan Cox error:
204082de724fSAlan Cox 	for (mp = ma; mp < ma + count; mp++)
2041fee2a2faSMark Johnston 		if (*mp != NULL)
2042fee2a2faSMark Johnston 			vm_page_unwire(*mp, PQ_INACTIVE);
204382de724fSAlan Cox 	return (-1);
204482de724fSAlan Cox }
204582de724fSAlan Cox 
204682de724fSAlan Cox /*
2047df8bae1dSRodney W. Grimes  *	Routine:
2048df8bae1dSRodney W. Grimes  *		vm_fault_copy_entry
2049df8bae1dSRodney W. Grimes  *	Function:
2050b57be759SMark Johnston  *		Create new object backing dst_entry with private copy of all
2051b57be759SMark Johnston  *		underlying pages. When src_entry is equal to dst_entry, function
2052b57be759SMark Johnston  *		implements COW for wired-down map entry. Otherwise, it forks
2053b57be759SMark Johnston  *		wired entry into dst_map.
2054df8bae1dSRodney W. Grimes  *
2055df8bae1dSRodney W. Grimes  *	In/out conditions:
2056df8bae1dSRodney W. Grimes  *		The source and destination maps must be locked for write.
2057df8bae1dSRodney W. Grimes  *		The source map entry must be wired down (or be a sharing map
2058df8bae1dSRodney W. Grimes  *		entry corresponding to a main map entry that is wired down).
2059df8bae1dSRodney W. Grimes  */
206026f9a767SRodney W. Grimes void
2061b57be759SMark Johnston vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map __unused,
2062121fd461SKonstantin Belousov     vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
2063121fd461SKonstantin Belousov     vm_ooffset_t *fork_charge)
2064df8bae1dSRodney W. Grimes {
2065210a6886SKonstantin Belousov 	vm_object_t backing_object, dst_object, object, src_object;
20667afab86cSAlan Cox 	vm_pindex_t dst_pindex, pindex, src_pindex;
2067210a6886SKonstantin Belousov 	vm_prot_t access, prot;
2068df8bae1dSRodney W. Grimes 	vm_offset_t vaddr;
2069df8bae1dSRodney W. Grimes 	vm_page_t dst_m;
2070df8bae1dSRodney W. Grimes 	vm_page_t src_m;
2071b57be759SMark Johnston 	bool upgrade;
2072df8bae1dSRodney W. Grimes 
2073210a6886SKonstantin Belousov 	upgrade = src_entry == dst_entry;
2074b57be759SMark Johnston 	KASSERT(upgrade || dst_entry->object.vm_object == NULL,
2075b57be759SMark Johnston 	    ("vm_fault_copy_entry: vm_object not NULL"));
2076b57be759SMark Johnston 
2077b57be759SMark Johnston 	/*
2078b57be759SMark Johnston 	 * If not an upgrade, then enter the mappings in the pmap as
2079b57be759SMark Johnston 	 * read and/or execute accesses.  Otherwise, enter them as
2080b57be759SMark Johnston 	 * write accesses.
2081b57be759SMark Johnston 	 *
2082b57be759SMark Johnston 	 * A writeable large page mapping is only created if all of
2083b57be759SMark Johnston 	 * the constituent small page mappings are modified. Marking
2084b57be759SMark Johnston 	 * PTEs as modified on inception allows promotion to happen
2085b57be759SMark Johnston 	 * without taking potentially large number of soft faults.
2086b57be759SMark Johnston 	 */
20870973283dSKonstantin Belousov 	access = prot = dst_entry->protection;
2088b57be759SMark Johnston 	if (!upgrade)
2089b57be759SMark Johnston 		access &= ~VM_PROT_WRITE;
2090210a6886SKonstantin Belousov 
2091df8bae1dSRodney W. Grimes 	src_object = src_entry->object.vm_object;
20927afab86cSAlan Cox 	src_pindex = OFF_TO_IDX(src_entry->offset);
2093df8bae1dSRodney W. Grimes 
20940973283dSKonstantin Belousov 	if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
20950973283dSKonstantin Belousov 		dst_object = src_object;
20960973283dSKonstantin Belousov 		vm_object_reference(dst_object);
20970973283dSKonstantin Belousov 	} else {
2098df8bae1dSRodney W. Grimes 		/*
209967388836SKonstantin Belousov 		 * Create the top-level object for the destination entry.
210067388836SKonstantin Belousov 		 * Doesn't actually shadow anything - we copy the pages
210167388836SKonstantin Belousov 		 * directly.
2102df8bae1dSRodney W. Grimes 		 */
210367388836SKonstantin Belousov 		dst_object = vm_object_allocate_anon(atop(dst_entry->end -
210467388836SKonstantin Belousov 		    dst_entry->start), NULL, NULL, 0);
2105f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
2106f8a47341SAlan Cox 		dst_object->flags |= OBJ_COLORED;
2107f8a47341SAlan Cox 		dst_object->pg_color = atop(dst_entry->start);
2108f8a47341SAlan Cox #endif
2109a60d3db1SKonstantin Belousov 		dst_object->domain = src_object->domain;
2110a60d3db1SKonstantin Belousov 		dst_object->charge = dst_entry->end - dst_entry->start;
2111df8bae1dSRodney W. Grimes 
2112df8bae1dSRodney W. Grimes 		dst_entry->object.vm_object = dst_object;
2113df8bae1dSRodney W. Grimes 		dst_entry->offset = 0;
211478022527SKonstantin Belousov 		dst_entry->eflags &= ~MAP_ENTRY_VN_EXEC;
21150973283dSKonstantin Belousov 	}
2116b57be759SMark Johnston 
2117b57be759SMark Johnston 	VM_OBJECT_WLOCK(dst_object);
2118210a6886SKonstantin Belousov 	if (fork_charge != NULL) {
2119ef694c1aSEdward Tomasz Napierala 		KASSERT(dst_entry->cred == NULL,
2120121fd461SKonstantin Belousov 		    ("vm_fault_copy_entry: leaked swp charge"));
2121ef694c1aSEdward Tomasz Napierala 		dst_object->cred = curthread->td_ucred;
2122ef694c1aSEdward Tomasz Napierala 		crhold(dst_object->cred);
2123121fd461SKonstantin Belousov 		*fork_charge += dst_object->charge;
21240cb2610eSMark Johnston 	} else if ((dst_object->flags & OBJ_SWAP) != 0 &&
21259f25ab83SKonstantin Belousov 	    dst_object->cred == NULL) {
21260973283dSKonstantin Belousov 		KASSERT(dst_entry->cred != NULL, ("no cred for entry %p",
21270973283dSKonstantin Belousov 		    dst_entry));
2128ef694c1aSEdward Tomasz Napierala 		dst_object->cred = dst_entry->cred;
2129ef694c1aSEdward Tomasz Napierala 		dst_entry->cred = NULL;
2130210a6886SKonstantin Belousov 	}
21310973283dSKonstantin Belousov 
2132210a6886SKonstantin Belousov 	/*
2133ef45823eSKonstantin Belousov 	 * Loop through all of the virtual pages within the entry's
2134ef45823eSKonstantin Belousov 	 * range, copying each page from the source object to the
2135ef45823eSKonstantin Belousov 	 * destination object.  Since the source is wired, those pages
2136ef45823eSKonstantin Belousov 	 * must exist.  In contrast, the destination is pageable.
21376939b4d3SMark Johnston 	 * Since the destination object doesn't share any backing storage
2138ef45823eSKonstantin Belousov 	 * with the source object, all of its pages must be dirtied,
2139ef45823eSKonstantin Belousov 	 * regardless of whether they can be written.
2140df8bae1dSRodney W. Grimes 	 */
21417afab86cSAlan Cox 	for (vaddr = dst_entry->start, dst_pindex = 0;
2142df8bae1dSRodney W. Grimes 	    vaddr < dst_entry->end;
21437afab86cSAlan Cox 	    vaddr += PAGE_SIZE, dst_pindex++) {
21440973283dSKonstantin Belousov again:
2145df8bae1dSRodney W. Grimes 		/*
2146df8bae1dSRodney W. Grimes 		 * Find the page in the source object, and copy it in.
21474c74acf7SKonstantin Belousov 		 * Because the source is wired down, the page will be
21484c74acf7SKonstantin Belousov 		 * in memory.
2149df8bae1dSRodney W. Grimes 		 */
21500973283dSKonstantin Belousov 		if (src_object != dst_object)
215183b375eaSAttilio Rao 			VM_OBJECT_RLOCK(src_object);
2152c5b65a67SAlan Cox 		object = src_object;
21537afab86cSAlan Cox 		pindex = src_pindex + dst_pindex;
21547afab86cSAlan Cox 		while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
2155c5b65a67SAlan Cox 		    (backing_object = object->backing_object) != NULL) {
2156c5b65a67SAlan Cox 			/*
21574c74acf7SKonstantin Belousov 			 * Unless the source mapping is read-only or
21584c74acf7SKonstantin Belousov 			 * it is presently being upgraded from
21594c74acf7SKonstantin Belousov 			 * read-only, the first object in the shadow
21604c74acf7SKonstantin Belousov 			 * chain should provide all of the pages.  In
21614c74acf7SKonstantin Belousov 			 * other words, this loop body should never be
21624c74acf7SKonstantin Belousov 			 * executed when the source mapping is already
21634c74acf7SKonstantin Belousov 			 * read/write.
2164c5b65a67SAlan Cox 			 */
21654c74acf7SKonstantin Belousov 			KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 ||
21664c74acf7SKonstantin Belousov 			    upgrade,
21674c74acf7SKonstantin Belousov 			    ("vm_fault_copy_entry: main object missing page"));
21684c74acf7SKonstantin Belousov 
216983b375eaSAttilio Rao 			VM_OBJECT_RLOCK(backing_object);
2170c5b65a67SAlan Cox 			pindex += OFF_TO_IDX(object->backing_object_offset);
21710973283dSKonstantin Belousov 			if (object != dst_object)
217283b375eaSAttilio Rao 				VM_OBJECT_RUNLOCK(object);
2173c5b65a67SAlan Cox 			object = backing_object;
2174c5b65a67SAlan Cox 		}
21754c74acf7SKonstantin Belousov 		KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing"));
21760973283dSKonstantin Belousov 
21770973283dSKonstantin Belousov 		if (object != dst_object) {
21780973283dSKonstantin Belousov 			/*
21790973283dSKonstantin Belousov 			 * Allocate a page in the destination object.
21800973283dSKonstantin Belousov 			 */
21812602a2eaSKonstantin Belousov 			dst_m = vm_page_alloc(dst_object, (src_object ==
21822602a2eaSKonstantin Belousov 			    dst_object ? src_pindex : 0) + dst_pindex,
21832602a2eaSKonstantin Belousov 			    VM_ALLOC_NORMAL);
21840973283dSKonstantin Belousov 			if (dst_m == NULL) {
21850973283dSKonstantin Belousov 				VM_OBJECT_WUNLOCK(dst_object);
21860973283dSKonstantin Belousov 				VM_OBJECT_RUNLOCK(object);
21872c0f13aaSKonstantin Belousov 				vm_wait(dst_object);
2188c8f780e3SKonstantin Belousov 				VM_OBJECT_WLOCK(dst_object);
21890973283dSKonstantin Belousov 				goto again;
21900973283dSKonstantin Belousov 			}
21915c50e900SMark Johnston 
21925c50e900SMark Johnston 			/*
21935c50e900SMark Johnston 			 * See the comment in vm_fault_cow().
21945c50e900SMark Johnston 			 */
21955c50e900SMark Johnston 			if (src_object == dst_object &&
21965c50e900SMark Johnston 			    (object->flags & OBJ_ONEMAPPING) == 0)
21975c50e900SMark Johnston 				pmap_remove_all(src_m);
2198669890eaSAlan Cox 			pmap_copy_page(src_m, dst_m);
2199d0443e2bSMark Johnston 
2200d0443e2bSMark Johnston 			/*
2201d0443e2bSMark Johnston 			 * The object lock does not guarantee that "src_m" will
2202d0443e2bSMark Johnston 			 * transition from invalid to valid, but it does ensure
2203d0443e2bSMark Johnston 			 * that "src_m" will not transition from valid to
2204d0443e2bSMark Johnston 			 * invalid.
2205d0443e2bSMark Johnston 			 */
220645d72c7dSKonstantin Belousov 			dst_m->dirty = dst_m->valid = src_m->valid;
2207d0443e2bSMark Johnston 			VM_OBJECT_RUNLOCK(object);
22080973283dSKonstantin Belousov 		} else {
22090973283dSKonstantin Belousov 			dst_m = src_m;
221063e97555SJeff Roberson 			if (vm_page_busy_acquire(dst_m, VM_ALLOC_WAITFAIL) == 0)
22110973283dSKonstantin Belousov 				goto again;
221263e97555SJeff Roberson 			if (dst_m->pindex >= dst_object->size) {
2213c62637d6SKonstantin Belousov 				/*
2214c62637d6SKonstantin Belousov 				 * We are upgrading.  Index can occur
2215c62637d6SKonstantin Belousov 				 * out of bounds if the object type is
2216c62637d6SKonstantin Belousov 				 * vnode and the file was truncated.
2217c62637d6SKonstantin Belousov 				 */
221863e97555SJeff Roberson 				vm_page_xunbusy(dst_m);
2219c62637d6SKonstantin Belousov 				break;
222063e97555SJeff Roberson 			}
22210973283dSKonstantin Belousov 		}
2222df8bae1dSRodney W. Grimes 
2223df8bae1dSRodney W. Grimes 		/*
2224210a6886SKonstantin Belousov 		 * Enter it in the pmap. If a wired, copy-on-write
2225210a6886SKonstantin Belousov 		 * mapping is being replaced by a write-enabled
2226210a6886SKonstantin Belousov 		 * mapping, then wire that new mapping.
222745d72c7dSKonstantin Belousov 		 *
222845d72c7dSKonstantin Belousov 		 * The page can be invalid if the user called
222945d72c7dSKonstantin Belousov 		 * msync(MS_INVALIDATE) or truncated the backing vnode
223045d72c7dSKonstantin Belousov 		 * or shared memory object.  In this case, do not
223145d72c7dSKonstantin Belousov 		 * insert it into pmap, but still do the copy so that
223245d72c7dSKonstantin Belousov 		 * all copies of the wired map entry have similar
223345d72c7dSKonstantin Belousov 		 * backing pages.
2234df8bae1dSRodney W. Grimes 		 */
22350012f373SJeff Roberson 		if (vm_page_all_valid(dst_m)) {
22361f88394bSMark Johnston 			VM_OBJECT_WUNLOCK(dst_object);
223739ffa8c1SKonstantin Belousov 			pmap_enter(dst_map->pmap, vaddr, dst_m, prot,
223839ffa8c1SKonstantin Belousov 			    access | (upgrade ? PMAP_ENTER_WIRED : 0), 0);
22391f88394bSMark Johnston 			VM_OBJECT_WLOCK(dst_object);
224045d72c7dSKonstantin Belousov 		}
2241df8bae1dSRodney W. Grimes 
2242df8bae1dSRodney W. Grimes 		/*
2243df8bae1dSRodney W. Grimes 		 * Mark it no longer busy, and put it on the active list.
2244df8bae1dSRodney W. Grimes 		 */
2245210a6886SKonstantin Belousov 		if (upgrade) {
22460973283dSKonstantin Belousov 			if (src_m != dst_m) {
22473ae10f74SAttilio Rao 				vm_page_unwire(src_m, PQ_INACTIVE);
2248210a6886SKonstantin Belousov 				vm_page_wire(dst_m);
22492965a453SKip Macy 			} else {
2250d842aa51SMark Johnston 				KASSERT(vm_page_wired(dst_m),
22510973283dSKonstantin Belousov 				    ("dst_m %p is not wired", dst_m));
22520973283dSKonstantin Belousov 			}
22530973283dSKonstantin Belousov 		} else {
2254df8bae1dSRodney W. Grimes 			vm_page_activate(dst_m);
22552965a453SKip Macy 		}
2256c7aebda8SAttilio Rao 		vm_page_xunbusy(dst_m);
2257df8bae1dSRodney W. Grimes 	}
225889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(dst_object);
2259210a6886SKonstantin Belousov 	if (upgrade) {
2260210a6886SKonstantin Belousov 		dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
2261210a6886SKonstantin Belousov 		vm_object_deallocate(src_object);
2262210a6886SKonstantin Belousov 	}
2263df8bae1dSRodney W. Grimes }
226426f9a767SRodney W. Grimes 
22655730afc9SAlan Cox /*
22665730afc9SAlan Cox  * Block entry into the machine-independent layer's page fault handler by
22675730afc9SAlan Cox  * the calling thread.  Subsequent calls to vm_fault() by that thread will
22685730afc9SAlan Cox  * return KERN_PROTECTION_FAILURE.  Enable machine-dependent handling of
22695730afc9SAlan Cox  * spurious page faults.
22705730afc9SAlan Cox  */
22712801687dSKonstantin Belousov int
22722801687dSKonstantin Belousov vm_fault_disable_pagefaults(void)
22732801687dSKonstantin Belousov {
22742801687dSKonstantin Belousov 
22755730afc9SAlan Cox 	return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
22762801687dSKonstantin Belousov }
22772801687dSKonstantin Belousov 
22782801687dSKonstantin Belousov void
22792801687dSKonstantin Belousov vm_fault_enable_pagefaults(int save)
22802801687dSKonstantin Belousov {
22812801687dSKonstantin Belousov 
22822801687dSKonstantin Belousov 	curthread_pflags_restore(save);
22832801687dSKonstantin Belousov }
2284