2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
10 * This code is derived from software contributed to Berkeley by
11 * The Mach Operating System project at Carnegie-Mellon University.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * from: @(#)vm_fault.c 8.4 (Berkeley) 1/12/94
44 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
45 * All rights reserved.
47 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
49 * Permission to use, copy, modify and distribute this software and
50 * its documentation is hereby granted, provided that both the copyright
51 * notice and this permission notice appear in all copies of the
52 * software, derivative works or modified versions, and any portions
53 * thereof, and that both notices appear in supporting documentation.
55 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
57 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
59 * Carnegie Mellon requests users of this software to return to
61 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
62 * School of Computer Science
63 * Carnegie Mellon University
64 * Pittsburgh PA 15213-3890
66 * any improvements or extensions that they make and grant Carnegie the
67 * rights to redistribute these changes.
71 * Page fault handling module.
74 #include <sys/cdefs.h>
75 __FBSDID("$FreeBSD$");
77 #include "opt_ktrace.h"
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
86 #include <sys/racct.h>
87 #include <sys/resourcevar.h>
88 #include <sys/rwlock.h>
89 #include <sys/sysctl.h>
90 #include <sys/vmmeter.h>
91 #include <sys/vnode.h>
93 #include <sys/ktrace.h>
97 #include <vm/vm_param.h>
99 #include <vm/vm_map.h>
100 #include <vm/vm_object.h>
101 #include <vm/vm_page.h>
102 #include <vm/vm_pageout.h>
103 #include <vm/vm_kern.h>
104 #include <vm/vm_pager.h>
105 #include <vm/vm_extern.h>
106 #include <vm/vm_reserv.h>
111 #define VM_FAULT_READ_DEFAULT (1 + VM_FAULT_READ_AHEAD_INIT)
112 #define VM_FAULT_READ_MAX (1 + VM_FAULT_READ_AHEAD_MAX)
114 #define VM_FAULT_DONTNEED_MIN 1048576
121 vm_object_t first_object;
122 vm_pindex_t first_pindex;
124 vm_map_entry_t entry;
126 bool lookup_still_valid;
130 static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr,
132 static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
133 int backward, int forward);
136 release_page(struct faultstate *fs)
139 vm_page_xunbusy(fs->m);
141 vm_page_deactivate(fs->m);
142 vm_page_unlock(fs->m);
147 unlock_map(struct faultstate *fs)
150 if (fs->lookup_still_valid) {
151 vm_map_lookup_done(fs->map, fs->entry);
152 fs->lookup_still_valid = false;
157 unlock_vp(struct faultstate *fs)
160 if (fs->vp != NULL) {
167 unlock_and_deallocate(struct faultstate *fs)
170 vm_object_pip_wakeup(fs->object);
171 VM_OBJECT_WUNLOCK(fs->object);
172 if (fs->object != fs->first_object) {
173 VM_OBJECT_WLOCK(fs->first_object);
174 vm_page_lock(fs->first_m);
175 vm_page_free(fs->first_m);
176 vm_page_unlock(fs->first_m);
177 vm_object_pip_wakeup(fs->first_object);
178 VM_OBJECT_WUNLOCK(fs->first_object);
181 vm_object_deallocate(fs->first_object);
187 vm_fault_dirty(vm_map_entry_t entry, vm_page_t m, vm_prot_t prot,
188 vm_prot_t fault_type, int fault_flags, bool set_wd)
192 if (((prot & VM_PROT_WRITE) == 0 &&
193 (fault_flags & VM_FAULT_DIRTY) == 0) ||
194 (m->oflags & VPO_UNMANAGED) != 0)
197 VM_OBJECT_ASSERT_LOCKED(m->object);
199 need_dirty = ((fault_type & VM_PROT_WRITE) != 0 &&
200 (fault_flags & VM_FAULT_WIRE) == 0) ||
201 (fault_flags & VM_FAULT_DIRTY) != 0;
204 vm_object_set_writeable_dirty(m->object);
207 * If two callers of vm_fault_dirty() with set_wd ==
208 * FALSE, one for the map entry with MAP_ENTRY_NOSYNC
209 * flag set, other with flag clear, race, it is
210 * possible for the no-NOSYNC thread to see m->dirty
211 * != 0 and not clear VPO_NOSYNC. Take vm_page lock
212 * around manipulation of VPO_NOSYNC and
213 * vm_page_dirty() call, to avoid the race and keep
214 * m->oflags consistent.
219 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
220 * if the page is already dirty to prevent data written with
221 * the expectation of being synced from not being synced.
222 * Likewise if this entry does not request NOSYNC then make
223 * sure the page isn't marked NOSYNC. Applications sharing
224 * data should use the same flags to avoid ping ponging.
226 if ((entry->eflags & MAP_ENTRY_NOSYNC) != 0) {
228 m->oflags |= VPO_NOSYNC;
231 m->oflags &= ~VPO_NOSYNC;
235 * If the fault is a write, we know that this page is being
236 * written NOW so dirty it explicitly to save on
237 * pmap_is_modified() calls later.
239 * Also tell the backing pager, if any, that it should remove
240 * any swap backing since the page is now dirty.
247 vm_pager_page_unswapped(m);
251 vm_fault_fill_hold(vm_page_t *m_hold, vm_page_t m)
254 if (m_hold != NULL) {
263 * Unlocks fs.first_object and fs.map on success.
266 vm_fault_soft_fast(struct faultstate *fs, vm_offset_t vaddr, vm_prot_t prot,
267 int fault_type, int fault_flags, boolean_t wired, vm_page_t *m_hold)
270 #if defined(__amd64__) && VM_NRESERVLEVEL > 0
276 MPASS(fs->vp == NULL);
277 m = vm_page_lookup(fs->first_object, fs->first_pindex);
278 /* A busy page can be mapped for read|execute access. */
279 if (m == NULL || ((prot & VM_PROT_WRITE) != 0 &&
280 vm_page_busied(m)) || m->valid != VM_PAGE_BITS_ALL)
281 return (KERN_FAILURE);
284 #if defined(__amd64__) && VM_NRESERVLEVEL > 0
285 if ((m->flags & PG_FICTITIOUS) == 0 &&
286 (m_super = vm_reserv_to_superpage(m)) != NULL &&
287 rounddown2(vaddr, pagesizes[m_super->psind]) >= fs->entry->start &&
288 roundup2(vaddr + 1, pagesizes[m_super->psind]) <= fs->entry->end &&
289 (vaddr & (pagesizes[m_super->psind] - 1)) == (VM_PAGE_TO_PHYS(m) &
290 (pagesizes[m_super->psind] - 1)) &&
291 pmap_ps_enabled(fs->map->pmap)) {
292 flags = PS_ALL_VALID;
293 if ((prot & VM_PROT_WRITE) != 0) {
295 * Create a superpage mapping allowing write access
296 * only if none of the constituent pages are busy and
297 * all of them are already dirty (except possibly for
298 * the page that was faulted on).
300 flags |= PS_NONE_BUSY;
301 if ((fs->first_object->flags & OBJ_UNMANAGED) == 0)
302 flags |= PS_ALL_DIRTY;
304 if (vm_page_ps_test(m_super, flags, m)) {
306 psind = m_super->psind;
307 vaddr = rounddown2(vaddr, pagesizes[psind]);
308 /* Preset the modified bit for dirty superpages. */
309 if ((flags & PS_ALL_DIRTY) != 0)
310 fault_type |= VM_PROT_WRITE;
314 rv = pmap_enter(fs->map->pmap, vaddr, m_map, prot, fault_type |
315 PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED : 0), psind);
316 if (rv != KERN_SUCCESS)
318 vm_fault_fill_hold(m_hold, m);
319 vm_fault_dirty(fs->entry, m, prot, fault_type, fault_flags, false);
320 VM_OBJECT_RUNLOCK(fs->first_object);
321 if (psind == 0 && !wired)
322 vm_fault_prefault(fs, vaddr, PFBAK, PFFOR);
323 vm_map_lookup_done(fs->map, fs->entry);
324 curthread->td_ru.ru_minflt++;
325 return (KERN_SUCCESS);
329 vm_fault_restore_map_lock(struct faultstate *fs)
332 VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
333 MPASS(fs->first_object->paging_in_progress > 0);
335 if (!vm_map_trylock_read(fs->map)) {
336 VM_OBJECT_WUNLOCK(fs->first_object);
337 vm_map_lock_read(fs->map);
338 VM_OBJECT_WLOCK(fs->first_object);
340 fs->lookup_still_valid = true;
344 vm_fault_populate_check_page(vm_page_t m)
348 * Check each page to ensure that the pager is obeying the
349 * interface: the page must be installed in the object, fully
350 * valid, and exclusively busied.
353 MPASS(m->valid == VM_PAGE_BITS_ALL);
354 MPASS(vm_page_xbusied(m));
358 vm_fault_populate_cleanup(vm_object_t object, vm_pindex_t first,
364 VM_OBJECT_ASSERT_WLOCKED(object);
365 MPASS(first <= last);
366 for (pidx = first, m = vm_page_lookup(object, pidx);
367 pidx <= last; pidx++, m = vm_page_next(m)) {
368 vm_fault_populate_check_page(m);
370 vm_page_deactivate(m);
377 vm_fault_populate(struct faultstate *fs, vm_offset_t vaddr, vm_prot_t prot,
378 int fault_type, int fault_flags, boolean_t wired, vm_page_t *m_hold)
381 vm_pindex_t map_first, map_last, pager_first, pager_last, pidx;
384 MPASS(fs->object == fs->first_object);
385 VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
386 MPASS(fs->first_object->paging_in_progress > 0);
387 MPASS(fs->first_object->backing_object == NULL);
388 MPASS(fs->lookup_still_valid);
390 pager_first = OFF_TO_IDX(fs->entry->offset);
391 pager_last = pager_first + atop(fs->entry->end - fs->entry->start) - 1;
396 * Call the pager (driver) populate() method.
398 * There is no guarantee that the method will be called again
399 * if the current fault is for read, and a future fault is
400 * for write. Report the entry's maximum allowed protection
403 rv = vm_pager_populate(fs->first_object, fs->first_pindex,
404 fault_type, fs->entry->max_protection, &pager_first, &pager_last);
406 VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
407 if (rv == VM_PAGER_BAD) {
409 * VM_PAGER_BAD is the backdoor for a pager to request
410 * normal fault handling.
412 vm_fault_restore_map_lock(fs);
413 if (fs->map->timestamp != fs->map_generation)
414 return (KERN_RESOURCE_SHORTAGE); /* RetryFault */
415 return (KERN_NOT_RECEIVER);
417 if (rv != VM_PAGER_OK)
418 return (KERN_FAILURE); /* AKA SIGSEGV */
420 /* Ensure that the driver is obeying the interface. */
421 MPASS(pager_first <= pager_last);
422 MPASS(fs->first_pindex <= pager_last);
423 MPASS(fs->first_pindex >= pager_first);
424 MPASS(pager_last < fs->first_object->size);
426 vm_fault_restore_map_lock(fs);
427 if (fs->map->timestamp != fs->map_generation) {
428 vm_fault_populate_cleanup(fs->first_object, pager_first,
430 return (KERN_RESOURCE_SHORTAGE); /* RetryFault */
434 * The map is unchanged after our last unlock. Process the fault.
436 * The range [pager_first, pager_last] that is given to the
437 * pager is only a hint. The pager may populate any range
438 * within the object that includes the requested page index.
439 * In case the pager expanded the range, clip it to fit into
442 map_first = OFF_TO_IDX(fs->entry->offset);
443 if (map_first > pager_first) {
444 vm_fault_populate_cleanup(fs->first_object, pager_first,
446 pager_first = map_first;
448 map_last = map_first + atop(fs->entry->end - fs->entry->start) - 1;
449 if (map_last < pager_last) {
450 vm_fault_populate_cleanup(fs->first_object, map_last + 1,
452 pager_last = map_last;
454 for (pidx = pager_first, m = vm_page_lookup(fs->first_object, pidx);
455 pidx <= pager_last; pidx++, m = vm_page_next(m)) {
456 vm_fault_populate_check_page(m);
457 vm_fault_dirty(fs->entry, m, prot, fault_type, fault_flags,
459 VM_OBJECT_WUNLOCK(fs->first_object);
460 pmap_enter(fs->map->pmap, fs->entry->start + IDX_TO_OFF(pidx) -
461 fs->entry->offset, m, prot, fault_type | (wired ?
462 PMAP_ENTER_WIRED : 0), 0);
463 VM_OBJECT_WLOCK(fs->first_object);
464 if (pidx == fs->first_pindex)
465 vm_fault_fill_hold(m_hold, m);
467 if ((fault_flags & VM_FAULT_WIRE) != 0) {
468 KASSERT(wired, ("VM_FAULT_WIRE && !wired"));
476 curthread->td_ru.ru_majflt++;
477 return (KERN_SUCCESS);
483 * Handle a page fault occurring at the given address,
484 * requiring the given permissions, in the map specified.
485 * If successful, the page is inserted into the
486 * associated physical map.
488 * NOTE: the given address should be truncated to the
489 * proper page address.
491 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
492 * a standard error specifying why the fault is fatal is returned.
494 * The map in question must be referenced, and remains so.
495 * Caller may hold no locks.
498 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
505 if ((td->td_pflags & TDP_NOFAULTING) != 0)
506 return (KERN_PROTECTION_FAILURE);
508 if (map != kernel_map && KTRPOINT(td, KTR_FAULT))
509 ktrfault(vaddr, fault_type);
511 result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags,
514 if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND))
521 vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
522 int fault_flags, vm_page_t *m_hold)
524 struct faultstate fs;
526 vm_object_t next_object, retry_object;
527 vm_offset_t e_end, e_start;
528 vm_pindex_t retry_pindex;
529 vm_prot_t prot, retry_prot;
530 int ahead, alloc_req, behind, cluster_offset, error, era, faultcount;
531 int locked, nera, result, rv;
533 boolean_t wired; /* Passed by reference. */
534 bool dead, hardfault, is_first_object_locked;
536 VM_CNT_INC(v_vm_faults);
545 * Find the backing store object and offset into it to begin the
549 result = vm_map_lookup(&fs.map, vaddr, fault_type |
550 VM_PROT_FAULT_LOOKUP, &fs.entry, &fs.first_object,
551 &fs.first_pindex, &prot, &wired);
552 if (result != KERN_SUCCESS) {
557 fs.map_generation = fs.map->timestamp;
559 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
560 panic("%s: fault on nofault entry, addr: %#lx",
561 __func__, (u_long)vaddr);
564 if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
565 fs.entry->wiring_thread != curthread) {
566 vm_map_unlock_read(fs.map);
568 if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
569 (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
571 fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
572 vm_map_unlock_and_wait(fs.map, 0);
574 vm_map_unlock(fs.map);
578 MPASS((fs.entry->eflags & MAP_ENTRY_GUARD) == 0);
581 fault_type = prot | (fault_type & VM_PROT_COPY);
583 KASSERT((fault_flags & VM_FAULT_WIRE) == 0,
584 ("!wired && VM_FAULT_WIRE"));
587 * Try to avoid lock contention on the top-level object through
588 * special-case handling of some types of page faults, specifically,
589 * those that are both (1) mapping an existing page from the top-
590 * level object and (2) not having to mark that object as containing
591 * dirty pages. Under these conditions, a read lock on the top-level
592 * object suffices, allowing multiple page faults of a similar type to
593 * run in parallel on the same top-level object.
595 if (fs.vp == NULL /* avoid locked vnode leak */ &&
596 (fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0 &&
597 /* avoid calling vm_object_set_writeable_dirty() */
598 ((prot & VM_PROT_WRITE) == 0 ||
599 (fs.first_object->type != OBJT_VNODE &&
600 (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
601 (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0)) {
602 VM_OBJECT_RLOCK(fs.first_object);
603 if ((prot & VM_PROT_WRITE) == 0 ||
604 (fs.first_object->type != OBJT_VNODE &&
605 (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
606 (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0) {
607 rv = vm_fault_soft_fast(&fs, vaddr, prot, fault_type,
608 fault_flags, wired, m_hold);
609 if (rv == KERN_SUCCESS)
612 if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) {
613 VM_OBJECT_RUNLOCK(fs.first_object);
614 VM_OBJECT_WLOCK(fs.first_object);
617 VM_OBJECT_WLOCK(fs.first_object);
621 * Make a reference to this object to prevent its disposal while we
622 * are messing with it. Once we have the reference, the map is free
623 * to be diddled. Since objects reference their shadows (and copies),
624 * they will stay around as well.
626 * Bump the paging-in-progress count to prevent size changes (e.g.
627 * truncation operations) during I/O.
629 vm_object_reference_locked(fs.first_object);
630 vm_object_pip_add(fs.first_object, 1);
632 fs.lookup_still_valid = true;
637 * Search for the page at object/offset.
639 fs.object = fs.first_object;
640 fs.pindex = fs.first_pindex;
643 * If the object is marked for imminent termination,
644 * we retry here, since the collapse pass has raced
645 * with us. Otherwise, if we see terminally dead
646 * object, return fail.
648 if ((fs.object->flags & OBJ_DEAD) != 0) {
649 dead = fs.object->type == OBJT_DEAD;
650 unlock_and_deallocate(&fs);
652 return (KERN_PROTECTION_FAILURE);
658 * See if page is resident
660 fs.m = vm_page_lookup(fs.object, fs.pindex);
663 * Wait/Retry if the page is busy. We have to do this
664 * if the page is either exclusive or shared busy
665 * because the vm_pager may be using read busy for
666 * pageouts (and even pageins if it is the vnode
667 * pager), and we could end up trying to pagein and
668 * pageout the same page simultaneously.
670 * We can theoretically allow the busy case on a read
671 * fault if the page is marked valid, but since such
672 * pages are typically already pmap'd, putting that
673 * special case in might be more effort then it is
674 * worth. We cannot under any circumstances mess
675 * around with a shared busied page except, perhaps,
678 if (vm_page_busied(fs.m)) {
680 * Reference the page before unlocking and
681 * sleeping so that the page daemon is less
682 * likely to reclaim it.
684 vm_page_aflag_set(fs.m, PGA_REFERENCED);
685 if (fs.object != fs.first_object) {
686 if (!VM_OBJECT_TRYWLOCK(
688 VM_OBJECT_WUNLOCK(fs.object);
689 VM_OBJECT_WLOCK(fs.first_object);
690 VM_OBJECT_WLOCK(fs.object);
692 vm_page_lock(fs.first_m);
693 vm_page_free(fs.first_m);
694 vm_page_unlock(fs.first_m);
695 vm_object_pip_wakeup(fs.first_object);
696 VM_OBJECT_WUNLOCK(fs.first_object);
700 if (fs.m == vm_page_lookup(fs.object,
702 vm_page_sleep_if_busy(fs.m, "vmpfw");
704 vm_object_pip_wakeup(fs.object);
705 VM_OBJECT_WUNLOCK(fs.object);
706 VM_CNT_INC(v_intrans);
707 vm_object_deallocate(fs.first_object);
711 vm_page_remque(fs.m);
712 vm_page_unlock(fs.m);
715 * Mark page busy for other processes, and the
716 * pagedaemon. If it still isn't completely valid
717 * (readable), jump to readrest, else break-out ( we
721 if (fs.m->valid != VM_PAGE_BITS_ALL)
725 KASSERT(fs.m == NULL, ("fs.m should be NULL, not %p", fs.m));
728 * Page is not resident. If the pager might contain the page
729 * or this is the beginning of the search, allocate a new
730 * page. (Default objects are zero-fill, so there is no real
733 if (fs.object->type != OBJT_DEFAULT ||
734 fs.object == fs.first_object) {
735 if (fs.pindex >= fs.object->size) {
736 unlock_and_deallocate(&fs);
737 return (KERN_PROTECTION_FAILURE);
740 if (fs.object == fs.first_object &&
741 (fs.first_object->flags & OBJ_POPULATE) != 0 &&
742 fs.first_object->shadow_count == 0) {
743 rv = vm_fault_populate(&fs, vaddr, prot,
744 fault_type, fault_flags, wired, m_hold);
748 unlock_and_deallocate(&fs);
750 case KERN_RESOURCE_SHORTAGE:
751 unlock_and_deallocate(&fs);
753 case KERN_NOT_RECEIVER:
755 * Pager's populate() method
756 * returned VM_PAGER_BAD.
760 panic("inconsistent return codes");
765 * Allocate a new page for this object/offset pair.
767 * Unlocked read of the p_flag is harmless. At
768 * worst, the P_KILLED might be not observed
769 * there, and allocation can fail, causing
770 * restart and new reading of the p_flag.
772 if (!vm_page_count_severe() || P_KILLED(curproc)) {
773 #if VM_NRESERVLEVEL > 0
774 vm_object_color(fs.object, atop(vaddr) -
777 alloc_req = P_KILLED(curproc) ?
778 VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
779 if (fs.object->type != OBJT_VNODE &&
780 fs.object->backing_object == NULL)
781 alloc_req |= VM_ALLOC_ZERO;
782 fs.m = vm_page_alloc(fs.object, fs.pindex,
786 unlock_and_deallocate(&fs);
794 * At this point, we have either allocated a new page or found
795 * an existing page that is only partially valid.
797 * We hold a reference on the current object and the page is
802 * If the pager for the current object might have the page,
803 * then determine the number of additional pages to read and
804 * potentially reprioritize previously read pages for earlier
805 * reclamation. These operations should only be performed
806 * once per page fault. Even if the current pager doesn't
807 * have the page, the number of additional pages to read will
808 * apply to subsequent objects in the shadow chain.
810 if (fs.object->type != OBJT_DEFAULT && nera == -1 &&
811 !P_KILLED(curproc)) {
812 KASSERT(fs.lookup_still_valid, ("map unlocked"));
813 era = fs.entry->read_ahead;
814 behavior = vm_map_entry_behavior(fs.entry);
815 if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
817 } else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
818 nera = VM_FAULT_READ_AHEAD_MAX;
819 if (vaddr == fs.entry->next_read)
820 vm_fault_dontneed(&fs, vaddr, nera);
821 } else if (vaddr == fs.entry->next_read) {
823 * This is a sequential fault. Arithmetically
824 * increase the requested number of pages in
825 * the read-ahead window. The requested
826 * number of pages is "# of sequential faults
827 * x (read ahead min + 1) + read ahead min"
829 nera = VM_FAULT_READ_AHEAD_MIN;
832 if (nera > VM_FAULT_READ_AHEAD_MAX)
833 nera = VM_FAULT_READ_AHEAD_MAX;
835 if (era == VM_FAULT_READ_AHEAD_MAX)
836 vm_fault_dontneed(&fs, vaddr, nera);
839 * This is a non-sequential fault.
845 * A read lock on the map suffices to update
846 * the read ahead count safely.
848 fs.entry->read_ahead = nera;
852 * Prepare for unlocking the map. Save the map
853 * entry's start and end addresses, which are used to
854 * optimize the size of the pager operation below.
855 * Even if the map entry's addresses change after
856 * unlocking the map, using the saved addresses is
859 e_start = fs.entry->start;
860 e_end = fs.entry->end;
864 * Call the pager to retrieve the page if there is a chance
865 * that the pager has it, and potentially retrieve additional
866 * pages at the same time.
868 if (fs.object->type != OBJT_DEFAULT) {
870 * Release the map lock before locking the vnode or
871 * sleeping in the pager. (If the current object has
872 * a shadow, then an earlier iteration of this loop
873 * may have already unlocked the map.)
877 if (fs.object->type == OBJT_VNODE &&
878 (vp = fs.object->handle) != fs.vp) {
880 * Perform an unlock in case the desired vnode
881 * changed while the map was unlocked during a
886 locked = VOP_ISLOCKED(vp);
887 if (locked != LK_EXCLUSIVE)
891 * We must not sleep acquiring the vnode lock
892 * while we have the page exclusive busied or
893 * the object's paging-in-progress count
894 * incremented. Otherwise, we could deadlock.
896 error = vget(vp, locked | LK_CANRECURSE |
897 LK_NOWAIT, curthread);
901 unlock_and_deallocate(&fs);
902 error = vget(vp, locked | LK_RETRY |
903 LK_CANRECURSE, curthread);
907 ("vm_fault: vget failed"));
912 KASSERT(fs.vp == NULL || !fs.map->system_map,
913 ("vm_fault: vnode-backed object mapped by system map"));
916 * Page in the requested page and hint the pager,
917 * that it may bring up surrounding pages.
919 if (nera == -1 || behavior == MAP_ENTRY_BEHAV_RANDOM ||
924 /* Is this a sequential fault? */
930 * Request a cluster of pages that is
931 * aligned to a VM_FAULT_READ_DEFAULT
932 * page offset boundary within the
933 * object. Alignment to a page offset
934 * boundary is more likely to coincide
935 * with the underlying file system
936 * block than alignment to a virtual
939 cluster_offset = fs.pindex %
940 VM_FAULT_READ_DEFAULT;
941 behind = ulmin(cluster_offset,
942 atop(vaddr - e_start));
943 ahead = VM_FAULT_READ_DEFAULT - 1 -
946 ahead = ulmin(ahead, atop(e_end - vaddr) - 1);
948 rv = vm_pager_get_pages(fs.object, &fs.m, 1,
950 if (rv == VM_PAGER_OK) {
951 faultcount = behind + 1 + ahead;
953 break; /* break to PAGE HAS BEEN FOUND */
955 if (rv == VM_PAGER_ERROR)
956 printf("vm_fault: pager read error, pid %d (%s)\n",
957 curproc->p_pid, curproc->p_comm);
960 * If an I/O error occurred or the requested page was
961 * outside the range of the pager, clean up and return
964 if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) {
966 if (fs.m->wire_count == 0)
969 vm_page_xunbusy_maybelocked(fs.m);
970 vm_page_unlock(fs.m);
972 unlock_and_deallocate(&fs);
973 return (rv == VM_PAGER_ERROR ? KERN_FAILURE :
974 KERN_PROTECTION_FAILURE);
978 * The requested page does not exist at this object/
979 * offset. Remove the invalid page from the object,
980 * waking up anyone waiting for it, and continue on to
981 * the next object. However, if this is the top-level
982 * object, we must leave the busy page in place to
983 * prevent another process from rushing past us, and
984 * inserting the page in that object at the same time
987 if (fs.object != fs.first_object) {
989 if (fs.m->wire_count == 0)
992 vm_page_xunbusy_maybelocked(fs.m);
993 vm_page_unlock(fs.m);
999 * We get here if the object has default pager (or unwiring)
1000 * or the pager doesn't have the page.
1002 if (fs.object == fs.first_object)
1006 * Move on to the next object. Lock the next object before
1007 * unlocking the current one.
1009 next_object = fs.object->backing_object;
1010 if (next_object == NULL) {
1012 * If there's no object left, fill the page in the top
1013 * object with zeros.
1015 if (fs.object != fs.first_object) {
1016 vm_object_pip_wakeup(fs.object);
1017 VM_OBJECT_WUNLOCK(fs.object);
1019 fs.object = fs.first_object;
1020 fs.pindex = fs.first_pindex;
1022 VM_OBJECT_WLOCK(fs.object);
1027 * Zero the page if necessary and mark it valid.
1029 if ((fs.m->flags & PG_ZERO) == 0) {
1030 pmap_zero_page(fs.m);
1032 VM_CNT_INC(v_ozfod);
1035 fs.m->valid = VM_PAGE_BITS_ALL;
1036 /* Don't try to prefault neighboring pages. */
1038 break; /* break to PAGE HAS BEEN FOUND */
1040 KASSERT(fs.object != next_object,
1041 ("object loop %p", next_object));
1042 VM_OBJECT_WLOCK(next_object);
1043 vm_object_pip_add(next_object, 1);
1044 if (fs.object != fs.first_object)
1045 vm_object_pip_wakeup(fs.object);
1047 OFF_TO_IDX(fs.object->backing_object_offset);
1048 VM_OBJECT_WUNLOCK(fs.object);
1049 fs.object = next_object;
1053 vm_page_assert_xbusied(fs.m);
1056 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1061 * If the page is being written, but isn't already owned by the
1062 * top-level object, we have to copy it into a new page owned by the
1065 if (fs.object != fs.first_object) {
1067 * We only really need to copy if we want to write it.
1069 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1071 * This allows pages to be virtually copied from a
1072 * backing_object into the first_object, where the
1073 * backing object has no other refs to it, and cannot
1074 * gain any more refs. Instead of a bcopy, we just
1075 * move the page from the backing object to the
1076 * first object. Note that we must mark the page
1077 * dirty in the first object so that it will go out
1078 * to swap when needed.
1080 is_first_object_locked = false;
1083 * Only one shadow object
1085 (fs.object->shadow_count == 1) &&
1087 * No COW refs, except us
1089 (fs.object->ref_count == 1) &&
1091 * No one else can look this object up
1093 (fs.object->handle == NULL) &&
1095 * No other ways to look the object up
1097 ((fs.object->type == OBJT_DEFAULT) ||
1098 (fs.object->type == OBJT_SWAP)) &&
1099 (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
1101 * We don't chase down the shadow chain
1103 fs.object == fs.first_object->backing_object) {
1105 vm_page_remove(fs.m);
1106 vm_page_unlock(fs.m);
1107 vm_page_lock(fs.first_m);
1108 vm_page_replace_checked(fs.m, fs.first_object,
1109 fs.first_pindex, fs.first_m);
1110 vm_page_free(fs.first_m);
1111 vm_page_unlock(fs.first_m);
1112 vm_page_dirty(fs.m);
1113 #if VM_NRESERVLEVEL > 0
1115 * Rename the reservation.
1117 vm_reserv_rename(fs.m, fs.first_object,
1118 fs.object, OFF_TO_IDX(
1119 fs.first_object->backing_object_offset));
1122 * Removing the page from the backing object
1125 vm_page_xbusy(fs.m);
1128 VM_CNT_INC(v_cow_optim);
1131 * Oh, well, lets copy it.
1133 pmap_copy_page(fs.m, fs.first_m);
1134 fs.first_m->valid = VM_PAGE_BITS_ALL;
1135 if (wired && (fault_flags &
1136 VM_FAULT_WIRE) == 0) {
1137 vm_page_lock(fs.first_m);
1138 vm_page_wire(fs.first_m);
1139 vm_page_unlock(fs.first_m);
1142 vm_page_unwire(fs.m, PQ_INACTIVE);
1143 vm_page_unlock(fs.m);
1146 * We no longer need the old page or object.
1151 * fs.object != fs.first_object due to above
1154 vm_object_pip_wakeup(fs.object);
1155 VM_OBJECT_WUNLOCK(fs.object);
1157 * Only use the new page below...
1159 fs.object = fs.first_object;
1160 fs.pindex = fs.first_pindex;
1162 if (!is_first_object_locked)
1163 VM_OBJECT_WLOCK(fs.object);
1164 VM_CNT_INC(v_cow_faults);
1165 curthread->td_cow++;
1167 prot &= ~VM_PROT_WRITE;
1172 * We must verify that the maps have not changed since our last
1175 if (!fs.lookup_still_valid) {
1176 if (!vm_map_trylock_read(fs.map)) {
1178 unlock_and_deallocate(&fs);
1181 fs.lookup_still_valid = true;
1182 if (fs.map->timestamp != fs.map_generation) {
1183 result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
1184 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
1187 * If we don't need the page any longer, put it on the inactive
1188 * list (the easiest thing to do here). If no one needs it,
1189 * pageout will grab it eventually.
1191 if (result != KERN_SUCCESS) {
1193 unlock_and_deallocate(&fs);
1196 * If retry of map lookup would have blocked then
1197 * retry fault from start.
1199 if (result == KERN_FAILURE)
1203 if ((retry_object != fs.first_object) ||
1204 (retry_pindex != fs.first_pindex)) {
1206 unlock_and_deallocate(&fs);
1211 * Check whether the protection has changed or the object has
1212 * been copied while we left the map unlocked. Changing from
1213 * read to write permission is OK - we leave the page
1214 * write-protected, and catch the write fault. Changing from
1215 * write to read permission means that we can't mark the page
1216 * write-enabled after all.
1223 * If the page was filled by a pager, save the virtual address that
1224 * should be faulted on next under a sequential access pattern to the
1225 * map entry. A read lock on the map suffices to update this address
1229 fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE;
1231 vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags, true);
1232 vm_page_assert_xbusied(fs.m);
1235 * Page must be completely valid or it is not fit to
1236 * map into user space. vm_pager_get_pages() ensures this.
1238 KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
1239 ("vm_fault: page %p partially invalid", fs.m));
1240 VM_OBJECT_WUNLOCK(fs.object);
1243 * Put this page into the physical map. We had to do the unlock above
1244 * because pmap_enter() may sleep. We don't put the page
1245 * back on the active queue until later so that the pageout daemon
1246 * won't find it (yet).
1248 pmap_enter(fs.map->pmap, vaddr, fs.m, prot,
1249 fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0);
1250 if (faultcount != 1 && (fault_flags & VM_FAULT_WIRE) == 0 &&
1252 vm_fault_prefault(&fs, vaddr,
1253 faultcount > 0 ? behind : PFBAK,
1254 faultcount > 0 ? ahead : PFFOR);
1255 VM_OBJECT_WLOCK(fs.object);
1259 * If the page is not wired down, then put it where the pageout daemon
1262 if ((fault_flags & VM_FAULT_WIRE) != 0) {
1263 KASSERT(wired, ("VM_FAULT_WIRE && !wired"));
1266 vm_page_activate(fs.m);
1267 if (m_hold != NULL) {
1271 vm_page_unlock(fs.m);
1272 vm_page_xunbusy(fs.m);
1275 * Unlock everything, and return
1277 unlock_and_deallocate(&fs);
1279 VM_CNT_INC(v_io_faults);
1280 curthread->td_ru.ru_majflt++;
1282 if (racct_enable && fs.object->type == OBJT_VNODE) {
1284 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1285 racct_add_force(curproc, RACCT_WRITEBPS,
1286 PAGE_SIZE + behind * PAGE_SIZE);
1287 racct_add_force(curproc, RACCT_WRITEIOPS, 1);
1289 racct_add_force(curproc, RACCT_READBPS,
1290 PAGE_SIZE + ahead * PAGE_SIZE);
1291 racct_add_force(curproc, RACCT_READIOPS, 1);
1293 PROC_UNLOCK(curproc);
1297 curthread->td_ru.ru_minflt++;
1299 return (KERN_SUCCESS);
1303 * Speed up the reclamation of pages that precede the faulting pindex within
1304 * the first object of the shadow chain. Essentially, perform the equivalent
1305 * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes
1306 * the faulting pindex by the cluster size when the pages read by vm_fault()
1307 * cross a cluster-size boundary. The cluster size is the greater of the
1308 * smallest superpage size and VM_FAULT_DONTNEED_MIN.
1310 * When "fs->first_object" is a shadow object, the pages in the backing object
1311 * that precede the faulting pindex are deactivated by vm_fault(). So, this
1312 * function must only be concerned with pages in the first object.
1315 vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead)
1317 vm_map_entry_t entry;
1318 vm_object_t first_object, object;
1319 vm_offset_t end, start;
1320 vm_page_t m, m_next;
1321 vm_pindex_t pend, pstart;
1324 object = fs->object;
1325 VM_OBJECT_ASSERT_WLOCKED(object);
1326 first_object = fs->first_object;
1327 if (first_object != object) {
1328 if (!VM_OBJECT_TRYWLOCK(first_object)) {
1329 VM_OBJECT_WUNLOCK(object);
1330 VM_OBJECT_WLOCK(first_object);
1331 VM_OBJECT_WLOCK(object);
1334 /* Neither fictitious nor unmanaged pages can be reclaimed. */
1335 if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
1336 size = VM_FAULT_DONTNEED_MIN;
1337 if (MAXPAGESIZES > 1 && size < pagesizes[1])
1338 size = pagesizes[1];
1339 end = rounddown2(vaddr, size);
1340 if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) &&
1341 (entry = fs->entry)->start < end) {
1342 if (end - entry->start < size)
1343 start = entry->start;
1346 pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED);
1347 pstart = OFF_TO_IDX(entry->offset) + atop(start -
1349 m_next = vm_page_find_least(first_object, pstart);
1350 pend = OFF_TO_IDX(entry->offset) + atop(end -
1352 while ((m = m_next) != NULL && m->pindex < pend) {
1353 m_next = TAILQ_NEXT(m, listq);
1354 if (m->valid != VM_PAGE_BITS_ALL ||
1359 * Don't clear PGA_REFERENCED, since it would
1360 * likely represent a reference by a different
1363 * Typically, at this point, prefetched pages
1364 * are still in the inactive queue. Only
1365 * pages that triggered page faults are in the
1369 vm_page_deactivate(m);
1374 if (first_object != object)
1375 VM_OBJECT_WUNLOCK(first_object);
1379 * vm_fault_prefault provides a quick way of clustering
1380 * pagefaults into a processes address space. It is a "cousin"
1381 * of vm_map_pmap_enter, except it runs at page fault time instead
1385 vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
1386 int backward, int forward)
1389 vm_map_entry_t entry;
1390 vm_object_t backing_object, lobject;
1391 vm_offset_t addr, starta;
1396 pmap = fs->map->pmap;
1397 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1402 if (addra < backward * PAGE_SIZE) {
1403 starta = entry->start;
1405 starta = addra - backward * PAGE_SIZE;
1406 if (starta < entry->start)
1407 starta = entry->start;
1411 * Generate the sequence of virtual addresses that are candidates for
1412 * prefaulting in an outward spiral from the faulting virtual address,
1413 * "addra". Specifically, the sequence is "addra - PAGE_SIZE", "addra
1414 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
1415 * If the candidate address doesn't have a backing physical page, then
1416 * the loop immediately terminates.
1418 for (i = 0; i < 2 * imax(backward, forward); i++) {
1419 addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
1421 if (addr > addra + forward * PAGE_SIZE)
1424 if (addr < starta || addr >= entry->end)
1427 if (!pmap_is_prefaultable(pmap, addr))
1430 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1431 lobject = entry->object.vm_object;
1432 VM_OBJECT_RLOCK(lobject);
1433 while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1434 lobject->type == OBJT_DEFAULT &&
1435 (backing_object = lobject->backing_object) != NULL) {
1436 KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
1437 0, ("vm_fault_prefault: unaligned object offset"));
1438 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1439 VM_OBJECT_RLOCK(backing_object);
1440 VM_OBJECT_RUNLOCK(lobject);
1441 lobject = backing_object;
1444 VM_OBJECT_RUNLOCK(lobject);
1447 if (m->valid == VM_PAGE_BITS_ALL &&
1448 (m->flags & PG_FICTITIOUS) == 0)
1449 pmap_enter_quick(pmap, addr, m, entry->protection);
1450 VM_OBJECT_RUNLOCK(lobject);
1455 * Hold each of the physical pages that are mapped by the specified range of
1456 * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
1457 * and allow the specified types of access, "prot". If all of the implied
1458 * pages are successfully held, then the number of held pages is returned
1459 * together with pointers to those pages in the array "ma". However, if any
1460 * of the pages cannot be held, -1 is returned.
1463 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
1464 vm_prot_t prot, vm_page_t *ma, int max_count)
1466 vm_offset_t end, va;
1469 boolean_t pmap_failed;
1473 end = round_page(addr + len);
1474 addr = trunc_page(addr);
1477 * Check for illegal addresses.
1479 if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
1482 if (atop(end - addr) > max_count)
1483 panic("vm_fault_quick_hold_pages: count > max_count");
1484 count = atop(end - addr);
1487 * Most likely, the physical pages are resident in the pmap, so it is
1488 * faster to try pmap_extract_and_hold() first.
1490 pmap_failed = FALSE;
1491 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
1492 *mp = pmap_extract_and_hold(map->pmap, va, prot);
1495 else if ((prot & VM_PROT_WRITE) != 0 &&
1496 (*mp)->dirty != VM_PAGE_BITS_ALL) {
1498 * Explicitly dirty the physical page. Otherwise, the
1499 * caller's changes may go unnoticed because they are
1500 * performed through an unmanaged mapping or by a DMA
1503 * The object lock is not held here.
1504 * See vm_page_clear_dirty_mask().
1511 * One or more pages could not be held by the pmap. Either no
1512 * page was mapped at the specified virtual address or that
1513 * mapping had insufficient permissions. Attempt to fault in
1514 * and hold these pages.
1516 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1517 if (*mp == NULL && vm_fault_hold(map, va, prot,
1518 VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
1523 for (mp = ma; mp < ma + count; mp++)
1526 vm_page_unhold(*mp);
1527 vm_page_unlock(*mp);
1534 * vm_fault_copy_entry
1536 * Create new shadow object backing dst_entry with private copy of
1537 * all underlying pages. When src_entry is equal to dst_entry,
1538 * function implements COW for wired-down map entry. Otherwise,
1539 * it forks wired entry into dst_map.
1541 * In/out conditions:
1542 * The source and destination maps must be locked for write.
1543 * The source map entry must be wired down (or be a sharing map
1544 * entry corresponding to a main map entry that is wired down).
1547 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1548 vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1549 vm_ooffset_t *fork_charge)
1551 vm_object_t backing_object, dst_object, object, src_object;
1552 vm_pindex_t dst_pindex, pindex, src_pindex;
1553 vm_prot_t access, prot;
1563 upgrade = src_entry == dst_entry;
1564 access = prot = dst_entry->protection;
1566 src_object = src_entry->object.vm_object;
1567 src_pindex = OFF_TO_IDX(src_entry->offset);
1569 if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
1570 dst_object = src_object;
1571 vm_object_reference(dst_object);
1574 * Create the top-level object for the destination entry. (Doesn't
1575 * actually shadow anything - we copy the pages directly.)
1577 dst_object = vm_object_allocate(OBJT_DEFAULT,
1578 atop(dst_entry->end - dst_entry->start));
1579 #if VM_NRESERVLEVEL > 0
1580 dst_object->flags |= OBJ_COLORED;
1581 dst_object->pg_color = atop(dst_entry->start);
1585 VM_OBJECT_WLOCK(dst_object);
1586 KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1587 ("vm_fault_copy_entry: vm_object not NULL"));
1588 if (src_object != dst_object) {
1589 dst_entry->object.vm_object = dst_object;
1590 dst_entry->offset = 0;
1591 dst_object->charge = dst_entry->end - dst_entry->start;
1593 if (fork_charge != NULL) {
1594 KASSERT(dst_entry->cred == NULL,
1595 ("vm_fault_copy_entry: leaked swp charge"));
1596 dst_object->cred = curthread->td_ucred;
1597 crhold(dst_object->cred);
1598 *fork_charge += dst_object->charge;
1599 } else if (dst_object->cred == NULL) {
1600 KASSERT(dst_entry->cred != NULL, ("no cred for entry %p",
1602 dst_object->cred = dst_entry->cred;
1603 dst_entry->cred = NULL;
1607 * If not an upgrade, then enter the mappings in the pmap as
1608 * read and/or execute accesses. Otherwise, enter them as
1611 * A writeable large page mapping is only created if all of
1612 * the constituent small page mappings are modified. Marking
1613 * PTEs as modified on inception allows promotion to happen
1614 * without taking potentially large number of soft faults.
1617 access &= ~VM_PROT_WRITE;
1620 * Loop through all of the virtual pages within the entry's
1621 * range, copying each page from the source object to the
1622 * destination object. Since the source is wired, those pages
1623 * must exist. In contrast, the destination is pageable.
1624 * Since the destination object does share any backing storage
1625 * with the source object, all of its pages must be dirtied,
1626 * regardless of whether they can be written.
1628 for (vaddr = dst_entry->start, dst_pindex = 0;
1629 vaddr < dst_entry->end;
1630 vaddr += PAGE_SIZE, dst_pindex++) {
1633 * Find the page in the source object, and copy it in.
1634 * Because the source is wired down, the page will be
1637 if (src_object != dst_object)
1638 VM_OBJECT_RLOCK(src_object);
1639 object = src_object;
1640 pindex = src_pindex + dst_pindex;
1641 while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1642 (backing_object = object->backing_object) != NULL) {
1644 * Unless the source mapping is read-only or
1645 * it is presently being upgraded from
1646 * read-only, the first object in the shadow
1647 * chain should provide all of the pages. In
1648 * other words, this loop body should never be
1649 * executed when the source mapping is already
1652 KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 ||
1654 ("vm_fault_copy_entry: main object missing page"));
1656 VM_OBJECT_RLOCK(backing_object);
1657 pindex += OFF_TO_IDX(object->backing_object_offset);
1658 if (object != dst_object)
1659 VM_OBJECT_RUNLOCK(object);
1660 object = backing_object;
1662 KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing"));
1664 if (object != dst_object) {
1666 * Allocate a page in the destination object.
1668 dst_m = vm_page_alloc(dst_object, (src_object ==
1669 dst_object ? src_pindex : 0) + dst_pindex,
1671 if (dst_m == NULL) {
1672 VM_OBJECT_WUNLOCK(dst_object);
1673 VM_OBJECT_RUNLOCK(object);
1675 VM_OBJECT_WLOCK(dst_object);
1678 pmap_copy_page(src_m, dst_m);
1679 VM_OBJECT_RUNLOCK(object);
1680 dst_m->valid = VM_PAGE_BITS_ALL;
1681 dst_m->dirty = VM_PAGE_BITS_ALL;
1684 if (vm_page_sleep_if_busy(dst_m, "fltupg"))
1686 vm_page_xbusy(dst_m);
1687 KASSERT(dst_m->valid == VM_PAGE_BITS_ALL,
1688 ("invalid dst page %p", dst_m));
1690 VM_OBJECT_WUNLOCK(dst_object);
1693 * Enter it in the pmap. If a wired, copy-on-write
1694 * mapping is being replaced by a write-enabled
1695 * mapping, then wire that new mapping.
1697 pmap_enter(dst_map->pmap, vaddr, dst_m, prot,
1698 access | (upgrade ? PMAP_ENTER_WIRED : 0), 0);
1701 * Mark it no longer busy, and put it on the active list.
1703 VM_OBJECT_WLOCK(dst_object);
1706 if (src_m != dst_m) {
1707 vm_page_lock(src_m);
1708 vm_page_unwire(src_m, PQ_INACTIVE);
1709 vm_page_unlock(src_m);
1710 vm_page_lock(dst_m);
1711 vm_page_wire(dst_m);
1712 vm_page_unlock(dst_m);
1714 KASSERT(dst_m->wire_count > 0,
1715 ("dst_m %p is not wired", dst_m));
1718 vm_page_lock(dst_m);
1719 vm_page_activate(dst_m);
1720 vm_page_unlock(dst_m);
1722 vm_page_xunbusy(dst_m);
1724 VM_OBJECT_WUNLOCK(dst_object);
1726 dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1727 vm_object_deallocate(src_object);
1732 * Block entry into the machine-independent layer's page fault handler by
1733 * the calling thread. Subsequent calls to vm_fault() by that thread will
1734 * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of
1735 * spurious page faults.
1738 vm_fault_disable_pagefaults(void)
1741 return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
1745 vm_fault_enable_pagefaults(int save)
1748 curthread_pflags_restore(save);