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