xref: /freebsd/sys/vm/vm_fault.c (revision 2c3f47a727372086b41ef9ce06eb1f1eb83a67d3)
1 /*-
2  * SPDX-License-Identifier: (BSD-4-Clause AND MIT-CMU)
3  *
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 1994 John S. Dyson
7  * All rights reserved.
8  * Copyright (c) 1994 David Greenman
9  * All rights reserved.
10  *
11  *
12  * This code is derived from software contributed to Berkeley by
13  * The Mach Operating System project at Carnegie-Mellon University.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. All advertising materials mentioning features or use of this software
24  *    must display the following acknowledgement:
25  *	This product includes software developed by the University of
26  *	California, Berkeley and its contributors.
27  * 4. Neither the name of the University nor the names of its contributors
28  *    may be used to endorse or promote products derived from this software
29  *    without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41  * SUCH DAMAGE.
42  *
43  *	from: @(#)vm_fault.c	8.4 (Berkeley) 1/12/94
44  *
45  *
46  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
47  * All rights reserved.
48  *
49  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
50  *
51  * Permission to use, copy, modify and distribute this software and
52  * its documentation is hereby granted, provided that both the copyright
53  * notice and this permission notice appear in all copies of the
54  * software, derivative works or modified versions, and any portions
55  * thereof, and that both notices appear in supporting documentation.
56  *
57  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
58  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
59  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
60  *
61  * Carnegie Mellon requests users of this software to return to
62  *
63  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
64  *  School of Computer Science
65  *  Carnegie Mellon University
66  *  Pittsburgh PA 15213-3890
67  *
68  * any improvements or extensions that they make and grant Carnegie the
69  * rights to redistribute these changes.
70  */
71 
72 /*
73  *	Page fault handling module.
74  */
75 
76 #include <sys/cdefs.h>
77 __FBSDID("$FreeBSD$");
78 
79 #include "opt_ktrace.h"
80 #include "opt_vm.h"
81 
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/kernel.h>
85 #include <sys/lock.h>
86 #include <sys/mman.h>
87 #include <sys/mutex.h>
88 #include <sys/proc.h>
89 #include <sys/racct.h>
90 #include <sys/refcount.h>
91 #include <sys/resourcevar.h>
92 #include <sys/rwlock.h>
93 #include <sys/sysctl.h>
94 #include <sys/vmmeter.h>
95 #include <sys/vnode.h>
96 #ifdef KTRACE
97 #include <sys/ktrace.h>
98 #endif
99 
100 #include <vm/vm.h>
101 #include <vm/vm_param.h>
102 #include <vm/pmap.h>
103 #include <vm/vm_map.h>
104 #include <vm/vm_object.h>
105 #include <vm/vm_page.h>
106 #include <vm/vm_pageout.h>
107 #include <vm/vm_kern.h>
108 #include <vm/vm_pager.h>
109 #include <vm/vm_extern.h>
110 #include <vm/vm_reserv.h>
111 
112 #define PFBAK 4
113 #define PFFOR 4
114 
115 #define	VM_FAULT_READ_DEFAULT	(1 + VM_FAULT_READ_AHEAD_INIT)
116 #define	VM_FAULT_READ_MAX	(1 + VM_FAULT_READ_AHEAD_MAX)
117 
118 #define	VM_FAULT_DONTNEED_MIN	1048576
119 
120 struct faultstate {
121 	vm_page_t m;
122 	vm_object_t object;
123 	vm_pindex_t pindex;
124 	vm_page_t first_m;
125 	vm_object_t	first_object;
126 	vm_pindex_t first_pindex;
127 	vm_map_t map;
128 	vm_map_entry_t entry;
129 	int map_generation;
130 	bool lookup_still_valid;
131 	struct vnode *vp;
132 };
133 
134 static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr,
135 	    int ahead);
136 static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
137 	    int backward, int forward, bool obj_locked);
138 
139 static int vm_pfault_oom_attempts = 3;
140 SYSCTL_INT(_vm, OID_AUTO, pfault_oom_attempts, CTLFLAG_RWTUN,
141     &vm_pfault_oom_attempts, 0,
142     "Number of page allocation attempts in page fault handler before it "
143     "triggers OOM handling");
144 
145 static int vm_pfault_oom_wait = 10;
146 SYSCTL_INT(_vm, OID_AUTO, pfault_oom_wait, CTLFLAG_RWTUN,
147     &vm_pfault_oom_wait, 0,
148     "Number of seconds to wait for free pages before retrying "
149     "the page fault handler");
150 
151 static inline void
152 release_page(struct faultstate *fs)
153 {
154 
155 	vm_page_xunbusy(fs->m);
156 	vm_page_lock(fs->m);
157 	vm_page_deactivate(fs->m);
158 	vm_page_unlock(fs->m);
159 	fs->m = NULL;
160 }
161 
162 static inline void
163 unlock_map(struct faultstate *fs)
164 {
165 
166 	if (fs->lookup_still_valid) {
167 		vm_map_lookup_done(fs->map, fs->entry);
168 		fs->lookup_still_valid = false;
169 	}
170 }
171 
172 static void
173 unlock_vp(struct faultstate *fs)
174 {
175 
176 	if (fs->vp != NULL) {
177 		vput(fs->vp);
178 		fs->vp = NULL;
179 	}
180 }
181 
182 static void
183 unlock_and_deallocate(struct faultstate *fs)
184 {
185 
186 	vm_object_pip_wakeup(fs->object);
187 	VM_OBJECT_WUNLOCK(fs->object);
188 	if (fs->object != fs->first_object) {
189 		VM_OBJECT_WLOCK(fs->first_object);
190 		vm_page_free(fs->first_m);
191 		vm_object_pip_wakeup(fs->first_object);
192 		VM_OBJECT_WUNLOCK(fs->first_object);
193 		fs->first_m = NULL;
194 	}
195 	vm_object_deallocate(fs->first_object);
196 	unlock_map(fs);
197 	unlock_vp(fs);
198 }
199 
200 static void
201 vm_fault_dirty(vm_map_entry_t entry, vm_page_t m, vm_prot_t prot,
202     vm_prot_t fault_type, int fault_flags, bool set_wd)
203 {
204 	bool need_dirty;
205 
206 	if (((prot & VM_PROT_WRITE) == 0 &&
207 	    (fault_flags & VM_FAULT_DIRTY) == 0) ||
208 	    (m->oflags & VPO_UNMANAGED) != 0)
209 		return;
210 
211 	VM_OBJECT_ASSERT_LOCKED(m->object);
212 
213 	need_dirty = ((fault_type & VM_PROT_WRITE) != 0 &&
214 	    (fault_flags & VM_FAULT_WIRE) == 0) ||
215 	    (fault_flags & VM_FAULT_DIRTY) != 0;
216 
217 	if (set_wd)
218 		vm_object_set_writeable_dirty(m->object);
219 	else
220 		/*
221 		 * If two callers of vm_fault_dirty() with set_wd ==
222 		 * FALSE, one for the map entry with MAP_ENTRY_NOSYNC
223 		 * flag set, other with flag clear, race, it is
224 		 * possible for the no-NOSYNC thread to see m->dirty
225 		 * != 0 and not clear VPO_NOSYNC.  Take vm_page lock
226 		 * around manipulation of VPO_NOSYNC and
227 		 * vm_page_dirty() call, to avoid the race and keep
228 		 * m->oflags consistent.
229 		 */
230 		vm_page_lock(m);
231 
232 	/*
233 	 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
234 	 * if the page is already dirty to prevent data written with
235 	 * the expectation of being synced from not being synced.
236 	 * Likewise if this entry does not request NOSYNC then make
237 	 * sure the page isn't marked NOSYNC.  Applications sharing
238 	 * data should use the same flags to avoid ping ponging.
239 	 */
240 	if ((entry->eflags & MAP_ENTRY_NOSYNC) != 0) {
241 		if (m->dirty == 0) {
242 			m->oflags |= VPO_NOSYNC;
243 		}
244 	} else {
245 		m->oflags &= ~VPO_NOSYNC;
246 	}
247 
248 	/*
249 	 * If the fault is a write, we know that this page is being
250 	 * written NOW so dirty it explicitly to save on
251 	 * pmap_is_modified() calls later.
252 	 *
253 	 * Also, since the page is now dirty, we can possibly tell
254 	 * the pager to release any swap backing the page.  Calling
255 	 * the pager requires a write lock on the object.
256 	 */
257 	if (need_dirty)
258 		vm_page_dirty(m);
259 	if (!set_wd)
260 		vm_page_unlock(m);
261 	else if (need_dirty)
262 		vm_pager_page_unswapped(m);
263 }
264 
265 /*
266  * Unlocks fs.first_object and fs.map on success.
267  */
268 static int
269 vm_fault_soft_fast(struct faultstate *fs, vm_offset_t vaddr, vm_prot_t prot,
270     int fault_type, int fault_flags, boolean_t wired, vm_page_t *m_hold)
271 {
272 	vm_page_t m, m_map;
273 #if (defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
274     __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)) && \
275     VM_NRESERVLEVEL > 0
276 	vm_page_t m_super;
277 	int flags;
278 #endif
279 	int psind, rv;
280 
281 	MPASS(fs->vp == NULL);
282 	m = vm_page_lookup(fs->first_object, fs->first_pindex);
283 	/* A busy page can be mapped for read|execute access. */
284 	if (m == NULL || ((prot & VM_PROT_WRITE) != 0 &&
285 	    vm_page_busied(m)) || m->valid != VM_PAGE_BITS_ALL)
286 		return (KERN_FAILURE);
287 	m_map = m;
288 	psind = 0;
289 #if (defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
290     __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)) && \
291     VM_NRESERVLEVEL > 0
292 	if ((m->flags & PG_FICTITIOUS) == 0 &&
293 	    (m_super = vm_reserv_to_superpage(m)) != NULL &&
294 	    rounddown2(vaddr, pagesizes[m_super->psind]) >= fs->entry->start &&
295 	    roundup2(vaddr + 1, pagesizes[m_super->psind]) <= fs->entry->end &&
296 	    (vaddr & (pagesizes[m_super->psind] - 1)) == (VM_PAGE_TO_PHYS(m) &
297 	    (pagesizes[m_super->psind] - 1)) && !wired &&
298 	    pmap_ps_enabled(fs->map->pmap)) {
299 		flags = PS_ALL_VALID;
300 		if ((prot & VM_PROT_WRITE) != 0) {
301 			/*
302 			 * Create a superpage mapping allowing write access
303 			 * only if none of the constituent pages are busy and
304 			 * all of them are already dirty (except possibly for
305 			 * the page that was faulted on).
306 			 */
307 			flags |= PS_NONE_BUSY;
308 			if ((fs->first_object->flags & OBJ_UNMANAGED) == 0)
309 				flags |= PS_ALL_DIRTY;
310 		}
311 		if (vm_page_ps_test(m_super, flags, m)) {
312 			m_map = m_super;
313 			psind = m_super->psind;
314 			vaddr = rounddown2(vaddr, pagesizes[psind]);
315 			/* Preset the modified bit for dirty superpages. */
316 			if ((flags & PS_ALL_DIRTY) != 0)
317 				fault_type |= VM_PROT_WRITE;
318 		}
319 	}
320 #endif
321 	rv = pmap_enter(fs->map->pmap, vaddr, m_map, prot, fault_type |
322 	    PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED : 0), psind);
323 	if (rv != KERN_SUCCESS)
324 		return (rv);
325 	if (m_hold != NULL) {
326 		*m_hold = m;
327 		vm_page_wire(m);
328 	}
329 	vm_fault_dirty(fs->entry, m, prot, fault_type, fault_flags, false);
330 	if (psind == 0 && !wired)
331 		vm_fault_prefault(fs, vaddr, PFBAK, PFFOR, true);
332 	VM_OBJECT_RUNLOCK(fs->first_object);
333 	vm_map_lookup_done(fs->map, fs->entry);
334 	curthread->td_ru.ru_minflt++;
335 	return (KERN_SUCCESS);
336 }
337 
338 static void
339 vm_fault_restore_map_lock(struct faultstate *fs)
340 {
341 
342 	VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
343 	MPASS(REFCOUNT_COUNT(fs->first_object->paging_in_progress) > 0);
344 
345 	if (!vm_map_trylock_read(fs->map)) {
346 		VM_OBJECT_WUNLOCK(fs->first_object);
347 		vm_map_lock_read(fs->map);
348 		VM_OBJECT_WLOCK(fs->first_object);
349 	}
350 	fs->lookup_still_valid = true;
351 }
352 
353 static void
354 vm_fault_populate_check_page(vm_page_t m)
355 {
356 
357 	/*
358 	 * Check each page to ensure that the pager is obeying the
359 	 * interface: the page must be installed in the object, fully
360 	 * valid, and exclusively busied.
361 	 */
362 	MPASS(m != NULL);
363 	MPASS(m->valid == VM_PAGE_BITS_ALL);
364 	MPASS(vm_page_xbusied(m));
365 }
366 
367 static void
368 vm_fault_populate_cleanup(vm_object_t object, vm_pindex_t first,
369     vm_pindex_t last)
370 {
371 	vm_page_t m;
372 	vm_pindex_t pidx;
373 
374 	VM_OBJECT_ASSERT_WLOCKED(object);
375 	MPASS(first <= last);
376 	for (pidx = first, m = vm_page_lookup(object, pidx);
377 	    pidx <= last; pidx++, m = vm_page_next(m)) {
378 		vm_fault_populate_check_page(m);
379 		vm_page_lock(m);
380 		vm_page_deactivate(m);
381 		vm_page_unlock(m);
382 		vm_page_xunbusy(m);
383 	}
384 }
385 
386 static int
387 vm_fault_populate(struct faultstate *fs, vm_prot_t prot, int fault_type,
388     int fault_flags, boolean_t wired, vm_page_t *m_hold)
389 {
390 	struct mtx *m_mtx;
391 	vm_offset_t vaddr;
392 	vm_page_t m;
393 	vm_pindex_t map_first, map_last, pager_first, pager_last, pidx;
394 	int i, npages, psind, rv;
395 
396 	MPASS(fs->object == fs->first_object);
397 	VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
398 	MPASS(REFCOUNT_COUNT(fs->first_object->paging_in_progress) > 0);
399 	MPASS(fs->first_object->backing_object == NULL);
400 	MPASS(fs->lookup_still_valid);
401 
402 	pager_first = OFF_TO_IDX(fs->entry->offset);
403 	pager_last = pager_first + atop(fs->entry->end - fs->entry->start) - 1;
404 	unlock_map(fs);
405 	unlock_vp(fs);
406 
407 	/*
408 	 * Call the pager (driver) populate() method.
409 	 *
410 	 * There is no guarantee that the method will be called again
411 	 * if the current fault is for read, and a future fault is
412 	 * for write.  Report the entry's maximum allowed protection
413 	 * to the driver.
414 	 */
415 	rv = vm_pager_populate(fs->first_object, fs->first_pindex,
416 	    fault_type, fs->entry->max_protection, &pager_first, &pager_last);
417 
418 	VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
419 	if (rv == VM_PAGER_BAD) {
420 		/*
421 		 * VM_PAGER_BAD is the backdoor for a pager to request
422 		 * normal fault handling.
423 		 */
424 		vm_fault_restore_map_lock(fs);
425 		if (fs->map->timestamp != fs->map_generation)
426 			return (KERN_RESOURCE_SHORTAGE); /* RetryFault */
427 		return (KERN_NOT_RECEIVER);
428 	}
429 	if (rv != VM_PAGER_OK)
430 		return (KERN_FAILURE); /* AKA SIGSEGV */
431 
432 	/* Ensure that the driver is obeying the interface. */
433 	MPASS(pager_first <= pager_last);
434 	MPASS(fs->first_pindex <= pager_last);
435 	MPASS(fs->first_pindex >= pager_first);
436 	MPASS(pager_last < fs->first_object->size);
437 
438 	vm_fault_restore_map_lock(fs);
439 	if (fs->map->timestamp != fs->map_generation) {
440 		vm_fault_populate_cleanup(fs->first_object, pager_first,
441 		    pager_last);
442 		return (KERN_RESOURCE_SHORTAGE); /* RetryFault */
443 	}
444 
445 	/*
446 	 * The map is unchanged after our last unlock.  Process the fault.
447 	 *
448 	 * The range [pager_first, pager_last] that is given to the
449 	 * pager is only a hint.  The pager may populate any range
450 	 * within the object that includes the requested page index.
451 	 * In case the pager expanded the range, clip it to fit into
452 	 * the map entry.
453 	 */
454 	map_first = OFF_TO_IDX(fs->entry->offset);
455 	if (map_first > pager_first) {
456 		vm_fault_populate_cleanup(fs->first_object, pager_first,
457 		    map_first - 1);
458 		pager_first = map_first;
459 	}
460 	map_last = map_first + atop(fs->entry->end - fs->entry->start) - 1;
461 	if (map_last < pager_last) {
462 		vm_fault_populate_cleanup(fs->first_object, map_last + 1,
463 		    pager_last);
464 		pager_last = map_last;
465 	}
466 	for (pidx = pager_first, m = vm_page_lookup(fs->first_object, pidx);
467 	    pidx <= pager_last;
468 	    pidx += npages, m = vm_page_next(&m[npages - 1])) {
469 		vaddr = fs->entry->start + IDX_TO_OFF(pidx) - fs->entry->offset;
470 #if defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
471     __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)
472 		psind = m->psind;
473 		if (psind > 0 && ((vaddr & (pagesizes[psind] - 1)) != 0 ||
474 		    pidx + OFF_TO_IDX(pagesizes[psind]) - 1 > pager_last ||
475 		    !pmap_ps_enabled(fs->map->pmap) || wired))
476 			psind = 0;
477 #else
478 		psind = 0;
479 #endif
480 		npages = atop(pagesizes[psind]);
481 		for (i = 0; i < npages; i++) {
482 			vm_fault_populate_check_page(&m[i]);
483 			vm_fault_dirty(fs->entry, &m[i], prot, fault_type,
484 			    fault_flags, true);
485 		}
486 		VM_OBJECT_WUNLOCK(fs->first_object);
487 		rv = pmap_enter(fs->map->pmap, vaddr, m, prot, fault_type |
488 		    (wired ? PMAP_ENTER_WIRED : 0), psind);
489 #if defined(__amd64__)
490 		if (psind > 0 && rv == KERN_FAILURE) {
491 			for (i = 0; i < npages; i++) {
492 				rv = pmap_enter(fs->map->pmap, vaddr + ptoa(i),
493 				    &m[i], prot, fault_type |
494 				    (wired ? PMAP_ENTER_WIRED : 0), 0);
495 				MPASS(rv == KERN_SUCCESS);
496 			}
497 		}
498 #else
499 		MPASS(rv == KERN_SUCCESS);
500 #endif
501 		VM_OBJECT_WLOCK(fs->first_object);
502 		m_mtx = NULL;
503 		for (i = 0; i < npages; i++) {
504 			if ((fault_flags & VM_FAULT_WIRE) != 0) {
505 				vm_page_wire(&m[i]);
506 			} else {
507 				vm_page_change_lock(&m[i], &m_mtx);
508 				vm_page_activate(&m[i]);
509 			}
510 			if (m_hold != NULL && m[i].pindex == fs->first_pindex) {
511 				*m_hold = &m[i];
512 				vm_page_wire(&m[i]);
513 			}
514 			vm_page_xunbusy(&m[i]);
515 		}
516 		if (m_mtx != NULL)
517 			mtx_unlock(m_mtx);
518 	}
519 	curthread->td_ru.ru_majflt++;
520 	return (KERN_SUCCESS);
521 }
522 
523 /*
524  *	vm_fault:
525  *
526  *	Handle a page fault occurring at the given address,
527  *	requiring the given permissions, in the map specified.
528  *	If successful, the page is inserted into the
529  *	associated physical map.
530  *
531  *	NOTE: the given address should be truncated to the
532  *	proper page address.
533  *
534  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
535  *	a standard error specifying why the fault is fatal is returned.
536  *
537  *	The map in question must be referenced, and remains so.
538  *	Caller may hold no locks.
539  */
540 int
541 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
542     int fault_flags)
543 {
544 	struct thread *td;
545 	int result;
546 
547 	td = curthread;
548 	if ((td->td_pflags & TDP_NOFAULTING) != 0)
549 		return (KERN_PROTECTION_FAILURE);
550 #ifdef KTRACE
551 	if (map != kernel_map && KTRPOINT(td, KTR_FAULT))
552 		ktrfault(vaddr, fault_type);
553 #endif
554 	result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags,
555 	    NULL);
556 #ifdef KTRACE
557 	if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND))
558 		ktrfaultend(result);
559 #endif
560 	return (result);
561 }
562 
563 int
564 vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
565     int fault_flags, vm_page_t *m_hold)
566 {
567 	struct faultstate fs;
568 	struct vnode *vp;
569 	struct domainset *dset;
570 	vm_object_t next_object, retry_object;
571 	vm_offset_t e_end, e_start;
572 	vm_pindex_t retry_pindex;
573 	vm_prot_t prot, retry_prot;
574 	int ahead, alloc_req, behind, cluster_offset, error, era, faultcount;
575 	int locked, nera, oom, result, rv;
576 	u_char behavior;
577 	boolean_t wired;	/* Passed by reference. */
578 	bool dead, hardfault, is_first_object_locked;
579 
580 	VM_CNT_INC(v_vm_faults);
581 	fs.vp = NULL;
582 	faultcount = 0;
583 	nera = -1;
584 	hardfault = false;
585 
586 RetryFault:
587 	oom = 0;
588 RetryFault_oom:
589 
590 	/*
591 	 * Find the backing store object and offset into it to begin the
592 	 * search.
593 	 */
594 	fs.map = map;
595 	result = vm_map_lookup(&fs.map, vaddr, fault_type |
596 	    VM_PROT_FAULT_LOOKUP, &fs.entry, &fs.first_object,
597 	    &fs.first_pindex, &prot, &wired);
598 	if (result != KERN_SUCCESS) {
599 		unlock_vp(&fs);
600 		return (result);
601 	}
602 
603 	fs.map_generation = fs.map->timestamp;
604 
605 	if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
606 		panic("%s: fault on nofault entry, addr: %#lx",
607 		    __func__, (u_long)vaddr);
608 	}
609 
610 	if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
611 	    fs.entry->wiring_thread != curthread) {
612 		vm_map_unlock_read(fs.map);
613 		vm_map_lock(fs.map);
614 		if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
615 		    (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
616 			unlock_vp(&fs);
617 			fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
618 			vm_map_unlock_and_wait(fs.map, 0);
619 		} else
620 			vm_map_unlock(fs.map);
621 		goto RetryFault;
622 	}
623 
624 	MPASS((fs.entry->eflags & MAP_ENTRY_GUARD) == 0);
625 
626 	if (wired)
627 		fault_type = prot | (fault_type & VM_PROT_COPY);
628 	else
629 		KASSERT((fault_flags & VM_FAULT_WIRE) == 0,
630 		    ("!wired && VM_FAULT_WIRE"));
631 
632 	/*
633 	 * Try to avoid lock contention on the top-level object through
634 	 * special-case handling of some types of page faults, specifically,
635 	 * those that are both (1) mapping an existing page from the top-
636 	 * level object and (2) not having to mark that object as containing
637 	 * dirty pages.  Under these conditions, a read lock on the top-level
638 	 * object suffices, allowing multiple page faults of a similar type to
639 	 * run in parallel on the same top-level object.
640 	 */
641 	if (fs.vp == NULL /* avoid locked vnode leak */ &&
642 	    (fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0 &&
643 	    /* avoid calling vm_object_set_writeable_dirty() */
644 	    ((prot & VM_PROT_WRITE) == 0 ||
645 	    (fs.first_object->type != OBJT_VNODE &&
646 	    (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
647 	    (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0)) {
648 		VM_OBJECT_RLOCK(fs.first_object);
649 		if ((prot & VM_PROT_WRITE) == 0 ||
650 		    (fs.first_object->type != OBJT_VNODE &&
651 		    (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
652 		    (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0) {
653 			rv = vm_fault_soft_fast(&fs, vaddr, prot, fault_type,
654 			    fault_flags, wired, m_hold);
655 			if (rv == KERN_SUCCESS)
656 				return (rv);
657 		}
658 		if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) {
659 			VM_OBJECT_RUNLOCK(fs.first_object);
660 			VM_OBJECT_WLOCK(fs.first_object);
661 		}
662 	} else {
663 		VM_OBJECT_WLOCK(fs.first_object);
664 	}
665 
666 	/*
667 	 * Make a reference to this object to prevent its disposal while we
668 	 * are messing with it.  Once we have the reference, the map is free
669 	 * to be diddled.  Since objects reference their shadows (and copies),
670 	 * they will stay around as well.
671 	 *
672 	 * Bump the paging-in-progress count to prevent size changes (e.g.
673 	 * truncation operations) during I/O.
674 	 */
675 	vm_object_reference_locked(fs.first_object);
676 	vm_object_pip_add(fs.first_object, 1);
677 
678 	fs.lookup_still_valid = true;
679 
680 	fs.first_m = NULL;
681 
682 	/*
683 	 * Search for the page at object/offset.
684 	 */
685 	fs.object = fs.first_object;
686 	fs.pindex = fs.first_pindex;
687 	while (TRUE) {
688 		/*
689 		 * If the object is marked for imminent termination,
690 		 * we retry here, since the collapse pass has raced
691 		 * with us.  Otherwise, if we see terminally dead
692 		 * object, return fail.
693 		 */
694 		if ((fs.object->flags & OBJ_DEAD) != 0) {
695 			dead = fs.object->type == OBJT_DEAD;
696 			unlock_and_deallocate(&fs);
697 			if (dead)
698 				return (KERN_PROTECTION_FAILURE);
699 			pause("vmf_de", 1);
700 			goto RetryFault;
701 		}
702 
703 		/*
704 		 * See if page is resident
705 		 */
706 		fs.m = vm_page_lookup(fs.object, fs.pindex);
707 		if (fs.m != NULL) {
708 			/*
709 			 * Wait/Retry if the page is busy.  We have to do this
710 			 * if the page is either exclusive or shared busy
711 			 * because the vm_pager may be using read busy for
712 			 * pageouts (and even pageins if it is the vnode
713 			 * pager), and we could end up trying to pagein and
714 			 * pageout the same page simultaneously.
715 			 *
716 			 * We can theoretically allow the busy case on a read
717 			 * fault if the page is marked valid, but since such
718 			 * pages are typically already pmap'd, putting that
719 			 * special case in might be more effort then it is
720 			 * worth.  We cannot under any circumstances mess
721 			 * around with a shared busied page except, perhaps,
722 			 * to pmap it.
723 			 */
724 			if (vm_page_busied(fs.m)) {
725 				/*
726 				 * Reference the page before unlocking and
727 				 * sleeping so that the page daemon is less
728 				 * likely to reclaim it.
729 				 */
730 				vm_page_aflag_set(fs.m, PGA_REFERENCED);
731 				if (fs.object != fs.first_object) {
732 					if (!VM_OBJECT_TRYWLOCK(
733 					    fs.first_object)) {
734 						VM_OBJECT_WUNLOCK(fs.object);
735 						VM_OBJECT_WLOCK(fs.first_object);
736 						VM_OBJECT_WLOCK(fs.object);
737 					}
738 					vm_page_free(fs.first_m);
739 					vm_object_pip_wakeup(fs.first_object);
740 					VM_OBJECT_WUNLOCK(fs.first_object);
741 					fs.first_m = NULL;
742 				}
743 				unlock_map(&fs);
744 				if (fs.m == vm_page_lookup(fs.object,
745 				    fs.pindex)) {
746 					vm_page_sleep_if_busy(fs.m, "vmpfw");
747 				}
748 				vm_object_pip_wakeup(fs.object);
749 				VM_OBJECT_WUNLOCK(fs.object);
750 				VM_CNT_INC(v_intrans);
751 				vm_object_deallocate(fs.first_object);
752 				goto RetryFault;
753 			}
754 
755 			/*
756 			 * Mark page busy for other processes, and the
757 			 * pagedaemon.  If it still isn't completely valid
758 			 * (readable), jump to readrest, else break-out ( we
759 			 * found the page ).
760 			 */
761 			vm_page_xbusy(fs.m);
762 			if (fs.m->valid != VM_PAGE_BITS_ALL)
763 				goto readrest;
764 			break; /* break to PAGE HAS BEEN FOUND */
765 		}
766 		KASSERT(fs.m == NULL, ("fs.m should be NULL, not %p", fs.m));
767 
768 		/*
769 		 * Page is not resident.  If the pager might contain the page
770 		 * or this is the beginning of the search, allocate a new
771 		 * page.  (Default objects are zero-fill, so there is no real
772 		 * pager for them.)
773 		 */
774 		if (fs.object->type != OBJT_DEFAULT ||
775 		    fs.object == fs.first_object) {
776 			if (fs.pindex >= fs.object->size) {
777 				unlock_and_deallocate(&fs);
778 				return (KERN_PROTECTION_FAILURE);
779 			}
780 
781 			if (fs.object == fs.first_object &&
782 			    (fs.first_object->flags & OBJ_POPULATE) != 0 &&
783 			    fs.first_object->shadow_count == 0) {
784 				rv = vm_fault_populate(&fs, prot, fault_type,
785 				    fault_flags, wired, m_hold);
786 				switch (rv) {
787 				case KERN_SUCCESS:
788 				case KERN_FAILURE:
789 					unlock_and_deallocate(&fs);
790 					return (rv);
791 				case KERN_RESOURCE_SHORTAGE:
792 					unlock_and_deallocate(&fs);
793 					goto RetryFault;
794 				case KERN_NOT_RECEIVER:
795 					/*
796 					 * Pager's populate() method
797 					 * returned VM_PAGER_BAD.
798 					 */
799 					break;
800 				default:
801 					panic("inconsistent return codes");
802 				}
803 			}
804 
805 			/*
806 			 * Allocate a new page for this object/offset pair.
807 			 *
808 			 * Unlocked read of the p_flag is harmless. At
809 			 * worst, the P_KILLED might be not observed
810 			 * there, and allocation can fail, causing
811 			 * restart and new reading of the p_flag.
812 			 */
813 			dset = fs.object->domain.dr_policy;
814 			if (dset == NULL)
815 				dset = curthread->td_domain.dr_policy;
816 			if (!vm_page_count_severe_set(&dset->ds_mask) ||
817 			    P_KILLED(curproc)) {
818 #if VM_NRESERVLEVEL > 0
819 				vm_object_color(fs.object, atop(vaddr) -
820 				    fs.pindex);
821 #endif
822 				alloc_req = P_KILLED(curproc) ?
823 				    VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
824 				if (fs.object->type != OBJT_VNODE &&
825 				    fs.object->backing_object == NULL)
826 					alloc_req |= VM_ALLOC_ZERO;
827 				fs.m = vm_page_alloc(fs.object, fs.pindex,
828 				    alloc_req);
829 			}
830 			if (fs.m == NULL) {
831 				unlock_and_deallocate(&fs);
832 				if (vm_pfault_oom_attempts < 0 ||
833 				    oom < vm_pfault_oom_attempts) {
834 					oom++;
835 					vm_waitpfault(dset,
836 					    vm_pfault_oom_wait * hz);
837 					goto RetryFault_oom;
838 				}
839 				if (bootverbose)
840 					printf(
841 	"proc %d (%s) failed to alloc page on fault, starting OOM\n",
842 					    curproc->p_pid, curproc->p_comm);
843 				vm_pageout_oom(VM_OOM_MEM_PF);
844 				goto RetryFault;
845 			}
846 		}
847 
848 readrest:
849 		/*
850 		 * At this point, we have either allocated a new page or found
851 		 * an existing page that is only partially valid.
852 		 *
853 		 * We hold a reference on the current object and the page is
854 		 * exclusive busied.
855 		 */
856 
857 		/*
858 		 * If the pager for the current object might have the page,
859 		 * then determine the number of additional pages to read and
860 		 * potentially reprioritize previously read pages for earlier
861 		 * reclamation.  These operations should only be performed
862 		 * once per page fault.  Even if the current pager doesn't
863 		 * have the page, the number of additional pages to read will
864 		 * apply to subsequent objects in the shadow chain.
865 		 */
866 		if (fs.object->type != OBJT_DEFAULT && nera == -1 &&
867 		    !P_KILLED(curproc)) {
868 			KASSERT(fs.lookup_still_valid, ("map unlocked"));
869 			era = fs.entry->read_ahead;
870 			behavior = vm_map_entry_behavior(fs.entry);
871 			if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
872 				nera = 0;
873 			} else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
874 				nera = VM_FAULT_READ_AHEAD_MAX;
875 				if (vaddr == fs.entry->next_read)
876 					vm_fault_dontneed(&fs, vaddr, nera);
877 			} else if (vaddr == fs.entry->next_read) {
878 				/*
879 				 * This is a sequential fault.  Arithmetically
880 				 * increase the requested number of pages in
881 				 * the read-ahead window.  The requested
882 				 * number of pages is "# of sequential faults
883 				 * x (read ahead min + 1) + read ahead min"
884 				 */
885 				nera = VM_FAULT_READ_AHEAD_MIN;
886 				if (era > 0) {
887 					nera += era + 1;
888 					if (nera > VM_FAULT_READ_AHEAD_MAX)
889 						nera = VM_FAULT_READ_AHEAD_MAX;
890 				}
891 				if (era == VM_FAULT_READ_AHEAD_MAX)
892 					vm_fault_dontneed(&fs, vaddr, nera);
893 			} else {
894 				/*
895 				 * This is a non-sequential fault.
896 				 */
897 				nera = 0;
898 			}
899 			if (era != nera) {
900 				/*
901 				 * A read lock on the map suffices to update
902 				 * the read ahead count safely.
903 				 */
904 				fs.entry->read_ahead = nera;
905 			}
906 
907 			/*
908 			 * Prepare for unlocking the map.  Save the map
909 			 * entry's start and end addresses, which are used to
910 			 * optimize the size of the pager operation below.
911 			 * Even if the map entry's addresses change after
912 			 * unlocking the map, using the saved addresses is
913 			 * safe.
914 			 */
915 			e_start = fs.entry->start;
916 			e_end = fs.entry->end;
917 		}
918 
919 		/*
920 		 * Call the pager to retrieve the page if there is a chance
921 		 * that the pager has it, and potentially retrieve additional
922 		 * pages at the same time.
923 		 */
924 		if (fs.object->type != OBJT_DEFAULT) {
925 			/*
926 			 * Release the map lock before locking the vnode or
927 			 * sleeping in the pager.  (If the current object has
928 			 * a shadow, then an earlier iteration of this loop
929 			 * may have already unlocked the map.)
930 			 */
931 			unlock_map(&fs);
932 
933 			if (fs.object->type == OBJT_VNODE &&
934 			    (vp = fs.object->handle) != fs.vp) {
935 				/*
936 				 * Perform an unlock in case the desired vnode
937 				 * changed while the map was unlocked during a
938 				 * retry.
939 				 */
940 				unlock_vp(&fs);
941 
942 				locked = VOP_ISLOCKED(vp);
943 				if (locked != LK_EXCLUSIVE)
944 					locked = LK_SHARED;
945 
946 				/*
947 				 * We must not sleep acquiring the vnode lock
948 				 * while we have the page exclusive busied or
949 				 * the object's paging-in-progress count
950 				 * incremented.  Otherwise, we could deadlock.
951 				 */
952 				error = vget(vp, locked | LK_CANRECURSE |
953 				    LK_NOWAIT, curthread);
954 				if (error != 0) {
955 					vhold(vp);
956 					release_page(&fs);
957 					unlock_and_deallocate(&fs);
958 					error = vget(vp, locked | LK_RETRY |
959 					    LK_CANRECURSE, curthread);
960 					vdrop(vp);
961 					fs.vp = vp;
962 					KASSERT(error == 0,
963 					    ("vm_fault: vget failed"));
964 					goto RetryFault;
965 				}
966 				fs.vp = vp;
967 			}
968 			KASSERT(fs.vp == NULL || !fs.map->system_map,
969 			    ("vm_fault: vnode-backed object mapped by system map"));
970 
971 			/*
972 			 * Page in the requested page and hint the pager,
973 			 * that it may bring up surrounding pages.
974 			 */
975 			if (nera == -1 || behavior == MAP_ENTRY_BEHAV_RANDOM ||
976 			    P_KILLED(curproc)) {
977 				behind = 0;
978 				ahead = 0;
979 			} else {
980 				/* Is this a sequential fault? */
981 				if (nera > 0) {
982 					behind = 0;
983 					ahead = nera;
984 				} else {
985 					/*
986 					 * Request a cluster of pages that is
987 					 * aligned to a VM_FAULT_READ_DEFAULT
988 					 * page offset boundary within the
989 					 * object.  Alignment to a page offset
990 					 * boundary is more likely to coincide
991 					 * with the underlying file system
992 					 * block than alignment to a virtual
993 					 * address boundary.
994 					 */
995 					cluster_offset = fs.pindex %
996 					    VM_FAULT_READ_DEFAULT;
997 					behind = ulmin(cluster_offset,
998 					    atop(vaddr - e_start));
999 					ahead = VM_FAULT_READ_DEFAULT - 1 -
1000 					    cluster_offset;
1001 				}
1002 				ahead = ulmin(ahead, atop(e_end - vaddr) - 1);
1003 			}
1004 			rv = vm_pager_get_pages(fs.object, &fs.m, 1,
1005 			    &behind, &ahead);
1006 			if (rv == VM_PAGER_OK) {
1007 				faultcount = behind + 1 + ahead;
1008 				hardfault = true;
1009 				break; /* break to PAGE HAS BEEN FOUND */
1010 			}
1011 			if (rv == VM_PAGER_ERROR)
1012 				printf("vm_fault: pager read error, pid %d (%s)\n",
1013 				    curproc->p_pid, curproc->p_comm);
1014 
1015 			/*
1016 			 * If an I/O error occurred or the requested page was
1017 			 * outside the range of the pager, clean up and return
1018 			 * an error.
1019 			 */
1020 			if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) {
1021 				if (!vm_page_wired(fs.m))
1022 					vm_page_free(fs.m);
1023 				else
1024 					vm_page_xunbusy(fs.m);
1025 				fs.m = NULL;
1026 				unlock_and_deallocate(&fs);
1027 				return (rv == VM_PAGER_ERROR ? KERN_FAILURE :
1028 				    KERN_PROTECTION_FAILURE);
1029 			}
1030 
1031 			/*
1032 			 * The requested page does not exist at this object/
1033 			 * offset.  Remove the invalid page from the object,
1034 			 * waking up anyone waiting for it, and continue on to
1035 			 * the next object.  However, if this is the top-level
1036 			 * object, we must leave the busy page in place to
1037 			 * prevent another process from rushing past us, and
1038 			 * inserting the page in that object at the same time
1039 			 * that we are.
1040 			 */
1041 			if (fs.object != fs.first_object) {
1042 				if (!vm_page_wired(fs.m))
1043 					vm_page_free(fs.m);
1044 				else
1045 					vm_page_xunbusy(fs.m);
1046 				fs.m = NULL;
1047 			}
1048 		}
1049 
1050 		/*
1051 		 * We get here if the object has default pager (or unwiring)
1052 		 * or the pager doesn't have the page.
1053 		 */
1054 		if (fs.object == fs.first_object)
1055 			fs.first_m = fs.m;
1056 
1057 		/*
1058 		 * Move on to the next object.  Lock the next object before
1059 		 * unlocking the current one.
1060 		 */
1061 		next_object = fs.object->backing_object;
1062 		if (next_object == NULL) {
1063 			/*
1064 			 * If there's no object left, fill the page in the top
1065 			 * object with zeros.
1066 			 */
1067 			if (fs.object != fs.first_object) {
1068 				vm_object_pip_wakeup(fs.object);
1069 				VM_OBJECT_WUNLOCK(fs.object);
1070 
1071 				fs.object = fs.first_object;
1072 				fs.pindex = fs.first_pindex;
1073 				fs.m = fs.first_m;
1074 				VM_OBJECT_WLOCK(fs.object);
1075 			}
1076 			fs.first_m = NULL;
1077 
1078 			/*
1079 			 * Zero the page if necessary and mark it valid.
1080 			 */
1081 			if ((fs.m->flags & PG_ZERO) == 0) {
1082 				pmap_zero_page(fs.m);
1083 			} else {
1084 				VM_CNT_INC(v_ozfod);
1085 			}
1086 			VM_CNT_INC(v_zfod);
1087 			fs.m->valid = VM_PAGE_BITS_ALL;
1088 			/* Don't try to prefault neighboring pages. */
1089 			faultcount = 1;
1090 			break;	/* break to PAGE HAS BEEN FOUND */
1091 		} else {
1092 			KASSERT(fs.object != next_object,
1093 			    ("object loop %p", next_object));
1094 			VM_OBJECT_WLOCK(next_object);
1095 			vm_object_pip_add(next_object, 1);
1096 			if (fs.object != fs.first_object)
1097 				vm_object_pip_wakeup(fs.object);
1098 			fs.pindex +=
1099 			    OFF_TO_IDX(fs.object->backing_object_offset);
1100 			VM_OBJECT_WUNLOCK(fs.object);
1101 			fs.object = next_object;
1102 		}
1103 	}
1104 
1105 	vm_page_assert_xbusied(fs.m);
1106 
1107 	/*
1108 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1109 	 * is held.]
1110 	 */
1111 
1112 	/*
1113 	 * If the page is being written, but isn't already owned by the
1114 	 * top-level object, we have to copy it into a new page owned by the
1115 	 * top-level object.
1116 	 */
1117 	if (fs.object != fs.first_object) {
1118 		/*
1119 		 * We only really need to copy if we want to write it.
1120 		 */
1121 		if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1122 			/*
1123 			 * This allows pages to be virtually copied from a
1124 			 * backing_object into the first_object, where the
1125 			 * backing object has no other refs to it, and cannot
1126 			 * gain any more refs.  Instead of a bcopy, we just
1127 			 * move the page from the backing object to the
1128 			 * first object.  Note that we must mark the page
1129 			 * dirty in the first object so that it will go out
1130 			 * to swap when needed.
1131 			 */
1132 			is_first_object_locked = false;
1133 			if (
1134 				/*
1135 				 * Only one shadow object
1136 				 */
1137 				(fs.object->shadow_count == 1) &&
1138 				/*
1139 				 * No COW refs, except us
1140 				 */
1141 				(fs.object->ref_count == 1) &&
1142 				/*
1143 				 * No one else can look this object up
1144 				 */
1145 				(fs.object->handle == NULL) &&
1146 				/*
1147 				 * No other ways to look the object up
1148 				 */
1149 				((fs.object->type == OBJT_DEFAULT) ||
1150 				 (fs.object->type == OBJT_SWAP)) &&
1151 			    (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
1152 				/*
1153 				 * We don't chase down the shadow chain
1154 				 */
1155 			    fs.object == fs.first_object->backing_object) {
1156 
1157 				(void)vm_page_remove(fs.m);
1158 				vm_page_replace_checked(fs.m, fs.first_object,
1159 				    fs.first_pindex, fs.first_m);
1160 				vm_page_free(fs.first_m);
1161 				vm_page_dirty(fs.m);
1162 #if VM_NRESERVLEVEL > 0
1163 				/*
1164 				 * Rename the reservation.
1165 				 */
1166 				vm_reserv_rename(fs.m, fs.first_object,
1167 				    fs.object, OFF_TO_IDX(
1168 				    fs.first_object->backing_object_offset));
1169 #endif
1170 				/*
1171 				 * Removing the page from the backing object
1172 				 * unbusied it.
1173 				 */
1174 				vm_page_xbusy(fs.m);
1175 				fs.first_m = fs.m;
1176 				fs.m = NULL;
1177 				VM_CNT_INC(v_cow_optim);
1178 			} else {
1179 				/*
1180 				 * Oh, well, lets copy it.
1181 				 */
1182 				pmap_copy_page(fs.m, fs.first_m);
1183 				fs.first_m->valid = VM_PAGE_BITS_ALL;
1184 				if (wired && (fault_flags &
1185 				    VM_FAULT_WIRE) == 0) {
1186 					vm_page_wire(fs.first_m);
1187 					vm_page_unwire(fs.m, PQ_INACTIVE);
1188 				}
1189 				/*
1190 				 * We no longer need the old page or object.
1191 				 */
1192 				release_page(&fs);
1193 			}
1194 			/*
1195 			 * fs.object != fs.first_object due to above
1196 			 * conditional
1197 			 */
1198 			vm_object_pip_wakeup(fs.object);
1199 			VM_OBJECT_WUNLOCK(fs.object);
1200 
1201 			/*
1202 			 * We only try to prefault read-only mappings to the
1203 			 * neighboring pages when this copy-on-write fault is
1204 			 * a hard fault.  In other cases, trying to prefault
1205 			 * is typically wasted effort.
1206 			 */
1207 			if (faultcount == 0)
1208 				faultcount = 1;
1209 
1210 			/*
1211 			 * Only use the new page below...
1212 			 */
1213 			fs.object = fs.first_object;
1214 			fs.pindex = fs.first_pindex;
1215 			fs.m = fs.first_m;
1216 			if (!is_first_object_locked)
1217 				VM_OBJECT_WLOCK(fs.object);
1218 			VM_CNT_INC(v_cow_faults);
1219 			curthread->td_cow++;
1220 		} else {
1221 			prot &= ~VM_PROT_WRITE;
1222 		}
1223 	}
1224 
1225 	/*
1226 	 * We must verify that the maps have not changed since our last
1227 	 * lookup.
1228 	 */
1229 	if (!fs.lookup_still_valid) {
1230 		if (!vm_map_trylock_read(fs.map)) {
1231 			release_page(&fs);
1232 			unlock_and_deallocate(&fs);
1233 			goto RetryFault;
1234 		}
1235 		fs.lookup_still_valid = true;
1236 		if (fs.map->timestamp != fs.map_generation) {
1237 			result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
1238 			    &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
1239 
1240 			/*
1241 			 * If we don't need the page any longer, put it on the inactive
1242 			 * list (the easiest thing to do here).  If no one needs it,
1243 			 * pageout will grab it eventually.
1244 			 */
1245 			if (result != KERN_SUCCESS) {
1246 				release_page(&fs);
1247 				unlock_and_deallocate(&fs);
1248 
1249 				/*
1250 				 * If retry of map lookup would have blocked then
1251 				 * retry fault from start.
1252 				 */
1253 				if (result == KERN_FAILURE)
1254 					goto RetryFault;
1255 				return (result);
1256 			}
1257 			if ((retry_object != fs.first_object) ||
1258 			    (retry_pindex != fs.first_pindex)) {
1259 				release_page(&fs);
1260 				unlock_and_deallocate(&fs);
1261 				goto RetryFault;
1262 			}
1263 
1264 			/*
1265 			 * Check whether the protection has changed or the object has
1266 			 * been copied while we left the map unlocked. Changing from
1267 			 * read to write permission is OK - we leave the page
1268 			 * write-protected, and catch the write fault. Changing from
1269 			 * write to read permission means that we can't mark the page
1270 			 * write-enabled after all.
1271 			 */
1272 			prot &= retry_prot;
1273 			fault_type &= retry_prot;
1274 			if (prot == 0) {
1275 				release_page(&fs);
1276 				unlock_and_deallocate(&fs);
1277 				goto RetryFault;
1278 			}
1279 
1280 			/* Reassert because wired may have changed. */
1281 			KASSERT(wired || (fault_flags & VM_FAULT_WIRE) == 0,
1282 			    ("!wired && VM_FAULT_WIRE"));
1283 		}
1284 	}
1285 
1286 	/*
1287 	 * If the page was filled by a pager, save the virtual address that
1288 	 * should be faulted on next under a sequential access pattern to the
1289 	 * map entry.  A read lock on the map suffices to update this address
1290 	 * safely.
1291 	 */
1292 	if (hardfault)
1293 		fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE;
1294 
1295 	vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags, true);
1296 	vm_page_assert_xbusied(fs.m);
1297 
1298 	/*
1299 	 * Page must be completely valid or it is not fit to
1300 	 * map into user space.  vm_pager_get_pages() ensures this.
1301 	 */
1302 	KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
1303 	    ("vm_fault: page %p partially invalid", fs.m));
1304 	VM_OBJECT_WUNLOCK(fs.object);
1305 
1306 	/*
1307 	 * Put this page into the physical map.  We had to do the unlock above
1308 	 * because pmap_enter() may sleep.  We don't put the page
1309 	 * back on the active queue until later so that the pageout daemon
1310 	 * won't find it (yet).
1311 	 */
1312 	pmap_enter(fs.map->pmap, vaddr, fs.m, prot,
1313 	    fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0);
1314 	if (faultcount != 1 && (fault_flags & VM_FAULT_WIRE) == 0 &&
1315 	    wired == 0)
1316 		vm_fault_prefault(&fs, vaddr,
1317 		    faultcount > 0 ? behind : PFBAK,
1318 		    faultcount > 0 ? ahead : PFFOR, false);
1319 	VM_OBJECT_WLOCK(fs.object);
1320 
1321 	/*
1322 	 * If the page is not wired down, then put it where the pageout daemon
1323 	 * can find it.
1324 	 */
1325 	if ((fault_flags & VM_FAULT_WIRE) != 0) {
1326 		vm_page_wire(fs.m);
1327 	} else {
1328 		vm_page_lock(fs.m);
1329 		vm_page_activate(fs.m);
1330 		vm_page_unlock(fs.m);
1331 	}
1332 	if (m_hold != NULL) {
1333 		*m_hold = fs.m;
1334 		vm_page_wire(fs.m);
1335 	}
1336 	vm_page_xunbusy(fs.m);
1337 
1338 	/*
1339 	 * Unlock everything, and return
1340 	 */
1341 	unlock_and_deallocate(&fs);
1342 	if (hardfault) {
1343 		VM_CNT_INC(v_io_faults);
1344 		curthread->td_ru.ru_majflt++;
1345 #ifdef RACCT
1346 		if (racct_enable && fs.object->type == OBJT_VNODE) {
1347 			PROC_LOCK(curproc);
1348 			if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1349 				racct_add_force(curproc, RACCT_WRITEBPS,
1350 				    PAGE_SIZE + behind * PAGE_SIZE);
1351 				racct_add_force(curproc, RACCT_WRITEIOPS, 1);
1352 			} else {
1353 				racct_add_force(curproc, RACCT_READBPS,
1354 				    PAGE_SIZE + ahead * PAGE_SIZE);
1355 				racct_add_force(curproc, RACCT_READIOPS, 1);
1356 			}
1357 			PROC_UNLOCK(curproc);
1358 		}
1359 #endif
1360 	} else
1361 		curthread->td_ru.ru_minflt++;
1362 
1363 	return (KERN_SUCCESS);
1364 }
1365 
1366 /*
1367  * Speed up the reclamation of pages that precede the faulting pindex within
1368  * the first object of the shadow chain.  Essentially, perform the equivalent
1369  * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes
1370  * the faulting pindex by the cluster size when the pages read by vm_fault()
1371  * cross a cluster-size boundary.  The cluster size is the greater of the
1372  * smallest superpage size and VM_FAULT_DONTNEED_MIN.
1373  *
1374  * When "fs->first_object" is a shadow object, the pages in the backing object
1375  * that precede the faulting pindex are deactivated by vm_fault().  So, this
1376  * function must only be concerned with pages in the first object.
1377  */
1378 static void
1379 vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead)
1380 {
1381 	vm_map_entry_t entry;
1382 	vm_object_t first_object, object;
1383 	vm_offset_t end, start;
1384 	vm_page_t m, m_next;
1385 	vm_pindex_t pend, pstart;
1386 	vm_size_t size;
1387 
1388 	object = fs->object;
1389 	VM_OBJECT_ASSERT_WLOCKED(object);
1390 	first_object = fs->first_object;
1391 	if (first_object != object) {
1392 		if (!VM_OBJECT_TRYWLOCK(first_object)) {
1393 			VM_OBJECT_WUNLOCK(object);
1394 			VM_OBJECT_WLOCK(first_object);
1395 			VM_OBJECT_WLOCK(object);
1396 		}
1397 	}
1398 	/* Neither fictitious nor unmanaged pages can be reclaimed. */
1399 	if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
1400 		size = VM_FAULT_DONTNEED_MIN;
1401 		if (MAXPAGESIZES > 1 && size < pagesizes[1])
1402 			size = pagesizes[1];
1403 		end = rounddown2(vaddr, size);
1404 		if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) &&
1405 		    (entry = fs->entry)->start < end) {
1406 			if (end - entry->start < size)
1407 				start = entry->start;
1408 			else
1409 				start = end - size;
1410 			pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED);
1411 			pstart = OFF_TO_IDX(entry->offset) + atop(start -
1412 			    entry->start);
1413 			m_next = vm_page_find_least(first_object, pstart);
1414 			pend = OFF_TO_IDX(entry->offset) + atop(end -
1415 			    entry->start);
1416 			while ((m = m_next) != NULL && m->pindex < pend) {
1417 				m_next = TAILQ_NEXT(m, listq);
1418 				if (m->valid != VM_PAGE_BITS_ALL ||
1419 				    vm_page_busied(m))
1420 					continue;
1421 
1422 				/*
1423 				 * Don't clear PGA_REFERENCED, since it would
1424 				 * likely represent a reference by a different
1425 				 * process.
1426 				 *
1427 				 * Typically, at this point, prefetched pages
1428 				 * are still in the inactive queue.  Only
1429 				 * pages that triggered page faults are in the
1430 				 * active queue.
1431 				 */
1432 				vm_page_lock(m);
1433 				if (!vm_page_inactive(m))
1434 					vm_page_deactivate(m);
1435 				vm_page_unlock(m);
1436 			}
1437 		}
1438 	}
1439 	if (first_object != object)
1440 		VM_OBJECT_WUNLOCK(first_object);
1441 }
1442 
1443 /*
1444  * vm_fault_prefault provides a quick way of clustering
1445  * pagefaults into a processes address space.  It is a "cousin"
1446  * of vm_map_pmap_enter, except it runs at page fault time instead
1447  * of mmap time.
1448  */
1449 static void
1450 vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
1451     int backward, int forward, bool obj_locked)
1452 {
1453 	pmap_t pmap;
1454 	vm_map_entry_t entry;
1455 	vm_object_t backing_object, lobject;
1456 	vm_offset_t addr, starta;
1457 	vm_pindex_t pindex;
1458 	vm_page_t m;
1459 	int i;
1460 
1461 	pmap = fs->map->pmap;
1462 	if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1463 		return;
1464 
1465 	entry = fs->entry;
1466 
1467 	if (addra < backward * PAGE_SIZE) {
1468 		starta = entry->start;
1469 	} else {
1470 		starta = addra - backward * PAGE_SIZE;
1471 		if (starta < entry->start)
1472 			starta = entry->start;
1473 	}
1474 
1475 	/*
1476 	 * Generate the sequence of virtual addresses that are candidates for
1477 	 * prefaulting in an outward spiral from the faulting virtual address,
1478 	 * "addra".  Specifically, the sequence is "addra - PAGE_SIZE", "addra
1479 	 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
1480 	 * If the candidate address doesn't have a backing physical page, then
1481 	 * the loop immediately terminates.
1482 	 */
1483 	for (i = 0; i < 2 * imax(backward, forward); i++) {
1484 		addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
1485 		    PAGE_SIZE);
1486 		if (addr > addra + forward * PAGE_SIZE)
1487 			addr = 0;
1488 
1489 		if (addr < starta || addr >= entry->end)
1490 			continue;
1491 
1492 		if (!pmap_is_prefaultable(pmap, addr))
1493 			continue;
1494 
1495 		pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1496 		lobject = entry->object.vm_object;
1497 		if (!obj_locked)
1498 			VM_OBJECT_RLOCK(lobject);
1499 		while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1500 		    lobject->type == OBJT_DEFAULT &&
1501 		    (backing_object = lobject->backing_object) != NULL) {
1502 			KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
1503 			    0, ("vm_fault_prefault: unaligned object offset"));
1504 			pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1505 			VM_OBJECT_RLOCK(backing_object);
1506 			if (!obj_locked || lobject != entry->object.vm_object)
1507 				VM_OBJECT_RUNLOCK(lobject);
1508 			lobject = backing_object;
1509 		}
1510 		if (m == NULL) {
1511 			if (!obj_locked || lobject != entry->object.vm_object)
1512 				VM_OBJECT_RUNLOCK(lobject);
1513 			break;
1514 		}
1515 		if (m->valid == VM_PAGE_BITS_ALL &&
1516 		    (m->flags & PG_FICTITIOUS) == 0)
1517 			pmap_enter_quick(pmap, addr, m, entry->protection);
1518 		if (!obj_locked || lobject != entry->object.vm_object)
1519 			VM_OBJECT_RUNLOCK(lobject);
1520 	}
1521 }
1522 
1523 /*
1524  * Hold each of the physical pages that are mapped by the specified range of
1525  * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
1526  * and allow the specified types of access, "prot".  If all of the implied
1527  * pages are successfully held, then the number of held pages is returned
1528  * together with pointers to those pages in the array "ma".  However, if any
1529  * of the pages cannot be held, -1 is returned.
1530  */
1531 int
1532 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
1533     vm_prot_t prot, vm_page_t *ma, int max_count)
1534 {
1535 	vm_offset_t end, va;
1536 	vm_page_t *mp;
1537 	int count;
1538 	boolean_t pmap_failed;
1539 
1540 	if (len == 0)
1541 		return (0);
1542 	end = round_page(addr + len);
1543 	addr = trunc_page(addr);
1544 
1545 	/*
1546 	 * Check for illegal addresses.
1547 	 */
1548 	if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
1549 		return (-1);
1550 
1551 	if (atop(end - addr) > max_count)
1552 		panic("vm_fault_quick_hold_pages: count > max_count");
1553 	count = atop(end - addr);
1554 
1555 	/*
1556 	 * Most likely, the physical pages are resident in the pmap, so it is
1557 	 * faster to try pmap_extract_and_hold() first.
1558 	 */
1559 	pmap_failed = FALSE;
1560 	for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
1561 		*mp = pmap_extract_and_hold(map->pmap, va, prot);
1562 		if (*mp == NULL)
1563 			pmap_failed = TRUE;
1564 		else if ((prot & VM_PROT_WRITE) != 0 &&
1565 		    (*mp)->dirty != VM_PAGE_BITS_ALL) {
1566 			/*
1567 			 * Explicitly dirty the physical page.  Otherwise, the
1568 			 * caller's changes may go unnoticed because they are
1569 			 * performed through an unmanaged mapping or by a DMA
1570 			 * operation.
1571 			 *
1572 			 * The object lock is not held here.
1573 			 * See vm_page_clear_dirty_mask().
1574 			 */
1575 			vm_page_dirty(*mp);
1576 		}
1577 	}
1578 	if (pmap_failed) {
1579 		/*
1580 		 * One or more pages could not be held by the pmap.  Either no
1581 		 * page was mapped at the specified virtual address or that
1582 		 * mapping had insufficient permissions.  Attempt to fault in
1583 		 * and hold these pages.
1584 		 *
1585 		 * If vm_fault_disable_pagefaults() was called,
1586 		 * i.e., TDP_NOFAULTING is set, we must not sleep nor
1587 		 * acquire MD VM locks, which means we must not call
1588 		 * vm_fault_hold().  Some (out of tree) callers mark
1589 		 * too wide a code area with vm_fault_disable_pagefaults()
1590 		 * already, use the VM_PROT_QUICK_NOFAULT flag to request
1591 		 * the proper behaviour explicitly.
1592 		 */
1593 		if ((prot & VM_PROT_QUICK_NOFAULT) != 0 &&
1594 		    (curthread->td_pflags & TDP_NOFAULTING) != 0)
1595 			goto error;
1596 		for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1597 			if (*mp == NULL && vm_fault_hold(map, va, prot,
1598 			    VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
1599 				goto error;
1600 	}
1601 	return (count);
1602 error:
1603 	for (mp = ma; mp < ma + count; mp++)
1604 		if (*mp != NULL)
1605 			vm_page_unwire(*mp, PQ_INACTIVE);
1606 	return (-1);
1607 }
1608 
1609 /*
1610  *	Routine:
1611  *		vm_fault_copy_entry
1612  *	Function:
1613  *		Create new shadow object backing dst_entry with private copy of
1614  *		all underlying pages. When src_entry is equal to dst_entry,
1615  *		function implements COW for wired-down map entry. Otherwise,
1616  *		it forks wired entry into dst_map.
1617  *
1618  *	In/out conditions:
1619  *		The source and destination maps must be locked for write.
1620  *		The source map entry must be wired down (or be a sharing map
1621  *		entry corresponding to a main map entry that is wired down).
1622  */
1623 void
1624 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1625     vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1626     vm_ooffset_t *fork_charge)
1627 {
1628 	vm_object_t backing_object, dst_object, object, src_object;
1629 	vm_pindex_t dst_pindex, pindex, src_pindex;
1630 	vm_prot_t access, prot;
1631 	vm_offset_t vaddr;
1632 	vm_page_t dst_m;
1633 	vm_page_t src_m;
1634 	boolean_t upgrade;
1635 
1636 #ifdef	lint
1637 	src_map++;
1638 #endif	/* lint */
1639 
1640 	upgrade = src_entry == dst_entry;
1641 	access = prot = dst_entry->protection;
1642 
1643 	src_object = src_entry->object.vm_object;
1644 	src_pindex = OFF_TO_IDX(src_entry->offset);
1645 
1646 	if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
1647 		dst_object = src_object;
1648 		vm_object_reference(dst_object);
1649 	} else {
1650 		/*
1651 		 * Create the top-level object for the destination entry. (Doesn't
1652 		 * actually shadow anything - we copy the pages directly.)
1653 		 */
1654 		dst_object = vm_object_allocate(OBJT_DEFAULT,
1655 		    atop(dst_entry->end - dst_entry->start));
1656 #if VM_NRESERVLEVEL > 0
1657 		dst_object->flags |= OBJ_COLORED;
1658 		dst_object->pg_color = atop(dst_entry->start);
1659 #endif
1660 		dst_object->domain = src_object->domain;
1661 		dst_object->charge = dst_entry->end - dst_entry->start;
1662 	}
1663 
1664 	VM_OBJECT_WLOCK(dst_object);
1665 	KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1666 	    ("vm_fault_copy_entry: vm_object not NULL"));
1667 	if (src_object != dst_object) {
1668 		dst_entry->object.vm_object = dst_object;
1669 		dst_entry->offset = 0;
1670 		dst_entry->eflags &= ~MAP_ENTRY_VN_EXEC;
1671 	}
1672 	if (fork_charge != NULL) {
1673 		KASSERT(dst_entry->cred == NULL,
1674 		    ("vm_fault_copy_entry: leaked swp charge"));
1675 		dst_object->cred = curthread->td_ucred;
1676 		crhold(dst_object->cred);
1677 		*fork_charge += dst_object->charge;
1678 	} else if ((dst_object->type == OBJT_DEFAULT ||
1679 	    dst_object->type == OBJT_SWAP) &&
1680 	    dst_object->cred == NULL) {
1681 		KASSERT(dst_entry->cred != NULL, ("no cred for entry %p",
1682 		    dst_entry));
1683 		dst_object->cred = dst_entry->cred;
1684 		dst_entry->cred = NULL;
1685 	}
1686 
1687 	/*
1688 	 * If not an upgrade, then enter the mappings in the pmap as
1689 	 * read and/or execute accesses.  Otherwise, enter them as
1690 	 * write accesses.
1691 	 *
1692 	 * A writeable large page mapping is only created if all of
1693 	 * the constituent small page mappings are modified. Marking
1694 	 * PTEs as modified on inception allows promotion to happen
1695 	 * without taking potentially large number of soft faults.
1696 	 */
1697 	if (!upgrade)
1698 		access &= ~VM_PROT_WRITE;
1699 
1700 	/*
1701 	 * Loop through all of the virtual pages within the entry's
1702 	 * range, copying each page from the source object to the
1703 	 * destination object.  Since the source is wired, those pages
1704 	 * must exist.  In contrast, the destination is pageable.
1705 	 * Since the destination object doesn't share any backing storage
1706 	 * with the source object, all of its pages must be dirtied,
1707 	 * regardless of whether they can be written.
1708 	 */
1709 	for (vaddr = dst_entry->start, dst_pindex = 0;
1710 	    vaddr < dst_entry->end;
1711 	    vaddr += PAGE_SIZE, dst_pindex++) {
1712 again:
1713 		/*
1714 		 * Find the page in the source object, and copy it in.
1715 		 * Because the source is wired down, the page will be
1716 		 * in memory.
1717 		 */
1718 		if (src_object != dst_object)
1719 			VM_OBJECT_RLOCK(src_object);
1720 		object = src_object;
1721 		pindex = src_pindex + dst_pindex;
1722 		while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1723 		    (backing_object = object->backing_object) != NULL) {
1724 			/*
1725 			 * Unless the source mapping is read-only or
1726 			 * it is presently being upgraded from
1727 			 * read-only, the first object in the shadow
1728 			 * chain should provide all of the pages.  In
1729 			 * other words, this loop body should never be
1730 			 * executed when the source mapping is already
1731 			 * read/write.
1732 			 */
1733 			KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 ||
1734 			    upgrade,
1735 			    ("vm_fault_copy_entry: main object missing page"));
1736 
1737 			VM_OBJECT_RLOCK(backing_object);
1738 			pindex += OFF_TO_IDX(object->backing_object_offset);
1739 			if (object != dst_object)
1740 				VM_OBJECT_RUNLOCK(object);
1741 			object = backing_object;
1742 		}
1743 		KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing"));
1744 
1745 		if (object != dst_object) {
1746 			/*
1747 			 * Allocate a page in the destination object.
1748 			 */
1749 			dst_m = vm_page_alloc(dst_object, (src_object ==
1750 			    dst_object ? src_pindex : 0) + dst_pindex,
1751 			    VM_ALLOC_NORMAL);
1752 			if (dst_m == NULL) {
1753 				VM_OBJECT_WUNLOCK(dst_object);
1754 				VM_OBJECT_RUNLOCK(object);
1755 				vm_wait(dst_object);
1756 				VM_OBJECT_WLOCK(dst_object);
1757 				goto again;
1758 			}
1759 			pmap_copy_page(src_m, dst_m);
1760 			VM_OBJECT_RUNLOCK(object);
1761 			dst_m->dirty = dst_m->valid = src_m->valid;
1762 		} else {
1763 			dst_m = src_m;
1764 			if (vm_page_sleep_if_busy(dst_m, "fltupg"))
1765 				goto again;
1766 			if (dst_m->pindex >= dst_object->size)
1767 				/*
1768 				 * We are upgrading.  Index can occur
1769 				 * out of bounds if the object type is
1770 				 * vnode and the file was truncated.
1771 				 */
1772 				break;
1773 			vm_page_xbusy(dst_m);
1774 		}
1775 		VM_OBJECT_WUNLOCK(dst_object);
1776 
1777 		/*
1778 		 * Enter it in the pmap. If a wired, copy-on-write
1779 		 * mapping is being replaced by a write-enabled
1780 		 * mapping, then wire that new mapping.
1781 		 *
1782 		 * The page can be invalid if the user called
1783 		 * msync(MS_INVALIDATE) or truncated the backing vnode
1784 		 * or shared memory object.  In this case, do not
1785 		 * insert it into pmap, but still do the copy so that
1786 		 * all copies of the wired map entry have similar
1787 		 * backing pages.
1788 		 */
1789 		if (dst_m->valid == VM_PAGE_BITS_ALL) {
1790 			pmap_enter(dst_map->pmap, vaddr, dst_m, prot,
1791 			    access | (upgrade ? PMAP_ENTER_WIRED : 0), 0);
1792 		}
1793 
1794 		/*
1795 		 * Mark it no longer busy, and put it on the active list.
1796 		 */
1797 		VM_OBJECT_WLOCK(dst_object);
1798 
1799 		if (upgrade) {
1800 			if (src_m != dst_m) {
1801 				vm_page_unwire(src_m, PQ_INACTIVE);
1802 				vm_page_wire(dst_m);
1803 			} else {
1804 				KASSERT(vm_page_wired(dst_m),
1805 				    ("dst_m %p is not wired", dst_m));
1806 			}
1807 		} else {
1808 			vm_page_lock(dst_m);
1809 			vm_page_activate(dst_m);
1810 			vm_page_unlock(dst_m);
1811 		}
1812 		vm_page_xunbusy(dst_m);
1813 	}
1814 	VM_OBJECT_WUNLOCK(dst_object);
1815 	if (upgrade) {
1816 		dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1817 		vm_object_deallocate(src_object);
1818 	}
1819 }
1820 
1821 /*
1822  * Block entry into the machine-independent layer's page fault handler by
1823  * the calling thread.  Subsequent calls to vm_fault() by that thread will
1824  * return KERN_PROTECTION_FAILURE.  Enable machine-dependent handling of
1825  * spurious page faults.
1826  */
1827 int
1828 vm_fault_disable_pagefaults(void)
1829 {
1830 
1831 	return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
1832 }
1833 
1834 void
1835 vm_fault_enable_pagefaults(int save)
1836 {
1837 
1838 	curthread_pflags_restore(save);
1839 }
1840