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