xref: /freebsd/sys/vm/vm_fault.c (revision 16b0c092253b1856e624067d842a2148e3dfaed6)
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>
8826f9a767SRodney W. Grimes #include <sys/proc.h>
89ae34b6ffSEdward Tomasz Napierala #include <sys/racct.h>
9011b57401SHans Petter Selasky #include <sys/refcount.h>
9126f9a767SRodney W. Grimes #include <sys/resourcevar.h>
9289f6b863SAttilio Rao #include <sys/rwlock.h>
93df08823dSKonstantin Belousov #include <sys/signalvar.h>
9423955314SAlfred Perlstein #include <sys/sysctl.h>
95df08823dSKonstantin Belousov #include <sys/sysent.h>
964edf4a58SJohn Baldwin #include <sys/vmmeter.h>
974edf4a58SJohn Baldwin #include <sys/vnode.h>
9835818d2eSJohn Baldwin #ifdef KTRACE
9935818d2eSJohn Baldwin #include <sys/ktrace.h>
10035818d2eSJohn Baldwin #endif
101df8bae1dSRodney W. Grimes 
102df8bae1dSRodney W. Grimes #include <vm/vm.h>
103efeaf95aSDavid Greenman #include <vm/vm_param.h>
104efeaf95aSDavid Greenman #include <vm/pmap.h>
105efeaf95aSDavid Greenman #include <vm/vm_map.h>
106efeaf95aSDavid Greenman #include <vm/vm_object.h>
107df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
108df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
109a83c285cSDavid Greenman #include <vm/vm_kern.h>
11024a1cce3SDavid Greenman #include <vm/vm_pager.h>
111efeaf95aSDavid Greenman #include <vm/vm_extern.h>
112dfdf9abdSAlan Cox #include <vm/vm_reserv.h>
113df8bae1dSRodney W. Grimes 
114566526a9SAlan Cox #define PFBAK 4
115566526a9SAlan Cox #define PFFOR 4
116566526a9SAlan Cox 
1175268042bSAlan Cox #define	VM_FAULT_READ_DEFAULT	(1 + VM_FAULT_READ_AHEAD_INIT)
11813458803SAlan Cox #define	VM_FAULT_READ_MAX	(1 + VM_FAULT_READ_AHEAD_MAX)
119a8b0f100SAlan Cox 
120a8b0f100SAlan Cox #define	VM_FAULT_DONTNEED_MIN	1048576
12126f9a767SRodney W. Grimes 
1224866e085SJohn Dyson struct faultstate {
1234866e085SJohn Dyson 	vm_page_t m;
1244866e085SJohn Dyson 	vm_object_t object;
1254866e085SJohn Dyson 	vm_pindex_t pindex;
1264866e085SJohn Dyson 	vm_page_t first_m;
1274866e085SJohn Dyson 	vm_object_t	first_object;
1284866e085SJohn Dyson 	vm_pindex_t first_pindex;
1294866e085SJohn Dyson 	vm_map_t map;
1304866e085SJohn Dyson 	vm_map_entry_t entry;
131dc5401d2SKonstantin Belousov 	int map_generation;
132cd8a6fe8SAlan Cox 	bool lookup_still_valid;
1334866e085SJohn Dyson 	struct vnode *vp;
1344866e085SJohn Dyson };
1354866e085SJohn Dyson 
136a8b0f100SAlan Cox static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr,
137a8b0f100SAlan Cox 	    int ahead);
13863281952SAlan Cox static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
139a7163bb9SKonstantin Belousov 	    int backward, int forward, bool obj_locked);
14013458803SAlan Cox 
141245139c6SKonstantin Belousov static int vm_pfault_oom_attempts = 3;
142245139c6SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, pfault_oom_attempts, CTLFLAG_RWTUN,
143245139c6SKonstantin Belousov     &vm_pfault_oom_attempts, 0,
144245139c6SKonstantin Belousov     "Number of page allocation attempts in page fault handler before it "
145245139c6SKonstantin Belousov     "triggers OOM handling");
146245139c6SKonstantin Belousov 
147245139c6SKonstantin Belousov static int vm_pfault_oom_wait = 10;
148245139c6SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, pfault_oom_wait, CTLFLAG_RWTUN,
149245139c6SKonstantin Belousov     &vm_pfault_oom_wait, 0,
150245139c6SKonstantin Belousov     "Number of seconds to wait for free pages before retrying "
151245139c6SKonstantin Belousov     "the page fault handler");
152245139c6SKonstantin Belousov 
15362a59e8fSWarner Losh static inline void
1544866e085SJohn Dyson release_page(struct faultstate *fs)
1554866e085SJohn Dyson {
1560d0be82aSKonstantin Belousov 
157c7aebda8SAttilio Rao 	vm_page_xunbusy(fs->m);
158e8bcf696SMark Johnston 	vm_page_lock(fs->m);
1594866e085SJohn Dyson 	vm_page_deactivate(fs->m);
160e8bcf696SMark Johnston 	vm_page_unlock(fs->m);
1614866e085SJohn Dyson 	fs->m = NULL;
1624866e085SJohn Dyson }
1634866e085SJohn Dyson 
16462a59e8fSWarner Losh static inline void
1654866e085SJohn Dyson unlock_map(struct faultstate *fs)
1664866e085SJohn Dyson {
1670d0be82aSKonstantin Belousov 
16825adb370SBrian Feldman 	if (fs->lookup_still_valid) {
1694866e085SJohn Dyson 		vm_map_lookup_done(fs->map, fs->entry);
170cd8a6fe8SAlan Cox 		fs->lookup_still_valid = false;
1714866e085SJohn Dyson 	}
1724866e085SJohn Dyson }
1734866e085SJohn Dyson 
1744866e085SJohn Dyson static void
175cfabea3dSKonstantin Belousov unlock_vp(struct faultstate *fs)
176cfabea3dSKonstantin Belousov {
177cfabea3dSKonstantin Belousov 
178cfabea3dSKonstantin Belousov 	if (fs->vp != NULL) {
179cfabea3dSKonstantin Belousov 		vput(fs->vp);
180cfabea3dSKonstantin Belousov 		fs->vp = NULL;
181cfabea3dSKonstantin Belousov 	}
182cfabea3dSKonstantin Belousov }
183cfabea3dSKonstantin Belousov 
184cfabea3dSKonstantin Belousov static void
185a51b0840SAlan Cox unlock_and_deallocate(struct faultstate *fs)
1864866e085SJohn Dyson {
187f29ba63eSAlan Cox 
1884866e085SJohn Dyson 	vm_object_pip_wakeup(fs->object);
18989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(fs->object);
1904866e085SJohn Dyson 	if (fs->object != fs->first_object) {
19189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(fs->first_object);
1924866e085SJohn Dyson 		vm_page_free(fs->first_m);
1934866e085SJohn Dyson 		vm_object_pip_wakeup(fs->first_object);
19489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(fs->first_object);
1954866e085SJohn Dyson 		fs->first_m = NULL;
1964866e085SJohn Dyson 	}
1974866e085SJohn Dyson 	vm_object_deallocate(fs->first_object);
1984866e085SJohn Dyson 	unlock_map(fs);
199cfabea3dSKonstantin Belousov 	unlock_vp(fs);
2004866e085SJohn Dyson }
2014866e085SJohn Dyson 
202a36f5532SKonstantin Belousov static void
203a36f5532SKonstantin Belousov vm_fault_dirty(vm_map_entry_t entry, vm_page_t m, vm_prot_t prot,
204e26236e9SKonstantin Belousov     vm_prot_t fault_type, int fault_flags, bool set_wd)
205a36f5532SKonstantin Belousov {
206e26236e9SKonstantin Belousov 	bool need_dirty;
207a36f5532SKonstantin Belousov 
208a36f5532SKonstantin Belousov 	if (((prot & VM_PROT_WRITE) == 0 &&
209a36f5532SKonstantin Belousov 	    (fault_flags & VM_FAULT_DIRTY) == 0) ||
210a36f5532SKonstantin Belousov 	    (m->oflags & VPO_UNMANAGED) != 0)
211a36f5532SKonstantin Belousov 		return;
212a36f5532SKonstantin Belousov 
213a36f5532SKonstantin Belousov 	VM_OBJECT_ASSERT_LOCKED(m->object);
2140012f373SJeff Roberson 	VM_PAGE_OBJECT_BUSY_ASSERT(m);
215a36f5532SKonstantin Belousov 
216a36f5532SKonstantin Belousov 	need_dirty = ((fault_type & VM_PROT_WRITE) != 0 &&
2176a875bf9SKonstantin Belousov 	    (fault_flags & VM_FAULT_WIRE) == 0) ||
218a36f5532SKonstantin Belousov 	    (fault_flags & VM_FAULT_DIRTY) != 0;
219a36f5532SKonstantin Belousov 
220a36f5532SKonstantin Belousov 	if (set_wd)
221a36f5532SKonstantin Belousov 		vm_object_set_writeable_dirty(m->object);
222a36f5532SKonstantin Belousov 	else
223a36f5532SKonstantin Belousov 		/*
224a36f5532SKonstantin Belousov 		 * If two callers of vm_fault_dirty() with set_wd ==
225a36f5532SKonstantin Belousov 		 * FALSE, one for the map entry with MAP_ENTRY_NOSYNC
226a36f5532SKonstantin Belousov 		 * flag set, other with flag clear, race, it is
227a36f5532SKonstantin Belousov 		 * possible for the no-NOSYNC thread to see m->dirty
228fff5403fSJeff Roberson 		 * != 0 and not clear PGA_NOSYNC.  Take vm_page lock
229fff5403fSJeff Roberson 		 * around manipulation of PGA_NOSYNC and
230fff5403fSJeff Roberson 		 * vm_page_dirty() call to avoid the race.
231a36f5532SKonstantin Belousov 		 */
232a36f5532SKonstantin Belousov 		vm_page_lock(m);
233a36f5532SKonstantin Belousov 
234a36f5532SKonstantin Belousov 	/*
235fff5403fSJeff Roberson 	 * If this is a NOSYNC mmap we do not want to set PGA_NOSYNC
236a36f5532SKonstantin Belousov 	 * if the page is already dirty to prevent data written with
237a36f5532SKonstantin Belousov 	 * the expectation of being synced from not being synced.
238a36f5532SKonstantin Belousov 	 * Likewise if this entry does not request NOSYNC then make
239a36f5532SKonstantin Belousov 	 * sure the page isn't marked NOSYNC.  Applications sharing
240a36f5532SKonstantin Belousov 	 * data should use the same flags to avoid ping ponging.
241a36f5532SKonstantin Belousov 	 */
242a36f5532SKonstantin Belousov 	if ((entry->eflags & MAP_ENTRY_NOSYNC) != 0) {
243a36f5532SKonstantin Belousov 		if (m->dirty == 0) {
244fff5403fSJeff Roberson 			vm_page_aflag_set(m, PGA_NOSYNC);
245a36f5532SKonstantin Belousov 		}
246a36f5532SKonstantin Belousov 	} else {
247fff5403fSJeff Roberson 		vm_page_aflag_clear(m, PGA_NOSYNC);
248a36f5532SKonstantin Belousov 	}
249a36f5532SKonstantin Belousov 
250a36f5532SKonstantin Belousov 	/*
251a36f5532SKonstantin Belousov 	 * If the fault is a write, we know that this page is being
252a36f5532SKonstantin Belousov 	 * written NOW so dirty it explicitly to save on
253a36f5532SKonstantin Belousov 	 * pmap_is_modified() calls later.
254a36f5532SKonstantin Belousov 	 *
255d5efa0a4SAlan Cox 	 * Also, since the page is now dirty, we can possibly tell
256d5efa0a4SAlan Cox 	 * the pager to release any swap backing the page.  Calling
257d5efa0a4SAlan Cox 	 * the pager requires a write lock on the object.
258a36f5532SKonstantin Belousov 	 */
259a36f5532SKonstantin Belousov 	if (need_dirty)
260a36f5532SKonstantin Belousov 		vm_page_dirty(m);
261a36f5532SKonstantin Belousov 	if (!set_wd)
262a36f5532SKonstantin Belousov 		vm_page_unlock(m);
263d5efa0a4SAlan Cox 	else if (need_dirty)
264a36f5532SKonstantin Belousov 		vm_pager_page_unswapped(m);
265a36f5532SKonstantin Belousov }
266a36f5532SKonstantin Belousov 
26741ddec83SKonstantin Belousov /*
26841ddec83SKonstantin Belousov  * Unlocks fs.first_object and fs.map on success.
26941ddec83SKonstantin Belousov  */
27041ddec83SKonstantin Belousov static int
27141ddec83SKonstantin Belousov vm_fault_soft_fast(struct faultstate *fs, vm_offset_t vaddr, vm_prot_t prot,
27241ddec83SKonstantin Belousov     int fault_type, int fault_flags, boolean_t wired, vm_page_t *m_hold)
27341ddec83SKonstantin Belousov {
2748b5e1472SAlan Cox 	vm_page_t m, m_map;
2752bf8cb38SAlan Cox #if (defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
276f6893f09SMark Johnston     __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)) && \
277f6893f09SMark Johnston     VM_NRESERVLEVEL > 0
2788b5e1472SAlan Cox 	vm_page_t m_super;
27990ea34bfSAlan Cox 	int flags;
2808b5e1472SAlan Cox #endif
28190ea34bfSAlan Cox 	int psind, rv;
28241ddec83SKonstantin Belousov 
28341ddec83SKonstantin Belousov 	MPASS(fs->vp == NULL);
284205be21dSJeff Roberson 	vm_object_busy(fs->first_object);
28541ddec83SKonstantin Belousov 	m = vm_page_lookup(fs->first_object, fs->first_pindex);
28641ddec83SKonstantin Belousov 	/* A busy page can be mapped for read|execute access. */
28741ddec83SKonstantin Belousov 	if (m == NULL || ((prot & VM_PROT_WRITE) != 0 &&
2880012f373SJeff Roberson 	    vm_page_busied(m)) || !vm_page_all_valid(m)) {
289205be21dSJeff Roberson 		rv = KERN_FAILURE;
290205be21dSJeff Roberson 		goto out;
291205be21dSJeff Roberson 	}
2928b5e1472SAlan Cox 	m_map = m;
2938b5e1472SAlan Cox 	psind = 0;
2942bf8cb38SAlan Cox #if (defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
295f6893f09SMark Johnston     __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)) && \
296f6893f09SMark Johnston     VM_NRESERVLEVEL > 0
2978b5e1472SAlan Cox 	if ((m->flags & PG_FICTITIOUS) == 0 &&
2988b5e1472SAlan Cox 	    (m_super = vm_reserv_to_superpage(m)) != NULL &&
2998b5e1472SAlan Cox 	    rounddown2(vaddr, pagesizes[m_super->psind]) >= fs->entry->start &&
3008b5e1472SAlan Cox 	    roundup2(vaddr + 1, pagesizes[m_super->psind]) <= fs->entry->end &&
3018b5e1472SAlan Cox 	    (vaddr & (pagesizes[m_super->psind] - 1)) == (VM_PAGE_TO_PHYS(m) &
30264087fd7SMark Johnston 	    (pagesizes[m_super->psind] - 1)) && !wired &&
3038b5e1472SAlan Cox 	    pmap_ps_enabled(fs->map->pmap)) {
3048b5e1472SAlan Cox 		flags = PS_ALL_VALID;
3058b5e1472SAlan Cox 		if ((prot & VM_PROT_WRITE) != 0) {
3068b5e1472SAlan Cox 			/*
3078b5e1472SAlan Cox 			 * Create a superpage mapping allowing write access
3088b5e1472SAlan Cox 			 * only if none of the constituent pages are busy and
3098b5e1472SAlan Cox 			 * all of them are already dirty (except possibly for
3108b5e1472SAlan Cox 			 * the page that was faulted on).
3118b5e1472SAlan Cox 			 */
3128b5e1472SAlan Cox 			flags |= PS_NONE_BUSY;
3138b5e1472SAlan Cox 			if ((fs->first_object->flags & OBJ_UNMANAGED) == 0)
3148b5e1472SAlan Cox 				flags |= PS_ALL_DIRTY;
3158b5e1472SAlan Cox 		}
3168b5e1472SAlan Cox 		if (vm_page_ps_test(m_super, flags, m)) {
3178b5e1472SAlan Cox 			m_map = m_super;
3188b5e1472SAlan Cox 			psind = m_super->psind;
3198b5e1472SAlan Cox 			vaddr = rounddown2(vaddr, pagesizes[psind]);
3208b5e1472SAlan Cox 			/* Preset the modified bit for dirty superpages. */
3218b5e1472SAlan Cox 			if ((flags & PS_ALL_DIRTY) != 0)
3228b5e1472SAlan Cox 				fault_type |= VM_PROT_WRITE;
3238b5e1472SAlan Cox 		}
3248b5e1472SAlan Cox 	}
3258b5e1472SAlan Cox #endif
3268b5e1472SAlan Cox 	rv = pmap_enter(fs->map->pmap, vaddr, m_map, prot, fault_type |
3278b5e1472SAlan Cox 	    PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED : 0), psind);
32841ddec83SKonstantin Belousov 	if (rv != KERN_SUCCESS)
329205be21dSJeff Roberson 		goto out;
330fee2a2faSMark Johnston 	if (m_hold != NULL) {
331fee2a2faSMark Johnston 		*m_hold = m;
332fee2a2faSMark Johnston 		vm_page_wire(m);
333fee2a2faSMark Johnston 	}
33441ddec83SKonstantin Belousov 	vm_fault_dirty(fs->entry, m, prot, fault_type, fault_flags, false);
3358b5e1472SAlan Cox 	if (psind == 0 && !wired)
336a7163bb9SKonstantin Belousov 		vm_fault_prefault(fs, vaddr, PFBAK, PFFOR, true);
337a7163bb9SKonstantin Belousov 	VM_OBJECT_RUNLOCK(fs->first_object);
33841ddec83SKonstantin Belousov 	vm_map_lookup_done(fs->map, fs->entry);
33941ddec83SKonstantin Belousov 	curthread->td_ru.ru_minflt++;
340205be21dSJeff Roberson 
341205be21dSJeff Roberson out:
342205be21dSJeff Roberson 	vm_object_unbusy(fs->first_object);
343205be21dSJeff Roberson 	return (rv);
34441ddec83SKonstantin Belousov }
34541ddec83SKonstantin Belousov 
346c42b43a0SKonstantin Belousov static void
347c42b43a0SKonstantin Belousov vm_fault_restore_map_lock(struct faultstate *fs)
348c42b43a0SKonstantin Belousov {
349c42b43a0SKonstantin Belousov 
350c42b43a0SKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
35111b57401SHans Petter Selasky 	MPASS(REFCOUNT_COUNT(fs->first_object->paging_in_progress) > 0);
352c42b43a0SKonstantin Belousov 
353c42b43a0SKonstantin Belousov 	if (!vm_map_trylock_read(fs->map)) {
354c42b43a0SKonstantin Belousov 		VM_OBJECT_WUNLOCK(fs->first_object);
355c42b43a0SKonstantin Belousov 		vm_map_lock_read(fs->map);
356c42b43a0SKonstantin Belousov 		VM_OBJECT_WLOCK(fs->first_object);
357c42b43a0SKonstantin Belousov 	}
358c42b43a0SKonstantin Belousov 	fs->lookup_still_valid = true;
359c42b43a0SKonstantin Belousov }
360c42b43a0SKonstantin Belousov 
3617a432b84SKonstantin Belousov static void
3627a432b84SKonstantin Belousov vm_fault_populate_check_page(vm_page_t m)
3637a432b84SKonstantin Belousov {
3647a432b84SKonstantin Belousov 
3657a432b84SKonstantin Belousov 	/*
3667a432b84SKonstantin Belousov 	 * Check each page to ensure that the pager is obeying the
3677a432b84SKonstantin Belousov 	 * interface: the page must be installed in the object, fully
3687a432b84SKonstantin Belousov 	 * valid, and exclusively busied.
3697a432b84SKonstantin Belousov 	 */
3707a432b84SKonstantin Belousov 	MPASS(m != NULL);
3710012f373SJeff Roberson 	MPASS(vm_page_all_valid(m));
3727a432b84SKonstantin Belousov 	MPASS(vm_page_xbusied(m));
3737a432b84SKonstantin Belousov }
3747a432b84SKonstantin Belousov 
3757a432b84SKonstantin Belousov static void
3767a432b84SKonstantin Belousov vm_fault_populate_cleanup(vm_object_t object, vm_pindex_t first,
3777a432b84SKonstantin Belousov     vm_pindex_t last)
3787a432b84SKonstantin Belousov {
3797a432b84SKonstantin Belousov 	vm_page_t m;
3807a432b84SKonstantin Belousov 	vm_pindex_t pidx;
3817a432b84SKonstantin Belousov 
3827a432b84SKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(object);
3837a432b84SKonstantin Belousov 	MPASS(first <= last);
3847a432b84SKonstantin Belousov 	for (pidx = first, m = vm_page_lookup(object, pidx);
3857a432b84SKonstantin Belousov 	    pidx <= last; pidx++, m = vm_page_next(m)) {
3867a432b84SKonstantin Belousov 		vm_fault_populate_check_page(m);
387e8bcf696SMark Johnston 		vm_page_lock(m);
3887a432b84SKonstantin Belousov 		vm_page_deactivate(m);
389e8bcf696SMark Johnston 		vm_page_unlock(m);
3907a432b84SKonstantin Belousov 		vm_page_xunbusy(m);
3917a432b84SKonstantin Belousov 	}
3927a432b84SKonstantin Belousov }
393c42b43a0SKonstantin Belousov 
394c42b43a0SKonstantin Belousov static int
395d3f8534eSAlan Cox vm_fault_populate(struct faultstate *fs, vm_prot_t prot, int fault_type,
396d3f8534eSAlan Cox     int fault_flags, boolean_t wired, vm_page_t *m_hold)
397c42b43a0SKonstantin Belousov {
39870183daaSAlan Cox 	struct mtx *m_mtx;
39970183daaSAlan Cox 	vm_offset_t vaddr;
400c42b43a0SKonstantin Belousov 	vm_page_t m;
4017a432b84SKonstantin Belousov 	vm_pindex_t map_first, map_last, pager_first, pager_last, pidx;
40270183daaSAlan Cox 	int i, npages, psind, rv;
403c42b43a0SKonstantin Belousov 
404c42b43a0SKonstantin Belousov 	MPASS(fs->object == fs->first_object);
405c42b43a0SKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
40611b57401SHans Petter Selasky 	MPASS(REFCOUNT_COUNT(fs->first_object->paging_in_progress) > 0);
407c42b43a0SKonstantin Belousov 	MPASS(fs->first_object->backing_object == NULL);
408c42b43a0SKonstantin Belousov 	MPASS(fs->lookup_still_valid);
409c42b43a0SKonstantin Belousov 
4107a432b84SKonstantin Belousov 	pager_first = OFF_TO_IDX(fs->entry->offset);
41189564188SAlan Cox 	pager_last = pager_first + atop(fs->entry->end - fs->entry->start) - 1;
412c42b43a0SKonstantin Belousov 	unlock_map(fs);
413c42b43a0SKonstantin Belousov 	unlock_vp(fs);
414c42b43a0SKonstantin Belousov 
415c42b43a0SKonstantin Belousov 	/*
416c42b43a0SKonstantin Belousov 	 * Call the pager (driver) populate() method.
417c42b43a0SKonstantin Belousov 	 *
418c42b43a0SKonstantin Belousov 	 * There is no guarantee that the method will be called again
419c42b43a0SKonstantin Belousov 	 * if the current fault is for read, and a future fault is
420c42b43a0SKonstantin Belousov 	 * for write.  Report the entry's maximum allowed protection
421c42b43a0SKonstantin Belousov 	 * to the driver.
422c42b43a0SKonstantin Belousov 	 */
423c42b43a0SKonstantin Belousov 	rv = vm_pager_populate(fs->first_object, fs->first_pindex,
4247a432b84SKonstantin Belousov 	    fault_type, fs->entry->max_protection, &pager_first, &pager_last);
425c42b43a0SKonstantin Belousov 
426c42b43a0SKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
427c42b43a0SKonstantin Belousov 	if (rv == VM_PAGER_BAD) {
428c42b43a0SKonstantin Belousov 		/*
429c42b43a0SKonstantin Belousov 		 * VM_PAGER_BAD is the backdoor for a pager to request
430c42b43a0SKonstantin Belousov 		 * normal fault handling.
431c42b43a0SKonstantin Belousov 		 */
432c42b43a0SKonstantin Belousov 		vm_fault_restore_map_lock(fs);
433c42b43a0SKonstantin Belousov 		if (fs->map->timestamp != fs->map_generation)
434c42b43a0SKonstantin Belousov 			return (KERN_RESOURCE_SHORTAGE); /* RetryFault */
435c42b43a0SKonstantin Belousov 		return (KERN_NOT_RECEIVER);
436c42b43a0SKonstantin Belousov 	}
437c42b43a0SKonstantin Belousov 	if (rv != VM_PAGER_OK)
438c42b43a0SKonstantin Belousov 		return (KERN_FAILURE); /* AKA SIGSEGV */
439c42b43a0SKonstantin Belousov 
440c42b43a0SKonstantin Belousov 	/* Ensure that the driver is obeying the interface. */
4417a432b84SKonstantin Belousov 	MPASS(pager_first <= pager_last);
4427a432b84SKonstantin Belousov 	MPASS(fs->first_pindex <= pager_last);
4437a432b84SKonstantin Belousov 	MPASS(fs->first_pindex >= pager_first);
4447a432b84SKonstantin Belousov 	MPASS(pager_last < fs->first_object->size);
445c42b43a0SKonstantin Belousov 
446c42b43a0SKonstantin Belousov 	vm_fault_restore_map_lock(fs);
4477a432b84SKonstantin Belousov 	if (fs->map->timestamp != fs->map_generation) {
4487a432b84SKonstantin Belousov 		vm_fault_populate_cleanup(fs->first_object, pager_first,
4497a432b84SKonstantin Belousov 		    pager_last);
450c42b43a0SKonstantin Belousov 		return (KERN_RESOURCE_SHORTAGE); /* RetryFault */
4517a432b84SKonstantin Belousov 	}
452c42b43a0SKonstantin Belousov 
453c42b43a0SKonstantin Belousov 	/*
4547a432b84SKonstantin Belousov 	 * The map is unchanged after our last unlock.  Process the fault.
4557a432b84SKonstantin Belousov 	 *
4567a432b84SKonstantin Belousov 	 * The range [pager_first, pager_last] that is given to the
4577a432b84SKonstantin Belousov 	 * pager is only a hint.  The pager may populate any range
4587a432b84SKonstantin Belousov 	 * within the object that includes the requested page index.
4597a432b84SKonstantin Belousov 	 * In case the pager expanded the range, clip it to fit into
4607a432b84SKonstantin Belousov 	 * the map entry.
461c42b43a0SKonstantin Belousov 	 */
46289564188SAlan Cox 	map_first = OFF_TO_IDX(fs->entry->offset);
46389564188SAlan Cox 	if (map_first > pager_first) {
4647a432b84SKonstantin Belousov 		vm_fault_populate_cleanup(fs->first_object, pager_first,
4657a432b84SKonstantin Belousov 		    map_first - 1);
46689564188SAlan Cox 		pager_first = map_first;
46789564188SAlan Cox 	}
46889564188SAlan Cox 	map_last = map_first + atop(fs->entry->end - fs->entry->start) - 1;
46989564188SAlan Cox 	if (map_last < pager_last) {
4707a432b84SKonstantin Belousov 		vm_fault_populate_cleanup(fs->first_object, map_last + 1,
4717a432b84SKonstantin Belousov 		    pager_last);
47289564188SAlan Cox 		pager_last = map_last;
47389564188SAlan Cox 	}
47489564188SAlan Cox 	for (pidx = pager_first, m = vm_page_lookup(fs->first_object, pidx);
47570183daaSAlan Cox 	    pidx <= pager_last;
47670183daaSAlan Cox 	    pidx += npages, m = vm_page_next(&m[npages - 1])) {
47770183daaSAlan Cox 		vaddr = fs->entry->start + IDX_TO_OFF(pidx) - fs->entry->offset;
4782bf8cb38SAlan Cox #if defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
479f6893f09SMark Johnston     __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)
48070183daaSAlan Cox 		psind = m->psind;
48170183daaSAlan Cox 		if (psind > 0 && ((vaddr & (pagesizes[psind] - 1)) != 0 ||
48270183daaSAlan Cox 		    pidx + OFF_TO_IDX(pagesizes[psind]) - 1 > pager_last ||
48364087fd7SMark Johnston 		    !pmap_ps_enabled(fs->map->pmap) || wired))
48470183daaSAlan Cox 			psind = 0;
48570183daaSAlan Cox #else
48670183daaSAlan Cox 		psind = 0;
48770183daaSAlan Cox #endif
48870183daaSAlan Cox 		npages = atop(pagesizes[psind]);
48970183daaSAlan Cox 		for (i = 0; i < npages; i++) {
49070183daaSAlan Cox 			vm_fault_populate_check_page(&m[i]);
49170183daaSAlan Cox 			vm_fault_dirty(fs->entry, &m[i], prot, fault_type,
49270183daaSAlan Cox 			    fault_flags, true);
49370183daaSAlan Cox 		}
494c42b43a0SKonstantin Belousov 		VM_OBJECT_WUNLOCK(fs->first_object);
495e7a9df16SKonstantin Belousov 		rv = pmap_enter(fs->map->pmap, vaddr, m, prot, fault_type |
496e7a9df16SKonstantin Belousov 		    (wired ? PMAP_ENTER_WIRED : 0), psind);
497e7a9df16SKonstantin Belousov #if defined(__amd64__)
498e7a9df16SKonstantin Belousov 		if (psind > 0 && rv == KERN_FAILURE) {
499e7a9df16SKonstantin Belousov 			for (i = 0; i < npages; i++) {
500e7a9df16SKonstantin Belousov 				rv = pmap_enter(fs->map->pmap, vaddr + ptoa(i),
501e7a9df16SKonstantin Belousov 				    &m[i], prot, fault_type |
502e7a9df16SKonstantin Belousov 				    (wired ? PMAP_ENTER_WIRED : 0), 0);
503e7a9df16SKonstantin Belousov 				MPASS(rv == KERN_SUCCESS);
504e7a9df16SKonstantin Belousov 			}
505e7a9df16SKonstantin Belousov 		}
506e7a9df16SKonstantin Belousov #else
507e7a9df16SKonstantin Belousov 		MPASS(rv == KERN_SUCCESS);
508e7a9df16SKonstantin Belousov #endif
509c42b43a0SKonstantin Belousov 		VM_OBJECT_WLOCK(fs->first_object);
51070183daaSAlan Cox 		m_mtx = NULL;
51170183daaSAlan Cox 		for (i = 0; i < npages; i++) {
512fee2a2faSMark Johnston 			if ((fault_flags & VM_FAULT_WIRE) != 0) {
51370183daaSAlan Cox 				vm_page_wire(&m[i]);
514fee2a2faSMark Johnston 			} else {
515fee2a2faSMark Johnston 				vm_page_change_lock(&m[i], &m_mtx);
51670183daaSAlan Cox 				vm_page_activate(&m[i]);
517fee2a2faSMark Johnston 			}
51870183daaSAlan Cox 			if (m_hold != NULL && m[i].pindex == fs->first_pindex) {
51970183daaSAlan Cox 				*m_hold = &m[i];
520eeacb3b0SMark Johnston 				vm_page_wire(&m[i]);
521c42b43a0SKonstantin Belousov 			}
5224cdea4a8SJeff Roberson 			vm_page_xunbusy(&m[i]);
52370183daaSAlan Cox 		}
52470183daaSAlan Cox 		if (m_mtx != NULL)
52570183daaSAlan Cox 			mtx_unlock(m_mtx);
526c42b43a0SKonstantin Belousov 	}
527c42b43a0SKonstantin Belousov 	curthread->td_ru.ru_majflt++;
528c42b43a0SKonstantin Belousov 	return (KERN_SUCCESS);
529c42b43a0SKonstantin Belousov }
530c42b43a0SKonstantin Belousov 
531df08823dSKonstantin Belousov static int prot_fault_translation;
532df08823dSKonstantin Belousov SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RWTUN,
533df08823dSKonstantin Belousov     &prot_fault_translation, 0,
534df08823dSKonstantin Belousov     "Control signal to deliver on protection fault");
535df08823dSKonstantin Belousov 
536df08823dSKonstantin Belousov /* compat definition to keep common code for signal translation */
537df08823dSKonstantin Belousov #define	UCODE_PAGEFLT	12
538df08823dSKonstantin Belousov #ifdef T_PAGEFLT
539df08823dSKonstantin Belousov _Static_assert(UCODE_PAGEFLT == T_PAGEFLT, "T_PAGEFLT");
540df08823dSKonstantin Belousov #endif
541df08823dSKonstantin Belousov 
542df8bae1dSRodney W. Grimes /*
543df08823dSKonstantin Belousov  *	vm_fault_trap:
544df8bae1dSRodney W. Grimes  *
545956f3135SPhilippe Charnier  *	Handle a page fault occurring at the given address,
546df8bae1dSRodney W. Grimes  *	requiring the given permissions, in the map specified.
547df8bae1dSRodney W. Grimes  *	If successful, the page is inserted into the
548df8bae1dSRodney W. Grimes  *	associated physical map.
549df8bae1dSRodney W. Grimes  *
550df8bae1dSRodney W. Grimes  *	NOTE: the given address should be truncated to the
551df8bae1dSRodney W. Grimes  *	proper page address.
552df8bae1dSRodney W. Grimes  *
553df8bae1dSRodney W. Grimes  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
554df8bae1dSRodney W. Grimes  *	a standard error specifying why the fault is fatal is returned.
555df8bae1dSRodney W. Grimes  *
556df8bae1dSRodney W. Grimes  *	The map in question must be referenced, and remains so.
5570cddd8f0SMatthew Dillon  *	Caller may hold no locks.
558df8bae1dSRodney W. Grimes  */
559df8bae1dSRodney W. Grimes int
560df08823dSKonstantin Belousov vm_fault_trap(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
561df08823dSKonstantin Belousov     int fault_flags, int *signo, int *ucode)
56223955314SAlfred Perlstein {
56335818d2eSJohn Baldwin 	int result;
564acd11c74SAlan Cox 
565df08823dSKonstantin Belousov 	MPASS(signo == NULL || ucode != NULL);
56635818d2eSJohn Baldwin #ifdef KTRACE
567c31cec45SKonstantin Belousov 	if (map != kernel_map && KTRPOINT(curthread, KTR_FAULT))
56835818d2eSJohn Baldwin 		ktrfault(vaddr, fault_type);
56935818d2eSJohn Baldwin #endif
570df08823dSKonstantin Belousov 	result = vm_fault(map, trunc_page(vaddr), fault_type, fault_flags,
571be996836SAttilio Rao 	    NULL);
572df08823dSKonstantin Belousov 	KASSERT(result == KERN_SUCCESS || result == KERN_FAILURE ||
573df08823dSKonstantin Belousov 	    result == KERN_INVALID_ADDRESS ||
574df08823dSKonstantin Belousov 	    result == KERN_RESOURCE_SHORTAGE ||
575df08823dSKonstantin Belousov 	    result == KERN_PROTECTION_FAILURE ||
576df08823dSKonstantin Belousov 	    result == KERN_OUT_OF_BOUNDS,
577df08823dSKonstantin Belousov 	    ("Unexpected Mach error %d from vm_fault()", result));
57835818d2eSJohn Baldwin #ifdef KTRACE
579c31cec45SKonstantin Belousov 	if (map != kernel_map && KTRPOINT(curthread, KTR_FAULTEND))
58035818d2eSJohn Baldwin 		ktrfaultend(result);
58135818d2eSJohn Baldwin #endif
582df08823dSKonstantin Belousov 	if (result != KERN_SUCCESS && signo != NULL) {
583df08823dSKonstantin Belousov 		switch (result) {
584df08823dSKonstantin Belousov 		case KERN_FAILURE:
585df08823dSKonstantin Belousov 		case KERN_INVALID_ADDRESS:
586df08823dSKonstantin Belousov 			*signo = SIGSEGV;
587df08823dSKonstantin Belousov 			*ucode = SEGV_MAPERR;
588df08823dSKonstantin Belousov 			break;
589df08823dSKonstantin Belousov 		case KERN_RESOURCE_SHORTAGE:
590df08823dSKonstantin Belousov 			*signo = SIGBUS;
591df08823dSKonstantin Belousov 			*ucode = BUS_OOMERR;
592df08823dSKonstantin Belousov 			break;
593df08823dSKonstantin Belousov 		case KERN_OUT_OF_BOUNDS:
594df08823dSKonstantin Belousov 			*signo = SIGBUS;
595df08823dSKonstantin Belousov 			*ucode = BUS_OBJERR;
596df08823dSKonstantin Belousov 			break;
597df08823dSKonstantin Belousov 		case KERN_PROTECTION_FAILURE:
598df08823dSKonstantin Belousov 			if (prot_fault_translation == 0) {
599df08823dSKonstantin Belousov 				/*
600df08823dSKonstantin Belousov 				 * Autodetect.  This check also covers
601df08823dSKonstantin Belousov 				 * the images without the ABI-tag ELF
602df08823dSKonstantin Belousov 				 * note.
603df08823dSKonstantin Belousov 				 */
604df08823dSKonstantin Belousov 				if (SV_CURPROC_ABI() == SV_ABI_FREEBSD &&
605df08823dSKonstantin Belousov 				    curproc->p_osrel >= P_OSREL_SIGSEGV) {
606df08823dSKonstantin Belousov 					*signo = SIGSEGV;
607df08823dSKonstantin Belousov 					*ucode = SEGV_ACCERR;
608df08823dSKonstantin Belousov 				} else {
609df08823dSKonstantin Belousov 					*signo = SIGBUS;
610df08823dSKonstantin Belousov 					*ucode = UCODE_PAGEFLT;
611df08823dSKonstantin Belousov 				}
612df08823dSKonstantin Belousov 			} else if (prot_fault_translation == 1) {
613df08823dSKonstantin Belousov 				/* Always compat mode. */
614df08823dSKonstantin Belousov 				*signo = SIGBUS;
615df08823dSKonstantin Belousov 				*ucode = UCODE_PAGEFLT;
616df08823dSKonstantin Belousov 			} else {
617df08823dSKonstantin Belousov 				/* Always SIGSEGV mode. */
618df08823dSKonstantin Belousov 				*signo = SIGSEGV;
619df08823dSKonstantin Belousov 				*ucode = SEGV_ACCERR;
620df08823dSKonstantin Belousov 			}
621df08823dSKonstantin Belousov 			break;
622df08823dSKonstantin Belousov 		default:
623df08823dSKonstantin Belousov 			KASSERT(0, ("Unexpected Mach error %d from vm_fault()",
624df08823dSKonstantin Belousov 			    result));
625df08823dSKonstantin Belousov 			break;
626df08823dSKonstantin Belousov 		}
627df08823dSKonstantin Belousov 	}
62835818d2eSJohn Baldwin 	return (result);
629acd11c74SAlan Cox }
630acd11c74SAlan Cox 
6310ddd3082SKonstantin Belousov static int
6320ddd3082SKonstantin Belousov vm_fault_lock_vnode(struct faultstate *fs)
6330ddd3082SKonstantin Belousov {
6340ddd3082SKonstantin Belousov 	struct vnode *vp;
6350ddd3082SKonstantin Belousov 	int error, locked;
6360ddd3082SKonstantin Belousov 
6370ddd3082SKonstantin Belousov 	if (fs->object->type != OBJT_VNODE)
6380ddd3082SKonstantin Belousov 		return (KERN_SUCCESS);
6390ddd3082SKonstantin Belousov 	vp = fs->object->handle;
640*16b0c092SKonstantin Belousov 	if (vp == fs->vp) {
641*16b0c092SKonstantin Belousov 		ASSERT_VOP_LOCKED(vp, "saved vnode is not locked");
6420ddd3082SKonstantin Belousov 		return (KERN_SUCCESS);
643*16b0c092SKonstantin Belousov 	}
6440ddd3082SKonstantin Belousov 
6450ddd3082SKonstantin Belousov 	/*
6460ddd3082SKonstantin Belousov 	 * Perform an unlock in case the desired vnode changed while
6470ddd3082SKonstantin Belousov 	 * the map was unlocked during a retry.
6480ddd3082SKonstantin Belousov 	 */
6490ddd3082SKonstantin Belousov 	unlock_vp(fs);
6500ddd3082SKonstantin Belousov 
6510ddd3082SKonstantin Belousov 	locked = VOP_ISLOCKED(vp);
6520ddd3082SKonstantin Belousov 	if (locked != LK_EXCLUSIVE)
6530ddd3082SKonstantin Belousov 		locked = LK_SHARED;
6540ddd3082SKonstantin Belousov 
6550ddd3082SKonstantin Belousov 	/*
6560ddd3082SKonstantin Belousov 	 * We must not sleep acquiring the vnode lock while we have
6570ddd3082SKonstantin Belousov 	 * the page exclusive busied or the object's
6580ddd3082SKonstantin Belousov 	 * paging-in-progress count incremented.  Otherwise, we could
6590ddd3082SKonstantin Belousov 	 * deadlock.
6600ddd3082SKonstantin Belousov 	 */
6610ddd3082SKonstantin Belousov 	error = vget(vp, locked | LK_CANRECURSE | LK_NOWAIT, curthread);
6620ddd3082SKonstantin Belousov 	if (error == 0) {
6630ddd3082SKonstantin Belousov 		fs->vp = vp;
6640ddd3082SKonstantin Belousov 		return (KERN_SUCCESS);
6650ddd3082SKonstantin Belousov 	}
6660ddd3082SKonstantin Belousov 
6670ddd3082SKonstantin Belousov 	vhold(vp);
6680ddd3082SKonstantin Belousov 	release_page(fs);
6690ddd3082SKonstantin Belousov 	unlock_and_deallocate(fs);
6700ddd3082SKonstantin Belousov 	error = vget(vp, locked | LK_RETRY | LK_CANRECURSE, curthread);
6710ddd3082SKonstantin Belousov 	vdrop(vp);
6720ddd3082SKonstantin Belousov 	fs->vp = vp;
6730ddd3082SKonstantin Belousov 	KASSERT(error == 0, ("vm_fault: vget failed %d", error));
6740ddd3082SKonstantin Belousov 	return (KERN_RESOURCE_SHORTAGE);
6750ddd3082SKonstantin Belousov }
6760ddd3082SKonstantin Belousov 
677acd11c74SAlan Cox int
678df08823dSKonstantin Belousov vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
679acd11c74SAlan Cox     int fault_flags, vm_page_t *m_hold)
680acd11c74SAlan Cox {
6814866e085SJohn Dyson 	struct faultstate fs;
68223984ce5SMark Johnston 	struct domainset *dset;
683ebcddc72SAlan Cox 	vm_object_t next_object, retry_object;
6840c3a4893SAlan Cox 	vm_offset_t e_end, e_start;
685ebcddc72SAlan Cox 	vm_pindex_t retry_pindex;
686ebcddc72SAlan Cox 	vm_prot_t prot, retry_prot;
6870ddd3082SKonstantin Belousov 	int ahead, alloc_req, behind, cluster_offset, era, faultcount;
6880ddd3082SKonstantin Belousov 	int nera, oom, result, rv;
6890c3a4893SAlan Cox 	u_char behavior;
690cd8a6fe8SAlan Cox 	boolean_t wired;	/* Passed by reference. */
69119bd0d9cSKonstantin Belousov 	bool dead, hardfault, is_first_object_locked;
692df8bae1dSRodney W. Grimes 
69383c9dea1SGleb Smirnoff 	VM_CNT_INC(v_vm_faults);
694c31cec45SKonstantin Belousov 
695c31cec45SKonstantin Belousov 	if ((curthread->td_pflags & TDP_NOFAULTING) != 0)
696c31cec45SKonstantin Belousov 		return (KERN_PROTECTION_FAILURE);
697c31cec45SKonstantin Belousov 
698d2bf64c3SKonstantin Belousov 	fs.vp = NULL;
699b0cd2017SGleb Smirnoff 	faultcount = 0;
7000c3a4893SAlan Cox 	nera = -1;
701320023e2SAlan Cox 	hardfault = false;
702df8bae1dSRodney W. Grimes 
703245139c6SKonstantin Belousov RetryFault:
704245139c6SKonstantin Belousov 	oom = 0;
705245139c6SKonstantin Belousov RetryFault_oom:
706df8bae1dSRodney W. Grimes 
707df8bae1dSRodney W. Grimes 	/*
7080d94caffSDavid Greenman 	 * Find the backing store object and offset into it to begin the
7090d94caffSDavid Greenman 	 * search.
710df8bae1dSRodney W. Grimes 	 */
71140360b1bSMatthew Dillon 	fs.map = map;
71219bd0d9cSKonstantin Belousov 	result = vm_map_lookup(&fs.map, vaddr, fault_type |
71319bd0d9cSKonstantin Belousov 	    VM_PROT_FAULT_LOOKUP, &fs.entry, &fs.first_object,
71419bd0d9cSKonstantin Belousov 	    &fs.first_pindex, &prot, &wired);
71592de35b0SAlan Cox 	if (result != KERN_SUCCESS) {
716a9ee028dSMark Johnston 		unlock_vp(&fs);
71792de35b0SAlan Cox 		return (result);
71809e0c6ccSJohn Dyson 	}
71909e0c6ccSJohn Dyson 
720dc5401d2SKonstantin Belousov 	fs.map_generation = fs.map->timestamp;
7212d8acc0fSJohn Dyson 
7224866e085SJohn Dyson 	if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
72305d58177SBjoern A. Zeeb 		panic("%s: fault on nofault entry, addr: %#lx",
72405d58177SBjoern A. Zeeb 		    __func__, (u_long)vaddr);
7257aaaa4fdSJohn Dyson 	}
7267aaaa4fdSJohn Dyson 
7274f9c9114SKonstantin Belousov 	if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
7284f9c9114SKonstantin Belousov 	    fs.entry->wiring_thread != curthread) {
7294f9c9114SKonstantin Belousov 		vm_map_unlock_read(fs.map);
7304f9c9114SKonstantin Belousov 		vm_map_lock(fs.map);
7314f9c9114SKonstantin Belousov 		if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
7324f9c9114SKonstantin Belousov 		    (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
733cfabea3dSKonstantin Belousov 			unlock_vp(&fs);
7344f9c9114SKonstantin Belousov 			fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
7354f9c9114SKonstantin Belousov 			vm_map_unlock_and_wait(fs.map, 0);
7364f9c9114SKonstantin Belousov 		} else
7374f9c9114SKonstantin Belousov 			vm_map_unlock(fs.map);
7384f9c9114SKonstantin Belousov 		goto RetryFault;
7394f9c9114SKonstantin Belousov 	}
7404f9c9114SKonstantin Belousov 
74119bd0d9cSKonstantin Belousov 	MPASS((fs.entry->eflags & MAP_ENTRY_GUARD) == 0);
74219bd0d9cSKonstantin Belousov 
743afe55ca3SKonstantin Belousov 	if (wired)
744afe55ca3SKonstantin Belousov 		fault_type = prot | (fault_type & VM_PROT_COPY);
7456a875bf9SKonstantin Belousov 	else
7466a875bf9SKonstantin Belousov 		KASSERT((fault_flags & VM_FAULT_WIRE) == 0,
7476a875bf9SKonstantin Belousov 		    ("!wired && VM_FAULT_WIRE"));
748afe55ca3SKonstantin Belousov 
7498d67b8c8SAlan Cox 	/*
7508d67b8c8SAlan Cox 	 * Try to avoid lock contention on the top-level object through
7518d67b8c8SAlan Cox 	 * special-case handling of some types of page faults, specifically,
7528d67b8c8SAlan Cox 	 * those that are both (1) mapping an existing page from the top-
7538d67b8c8SAlan Cox 	 * level object and (2) not having to mark that object as containing
7548d67b8c8SAlan Cox 	 * dirty pages.  Under these conditions, a read lock on the top-level
7558d67b8c8SAlan Cox 	 * object suffices, allowing multiple page faults of a similar type to
7568d67b8c8SAlan Cox 	 * run in parallel on the same top-level object.
7578d67b8c8SAlan Cox 	 */
758afe55ca3SKonstantin Belousov 	if (fs.vp == NULL /* avoid locked vnode leak */ &&
7596a875bf9SKonstantin Belousov 	    (fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0 &&
760afe55ca3SKonstantin Belousov 	    /* avoid calling vm_object_set_writeable_dirty() */
761afe55ca3SKonstantin Belousov 	    ((prot & VM_PROT_WRITE) == 0 ||
762f40cb1c6SKonstantin Belousov 	    (fs.first_object->type != OBJT_VNODE &&
763f40cb1c6SKonstantin Belousov 	    (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
764afe55ca3SKonstantin Belousov 	    (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0)) {
765afe55ca3SKonstantin Belousov 		VM_OBJECT_RLOCK(fs.first_object);
76641ddec83SKonstantin Belousov 		if ((prot & VM_PROT_WRITE) == 0 ||
76741ddec83SKonstantin Belousov 		    (fs.first_object->type != OBJT_VNODE &&
76841ddec83SKonstantin Belousov 		    (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
76941ddec83SKonstantin Belousov 		    (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0) {
77041ddec83SKonstantin Belousov 			rv = vm_fault_soft_fast(&fs, vaddr, prot, fault_type,
77141ddec83SKonstantin Belousov 			    fault_flags, wired, m_hold);
77241ddec83SKonstantin Belousov 			if (rv == KERN_SUCCESS)
77341ddec83SKonstantin Belousov 				return (rv);
774afe55ca3SKonstantin Belousov 		}
775afe55ca3SKonstantin Belousov 		if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) {
776afe55ca3SKonstantin Belousov 			VM_OBJECT_RUNLOCK(fs.first_object);
777afe55ca3SKonstantin Belousov 			VM_OBJECT_WLOCK(fs.first_object);
778afe55ca3SKonstantin Belousov 		}
779afe55ca3SKonstantin Belousov 	} else {
780afe55ca3SKonstantin Belousov 		VM_OBJECT_WLOCK(fs.first_object);
781afe55ca3SKonstantin Belousov 	}
782afe55ca3SKonstantin Belousov 
78395e5e988SJohn Dyson 	/*
78495e5e988SJohn Dyson 	 * Make a reference to this object to prevent its disposal while we
78595e5e988SJohn Dyson 	 * are messing with it.  Once we have the reference, the map is free
78695e5e988SJohn Dyson 	 * to be diddled.  Since objects reference their shadows (and copies),
78795e5e988SJohn Dyson 	 * they will stay around as well.
788fe8e0238SMatthew Dillon 	 *
789fe8e0238SMatthew Dillon 	 * Bump the paging-in-progress count to prevent size changes (e.g.
790dda4d369SAlan Cox 	 * truncation operations) during I/O.
79195e5e988SJohn Dyson 	 */
792a976eb5eSAlan Cox 	vm_object_reference_locked(fs.first_object);
793d474eaaaSDoug Rabson 	vm_object_pip_add(fs.first_object, 1);
79495e5e988SJohn Dyson 
795cd8a6fe8SAlan Cox 	fs.lookup_still_valid = true;
796df8bae1dSRodney W. Grimes 
7974866e085SJohn Dyson 	fs.first_m = NULL;
798df8bae1dSRodney W. Grimes 
799df8bae1dSRodney W. Grimes 	/*
800df8bae1dSRodney W. Grimes 	 * Search for the page at object/offset.
801df8bae1dSRodney W. Grimes 	 */
8024866e085SJohn Dyson 	fs.object = fs.first_object;
8034866e085SJohn Dyson 	fs.pindex = fs.first_pindex;
804df8bae1dSRodney W. Grimes 	while (TRUE) {
8051c7c3c6aSMatthew Dillon 		/*
806725441f6SKonstantin Belousov 		 * If the object is marked for imminent termination,
807725441f6SKonstantin Belousov 		 * we retry here, since the collapse pass has raced
808725441f6SKonstantin Belousov 		 * with us.  Otherwise, if we see terminally dead
809725441f6SKonstantin Belousov 		 * object, return fail.
8101c7c3c6aSMatthew Dillon 		 */
811725441f6SKonstantin Belousov 		if ((fs.object->flags & OBJ_DEAD) != 0) {
812725441f6SKonstantin Belousov 			dead = fs.object->type == OBJT_DEAD;
8134866e085SJohn Dyson 			unlock_and_deallocate(&fs);
814725441f6SKonstantin Belousov 			if (dead)
81547221757SJohn Dyson 				return (KERN_PROTECTION_FAILURE);
816725441f6SKonstantin Belousov 			pause("vmf_de", 1);
817725441f6SKonstantin Belousov 			goto RetryFault;
81847221757SJohn Dyson 		}
81947221757SJohn Dyson 
8201c7c3c6aSMatthew Dillon 		/*
8211c7c3c6aSMatthew Dillon 		 * See if page is resident
8221c7c3c6aSMatthew Dillon 		 */
8234866e085SJohn Dyson 		fs.m = vm_page_lookup(fs.object, fs.pindex);
8244866e085SJohn Dyson 		if (fs.m != NULL) {
82598cb733cSKenneth D. Merry 			/*
8261c7c3c6aSMatthew Dillon 			 * Wait/Retry if the page is busy.  We have to do this
827c7aebda8SAttilio Rao 			 * if the page is either exclusive or shared busy
828c7aebda8SAttilio Rao 			 * because the vm_pager may be using read busy for
829c7aebda8SAttilio Rao 			 * pageouts (and even pageins if it is the vnode
830c7aebda8SAttilio Rao 			 * pager), and we could end up trying to pagein and
831c7aebda8SAttilio Rao 			 * pageout the same page simultaneously.
8321c7c3c6aSMatthew Dillon 			 *
8331c7c3c6aSMatthew Dillon 			 * We can theoretically allow the busy case on a read
8341c7c3c6aSMatthew Dillon 			 * fault if the page is marked valid, but since such
8351c7c3c6aSMatthew Dillon 			 * pages are typically already pmap'd, putting that
8361c7c3c6aSMatthew Dillon 			 * special case in might be more effort then it is
8371c7c3c6aSMatthew Dillon 			 * worth.  We cannot under any circumstances mess
838c7aebda8SAttilio Rao 			 * around with a shared busied page except, perhaps,
8391c7c3c6aSMatthew Dillon 			 * to pmap it.
840df8bae1dSRodney W. Grimes 			 */
84163e97555SJeff Roberson 			if (vm_page_tryxbusy(fs.m) == 0) {
842b88b6c9dSAlan Cox 				/*
843b88b6c9dSAlan Cox 				 * Reference the page before unlocking and
844b88b6c9dSAlan Cox 				 * sleeping so that the page daemon is less
845b88b6c9dSAlan Cox 				 * likely to reclaim it.
846b88b6c9dSAlan Cox 				 */
8473407fefeSKonstantin Belousov 				vm_page_aflag_set(fs.m, PGA_REFERENCED);
848a51b0840SAlan Cox 				if (fs.object != fs.first_object) {
84989f6b863SAttilio Rao 					if (!VM_OBJECT_TRYWLOCK(
850a6e38685SKonstantin Belousov 					    fs.first_object)) {
85189f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(fs.object);
85289f6b863SAttilio Rao 						VM_OBJECT_WLOCK(fs.first_object);
85389f6b863SAttilio Rao 						VM_OBJECT_WLOCK(fs.object);
854a6e38685SKonstantin Belousov 					}
855a51b0840SAlan Cox 					vm_page_free(fs.first_m);
856a51b0840SAlan Cox 					vm_object_pip_wakeup(fs.first_object);
85789f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(fs.first_object);
858a51b0840SAlan Cox 					fs.first_m = NULL;
859a51b0840SAlan Cox 				}
860a51b0840SAlan Cox 				unlock_map(&fs);
861a51b0840SAlan Cox 				if (fs.m == vm_page_lookup(fs.object,
862a51b0840SAlan Cox 				    fs.pindex)) {
863c7aebda8SAttilio Rao 					vm_page_sleep_if_busy(fs.m, "vmpfw");
864a51b0840SAlan Cox 				}
865a51b0840SAlan Cox 				vm_object_pip_wakeup(fs.object);
86689f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(fs.object);
86783c9dea1SGleb Smirnoff 				VM_CNT_INC(v_intrans);
8684866e085SJohn Dyson 				vm_object_deallocate(fs.first_object);
869df8bae1dSRodney W. Grimes 				goto RetryFault;
870df8bae1dSRodney W. Grimes 			}
8717615edaaSMatthew Dillon 
8721c7c3c6aSMatthew Dillon 			/*
87363e97555SJeff Roberson 			 * The page is marked busy for other processes and the
8741c7c3c6aSMatthew Dillon 			 * pagedaemon.  If it still isn't completely valid
8751c7c3c6aSMatthew Dillon 			 * (readable), jump to readrest, else break-out ( we
8761c7c3c6aSMatthew Dillon 			 * found the page ).
8771c7c3c6aSMatthew Dillon 			 */
8780012f373SJeff Roberson 			if (!vm_page_all_valid(fs.m))
8790d94caffSDavid Greenman 				goto readrest;
880c6a70eaeSMark Johnston 			break; /* break to PAGE HAS BEEN FOUND */
881df8bae1dSRodney W. Grimes 		}
88210b4196bSAlan Cox 		KASSERT(fs.m == NULL, ("fs.m should be NULL, not %p", fs.m));
8831c7c3c6aSMatthew Dillon 
8841c7c3c6aSMatthew Dillon 		/*
88510b4196bSAlan Cox 		 * Page is not resident.  If the pager might contain the page
88610b4196bSAlan Cox 		 * or this is the beginning of the search, allocate a new
88710b4196bSAlan Cox 		 * page.  (Default objects are zero-fill, so there is no real
88810b4196bSAlan Cox 		 * pager for them.)
8891c7c3c6aSMatthew Dillon 		 */
8906a875bf9SKonstantin Belousov 		if (fs.object->type != OBJT_DEFAULT ||
8916a875bf9SKonstantin Belousov 		    fs.object == fs.first_object) {
892208b81bbSKonstantin Belousov 			if ((fs.object->flags & OBJ_SIZEVNLOCK) != 0) {
893208b81bbSKonstantin Belousov 				rv = vm_fault_lock_vnode(&fs);
894208b81bbSKonstantin Belousov 				MPASS(rv == KERN_SUCCESS ||
895208b81bbSKonstantin Belousov 				    rv == KERN_RESOURCE_SHORTAGE);
896208b81bbSKonstantin Belousov 				if (rv == KERN_RESOURCE_SHORTAGE)
897208b81bbSKonstantin Belousov 					goto RetryFault;
898208b81bbSKonstantin Belousov 			}
8994866e085SJohn Dyson 			if (fs.pindex >= fs.object->size) {
9004866e085SJohn Dyson 				unlock_and_deallocate(&fs);
901df08823dSKonstantin Belousov 				return (KERN_OUT_OF_BOUNDS);
9025f55e841SDavid Greenman 			}
90322ba64e8SJohn Dyson 
904c42b43a0SKonstantin Belousov 			if (fs.object == fs.first_object &&
905c42b43a0SKonstantin Belousov 			    (fs.first_object->flags & OBJ_POPULATE) != 0 &&
906c42b43a0SKonstantin Belousov 			    fs.first_object->shadow_count == 0) {
907d3f8534eSAlan Cox 				rv = vm_fault_populate(&fs, prot, fault_type,
908d3f8534eSAlan Cox 				    fault_flags, wired, m_hold);
909c42b43a0SKonstantin Belousov 				switch (rv) {
910c42b43a0SKonstantin Belousov 				case KERN_SUCCESS:
911c42b43a0SKonstantin Belousov 				case KERN_FAILURE:
912c42b43a0SKonstantin Belousov 					unlock_and_deallocate(&fs);
913c42b43a0SKonstantin Belousov 					return (rv);
914c42b43a0SKonstantin Belousov 				case KERN_RESOURCE_SHORTAGE:
915c42b43a0SKonstantin Belousov 					unlock_and_deallocate(&fs);
916c42b43a0SKonstantin Belousov 					goto RetryFault;
917c42b43a0SKonstantin Belousov 				case KERN_NOT_RECEIVER:
918c42b43a0SKonstantin Belousov 					/*
919c42b43a0SKonstantin Belousov 					 * Pager's populate() method
920c42b43a0SKonstantin Belousov 					 * returned VM_PAGER_BAD.
921c42b43a0SKonstantin Belousov 					 */
922c42b43a0SKonstantin Belousov 					break;
923c42b43a0SKonstantin Belousov 				default:
924c42b43a0SKonstantin Belousov 					panic("inconsistent return codes");
925c42b43a0SKonstantin Belousov 				}
926c42b43a0SKonstantin Belousov 			}
927c42b43a0SKonstantin Belousov 
928df8bae1dSRodney W. Grimes 			/*
9290d94caffSDavid Greenman 			 * Allocate a new page for this object/offset pair.
9303f1c4c4fSKonstantin Belousov 			 *
9313f1c4c4fSKonstantin Belousov 			 * Unlocked read of the p_flag is harmless. At
9323f1c4c4fSKonstantin Belousov 			 * worst, the P_KILLED might be not observed
9333f1c4c4fSKonstantin Belousov 			 * there, and allocation can fail, causing
9343f1c4c4fSKonstantin Belousov 			 * restart and new reading of the p_flag.
935df8bae1dSRodney W. Grimes 			 */
93623984ce5SMark Johnston 			dset = fs.object->domain.dr_policy;
93723984ce5SMark Johnston 			if (dset == NULL)
93823984ce5SMark Johnston 				dset = curthread->td_domain.dr_policy;
93923984ce5SMark Johnston 			if (!vm_page_count_severe_set(&dset->ds_mask) ||
94023984ce5SMark Johnston 			    P_KILLED(curproc)) {
941f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
9423d653db0SAlan Cox 				vm_object_color(fs.object, atop(vaddr) -
9433d653db0SAlan Cox 				    fs.pindex);
944f8a47341SAlan Cox #endif
9453f1c4c4fSKonstantin Belousov 				alloc_req = P_KILLED(curproc) ?
9463f1c4c4fSKonstantin Belousov 				    VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
9473f1c4c4fSKonstantin Belousov 				if (fs.object->type != OBJT_VNODE &&
9483f1c4c4fSKonstantin Belousov 				    fs.object->backing_object == NULL)
9493f1c4c4fSKonstantin Belousov 					alloc_req |= VM_ALLOC_ZERO;
9504866e085SJohn Dyson 				fs.m = vm_page_alloc(fs.object, fs.pindex,
9513f1c4c4fSKonstantin Belousov 				    alloc_req);
95240360b1bSMatthew Dillon 			}
9534866e085SJohn Dyson 			if (fs.m == NULL) {
9544866e085SJohn Dyson 				unlock_and_deallocate(&fs);
955245139c6SKonstantin Belousov 				if (vm_pfault_oom_attempts < 0 ||
956245139c6SKonstantin Belousov 				    oom < vm_pfault_oom_attempts) {
957245139c6SKonstantin Belousov 					oom++;
958245139c6SKonstantin Belousov 					vm_waitpfault(dset,
959245139c6SKonstantin Belousov 					    vm_pfault_oom_wait * hz);
960245139c6SKonstantin Belousov 					goto RetryFault_oom;
961245139c6SKonstantin Belousov 				}
962245139c6SKonstantin Belousov 				if (bootverbose)
963245139c6SKonstantin Belousov 					printf(
964245139c6SKonstantin Belousov 	"proc %d (%s) failed to alloc page on fault, starting OOM\n",
965245139c6SKonstantin Belousov 					    curproc->p_pid, curproc->p_comm);
966245139c6SKonstantin Belousov 				vm_pageout_oom(VM_OOM_MEM_PF);
967df8bae1dSRodney W. Grimes 				goto RetryFault;
9687667839aSAlan Cox 			}
969df8bae1dSRodney W. Grimes 		}
97047221757SJohn Dyson 
9710d94caffSDavid Greenman readrest:
9721c7c3c6aSMatthew Dillon 		/*
97385702505SAlan Cox 		 * At this point, we have either allocated a new page or found
97485702505SAlan Cox 		 * an existing page that is only partially valid.
97585702505SAlan Cox 		 *
97685702505SAlan Cox 		 * We hold a reference on the current object and the page is
97785702505SAlan Cox 		 * exclusive busied.
97885702505SAlan Cox 		 */
97985702505SAlan Cox 
98085702505SAlan Cox 		/*
9810c3a4893SAlan Cox 		 * If the pager for the current object might have the page,
9820c3a4893SAlan Cox 		 * then determine the number of additional pages to read and
9830c3a4893SAlan Cox 		 * potentially reprioritize previously read pages for earlier
9840c3a4893SAlan Cox 		 * reclamation.  These operations should only be performed
9850c3a4893SAlan Cox 		 * once per page fault.  Even if the current pager doesn't
9860c3a4893SAlan Cox 		 * have the page, the number of additional pages to read will
9870c3a4893SAlan Cox 		 * apply to subsequent objects in the shadow chain.
9881c7c3c6aSMatthew Dillon 		 */
9890c3a4893SAlan Cox 		if (fs.object->type != OBJT_DEFAULT && nera == -1 &&
9900c3a4893SAlan Cox 		    !P_KILLED(curproc)) {
9910c3a4893SAlan Cox 			KASSERT(fs.lookup_still_valid, ("map unlocked"));
9925268042bSAlan Cox 			era = fs.entry->read_ahead;
9930c3a4893SAlan Cox 			behavior = vm_map_entry_behavior(fs.entry);
9940c3a4893SAlan Cox 			if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
9955268042bSAlan Cox 				nera = 0;
99613458803SAlan Cox 			} else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
9975268042bSAlan Cox 				nera = VM_FAULT_READ_AHEAD_MAX;
998381b7242SAlan Cox 				if (vaddr == fs.entry->next_read)
9990c3a4893SAlan Cox 					vm_fault_dontneed(&fs, vaddr, nera);
1000381b7242SAlan Cox 			} else if (vaddr == fs.entry->next_read) {
100113458803SAlan Cox 				/*
10025268042bSAlan Cox 				 * This is a sequential fault.  Arithmetically
10035268042bSAlan Cox 				 * increase the requested number of pages in
10045268042bSAlan Cox 				 * the read-ahead window.  The requested
10055268042bSAlan Cox 				 * number of pages is "# of sequential faults
10065268042bSAlan Cox 				 * x (read ahead min + 1) + read ahead min"
100713458803SAlan Cox 				 */
10085268042bSAlan Cox 				nera = VM_FAULT_READ_AHEAD_MIN;
10095268042bSAlan Cox 				if (era > 0) {
10105268042bSAlan Cox 					nera += era + 1;
101113458803SAlan Cox 					if (nera > VM_FAULT_READ_AHEAD_MAX)
101213458803SAlan Cox 						nera = VM_FAULT_READ_AHEAD_MAX;
10135268042bSAlan Cox 				}
101413458803SAlan Cox 				if (era == VM_FAULT_READ_AHEAD_MAX)
10150c3a4893SAlan Cox 					vm_fault_dontneed(&fs, vaddr, nera);
10165268042bSAlan Cox 			} else {
10175268042bSAlan Cox 				/*
10180c3a4893SAlan Cox 				 * This is a non-sequential fault.
10195268042bSAlan Cox 				 */
10205268042bSAlan Cox 				nera = 0;
1021867a482dSJohn Dyson 			}
10220c3a4893SAlan Cox 			if (era != nera) {
10230c3a4893SAlan Cox 				/*
10240c3a4893SAlan Cox 				 * A read lock on the map suffices to update
10250c3a4893SAlan Cox 				 * the read ahead count safely.
10260c3a4893SAlan Cox 				 */
10275268042bSAlan Cox 				fs.entry->read_ahead = nera;
10280c3a4893SAlan Cox 			}
1029d2bf64c3SKonstantin Belousov 
1030d2bf64c3SKonstantin Belousov 			/*
10310c3a4893SAlan Cox 			 * Prepare for unlocking the map.  Save the map
10320c3a4893SAlan Cox 			 * entry's start and end addresses, which are used to
10330c3a4893SAlan Cox 			 * optimize the size of the pager operation below.
10340c3a4893SAlan Cox 			 * Even if the map entry's addresses change after
10350c3a4893SAlan Cox 			 * unlocking the map, using the saved addresses is
10360c3a4893SAlan Cox 			 * safe.
10370c3a4893SAlan Cox 			 */
10380c3a4893SAlan Cox 			e_start = fs.entry->start;
10390c3a4893SAlan Cox 			e_end = fs.entry->end;
10400c3a4893SAlan Cox 		}
10410c3a4893SAlan Cox 
10420c3a4893SAlan Cox 		/*
10430c3a4893SAlan Cox 		 * Call the pager to retrieve the page if there is a chance
10440c3a4893SAlan Cox 		 * that the pager has it, and potentially retrieve additional
10450c3a4893SAlan Cox 		 * pages at the same time.
10460c3a4893SAlan Cox 		 */
10470c3a4893SAlan Cox 		if (fs.object->type != OBJT_DEFAULT) {
10480c3a4893SAlan Cox 			/*
104985702505SAlan Cox 			 * Release the map lock before locking the vnode or
105085702505SAlan Cox 			 * sleeping in the pager.  (If the current object has
105185702505SAlan Cox 			 * a shadow, then an earlier iteration of this loop
105285702505SAlan Cox 			 * may have already unlocked the map.)
1053d2bf64c3SKonstantin Belousov 			 */
1054d2bf64c3SKonstantin Belousov 			unlock_map(&fs);
1055d2bf64c3SKonstantin Belousov 
10560ddd3082SKonstantin Belousov 			rv = vm_fault_lock_vnode(&fs);
10570ddd3082SKonstantin Belousov 			MPASS(rv == KERN_SUCCESS ||
10580ddd3082SKonstantin Belousov 			    rv == KERN_RESOURCE_SHORTAGE);
10590ddd3082SKonstantin Belousov 			if (rv == KERN_RESOURCE_SHORTAGE)
1060d2bf64c3SKonstantin Belousov 				goto RetryFault;
1061d2bf64c3SKonstantin Belousov 			KASSERT(fs.vp == NULL || !fs.map->system_map,
1062d2bf64c3SKonstantin Belousov 			    ("vm_fault: vnode-backed object mapped by system map"));
1063d2bf64c3SKonstantin Belousov 
1064df8bae1dSRodney W. Grimes 			/*
1065b0cd2017SGleb Smirnoff 			 * Page in the requested page and hint the pager,
1066b0cd2017SGleb Smirnoff 			 * that it may bring up surrounding pages.
106726f9a767SRodney W. Grimes 			 */
10680c3a4893SAlan Cox 			if (nera == -1 || behavior == MAP_ENTRY_BEHAV_RANDOM ||
10690c3a4893SAlan Cox 			    P_KILLED(curproc)) {
10700c3a4893SAlan Cox 				behind = 0;
10710c3a4893SAlan Cox 				ahead = 0;
10720c3a4893SAlan Cox 			} else {
10730c3a4893SAlan Cox 				/* Is this a sequential fault? */
10740c3a4893SAlan Cox 				if (nera > 0) {
10750c3a4893SAlan Cox 					behind = 0;
10760c3a4893SAlan Cox 					ahead = nera;
10770c3a4893SAlan Cox 				} else {
10780c3a4893SAlan Cox 					/*
10790c3a4893SAlan Cox 					 * Request a cluster of pages that is
10800c3a4893SAlan Cox 					 * aligned to a VM_FAULT_READ_DEFAULT
10810c3a4893SAlan Cox 					 * page offset boundary within the
10820c3a4893SAlan Cox 					 * object.  Alignment to a page offset
10830c3a4893SAlan Cox 					 * boundary is more likely to coincide
10840c3a4893SAlan Cox 					 * with the underlying file system
10850c3a4893SAlan Cox 					 * block than alignment to a virtual
10860c3a4893SAlan Cox 					 * address boundary.
10870c3a4893SAlan Cox 					 */
10880c3a4893SAlan Cox 					cluster_offset = fs.pindex %
10890c3a4893SAlan Cox 					    VM_FAULT_READ_DEFAULT;
10900c3a4893SAlan Cox 					behind = ulmin(cluster_offset,
10910c3a4893SAlan Cox 					    atop(vaddr - e_start));
10920c3a4893SAlan Cox 					ahead = VM_FAULT_READ_DEFAULT - 1 -
10930c3a4893SAlan Cox 					    cluster_offset;
10940c3a4893SAlan Cox 				}
10950c3a4893SAlan Cox 				ahead = ulmin(ahead, atop(e_end - vaddr) - 1);
10960c3a4893SAlan Cox 			}
1097b0cd2017SGleb Smirnoff 			rv = vm_pager_get_pages(fs.object, &fs.m, 1,
1098b0cd2017SGleb Smirnoff 			    &behind, &ahead);
109926f9a767SRodney W. Grimes 			if (rv == VM_PAGER_OK) {
1100b0cd2017SGleb Smirnoff 				faultcount = behind + 1 + ahead;
1101320023e2SAlan Cox 				hardfault = true;
11021c7c3c6aSMatthew Dillon 				break; /* break to PAGE HAS BEEN FOUND */
1103df8bae1dSRodney W. Grimes 			}
1104a83c285cSDavid Greenman 			if (rv == VM_PAGER_ERROR)
1105f3679e35SDavid Greenman 				printf("vm_fault: pager read error, pid %d (%s)\n",
1106f3679e35SDavid Greenman 				    curproc->p_pid, curproc->p_comm);
1107521ddf39SAlan Cox 
110826f9a767SRodney W. Grimes 			/*
1109521ddf39SAlan Cox 			 * If an I/O error occurred or the requested page was
1110521ddf39SAlan Cox 			 * outside the range of the pager, clean up and return
1111521ddf39SAlan Cox 			 * an error.
111226f9a767SRodney W. Grimes 			 */
1113521ddf39SAlan Cox 			if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) {
1114d842aa51SMark Johnston 				if (!vm_page_wired(fs.m))
11154866e085SJohn Dyson 					vm_page_free(fs.m);
1116230afe0bSKonstantin Belousov 				else
1117fee2a2faSMark Johnston 					vm_page_xunbusy(fs.m);
11184866e085SJohn Dyson 				fs.m = NULL;
11194866e085SJohn Dyson 				unlock_and_deallocate(&fs);
1120df08823dSKonstantin Belousov 				return (KERN_OUT_OF_BOUNDS);
112126f9a767SRodney W. Grimes 			}
1122521ddf39SAlan Cox 
1123521ddf39SAlan Cox 			/*
1124521ddf39SAlan Cox 			 * The requested page does not exist at this object/
1125521ddf39SAlan Cox 			 * offset.  Remove the invalid page from the object,
1126521ddf39SAlan Cox 			 * waking up anyone waiting for it, and continue on to
1127521ddf39SAlan Cox 			 * the next object.  However, if this is the top-level
1128521ddf39SAlan Cox 			 * object, we must leave the busy page in place to
1129521ddf39SAlan Cox 			 * prevent another process from rushing past us, and
1130521ddf39SAlan Cox 			 * inserting the page in that object at the same time
1131521ddf39SAlan Cox 			 * that we are.
1132521ddf39SAlan Cox 			 */
11334866e085SJohn Dyson 			if (fs.object != fs.first_object) {
1134d842aa51SMark Johnston 				if (!vm_page_wired(fs.m))
11354866e085SJohn Dyson 					vm_page_free(fs.m);
1136230afe0bSKonstantin Belousov 				else
1137fee2a2faSMark Johnston 					vm_page_xunbusy(fs.m);
11384866e085SJohn Dyson 				fs.m = NULL;
1139df8bae1dSRodney W. Grimes 			}
1140df8bae1dSRodney W. Grimes 		}
114140360b1bSMatthew Dillon 
1142df8bae1dSRodney W. Grimes 		/*
11431c7c3c6aSMatthew Dillon 		 * We get here if the object has default pager (or unwiring)
11441c7c3c6aSMatthew Dillon 		 * or the pager doesn't have the page.
1145df8bae1dSRodney W. Grimes 		 */
11464866e085SJohn Dyson 		if (fs.object == fs.first_object)
11474866e085SJohn Dyson 			fs.first_m = fs.m;
1148df8bae1dSRodney W. Grimes 
1149df8bae1dSRodney W. Grimes 		/*
11500d94caffSDavid Greenman 		 * Move on to the next object.  Lock the next object before
11510d94caffSDavid Greenman 		 * unlocking the current one.
1152df8bae1dSRodney W. Grimes 		 */
11534866e085SJohn Dyson 		next_object = fs.object->backing_object;
1154df8bae1dSRodney W. Grimes 		if (next_object == NULL) {
1155df8bae1dSRodney W. Grimes 			/*
11560d94caffSDavid Greenman 			 * If there's no object left, fill the page in the top
11570d94caffSDavid Greenman 			 * object with zeros.
1158df8bae1dSRodney W. Grimes 			 */
11594866e085SJohn Dyson 			if (fs.object != fs.first_object) {
11604866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
116189f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(fs.object);
1162df8bae1dSRodney W. Grimes 
11634866e085SJohn Dyson 				fs.object = fs.first_object;
11644866e085SJohn Dyson 				fs.pindex = fs.first_pindex;
11654866e085SJohn Dyson 				fs.m = fs.first_m;
116689f6b863SAttilio Rao 				VM_OBJECT_WLOCK(fs.object);
1167df8bae1dSRodney W. Grimes 			}
11684866e085SJohn Dyson 			fs.first_m = NULL;
1169df8bae1dSRodney W. Grimes 
11704221e284SAlan Cox 			/*
11714221e284SAlan Cox 			 * Zero the page if necessary and mark it valid.
11724221e284SAlan Cox 			 */
11734866e085SJohn Dyson 			if ((fs.m->flags & PG_ZERO) == 0) {
1174fff6062aSAlan Cox 				pmap_zero_page(fs.m);
11754221e284SAlan Cox 			} else {
117683c9dea1SGleb Smirnoff 				VM_CNT_INC(v_ozfod);
11774221e284SAlan Cox 			}
117883c9dea1SGleb Smirnoff 			VM_CNT_INC(v_zfod);
11790012f373SJeff Roberson 			vm_page_valid(fs.m);
11807b9b301cSAlan Cox 			/* Don't try to prefault neighboring pages. */
11817b9b301cSAlan Cox 			faultcount = 1;
11821c7c3c6aSMatthew Dillon 			break;	/* break to PAGE HAS BEEN FOUND */
11830d94caffSDavid Greenman 		} else {
1184c8567c3aSAlan Cox 			KASSERT(fs.object != next_object,
1185c8567c3aSAlan Cox 			    ("object loop %p", next_object));
118689f6b863SAttilio Rao 			VM_OBJECT_WLOCK(next_object);
1187c8567c3aSAlan Cox 			vm_object_pip_add(next_object, 1);
1188c8567c3aSAlan Cox 			if (fs.object != fs.first_object)
11894866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
11906753423cSAlan Cox 			fs.pindex +=
11916753423cSAlan Cox 			    OFF_TO_IDX(fs.object->backing_object_offset);
119289f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(fs.object);
11934866e085SJohn Dyson 			fs.object = next_object;
1194df8bae1dSRodney W. Grimes 		}
1195df8bae1dSRodney W. Grimes 	}
11961c7c3c6aSMatthew Dillon 
1197c7aebda8SAttilio Rao 	vm_page_assert_xbusied(fs.m);
1198df8bae1dSRodney W. Grimes 
1199df8bae1dSRodney W. Grimes 	/*
12000d94caffSDavid Greenman 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1201df8bae1dSRodney W. Grimes 	 * is held.]
1202df8bae1dSRodney W. Grimes 	 */
1203df8bae1dSRodney W. Grimes 
1204df8bae1dSRodney W. Grimes 	/*
12050d94caffSDavid Greenman 	 * If the page is being written, but isn't already owned by the
12060d94caffSDavid Greenman 	 * top-level object, we have to copy it into a new page owned by the
12070d94caffSDavid Greenman 	 * top-level object.
1208df8bae1dSRodney W. Grimes 	 */
12094866e085SJohn Dyson 	if (fs.object != fs.first_object) {
1210df8bae1dSRodney W. Grimes 		/*
12110d94caffSDavid Greenman 		 * We only really need to copy if we want to write it.
1212df8bae1dSRodney W. Grimes 		 */
1213a6d42a0dSAlan Cox 		if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1214df8bae1dSRodney W. Grimes 			/*
12151c7c3c6aSMatthew Dillon 			 * This allows pages to be virtually copied from a
12161c7c3c6aSMatthew Dillon 			 * backing_object into the first_object, where the
12171c7c3c6aSMatthew Dillon 			 * backing object has no other refs to it, and cannot
12181c7c3c6aSMatthew Dillon 			 * gain any more refs.  Instead of a bcopy, we just
12191c7c3c6aSMatthew Dillon 			 * move the page from the backing object to the
12201c7c3c6aSMatthew Dillon 			 * first object.  Note that we must mark the page
12211c7c3c6aSMatthew Dillon 			 * dirty in the first object so that it will go out
12221c7c3c6aSMatthew Dillon 			 * to swap when needed.
1223df8bae1dSRodney W. Grimes 			 */
1224cd8a6fe8SAlan Cox 			is_first_object_locked = false;
1225e50346b5SAlan Cox 			if (
1226de5f6a77SJohn Dyson 				/*
1227de5f6a77SJohn Dyson 				 * Only one shadow object
1228de5f6a77SJohn Dyson 				 */
12294866e085SJohn Dyson 				(fs.object->shadow_count == 1) &&
1230de5f6a77SJohn Dyson 				/*
1231de5f6a77SJohn Dyson 				 * No COW refs, except us
1232de5f6a77SJohn Dyson 				 */
12334866e085SJohn Dyson 				(fs.object->ref_count == 1) &&
1234de5f6a77SJohn Dyson 				/*
12355929bcfaSPhilippe Charnier 				 * No one else can look this object up
1236de5f6a77SJohn Dyson 				 */
12374866e085SJohn Dyson 				(fs.object->handle == NULL) &&
1238de5f6a77SJohn Dyson 				/*
1239de5f6a77SJohn Dyson 				 * No other ways to look the object up
1240de5f6a77SJohn Dyson 				 */
12414866e085SJohn Dyson 				((fs.object->type == OBJT_DEFAULT) ||
12424866e085SJohn Dyson 				 (fs.object->type == OBJT_SWAP)) &&
124389f6b863SAttilio Rao 			    (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
1244de5f6a77SJohn Dyson 				/*
1245de5f6a77SJohn Dyson 				 * We don't chase down the shadow chain
1246de5f6a77SJohn Dyson 				 */
1247e50346b5SAlan Cox 			    fs.object == fs.first_object->backing_object) {
1248fee2a2faSMark Johnston 
12490fd977b3SMark Johnston 				(void)vm_page_remove(fs.m);
1250bccdea45SAlan Cox 				vm_page_replace_checked(fs.m, fs.first_object,
1251bccdea45SAlan Cox 				    fs.first_pindex, fs.first_m);
12526fee422eSConrad Meyer 				vm_page_free(fs.first_m);
1253bccdea45SAlan Cox 				vm_page_dirty(fs.m);
1254dfdf9abdSAlan Cox #if VM_NRESERVLEVEL > 0
1255dfdf9abdSAlan Cox 				/*
1256dfdf9abdSAlan Cox 				 * Rename the reservation.
1257dfdf9abdSAlan Cox 				 */
1258dfdf9abdSAlan Cox 				vm_reserv_rename(fs.m, fs.first_object,
1259dfdf9abdSAlan Cox 				    fs.object, OFF_TO_IDX(
1260dfdf9abdSAlan Cox 				    fs.first_object->backing_object_offset));
1261dfdf9abdSAlan Cox #endif
1262d98ddc46SAlan Cox 				fs.first_m = fs.m;
12634866e085SJohn Dyson 				fs.m = NULL;
126483c9dea1SGleb Smirnoff 				VM_CNT_INC(v_cow_optim);
1265de5f6a77SJohn Dyson 			} else {
1266de5f6a77SJohn Dyson 				/*
1267de5f6a77SJohn Dyson 				 * Oh, well, lets copy it.
1268de5f6a77SJohn Dyson 				 */
1269669890eaSAlan Cox 				pmap_copy_page(fs.m, fs.first_m);
12700012f373SJeff Roberson 				vm_page_valid(fs.first_m);
1271d8778512SAlan Cox 				if (wired && (fault_flags &
12726a875bf9SKonstantin Belousov 				    VM_FAULT_WIRE) == 0) {
1273d8778512SAlan Cox 					vm_page_wire(fs.first_m);
12743ae10f74SAttilio Rao 					vm_page_unwire(fs.m, PQ_INACTIVE);
1275de5f6a77SJohn Dyson 				}
1276df8bae1dSRodney W. Grimes 				/*
1277df8bae1dSRodney W. Grimes 				 * We no longer need the old page or object.
1278df8bae1dSRodney W. Grimes 				 */
12794866e085SJohn Dyson 				release_page(&fs);
1280de5f6a77SJohn Dyson 			}
12811c7c3c6aSMatthew Dillon 			/*
12821c7c3c6aSMatthew Dillon 			 * fs.object != fs.first_object due to above
12831c7c3c6aSMatthew Dillon 			 * conditional
12841c7c3c6aSMatthew Dillon 			 */
12854866e085SJohn Dyson 			vm_object_pip_wakeup(fs.object);
128689f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(fs.object);
12879f1abe3dSAlan Cox 
12889f1abe3dSAlan Cox 			/*
12899f1abe3dSAlan Cox 			 * We only try to prefault read-only mappings to the
12909f1abe3dSAlan Cox 			 * neighboring pages when this copy-on-write fault is
12919f1abe3dSAlan Cox 			 * a hard fault.  In other cases, trying to prefault
12929f1abe3dSAlan Cox 			 * is typically wasted effort.
12939f1abe3dSAlan Cox 			 */
12949f1abe3dSAlan Cox 			if (faultcount == 0)
12959f1abe3dSAlan Cox 				faultcount = 1;
12969f1abe3dSAlan Cox 
1297df8bae1dSRodney W. Grimes 			/*
1298df8bae1dSRodney W. Grimes 			 * Only use the new page below...
1299df8bae1dSRodney W. Grimes 			 */
13004866e085SJohn Dyson 			fs.object = fs.first_object;
13014866e085SJohn Dyson 			fs.pindex = fs.first_pindex;
1302d98ddc46SAlan Cox 			fs.m = fs.first_m;
1303f29ba63eSAlan Cox 			if (!is_first_object_locked)
130489f6b863SAttilio Rao 				VM_OBJECT_WLOCK(fs.object);
130583c9dea1SGleb Smirnoff 			VM_CNT_INC(v_cow_faults);
13064d34e019SKonstantin Belousov 			curthread->td_cow++;
13070d94caffSDavid Greenman 		} else {
1308df8bae1dSRodney W. Grimes 			prot &= ~VM_PROT_WRITE;
1309df8bae1dSRodney W. Grimes 		}
1310df8bae1dSRodney W. Grimes 	}
1311df8bae1dSRodney W. Grimes 
1312df8bae1dSRodney W. Grimes 	/*
13130d94caffSDavid Greenman 	 * We must verify that the maps have not changed since our last
13140d94caffSDavid Greenman 	 * lookup.
1315df8bae1dSRodney W. Grimes 	 */
131619dc5607STor Egge 	if (!fs.lookup_still_valid) {
131719dc5607STor Egge 		if (!vm_map_trylock_read(fs.map)) {
1318b823bbd6SMatthew Dillon 			release_page(&fs);
1319b823bbd6SMatthew Dillon 			unlock_and_deallocate(&fs);
1320b823bbd6SMatthew Dillon 			goto RetryFault;
1321b823bbd6SMatthew Dillon 		}
1322cd8a6fe8SAlan Cox 		fs.lookup_still_valid = true;
1323dc5401d2SKonstantin Belousov 		if (fs.map->timestamp != fs.map_generation) {
132419dc5607STor Egge 			result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
13254866e085SJohn Dyson 			    &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
1326df8bae1dSRodney W. Grimes 
1327df8bae1dSRodney W. Grimes 			/*
132844ed3417STor Egge 			 * If we don't need the page any longer, put it on the inactive
13290d94caffSDavid Greenman 			 * list (the easiest thing to do here).  If no one needs it,
13300d94caffSDavid Greenman 			 * pageout will grab it eventually.
1331df8bae1dSRodney W. Grimes 			 */
1332df8bae1dSRodney W. Grimes 			if (result != KERN_SUCCESS) {
13334866e085SJohn Dyson 				release_page(&fs);
13344866e085SJohn Dyson 				unlock_and_deallocate(&fs);
133519dc5607STor Egge 
133619dc5607STor Egge 				/*
133719dc5607STor Egge 				 * If retry of map lookup would have blocked then
133819dc5607STor Egge 				 * retry fault from start.
133919dc5607STor Egge 				 */
134019dc5607STor Egge 				if (result == KERN_FAILURE)
134119dc5607STor Egge 					goto RetryFault;
1342df8bae1dSRodney W. Grimes 				return (result);
1343df8bae1dSRodney W. Grimes 			}
13444866e085SJohn Dyson 			if ((retry_object != fs.first_object) ||
13454866e085SJohn Dyson 			    (retry_pindex != fs.first_pindex)) {
13464866e085SJohn Dyson 				release_page(&fs);
13474866e085SJohn Dyson 				unlock_and_deallocate(&fs);
1348df8bae1dSRodney W. Grimes 				goto RetryFault;
1349df8bae1dSRodney W. Grimes 			}
135019dc5607STor Egge 
1351df8bae1dSRodney W. Grimes 			/*
13520d94caffSDavid Greenman 			 * Check whether the protection has changed or the object has
13530d94caffSDavid Greenman 			 * been copied while we left the map unlocked. Changing from
13540d94caffSDavid Greenman 			 * read to write permission is OK - we leave the page
13550d94caffSDavid Greenman 			 * write-protected, and catch the write fault. Changing from
13560d94caffSDavid Greenman 			 * write to read permission means that we can't mark the page
13570d94caffSDavid Greenman 			 * write-enabled after all.
1358df8bae1dSRodney W. Grimes 			 */
1359df8bae1dSRodney W. Grimes 			prot &= retry_prot;
1360607970bcSKonstantin Belousov 			fault_type &= retry_prot;
1361607970bcSKonstantin Belousov 			if (prot == 0) {
1362607970bcSKonstantin Belousov 				release_page(&fs);
1363607970bcSKonstantin Belousov 				unlock_and_deallocate(&fs);
1364607970bcSKonstantin Belousov 				goto RetryFault;
1365607970bcSKonstantin Belousov 			}
1366fccdefa1SAlan Cox 
1367fccdefa1SAlan Cox 			/* Reassert because wired may have changed. */
1368fccdefa1SAlan Cox 			KASSERT(wired || (fault_flags & VM_FAULT_WIRE) == 0,
1369fccdefa1SAlan Cox 			    ("!wired && VM_FAULT_WIRE"));
1370df8bae1dSRodney W. Grimes 		}
137119dc5607STor Egge 	}
1372381b7242SAlan Cox 
1373d2bf64c3SKonstantin Belousov 	/*
1374381b7242SAlan Cox 	 * If the page was filled by a pager, save the virtual address that
1375381b7242SAlan Cox 	 * should be faulted on next under a sequential access pattern to the
1376381b7242SAlan Cox 	 * map entry.  A read lock on the map suffices to update this address
1377381b7242SAlan Cox 	 * safely.
1378d2bf64c3SKonstantin Belousov 	 */
13795758fe71SAlan Cox 	if (hardfault)
1380381b7242SAlan Cox 		fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE;
1381d2bf64c3SKonstantin Belousov 
1382c7aebda8SAttilio Rao 	vm_page_assert_xbusied(fs.m);
1383205be21dSJeff Roberson 	vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags, true);
1384c7aebda8SAttilio Rao 
13854221e284SAlan Cox 	/*
138678cfe1f7SAlan Cox 	 * Page must be completely valid or it is not fit to
13874221e284SAlan Cox 	 * map into user space.  vm_pager_get_pages() ensures this.
13884221e284SAlan Cox 	 */
13890012f373SJeff Roberson 	KASSERT(vm_page_all_valid(fs.m),
139078cfe1f7SAlan Cox 	    ("vm_fault: page %p partially invalid", fs.m));
139189f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(fs.object);
1392cbfbaad8SAlan Cox 
139386735996SAlan Cox 	/*
139486735996SAlan Cox 	 * Put this page into the physical map.  We had to do the unlock above
139586735996SAlan Cox 	 * because pmap_enter() may sleep.  We don't put the page
139686735996SAlan Cox 	 * back on the active queue until later so that the pageout daemon
139786735996SAlan Cox 	 * won't find it (yet).
139886735996SAlan Cox 	 */
139939ffa8c1SKonstantin Belousov 	pmap_enter(fs.map->pmap, vaddr, fs.m, prot,
140039ffa8c1SKonstantin Belousov 	    fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0);
14016a875bf9SKonstantin Belousov 	if (faultcount != 1 && (fault_flags & VM_FAULT_WIRE) == 0 &&
14027b9b301cSAlan Cox 	    wired == 0)
1403b0cd2017SGleb Smirnoff 		vm_fault_prefault(&fs, vaddr,
1404b0cd2017SGleb Smirnoff 		    faultcount > 0 ? behind : PFBAK,
1405a7163bb9SKonstantin Belousov 		    faultcount > 0 ? ahead : PFFOR, false);
140689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(fs.object);
1407ff97964aSJohn Dyson 
1408df8bae1dSRodney W. Grimes 	/*
14090d94caffSDavid Greenman 	 * If the page is not wired down, then put it where the pageout daemon
14100d94caffSDavid Greenman 	 * can find it.
1411df8bae1dSRodney W. Grimes 	 */
1412fee2a2faSMark Johnston 	if ((fault_flags & VM_FAULT_WIRE) != 0) {
14134866e085SJohn Dyson 		vm_page_wire(fs.m);
1414fee2a2faSMark Johnston 	} else {
1415e8bcf696SMark Johnston 		vm_page_lock(fs.m);
14164866e085SJohn Dyson 		vm_page_activate(fs.m);
1417e8bcf696SMark Johnston 		vm_page_unlock(fs.m);
1418fee2a2faSMark Johnston 	}
1419acd11c74SAlan Cox 	if (m_hold != NULL) {
1420acd11c74SAlan Cox 		*m_hold = fs.m;
1421eeacb3b0SMark Johnston 		vm_page_wire(fs.m);
1422acd11c74SAlan Cox 	}
1423c7aebda8SAttilio Rao 	vm_page_xunbusy(fs.m);
1424eeec6babSJohn Baldwin 
1425eebf3286SAlan Cox 	/*
1426eebf3286SAlan Cox 	 * Unlock everything, and return
1427eebf3286SAlan Cox 	 */
1428eebf3286SAlan Cox 	unlock_and_deallocate(&fs);
1429b3a01bdfSAndrey Zonov 	if (hardfault) {
143083c9dea1SGleb Smirnoff 		VM_CNT_INC(v_io_faults);
14311c4bcd05SJeff Roberson 		curthread->td_ru.ru_majflt++;
1432ae34b6ffSEdward Tomasz Napierala #ifdef RACCT
1433ae34b6ffSEdward Tomasz Napierala 		if (racct_enable && fs.object->type == OBJT_VNODE) {
1434ae34b6ffSEdward Tomasz Napierala 			PROC_LOCK(curproc);
1435ae34b6ffSEdward Tomasz Napierala 			if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1436ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_WRITEBPS,
1437ae34b6ffSEdward Tomasz Napierala 				    PAGE_SIZE + behind * PAGE_SIZE);
1438ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_WRITEIOPS, 1);
1439ae34b6ffSEdward Tomasz Napierala 			} else {
1440ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_READBPS,
1441ae34b6ffSEdward Tomasz Napierala 				    PAGE_SIZE + ahead * PAGE_SIZE);
1442ae34b6ffSEdward Tomasz Napierala 				racct_add_force(curproc, RACCT_READIOPS, 1);
1443ae34b6ffSEdward Tomasz Napierala 			}
1444ae34b6ffSEdward Tomasz Napierala 			PROC_UNLOCK(curproc);
1445ae34b6ffSEdward Tomasz Napierala 		}
1446ae34b6ffSEdward Tomasz Napierala #endif
1447b3a01bdfSAndrey Zonov 	} else
14481c4bcd05SJeff Roberson 		curthread->td_ru.ru_minflt++;
1449df8bae1dSRodney W. Grimes 
1450df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1451df8bae1dSRodney W. Grimes }
1452df8bae1dSRodney W. Grimes 
1453df8bae1dSRodney W. Grimes /*
1454a8b0f100SAlan Cox  * Speed up the reclamation of pages that precede the faulting pindex within
1455a8b0f100SAlan Cox  * the first object of the shadow chain.  Essentially, perform the equivalent
1456a8b0f100SAlan Cox  * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes
1457a8b0f100SAlan Cox  * the faulting pindex by the cluster size when the pages read by vm_fault()
1458a8b0f100SAlan Cox  * cross a cluster-size boundary.  The cluster size is the greater of the
1459a8b0f100SAlan Cox  * smallest superpage size and VM_FAULT_DONTNEED_MIN.
1460a8b0f100SAlan Cox  *
1461a8b0f100SAlan Cox  * When "fs->first_object" is a shadow object, the pages in the backing object
1462a8b0f100SAlan Cox  * that precede the faulting pindex are deactivated by vm_fault().  So, this
1463a8b0f100SAlan Cox  * function must only be concerned with pages in the first object.
146413458803SAlan Cox  */
146513458803SAlan Cox static void
1466a8b0f100SAlan Cox vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead)
146713458803SAlan Cox {
1468a8b0f100SAlan Cox 	vm_map_entry_t entry;
146913458803SAlan Cox 	vm_object_t first_object, object;
1470a8b0f100SAlan Cox 	vm_offset_t end, start;
1471a8b0f100SAlan Cox 	vm_page_t m, m_next;
1472a8b0f100SAlan Cox 	vm_pindex_t pend, pstart;
1473a8b0f100SAlan Cox 	vm_size_t size;
147413458803SAlan Cox 
147513458803SAlan Cox 	object = fs->object;
147689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
147713458803SAlan Cox 	first_object = fs->first_object;
147813458803SAlan Cox 	if (first_object != object) {
1479b5ab20c0SAlan Cox 		if (!VM_OBJECT_TRYWLOCK(first_object)) {
148089f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
1481b5ab20c0SAlan Cox 			VM_OBJECT_WLOCK(first_object);
148289f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
148313458803SAlan Cox 		}
148413458803SAlan Cox 	}
1485a8b0f100SAlan Cox 	/* Neither fictitious nor unmanaged pages can be reclaimed. */
148628634820SAlan Cox 	if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
1487a8b0f100SAlan Cox 		size = VM_FAULT_DONTNEED_MIN;
1488a8b0f100SAlan Cox 		if (MAXPAGESIZES > 1 && size < pagesizes[1])
1489a8b0f100SAlan Cox 			size = pagesizes[1];
1490a8b0f100SAlan Cox 		end = rounddown2(vaddr, size);
1491a8b0f100SAlan Cox 		if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) &&
1492a8b0f100SAlan Cox 		    (entry = fs->entry)->start < end) {
1493a8b0f100SAlan Cox 			if (end - entry->start < size)
1494a8b0f100SAlan Cox 				start = entry->start;
149513458803SAlan Cox 			else
1496a8b0f100SAlan Cox 				start = end - size;
1497a8b0f100SAlan Cox 			pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED);
1498a8b0f100SAlan Cox 			pstart = OFF_TO_IDX(entry->offset) + atop(start -
1499a8b0f100SAlan Cox 			    entry->start);
1500a8b0f100SAlan Cox 			m_next = vm_page_find_least(first_object, pstart);
1501a8b0f100SAlan Cox 			pend = OFF_TO_IDX(entry->offset) + atop(end -
1502a8b0f100SAlan Cox 			    entry->start);
1503a8b0f100SAlan Cox 			while ((m = m_next) != NULL && m->pindex < pend) {
1504a8b0f100SAlan Cox 				m_next = TAILQ_NEXT(m, listq);
15050012f373SJeff Roberson 				if (!vm_page_all_valid(m) ||
1506a8b0f100SAlan Cox 				    vm_page_busied(m))
150713458803SAlan Cox 					continue;
1508d8015db3SAlan Cox 
1509d8015db3SAlan Cox 				/*
1510d8015db3SAlan Cox 				 * Don't clear PGA_REFERENCED, since it would
1511d8015db3SAlan Cox 				 * likely represent a reference by a different
1512d8015db3SAlan Cox 				 * process.
1513d8015db3SAlan Cox 				 *
1514d8015db3SAlan Cox 				 * Typically, at this point, prefetched pages
1515d8015db3SAlan Cox 				 * are still in the inactive queue.  Only
1516d8015db3SAlan Cox 				 * pages that triggered page faults are in the
1517d8015db3SAlan Cox 				 * active queue.
1518d8015db3SAlan Cox 				 */
151913458803SAlan Cox 				vm_page_lock(m);
15200eb50f9cSMark Johnston 				if (!vm_page_inactive(m))
1521d8015db3SAlan Cox 					vm_page_deactivate(m);
152213458803SAlan Cox 				vm_page_unlock(m);
152313458803SAlan Cox 			}
152413458803SAlan Cox 		}
1525a8b0f100SAlan Cox 	}
152613458803SAlan Cox 	if (first_object != object)
1527b5ab20c0SAlan Cox 		VM_OBJECT_WUNLOCK(first_object);
152813458803SAlan Cox }
152913458803SAlan Cox 
153013458803SAlan Cox /*
1531566526a9SAlan Cox  * vm_fault_prefault provides a quick way of clustering
1532566526a9SAlan Cox  * pagefaults into a processes address space.  It is a "cousin"
1533566526a9SAlan Cox  * of vm_map_pmap_enter, except it runs at page fault time instead
1534566526a9SAlan Cox  * of mmap time.
1535566526a9SAlan Cox  */
1536566526a9SAlan Cox static void
153763281952SAlan Cox vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
1538a7163bb9SKonstantin Belousov     int backward, int forward, bool obj_locked)
1539566526a9SAlan Cox {
154063281952SAlan Cox 	pmap_t pmap;
154163281952SAlan Cox 	vm_map_entry_t entry;
154263281952SAlan Cox 	vm_object_t backing_object, lobject;
1543566526a9SAlan Cox 	vm_offset_t addr, starta;
1544566526a9SAlan Cox 	vm_pindex_t pindex;
15452053c127SStephan Uphoff 	vm_page_t m;
1546b0cd2017SGleb Smirnoff 	int i;
1547566526a9SAlan Cox 
154863281952SAlan Cox 	pmap = fs->map->pmap;
1549950d5f7aSAlan Cox 	if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1550566526a9SAlan Cox 		return;
1551566526a9SAlan Cox 
155263281952SAlan Cox 	entry = fs->entry;
1553566526a9SAlan Cox 
155463cdcaaeSKonstantin Belousov 	if (addra < backward * PAGE_SIZE) {
1555566526a9SAlan Cox 		starta = entry->start;
155663cdcaaeSKonstantin Belousov 	} else {
155763cdcaaeSKonstantin Belousov 		starta = addra - backward * PAGE_SIZE;
155863cdcaaeSKonstantin Belousov 		if (starta < entry->start)
155963cdcaaeSKonstantin Belousov 			starta = entry->start;
1560566526a9SAlan Cox 	}
1561566526a9SAlan Cox 
156263281952SAlan Cox 	/*
156363281952SAlan Cox 	 * Generate the sequence of virtual addresses that are candidates for
156463281952SAlan Cox 	 * prefaulting in an outward spiral from the faulting virtual address,
156563281952SAlan Cox 	 * "addra".  Specifically, the sequence is "addra - PAGE_SIZE", "addra
156663281952SAlan Cox 	 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
156763281952SAlan Cox 	 * If the candidate address doesn't have a backing physical page, then
156863281952SAlan Cox 	 * the loop immediately terminates.
156963281952SAlan Cox 	 */
157063281952SAlan Cox 	for (i = 0; i < 2 * imax(backward, forward); i++) {
157163281952SAlan Cox 		addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
157263281952SAlan Cox 		    PAGE_SIZE);
157363281952SAlan Cox 		if (addr > addra + forward * PAGE_SIZE)
1574566526a9SAlan Cox 			addr = 0;
1575566526a9SAlan Cox 
1576566526a9SAlan Cox 		if (addr < starta || addr >= entry->end)
1577566526a9SAlan Cox 			continue;
1578566526a9SAlan Cox 
1579566526a9SAlan Cox 		if (!pmap_is_prefaultable(pmap, addr))
1580566526a9SAlan Cox 			continue;
1581566526a9SAlan Cox 
1582566526a9SAlan Cox 		pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
158363281952SAlan Cox 		lobject = entry->object.vm_object;
1584a7163bb9SKonstantin Belousov 		if (!obj_locked)
1585c141ae7fSAlan Cox 			VM_OBJECT_RLOCK(lobject);
1586566526a9SAlan Cox 		while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1587566526a9SAlan Cox 		    lobject->type == OBJT_DEFAULT &&
1588566526a9SAlan Cox 		    (backing_object = lobject->backing_object) != NULL) {
158936930fc9SAlan Cox 			KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
159036930fc9SAlan Cox 			    0, ("vm_fault_prefault: unaligned object offset"));
1591566526a9SAlan Cox 			pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1592c141ae7fSAlan Cox 			VM_OBJECT_RLOCK(backing_object);
1593a7163bb9SKonstantin Belousov 			if (!obj_locked || lobject != entry->object.vm_object)
1594c141ae7fSAlan Cox 				VM_OBJECT_RUNLOCK(lobject);
1595566526a9SAlan Cox 			lobject = backing_object;
1596566526a9SAlan Cox 		}
1597cbfbaad8SAlan Cox 		if (m == NULL) {
1598a7163bb9SKonstantin Belousov 			if (!obj_locked || lobject != entry->object.vm_object)
1599c141ae7fSAlan Cox 				VM_OBJECT_RUNLOCK(lobject);
1600566526a9SAlan Cox 			break;
1601cbfbaad8SAlan Cox 		}
16020012f373SJeff Roberson 		if (vm_page_all_valid(m) &&
16033c4a2440SAlan Cox 		    (m->flags & PG_FICTITIOUS) == 0)
16047bfda801SAlan Cox 			pmap_enter_quick(pmap, addr, m, entry->protection);
1605a7163bb9SKonstantin Belousov 		if (!obj_locked || lobject != entry->object.vm_object)
1606c141ae7fSAlan Cox 			VM_OBJECT_RUNLOCK(lobject);
1607566526a9SAlan Cox 	}
1608566526a9SAlan Cox }
1609566526a9SAlan Cox 
1610566526a9SAlan Cox /*
161182de724fSAlan Cox  * Hold each of the physical pages that are mapped by the specified range of
161282de724fSAlan Cox  * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
161382de724fSAlan Cox  * and allow the specified types of access, "prot".  If all of the implied
161482de724fSAlan Cox  * pages are successfully held, then the number of held pages is returned
161582de724fSAlan Cox  * together with pointers to those pages in the array "ma".  However, if any
161682de724fSAlan Cox  * of the pages cannot be held, -1 is returned.
161782de724fSAlan Cox  */
161882de724fSAlan Cox int
161982de724fSAlan Cox vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
162082de724fSAlan Cox     vm_prot_t prot, vm_page_t *ma, int max_count)
162182de724fSAlan Cox {
162282de724fSAlan Cox 	vm_offset_t end, va;
162382de724fSAlan Cox 	vm_page_t *mp;
16247e14088dSKonstantin Belousov 	int count;
162582de724fSAlan Cox 	boolean_t pmap_failed;
162682de724fSAlan Cox 
1627af32c419SKonstantin Belousov 	if (len == 0)
1628af32c419SKonstantin Belousov 		return (0);
162982de724fSAlan Cox 	end = round_page(addr + len);
163082de724fSAlan Cox 	addr = trunc_page(addr);
163182de724fSAlan Cox 
163282de724fSAlan Cox 	/*
163382de724fSAlan Cox 	 * Check for illegal addresses.
163482de724fSAlan Cox 	 */
163582de724fSAlan Cox 	if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
163682de724fSAlan Cox 		return (-1);
163782de724fSAlan Cox 
16387e14088dSKonstantin Belousov 	if (atop(end - addr) > max_count)
163982de724fSAlan Cox 		panic("vm_fault_quick_hold_pages: count > max_count");
16407e14088dSKonstantin Belousov 	count = atop(end - addr);
164182de724fSAlan Cox 
164282de724fSAlan Cox 	/*
164382de724fSAlan Cox 	 * Most likely, the physical pages are resident in the pmap, so it is
164482de724fSAlan Cox 	 * faster to try pmap_extract_and_hold() first.
164582de724fSAlan Cox 	 */
164682de724fSAlan Cox 	pmap_failed = FALSE;
164782de724fSAlan Cox 	for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
164882de724fSAlan Cox 		*mp = pmap_extract_and_hold(map->pmap, va, prot);
164982de724fSAlan Cox 		if (*mp == NULL)
165082de724fSAlan Cox 			pmap_failed = TRUE;
165182de724fSAlan Cox 		else if ((prot & VM_PROT_WRITE) != 0 &&
1652a5dbab54SAlan Cox 		    (*mp)->dirty != VM_PAGE_BITS_ALL) {
165382de724fSAlan Cox 			/*
165482de724fSAlan Cox 			 * Explicitly dirty the physical page.  Otherwise, the
165582de724fSAlan Cox 			 * caller's changes may go unnoticed because they are
165682de724fSAlan Cox 			 * performed through an unmanaged mapping or by a DMA
165782de724fSAlan Cox 			 * operation.
16583c76db4cSAlan Cox 			 *
1659abb9b935SKonstantin Belousov 			 * The object lock is not held here.
1660abb9b935SKonstantin Belousov 			 * See vm_page_clear_dirty_mask().
166182de724fSAlan Cox 			 */
16623c76db4cSAlan Cox 			vm_page_dirty(*mp);
166382de724fSAlan Cox 		}
166482de724fSAlan Cox 	}
166582de724fSAlan Cox 	if (pmap_failed) {
166682de724fSAlan Cox 		/*
166782de724fSAlan Cox 		 * One or more pages could not be held by the pmap.  Either no
166882de724fSAlan Cox 		 * page was mapped at the specified virtual address or that
166982de724fSAlan Cox 		 * mapping had insufficient permissions.  Attempt to fault in
167082de724fSAlan Cox 		 * and hold these pages.
16718ec533d3SKonstantin Belousov 		 *
16728ec533d3SKonstantin Belousov 		 * If vm_fault_disable_pagefaults() was called,
16738ec533d3SKonstantin Belousov 		 * i.e., TDP_NOFAULTING is set, we must not sleep nor
16748ec533d3SKonstantin Belousov 		 * acquire MD VM locks, which means we must not call
1675df08823dSKonstantin Belousov 		 * vm_fault().  Some (out of tree) callers mark
16768ec533d3SKonstantin Belousov 		 * too wide a code area with vm_fault_disable_pagefaults()
16778ec533d3SKonstantin Belousov 		 * already, use the VM_PROT_QUICK_NOFAULT flag to request
16788ec533d3SKonstantin Belousov 		 * the proper behaviour explicitly.
167982de724fSAlan Cox 		 */
16808ec533d3SKonstantin Belousov 		if ((prot & VM_PROT_QUICK_NOFAULT) != 0 &&
16818ec533d3SKonstantin Belousov 		    (curthread->td_pflags & TDP_NOFAULTING) != 0)
16828ec533d3SKonstantin Belousov 			goto error;
168382de724fSAlan Cox 		for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1684df08823dSKonstantin Belousov 			if (*mp == NULL && vm_fault(map, va, prot,
168582de724fSAlan Cox 			    VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
168682de724fSAlan Cox 				goto error;
168782de724fSAlan Cox 	}
168882de724fSAlan Cox 	return (count);
168982de724fSAlan Cox error:
169082de724fSAlan Cox 	for (mp = ma; mp < ma + count; mp++)
1691fee2a2faSMark Johnston 		if (*mp != NULL)
1692fee2a2faSMark Johnston 			vm_page_unwire(*mp, PQ_INACTIVE);
169382de724fSAlan Cox 	return (-1);
169482de724fSAlan Cox }
169582de724fSAlan Cox 
169682de724fSAlan Cox /*
1697df8bae1dSRodney W. Grimes  *	Routine:
1698df8bae1dSRodney W. Grimes  *		vm_fault_copy_entry
1699df8bae1dSRodney W. Grimes  *	Function:
1700210a6886SKonstantin Belousov  *		Create new shadow object backing dst_entry with private copy of
1701210a6886SKonstantin Belousov  *		all underlying pages. When src_entry is equal to dst_entry,
1702210a6886SKonstantin Belousov  *		function implements COW for wired-down map entry. Otherwise,
1703210a6886SKonstantin Belousov  *		it forks wired entry into dst_map.
1704df8bae1dSRodney W. Grimes  *
1705df8bae1dSRodney W. Grimes  *	In/out conditions:
1706df8bae1dSRodney W. Grimes  *		The source and destination maps must be locked for write.
1707df8bae1dSRodney W. Grimes  *		The source map entry must be wired down (or be a sharing map
1708df8bae1dSRodney W. Grimes  *		entry corresponding to a main map entry that is wired down).
1709df8bae1dSRodney W. Grimes  */
171026f9a767SRodney W. Grimes void
1711121fd461SKonstantin Belousov vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1712121fd461SKonstantin Belousov     vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1713121fd461SKonstantin Belousov     vm_ooffset_t *fork_charge)
1714df8bae1dSRodney W. Grimes {
1715210a6886SKonstantin Belousov 	vm_object_t backing_object, dst_object, object, src_object;
17167afab86cSAlan Cox 	vm_pindex_t dst_pindex, pindex, src_pindex;
1717210a6886SKonstantin Belousov 	vm_prot_t access, prot;
1718df8bae1dSRodney W. Grimes 	vm_offset_t vaddr;
1719df8bae1dSRodney W. Grimes 	vm_page_t dst_m;
1720df8bae1dSRodney W. Grimes 	vm_page_t src_m;
17214c74acf7SKonstantin Belousov 	boolean_t upgrade;
1722df8bae1dSRodney W. Grimes 
1723df8bae1dSRodney W. Grimes #ifdef	lint
1724df8bae1dSRodney W. Grimes 	src_map++;
17250d94caffSDavid Greenman #endif	/* lint */
1726df8bae1dSRodney W. Grimes 
1727210a6886SKonstantin Belousov 	upgrade = src_entry == dst_entry;
17280973283dSKonstantin Belousov 	access = prot = dst_entry->protection;
1729210a6886SKonstantin Belousov 
1730df8bae1dSRodney W. Grimes 	src_object = src_entry->object.vm_object;
17317afab86cSAlan Cox 	src_pindex = OFF_TO_IDX(src_entry->offset);
1732df8bae1dSRodney W. Grimes 
17330973283dSKonstantin Belousov 	if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
17340973283dSKonstantin Belousov 		dst_object = src_object;
17350973283dSKonstantin Belousov 		vm_object_reference(dst_object);
17360973283dSKonstantin Belousov 	} else {
1737df8bae1dSRodney W. Grimes 		/*
17380d94caffSDavid Greenman 		 * Create the top-level object for the destination entry. (Doesn't
17390d94caffSDavid Greenman 		 * actually shadow anything - we copy the pages directly.)
1740df8bae1dSRodney W. Grimes 		 */
174124a1cce3SDavid Greenman 		dst_object = vm_object_allocate(OBJT_DEFAULT,
1742d1780e8dSKonstantin Belousov 		    atop(dst_entry->end - dst_entry->start));
1743f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1744f8a47341SAlan Cox 		dst_object->flags |= OBJ_COLORED;
1745f8a47341SAlan Cox 		dst_object->pg_color = atop(dst_entry->start);
1746f8a47341SAlan Cox #endif
1747a60d3db1SKonstantin Belousov 		dst_object->domain = src_object->domain;
1748a60d3db1SKonstantin Belousov 		dst_object->charge = dst_entry->end - dst_entry->start;
17490973283dSKonstantin Belousov 	}
1750df8bae1dSRodney W. Grimes 
175189f6b863SAttilio Rao 	VM_OBJECT_WLOCK(dst_object);
1752210a6886SKonstantin Belousov 	KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1753121fd461SKonstantin Belousov 	    ("vm_fault_copy_entry: vm_object not NULL"));
17540973283dSKonstantin Belousov 	if (src_object != dst_object) {
1755df8bae1dSRodney W. Grimes 		dst_entry->object.vm_object = dst_object;
1756df8bae1dSRodney W. Grimes 		dst_entry->offset = 0;
175778022527SKonstantin Belousov 		dst_entry->eflags &= ~MAP_ENTRY_VN_EXEC;
17580973283dSKonstantin Belousov 	}
1759210a6886SKonstantin Belousov 	if (fork_charge != NULL) {
1760ef694c1aSEdward Tomasz Napierala 		KASSERT(dst_entry->cred == NULL,
1761121fd461SKonstantin Belousov 		    ("vm_fault_copy_entry: leaked swp charge"));
1762ef694c1aSEdward Tomasz Napierala 		dst_object->cred = curthread->td_ucred;
1763ef694c1aSEdward Tomasz Napierala 		crhold(dst_object->cred);
1764121fd461SKonstantin Belousov 		*fork_charge += dst_object->charge;
17659f25ab83SKonstantin Belousov 	} else if ((dst_object->type == OBJT_DEFAULT ||
17669f25ab83SKonstantin Belousov 	    dst_object->type == OBJT_SWAP) &&
17679f25ab83SKonstantin Belousov 	    dst_object->cred == NULL) {
17680973283dSKonstantin Belousov 		KASSERT(dst_entry->cred != NULL, ("no cred for entry %p",
17690973283dSKonstantin Belousov 		    dst_entry));
1770ef694c1aSEdward Tomasz Napierala 		dst_object->cred = dst_entry->cred;
1771ef694c1aSEdward Tomasz Napierala 		dst_entry->cred = NULL;
1772210a6886SKonstantin Belousov 	}
17730973283dSKonstantin Belousov 
1774210a6886SKonstantin Belousov 	/*
1775210a6886SKonstantin Belousov 	 * If not an upgrade, then enter the mappings in the pmap as
1776210a6886SKonstantin Belousov 	 * read and/or execute accesses.  Otherwise, enter them as
1777210a6886SKonstantin Belousov 	 * write accesses.
1778210a6886SKonstantin Belousov 	 *
1779210a6886SKonstantin Belousov 	 * A writeable large page mapping is only created if all of
1780210a6886SKonstantin Belousov 	 * the constituent small page mappings are modified. Marking
1781210a6886SKonstantin Belousov 	 * PTEs as modified on inception allows promotion to happen
1782210a6886SKonstantin Belousov 	 * without taking potentially large number of soft faults.
1783210a6886SKonstantin Belousov 	 */
1784210a6886SKonstantin Belousov 	if (!upgrade)
1785210a6886SKonstantin Belousov 		access &= ~VM_PROT_WRITE;
1786df8bae1dSRodney W. Grimes 
1787df8bae1dSRodney W. Grimes 	/*
1788ef45823eSKonstantin Belousov 	 * Loop through all of the virtual pages within the entry's
1789ef45823eSKonstantin Belousov 	 * range, copying each page from the source object to the
1790ef45823eSKonstantin Belousov 	 * destination object.  Since the source is wired, those pages
1791ef45823eSKonstantin Belousov 	 * must exist.  In contrast, the destination is pageable.
17926939b4d3SMark Johnston 	 * Since the destination object doesn't share any backing storage
1793ef45823eSKonstantin Belousov 	 * with the source object, all of its pages must be dirtied,
1794ef45823eSKonstantin Belousov 	 * regardless of whether they can be written.
1795df8bae1dSRodney W. Grimes 	 */
17967afab86cSAlan Cox 	for (vaddr = dst_entry->start, dst_pindex = 0;
1797df8bae1dSRodney W. Grimes 	    vaddr < dst_entry->end;
17987afab86cSAlan Cox 	    vaddr += PAGE_SIZE, dst_pindex++) {
17990973283dSKonstantin Belousov again:
1800df8bae1dSRodney W. Grimes 		/*
1801df8bae1dSRodney W. Grimes 		 * Find the page in the source object, and copy it in.
18024c74acf7SKonstantin Belousov 		 * Because the source is wired down, the page will be
18034c74acf7SKonstantin Belousov 		 * in memory.
1804df8bae1dSRodney W. Grimes 		 */
18050973283dSKonstantin Belousov 		if (src_object != dst_object)
180683b375eaSAttilio Rao 			VM_OBJECT_RLOCK(src_object);
1807c5b65a67SAlan Cox 		object = src_object;
18087afab86cSAlan Cox 		pindex = src_pindex + dst_pindex;
18097afab86cSAlan Cox 		while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1810c5b65a67SAlan Cox 		    (backing_object = object->backing_object) != NULL) {
1811c5b65a67SAlan Cox 			/*
18124c74acf7SKonstantin Belousov 			 * Unless the source mapping is read-only or
18134c74acf7SKonstantin Belousov 			 * it is presently being upgraded from
18144c74acf7SKonstantin Belousov 			 * read-only, the first object in the shadow
18154c74acf7SKonstantin Belousov 			 * chain should provide all of the pages.  In
18164c74acf7SKonstantin Belousov 			 * other words, this loop body should never be
18174c74acf7SKonstantin Belousov 			 * executed when the source mapping is already
18184c74acf7SKonstantin Belousov 			 * read/write.
1819c5b65a67SAlan Cox 			 */
18204c74acf7SKonstantin Belousov 			KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 ||
18214c74acf7SKonstantin Belousov 			    upgrade,
18224c74acf7SKonstantin Belousov 			    ("vm_fault_copy_entry: main object missing page"));
18234c74acf7SKonstantin Belousov 
182483b375eaSAttilio Rao 			VM_OBJECT_RLOCK(backing_object);
1825c5b65a67SAlan Cox 			pindex += OFF_TO_IDX(object->backing_object_offset);
18260973283dSKonstantin Belousov 			if (object != dst_object)
182783b375eaSAttilio Rao 				VM_OBJECT_RUNLOCK(object);
1828c5b65a67SAlan Cox 			object = backing_object;
1829c5b65a67SAlan Cox 		}
18304c74acf7SKonstantin Belousov 		KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing"));
18310973283dSKonstantin Belousov 
18320973283dSKonstantin Belousov 		if (object != dst_object) {
18330973283dSKonstantin Belousov 			/*
18340973283dSKonstantin Belousov 			 * Allocate a page in the destination object.
18350973283dSKonstantin Belousov 			 */
18362602a2eaSKonstantin Belousov 			dst_m = vm_page_alloc(dst_object, (src_object ==
18372602a2eaSKonstantin Belousov 			    dst_object ? src_pindex : 0) + dst_pindex,
18382602a2eaSKonstantin Belousov 			    VM_ALLOC_NORMAL);
18390973283dSKonstantin Belousov 			if (dst_m == NULL) {
18400973283dSKonstantin Belousov 				VM_OBJECT_WUNLOCK(dst_object);
18410973283dSKonstantin Belousov 				VM_OBJECT_RUNLOCK(object);
18422c0f13aaSKonstantin Belousov 				vm_wait(dst_object);
1843c8f780e3SKonstantin Belousov 				VM_OBJECT_WLOCK(dst_object);
18440973283dSKonstantin Belousov 				goto again;
18450973283dSKonstantin Belousov 			}
1846669890eaSAlan Cox 			pmap_copy_page(src_m, dst_m);
184783b375eaSAttilio Rao 			VM_OBJECT_RUNLOCK(object);
184845d72c7dSKonstantin Belousov 			dst_m->dirty = dst_m->valid = src_m->valid;
18490973283dSKonstantin Belousov 		} else {
18500973283dSKonstantin Belousov 			dst_m = src_m;
185163e97555SJeff Roberson 			if (vm_page_busy_acquire(dst_m, VM_ALLOC_WAITFAIL) == 0)
18520973283dSKonstantin Belousov 				goto again;
185363e97555SJeff Roberson 			if (dst_m->pindex >= dst_object->size) {
1854c62637d6SKonstantin Belousov 				/*
1855c62637d6SKonstantin Belousov 				 * We are upgrading.  Index can occur
1856c62637d6SKonstantin Belousov 				 * out of bounds if the object type is
1857c62637d6SKonstantin Belousov 				 * vnode and the file was truncated.
1858c62637d6SKonstantin Belousov 				 */
185963e97555SJeff Roberson 				vm_page_xunbusy(dst_m);
1860c62637d6SKonstantin Belousov 				break;
186163e97555SJeff Roberson 			}
18620973283dSKonstantin Belousov 		}
186389f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(dst_object);
1864df8bae1dSRodney W. Grimes 
1865df8bae1dSRodney W. Grimes 		/*
1866210a6886SKonstantin Belousov 		 * Enter it in the pmap. If a wired, copy-on-write
1867210a6886SKonstantin Belousov 		 * mapping is being replaced by a write-enabled
1868210a6886SKonstantin Belousov 		 * mapping, then wire that new mapping.
186945d72c7dSKonstantin Belousov 		 *
187045d72c7dSKonstantin Belousov 		 * The page can be invalid if the user called
187145d72c7dSKonstantin Belousov 		 * msync(MS_INVALIDATE) or truncated the backing vnode
187245d72c7dSKonstantin Belousov 		 * or shared memory object.  In this case, do not
187345d72c7dSKonstantin Belousov 		 * insert it into pmap, but still do the copy so that
187445d72c7dSKonstantin Belousov 		 * all copies of the wired map entry have similar
187545d72c7dSKonstantin Belousov 		 * backing pages.
1876df8bae1dSRodney W. Grimes 		 */
18770012f373SJeff Roberson 		if (vm_page_all_valid(dst_m)) {
187839ffa8c1SKonstantin Belousov 			pmap_enter(dst_map->pmap, vaddr, dst_m, prot,
187939ffa8c1SKonstantin Belousov 			    access | (upgrade ? PMAP_ENTER_WIRED : 0), 0);
188045d72c7dSKonstantin Belousov 		}
1881df8bae1dSRodney W. Grimes 
1882df8bae1dSRodney W. Grimes 		/*
1883df8bae1dSRodney W. Grimes 		 * Mark it no longer busy, and put it on the active list.
1884df8bae1dSRodney W. Grimes 		 */
188589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(dst_object);
18862965a453SKip Macy 
1887210a6886SKonstantin Belousov 		if (upgrade) {
18880973283dSKonstantin Belousov 			if (src_m != dst_m) {
18893ae10f74SAttilio Rao 				vm_page_unwire(src_m, PQ_INACTIVE);
1890210a6886SKonstantin Belousov 				vm_page_wire(dst_m);
18912965a453SKip Macy 			} else {
1892d842aa51SMark Johnston 				KASSERT(vm_page_wired(dst_m),
18930973283dSKonstantin Belousov 				    ("dst_m %p is not wired", dst_m));
18940973283dSKonstantin Belousov 			}
18950973283dSKonstantin Belousov 		} else {
18962965a453SKip Macy 			vm_page_lock(dst_m);
1897df8bae1dSRodney W. Grimes 			vm_page_activate(dst_m);
1898e20e8c15SKonstantin Belousov 			vm_page_unlock(dst_m);
18992965a453SKip Macy 		}
1900c7aebda8SAttilio Rao 		vm_page_xunbusy(dst_m);
1901df8bae1dSRodney W. Grimes 	}
190289f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(dst_object);
1903210a6886SKonstantin Belousov 	if (upgrade) {
1904210a6886SKonstantin Belousov 		dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1905210a6886SKonstantin Belousov 		vm_object_deallocate(src_object);
1906210a6886SKonstantin Belousov 	}
1907df8bae1dSRodney W. Grimes }
190826f9a767SRodney W. Grimes 
19095730afc9SAlan Cox /*
19105730afc9SAlan Cox  * Block entry into the machine-independent layer's page fault handler by
19115730afc9SAlan Cox  * the calling thread.  Subsequent calls to vm_fault() by that thread will
19125730afc9SAlan Cox  * return KERN_PROTECTION_FAILURE.  Enable machine-dependent handling of
19135730afc9SAlan Cox  * spurious page faults.
19145730afc9SAlan Cox  */
19152801687dSKonstantin Belousov int
19162801687dSKonstantin Belousov vm_fault_disable_pagefaults(void)
19172801687dSKonstantin Belousov {
19182801687dSKonstantin Belousov 
19195730afc9SAlan Cox 	return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
19202801687dSKonstantin Belousov }
19212801687dSKonstantin Belousov 
19222801687dSKonstantin Belousov void
19232801687dSKonstantin Belousov vm_fault_enable_pagefaults(int save)
19242801687dSKonstantin Belousov {
19252801687dSKonstantin Belousov 
19262801687dSKonstantin Belousov 	curthread_pflags_restore(save);
19272801687dSKonstantin Belousov }
1928