2 * SPDX-License-Identifier: (BSD-4-Clause AND MIT-CMU)
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1994 John S. Dyson
8 * Copyright (c) 1994 David Greenman
12 * This code is derived from software contributed to Berkeley by
13 * The Mach Operating System project at Carnegie-Mellon University.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
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.
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
43 * from: @(#)vm_fault.c 8.4 (Berkeley) 1/12/94
46 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
47 * All rights reserved.
49 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
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.
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.
61 * Carnegie Mellon requests users of this software to return to
63 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
64 * School of Computer Science
65 * Carnegie Mellon University
66 * Pittsburgh PA 15213-3890
68 * any improvements or extensions that they make and grant Carnegie the
69 * rights to redistribute these changes.
73 * Page fault handling module.
76 #include <sys/cdefs.h>
77 __FBSDID("$FreeBSD$");
79 #include "opt_ktrace.h"
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/kernel.h>
87 #include <sys/mutex.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>
96 #include <sys/ktrace.h>
100 #include <vm/vm_param.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>
114 #define VM_FAULT_READ_DEFAULT (1 + VM_FAULT_READ_AHEAD_INIT)
115 #define VM_FAULT_READ_MAX (1 + VM_FAULT_READ_AHEAD_MAX)
117 #define VM_FAULT_DONTNEED_MIN 1048576
124 vm_object_t first_object;
125 vm_pindex_t first_pindex;
127 vm_map_entry_t entry;
129 bool lookup_still_valid;
133 static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr,
135 static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
136 int backward, int forward, bool obj_locked);
139 release_page(struct faultstate *fs)
142 vm_page_xunbusy(fs->m);
144 vm_page_deactivate(fs->m);
145 vm_page_unlock(fs->m);
150 unlock_map(struct faultstate *fs)
153 if (fs->lookup_still_valid) {
154 vm_map_lookup_done(fs->map, fs->entry);
155 fs->lookup_still_valid = false;
160 unlock_vp(struct faultstate *fs)
163 if (fs->vp != NULL) {
170 unlock_and_deallocate(struct faultstate *fs)
173 vm_object_pip_wakeup(fs->object);
174 VM_OBJECT_WUNLOCK(fs->object);
175 if (fs->object != fs->first_object) {
176 VM_OBJECT_WLOCK(fs->first_object);
177 vm_page_lock(fs->first_m);
178 vm_page_free(fs->first_m);
179 vm_page_unlock(fs->first_m);
180 vm_object_pip_wakeup(fs->first_object);
181 VM_OBJECT_WUNLOCK(fs->first_object);
184 vm_object_deallocate(fs->first_object);
190 vm_fault_dirty(vm_map_entry_t entry, vm_page_t m, vm_prot_t prot,
191 vm_prot_t fault_type, int fault_flags, bool set_wd)
195 if (((prot & VM_PROT_WRITE) == 0 &&
196 (fault_flags & VM_FAULT_DIRTY) == 0) ||
197 (m->oflags & VPO_UNMANAGED) != 0)
200 VM_OBJECT_ASSERT_LOCKED(m->object);
202 need_dirty = ((fault_type & VM_PROT_WRITE) != 0 &&
203 (fault_flags & VM_FAULT_WIRE) == 0) ||
204 (fault_flags & VM_FAULT_DIRTY) != 0;
207 vm_object_set_writeable_dirty(m->object);
210 * If two callers of vm_fault_dirty() with set_wd ==
211 * FALSE, one for the map entry with MAP_ENTRY_NOSYNC
212 * flag set, other with flag clear, race, it is
213 * possible for the no-NOSYNC thread to see m->dirty
214 * != 0 and not clear VPO_NOSYNC. Take vm_page lock
215 * around manipulation of VPO_NOSYNC and
216 * vm_page_dirty() call, to avoid the race and keep
217 * m->oflags consistent.
222 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
223 * if the page is already dirty to prevent data written with
224 * the expectation of being synced from not being synced.
225 * Likewise if this entry does not request NOSYNC then make
226 * sure the page isn't marked NOSYNC. Applications sharing
227 * data should use the same flags to avoid ping ponging.
229 if ((entry->eflags & MAP_ENTRY_NOSYNC) != 0) {
231 m->oflags |= VPO_NOSYNC;
234 m->oflags &= ~VPO_NOSYNC;
238 * If the fault is a write, we know that this page is being
239 * written NOW so dirty it explicitly to save on
240 * pmap_is_modified() calls later.
242 * Also, since the page is now dirty, we can possibly tell
243 * the pager to release any swap backing the page. Calling
244 * the pager requires a write lock on the object.
251 vm_pager_page_unswapped(m);
255 vm_fault_fill_hold(vm_page_t *m_hold, vm_page_t m)
258 if (m_hold != NULL) {
267 * Unlocks fs.first_object and fs.map on success.
270 vm_fault_soft_fast(struct faultstate *fs, vm_offset_t vaddr, vm_prot_t prot,
271 int fault_type, int fault_flags, boolean_t wired, vm_page_t *m_hold)
274 #if (defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
275 __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)) && \
282 MPASS(fs->vp == NULL);
283 m = vm_page_lookup(fs->first_object, fs->first_pindex);
284 /* A busy page can be mapped for read|execute access. */
285 if (m == NULL || ((prot & VM_PROT_WRITE) != 0 &&
286 vm_page_busied(m)) || m->valid != VM_PAGE_BITS_ALL)
287 return (KERN_FAILURE);
290 #if (defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
291 __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)) && \
293 if ((m->flags & PG_FICTITIOUS) == 0 &&
294 (m_super = vm_reserv_to_superpage(m)) != NULL &&
295 rounddown2(vaddr, pagesizes[m_super->psind]) >= fs->entry->start &&
296 roundup2(vaddr + 1, pagesizes[m_super->psind]) <= fs->entry->end &&
297 (vaddr & (pagesizes[m_super->psind] - 1)) == (VM_PAGE_TO_PHYS(m) &
298 (pagesizes[m_super->psind] - 1)) && !wired &&
299 pmap_ps_enabled(fs->map->pmap)) {
300 flags = PS_ALL_VALID;
301 if ((prot & VM_PROT_WRITE) != 0) {
303 * Create a superpage mapping allowing write access
304 * only if none of the constituent pages are busy and
305 * all of them are already dirty (except possibly for
306 * the page that was faulted on).
308 flags |= PS_NONE_BUSY;
309 if ((fs->first_object->flags & OBJ_UNMANAGED) == 0)
310 flags |= PS_ALL_DIRTY;
312 if (vm_page_ps_test(m_super, flags, m)) {
314 psind = m_super->psind;
315 vaddr = rounddown2(vaddr, pagesizes[psind]);
316 /* Preset the modified bit for dirty superpages. */
317 if ((flags & PS_ALL_DIRTY) != 0)
318 fault_type |= VM_PROT_WRITE;
322 rv = pmap_enter(fs->map->pmap, vaddr, m_map, prot, fault_type |
323 PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED : 0), psind);
324 if (rv != KERN_SUCCESS)
326 vm_fault_fill_hold(m_hold, m);
327 vm_fault_dirty(fs->entry, m, prot, fault_type, fault_flags, false);
328 if (psind == 0 && !wired)
329 vm_fault_prefault(fs, vaddr, PFBAK, PFFOR, true);
330 VM_OBJECT_RUNLOCK(fs->first_object);
331 vm_map_lookup_done(fs->map, fs->entry);
332 curthread->td_ru.ru_minflt++;
333 return (KERN_SUCCESS);
337 vm_fault_restore_map_lock(struct faultstate *fs)
340 VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
341 MPASS(fs->first_object->paging_in_progress > 0);
343 if (!vm_map_trylock_read(fs->map)) {
344 VM_OBJECT_WUNLOCK(fs->first_object);
345 vm_map_lock_read(fs->map);
346 VM_OBJECT_WLOCK(fs->first_object);
348 fs->lookup_still_valid = true;
352 vm_fault_populate_check_page(vm_page_t m)
356 * Check each page to ensure that the pager is obeying the
357 * interface: the page must be installed in the object, fully
358 * valid, and exclusively busied.
361 MPASS(m->valid == VM_PAGE_BITS_ALL);
362 MPASS(vm_page_xbusied(m));
366 vm_fault_populate_cleanup(vm_object_t object, vm_pindex_t first,
372 VM_OBJECT_ASSERT_WLOCKED(object);
373 MPASS(first <= last);
374 for (pidx = first, m = vm_page_lookup(object, pidx);
375 pidx <= last; pidx++, m = vm_page_next(m)) {
376 vm_fault_populate_check_page(m);
378 vm_page_deactivate(m);
385 vm_fault_populate(struct faultstate *fs, vm_prot_t prot, int fault_type,
386 int fault_flags, boolean_t wired, vm_page_t *m_hold)
391 vm_pindex_t map_first, map_last, pager_first, pager_last, pidx;
392 int i, npages, psind, rv;
394 MPASS(fs->object == fs->first_object);
395 VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
396 MPASS(fs->first_object->paging_in_progress > 0);
397 MPASS(fs->first_object->backing_object == NULL);
398 MPASS(fs->lookup_still_valid);
400 pager_first = OFF_TO_IDX(fs->entry->offset);
401 pager_last = pager_first + atop(fs->entry->end - fs->entry->start) - 1;
406 * Call the pager (driver) populate() method.
408 * There is no guarantee that the method will be called again
409 * if the current fault is for read, and a future fault is
410 * for write. Report the entry's maximum allowed protection
413 rv = vm_pager_populate(fs->first_object, fs->first_pindex,
414 fault_type, fs->entry->max_protection, &pager_first, &pager_last);
416 VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
417 if (rv == VM_PAGER_BAD) {
419 * VM_PAGER_BAD is the backdoor for a pager to request
420 * normal fault handling.
422 vm_fault_restore_map_lock(fs);
423 if (fs->map->timestamp != fs->map_generation)
424 return (KERN_RESOURCE_SHORTAGE); /* RetryFault */
425 return (KERN_NOT_RECEIVER);
427 if (rv != VM_PAGER_OK)
428 return (KERN_FAILURE); /* AKA SIGSEGV */
430 /* Ensure that the driver is obeying the interface. */
431 MPASS(pager_first <= pager_last);
432 MPASS(fs->first_pindex <= pager_last);
433 MPASS(fs->first_pindex >= pager_first);
434 MPASS(pager_last < fs->first_object->size);
436 vm_fault_restore_map_lock(fs);
437 if (fs->map->timestamp != fs->map_generation) {
438 vm_fault_populate_cleanup(fs->first_object, pager_first,
440 return (KERN_RESOURCE_SHORTAGE); /* RetryFault */
444 * The map is unchanged after our last unlock. Process the fault.
446 * The range [pager_first, pager_last] that is given to the
447 * pager is only a hint. The pager may populate any range
448 * within the object that includes the requested page index.
449 * In case the pager expanded the range, clip it to fit into
452 map_first = OFF_TO_IDX(fs->entry->offset);
453 if (map_first > pager_first) {
454 vm_fault_populate_cleanup(fs->first_object, pager_first,
456 pager_first = map_first;
458 map_last = map_first + atop(fs->entry->end - fs->entry->start) - 1;
459 if (map_last < pager_last) {
460 vm_fault_populate_cleanup(fs->first_object, map_last + 1,
462 pager_last = map_last;
464 for (pidx = pager_first, m = vm_page_lookup(fs->first_object, pidx);
466 pidx += npages, m = vm_page_next(&m[npages - 1])) {
467 vaddr = fs->entry->start + IDX_TO_OFF(pidx) - fs->entry->offset;
468 #if defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
469 __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)
471 if (psind > 0 && ((vaddr & (pagesizes[psind] - 1)) != 0 ||
472 pidx + OFF_TO_IDX(pagesizes[psind]) - 1 > pager_last ||
473 !pmap_ps_enabled(fs->map->pmap) || wired))
478 npages = atop(pagesizes[psind]);
479 for (i = 0; i < npages; i++) {
480 vm_fault_populate_check_page(&m[i]);
481 vm_fault_dirty(fs->entry, &m[i], prot, fault_type,
484 VM_OBJECT_WUNLOCK(fs->first_object);
485 rv = pmap_enter(fs->map->pmap, vaddr, m, prot, fault_type |
486 (wired ? PMAP_ENTER_WIRED : 0), psind);
487 #if defined(__amd64__)
488 if (psind > 0 && rv == KERN_FAILURE) {
489 for (i = 0; i < npages; i++) {
490 rv = pmap_enter(fs->map->pmap, vaddr + ptoa(i),
491 &m[i], prot, fault_type |
492 (wired ? PMAP_ENTER_WIRED : 0), 0);
493 MPASS(rv == KERN_SUCCESS);
497 MPASS(rv == KERN_SUCCESS);
499 VM_OBJECT_WLOCK(fs->first_object);
501 for (i = 0; i < npages; i++) {
502 vm_page_change_lock(&m[i], &m_mtx);
503 if ((fault_flags & VM_FAULT_WIRE) != 0)
506 vm_page_activate(&m[i]);
507 if (m_hold != NULL && m[i].pindex == fs->first_pindex) {
511 vm_page_xunbusy_maybelocked(&m[i]);
516 curthread->td_ru.ru_majflt++;
517 return (KERN_SUCCESS);
523 * Handle a page fault occurring at the given address,
524 * requiring the given permissions, in the map specified.
525 * If successful, the page is inserted into the
526 * associated physical map.
528 * NOTE: the given address should be truncated to the
529 * proper page address.
531 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
532 * a standard error specifying why the fault is fatal is returned.
534 * The map in question must be referenced, and remains so.
535 * Caller may hold no locks.
538 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
545 if ((td->td_pflags & TDP_NOFAULTING) != 0)
546 return (KERN_PROTECTION_FAILURE);
548 if (map != kernel_map && KTRPOINT(td, KTR_FAULT))
549 ktrfault(vaddr, fault_type);
551 result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags,
554 if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND))
561 vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
562 int fault_flags, vm_page_t *m_hold)
564 struct faultstate fs;
566 struct domainset *dset;
568 vm_object_t next_object, retry_object;
569 vm_offset_t e_end, e_start;
570 vm_pindex_t retry_pindex;
571 vm_prot_t prot, retry_prot;
572 int ahead, alloc_req, behind, cluster_offset, error, era, faultcount;
573 int locked, nera, result, rv;
575 boolean_t wired; /* Passed by reference. */
576 bool dead, hardfault, is_first_object_locked;
578 VM_CNT_INC(v_vm_faults);
587 * Find the backing store object and offset into it to begin the
591 result = vm_map_lookup(&fs.map, vaddr, fault_type |
592 VM_PROT_FAULT_LOOKUP, &fs.entry, &fs.first_object,
593 &fs.first_pindex, &prot, &wired);
594 if (result != KERN_SUCCESS) {
599 fs.map_generation = fs.map->timestamp;
601 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
602 panic("%s: fault on nofault entry, addr: %#lx",
603 __func__, (u_long)vaddr);
606 if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
607 fs.entry->wiring_thread != curthread) {
608 vm_map_unlock_read(fs.map);
610 if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
611 (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
613 fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
614 vm_map_unlock_and_wait(fs.map, 0);
616 vm_map_unlock(fs.map);
620 MPASS((fs.entry->eflags & MAP_ENTRY_GUARD) == 0);
623 fault_type = prot | (fault_type & VM_PROT_COPY);
625 KASSERT((fault_flags & VM_FAULT_WIRE) == 0,
626 ("!wired && VM_FAULT_WIRE"));
629 * Try to avoid lock contention on the top-level object through
630 * special-case handling of some types of page faults, specifically,
631 * those that are both (1) mapping an existing page from the top-
632 * level object and (2) not having to mark that object as containing
633 * dirty pages. Under these conditions, a read lock on the top-level
634 * object suffices, allowing multiple page faults of a similar type to
635 * run in parallel on the same top-level object.
637 if (fs.vp == NULL /* avoid locked vnode leak */ &&
638 (fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0 &&
639 /* avoid calling vm_object_set_writeable_dirty() */
640 ((prot & VM_PROT_WRITE) == 0 ||
641 (fs.first_object->type != OBJT_VNODE &&
642 (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
643 (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0)) {
644 VM_OBJECT_RLOCK(fs.first_object);
645 if ((prot & VM_PROT_WRITE) == 0 ||
646 (fs.first_object->type != OBJT_VNODE &&
647 (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
648 (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0) {
649 rv = vm_fault_soft_fast(&fs, vaddr, prot, fault_type,
650 fault_flags, wired, m_hold);
651 if (rv == KERN_SUCCESS)
654 if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) {
655 VM_OBJECT_RUNLOCK(fs.first_object);
656 VM_OBJECT_WLOCK(fs.first_object);
659 VM_OBJECT_WLOCK(fs.first_object);
663 * Make a reference to this object to prevent its disposal while we
664 * are messing with it. Once we have the reference, the map is free
665 * to be diddled. Since objects reference their shadows (and copies),
666 * they will stay around as well.
668 * Bump the paging-in-progress count to prevent size changes (e.g.
669 * truncation operations) during I/O.
671 vm_object_reference_locked(fs.first_object);
672 vm_object_pip_add(fs.first_object, 1);
674 fs.lookup_still_valid = true;
679 * Search for the page at object/offset.
681 fs.object = fs.first_object;
682 fs.pindex = fs.first_pindex;
685 * If the object is marked for imminent termination,
686 * we retry here, since the collapse pass has raced
687 * with us. Otherwise, if we see terminally dead
688 * object, return fail.
690 if ((fs.object->flags & OBJ_DEAD) != 0) {
691 dead = fs.object->type == OBJT_DEAD;
692 unlock_and_deallocate(&fs);
694 return (KERN_PROTECTION_FAILURE);
700 * See if page is resident
702 fs.m = vm_page_lookup(fs.object, fs.pindex);
705 * Wait/Retry if the page is busy. We have to do this
706 * if the page is either exclusive or shared busy
707 * because the vm_pager may be using read busy for
708 * pageouts (and even pageins if it is the vnode
709 * pager), and we could end up trying to pagein and
710 * pageout the same page simultaneously.
712 * We can theoretically allow the busy case on a read
713 * fault if the page is marked valid, but since such
714 * pages are typically already pmap'd, putting that
715 * special case in might be more effort then it is
716 * worth. We cannot under any circumstances mess
717 * around with a shared busied page except, perhaps,
720 if (vm_page_busied(fs.m)) {
722 * Reference the page before unlocking and
723 * sleeping so that the page daemon is less
724 * likely to reclaim it.
726 vm_page_aflag_set(fs.m, PGA_REFERENCED);
727 if (fs.object != fs.first_object) {
728 if (!VM_OBJECT_TRYWLOCK(
730 VM_OBJECT_WUNLOCK(fs.object);
731 VM_OBJECT_WLOCK(fs.first_object);
732 VM_OBJECT_WLOCK(fs.object);
734 vm_page_lock(fs.first_m);
735 vm_page_free(fs.first_m);
736 vm_page_unlock(fs.first_m);
737 vm_object_pip_wakeup(fs.first_object);
738 VM_OBJECT_WUNLOCK(fs.first_object);
742 if (fs.m == vm_page_lookup(fs.object,
744 vm_page_sleep_if_busy(fs.m, "vmpfw");
746 vm_object_pip_wakeup(fs.object);
747 VM_OBJECT_WUNLOCK(fs.object);
748 VM_CNT_INC(v_intrans);
749 vm_object_deallocate(fs.first_object);
754 * Mark page busy for other processes, and the
755 * pagedaemon. If it still isn't completely valid
756 * (readable), jump to readrest, else break-out ( we
760 if (fs.m->valid != VM_PAGE_BITS_ALL)
762 break; /* break to PAGE HAS BEEN FOUND */
764 KASSERT(fs.m == NULL, ("fs.m should be NULL, not %p", fs.m));
767 * Page is not resident. If the pager might contain the page
768 * or this is the beginning of the search, allocate a new
769 * page. (Default objects are zero-fill, so there is no real
772 if (fs.object->type != OBJT_DEFAULT ||
773 fs.object == fs.first_object) {
774 if (fs.pindex >= fs.object->size) {
775 unlock_and_deallocate(&fs);
776 return (KERN_PROTECTION_FAILURE);
779 if (fs.object == fs.first_object &&
780 (fs.first_object->flags & OBJ_POPULATE) != 0 &&
781 fs.first_object->shadow_count == 0) {
782 rv = vm_fault_populate(&fs, prot, fault_type,
783 fault_flags, wired, m_hold);
787 unlock_and_deallocate(&fs);
789 case KERN_RESOURCE_SHORTAGE:
790 unlock_and_deallocate(&fs);
792 case KERN_NOT_RECEIVER:
794 * Pager's populate() method
795 * returned VM_PAGER_BAD.
799 panic("inconsistent return codes");
804 * Allocate a new page for this object/offset pair.
806 * Unlocked read of the p_flag is harmless. At
807 * worst, the P_KILLED might be not observed
808 * there, and allocation can fail, causing
809 * restart and new reading of the p_flag.
811 dset = fs.object->domain.dr_policy;
813 dset = curthread->td_domain.dr_policy;
814 if (!vm_page_count_severe_set(&dset->ds_mask) ||
816 #if VM_NRESERVLEVEL > 0
817 vm_object_color(fs.object, atop(vaddr) -
820 alloc_req = P_KILLED(curproc) ?
821 VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
822 if (fs.object->type != OBJT_VNODE &&
823 fs.object->backing_object == NULL)
824 alloc_req |= VM_ALLOC_ZERO;
825 fs.m = vm_page_alloc(fs.object, fs.pindex,
829 unlock_and_deallocate(&fs);
837 * At this point, we have either allocated a new page or found
838 * an existing page that is only partially valid.
840 * We hold a reference on the current object and the page is
845 * If the pager for the current object might have the page,
846 * then determine the number of additional pages to read and
847 * potentially reprioritize previously read pages for earlier
848 * reclamation. These operations should only be performed
849 * once per page fault. Even if the current pager doesn't
850 * have the page, the number of additional pages to read will
851 * apply to subsequent objects in the shadow chain.
853 if (fs.object->type != OBJT_DEFAULT && nera == -1 &&
854 !P_KILLED(curproc)) {
855 KASSERT(fs.lookup_still_valid, ("map unlocked"));
856 era = fs.entry->read_ahead;
857 behavior = vm_map_entry_behavior(fs.entry);
858 if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
860 } else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
861 nera = VM_FAULT_READ_AHEAD_MAX;
862 if (vaddr == fs.entry->next_read)
863 vm_fault_dontneed(&fs, vaddr, nera);
864 } else if (vaddr == fs.entry->next_read) {
866 * This is a sequential fault. Arithmetically
867 * increase the requested number of pages in
868 * the read-ahead window. The requested
869 * number of pages is "# of sequential faults
870 * x (read ahead min + 1) + read ahead min"
872 nera = VM_FAULT_READ_AHEAD_MIN;
875 if (nera > VM_FAULT_READ_AHEAD_MAX)
876 nera = VM_FAULT_READ_AHEAD_MAX;
878 if (era == VM_FAULT_READ_AHEAD_MAX)
879 vm_fault_dontneed(&fs, vaddr, nera);
882 * This is a non-sequential fault.
888 * A read lock on the map suffices to update
889 * the read ahead count safely.
891 fs.entry->read_ahead = nera;
895 * Prepare for unlocking the map. Save the map
896 * entry's start and end addresses, which are used to
897 * optimize the size of the pager operation below.
898 * Even if the map entry's addresses change after
899 * unlocking the map, using the saved addresses is
902 e_start = fs.entry->start;
903 e_end = fs.entry->end;
907 * Call the pager to retrieve the page if there is a chance
908 * that the pager has it, and potentially retrieve additional
909 * pages at the same time.
911 if (fs.object->type != OBJT_DEFAULT) {
913 * Release the map lock before locking the vnode or
914 * sleeping in the pager. (If the current object has
915 * a shadow, then an earlier iteration of this loop
916 * may have already unlocked the map.)
920 if (fs.object->type == OBJT_VNODE &&
921 (vp = fs.object->handle) != fs.vp) {
923 * Perform an unlock in case the desired vnode
924 * changed while the map was unlocked during a
929 locked = VOP_ISLOCKED(vp);
930 if (locked != LK_EXCLUSIVE)
934 * We must not sleep acquiring the vnode lock
935 * while we have the page exclusive busied or
936 * the object's paging-in-progress count
937 * incremented. Otherwise, we could deadlock.
939 error = vget(vp, locked | LK_CANRECURSE |
940 LK_NOWAIT, curthread);
944 unlock_and_deallocate(&fs);
945 error = vget(vp, locked | LK_RETRY |
946 LK_CANRECURSE, curthread);
950 ("vm_fault: vget failed"));
955 KASSERT(fs.vp == NULL || !fs.map->system_map,
956 ("vm_fault: vnode-backed object mapped by system map"));
959 * Page in the requested page and hint the pager,
960 * that it may bring up surrounding pages.
962 if (nera == -1 || behavior == MAP_ENTRY_BEHAV_RANDOM ||
967 /* Is this a sequential fault? */
973 * Request a cluster of pages that is
974 * aligned to a VM_FAULT_READ_DEFAULT
975 * page offset boundary within the
976 * object. Alignment to a page offset
977 * boundary is more likely to coincide
978 * with the underlying file system
979 * block than alignment to a virtual
982 cluster_offset = fs.pindex %
983 VM_FAULT_READ_DEFAULT;
984 behind = ulmin(cluster_offset,
985 atop(vaddr - e_start));
986 ahead = VM_FAULT_READ_DEFAULT - 1 -
989 ahead = ulmin(ahead, atop(e_end - vaddr) - 1);
991 rv = vm_pager_get_pages(fs.object, &fs.m, 1,
993 if (rv == VM_PAGER_OK) {
994 faultcount = behind + 1 + ahead;
996 break; /* break to PAGE HAS BEEN FOUND */
998 if (rv == VM_PAGER_ERROR)
999 printf("vm_fault: pager read error, pid %d (%s)\n",
1000 curproc->p_pid, curproc->p_comm);
1003 * If an I/O error occurred or the requested page was
1004 * outside the range of the pager, clean up and return
1007 if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) {
1009 if (!vm_page_wired(fs.m))
1012 vm_page_xunbusy_maybelocked(fs.m);
1013 vm_page_unlock(fs.m);
1015 unlock_and_deallocate(&fs);
1016 return (rv == VM_PAGER_ERROR ? KERN_FAILURE :
1017 KERN_PROTECTION_FAILURE);
1021 * The requested page does not exist at this object/
1022 * offset. Remove the invalid page from the object,
1023 * waking up anyone waiting for it, and continue on to
1024 * the next object. However, if this is the top-level
1025 * object, we must leave the busy page in place to
1026 * prevent another process from rushing past us, and
1027 * inserting the page in that object at the same time
1030 if (fs.object != fs.first_object) {
1032 if (!vm_page_wired(fs.m))
1035 vm_page_xunbusy_maybelocked(fs.m);
1036 vm_page_unlock(fs.m);
1042 * We get here if the object has default pager (or unwiring)
1043 * or the pager doesn't have the page.
1045 if (fs.object == fs.first_object)
1049 * Move on to the next object. Lock the next object before
1050 * unlocking the current one.
1052 next_object = fs.object->backing_object;
1053 if (next_object == NULL) {
1055 * If there's no object left, fill the page in the top
1056 * object with zeros.
1058 if (fs.object != fs.first_object) {
1059 vm_object_pip_wakeup(fs.object);
1060 VM_OBJECT_WUNLOCK(fs.object);
1062 fs.object = fs.first_object;
1063 fs.pindex = fs.first_pindex;
1065 VM_OBJECT_WLOCK(fs.object);
1070 * Zero the page if necessary and mark it valid.
1072 if ((fs.m->flags & PG_ZERO) == 0) {
1073 pmap_zero_page(fs.m);
1075 VM_CNT_INC(v_ozfod);
1078 fs.m->valid = VM_PAGE_BITS_ALL;
1079 /* Don't try to prefault neighboring pages. */
1081 break; /* break to PAGE HAS BEEN FOUND */
1083 KASSERT(fs.object != next_object,
1084 ("object loop %p", next_object));
1085 VM_OBJECT_WLOCK(next_object);
1086 vm_object_pip_add(next_object, 1);
1087 if (fs.object != fs.first_object)
1088 vm_object_pip_wakeup(fs.object);
1090 OFF_TO_IDX(fs.object->backing_object_offset);
1091 VM_OBJECT_WUNLOCK(fs.object);
1092 fs.object = next_object;
1096 vm_page_assert_xbusied(fs.m);
1099 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1104 * If the page is being written, but isn't already owned by the
1105 * top-level object, we have to copy it into a new page owned by the
1108 if (fs.object != fs.first_object) {
1110 * We only really need to copy if we want to write it.
1112 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1114 * This allows pages to be virtually copied from a
1115 * backing_object into the first_object, where the
1116 * backing object has no other refs to it, and cannot
1117 * gain any more refs. Instead of a bcopy, we just
1118 * move the page from the backing object to the
1119 * first object. Note that we must mark the page
1120 * dirty in the first object so that it will go out
1121 * to swap when needed.
1123 is_first_object_locked = false;
1126 * Only one shadow object
1128 (fs.object->shadow_count == 1) &&
1130 * No COW refs, except us
1132 (fs.object->ref_count == 1) &&
1134 * No one else can look this object up
1136 (fs.object->handle == NULL) &&
1138 * No other ways to look the object up
1140 ((fs.object->type == OBJT_DEFAULT) ||
1141 (fs.object->type == OBJT_SWAP)) &&
1142 (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
1144 * We don't chase down the shadow chain
1146 fs.object == fs.first_object->backing_object) {
1148 * Keep the page wired to ensure that it is not
1149 * freed by another thread, such as the page
1150 * daemon, while it is disassociated from an
1154 vm_page_change_lock(fs.m, &mtx);
1156 (void)vm_page_remove(fs.m);
1157 vm_page_change_lock(fs.first_m, &mtx);
1158 vm_page_replace_checked(fs.m, fs.first_object,
1159 fs.first_pindex, fs.first_m);
1160 vm_page_free(fs.first_m);
1161 vm_page_change_lock(fs.m, &mtx);
1162 vm_page_unwire(fs.m, PQ_ACTIVE);
1164 vm_page_dirty(fs.m);
1165 #if VM_NRESERVLEVEL > 0
1167 * Rename the reservation.
1169 vm_reserv_rename(fs.m, fs.first_object,
1170 fs.object, OFF_TO_IDX(
1171 fs.first_object->backing_object_offset));
1174 * Removing the page from the backing object
1177 vm_page_xbusy(fs.m);
1180 VM_CNT_INC(v_cow_optim);
1183 * Oh, well, lets copy it.
1185 pmap_copy_page(fs.m, fs.first_m);
1186 fs.first_m->valid = VM_PAGE_BITS_ALL;
1187 if (wired && (fault_flags &
1188 VM_FAULT_WIRE) == 0) {
1189 vm_page_lock(fs.first_m);
1190 vm_page_wire(fs.first_m);
1191 vm_page_unlock(fs.first_m);
1194 vm_page_unwire(fs.m, PQ_INACTIVE);
1195 vm_page_unlock(fs.m);
1198 * We no longer need the old page or object.
1203 * fs.object != fs.first_object due to above
1206 vm_object_pip_wakeup(fs.object);
1207 VM_OBJECT_WUNLOCK(fs.object);
1210 * We only try to prefault read-only mappings to the
1211 * neighboring pages when this copy-on-write fault is
1212 * a hard fault. In other cases, trying to prefault
1213 * is typically wasted effort.
1215 if (faultcount == 0)
1219 * Only use the new page below...
1221 fs.object = fs.first_object;
1222 fs.pindex = fs.first_pindex;
1224 if (!is_first_object_locked)
1225 VM_OBJECT_WLOCK(fs.object);
1226 VM_CNT_INC(v_cow_faults);
1227 curthread->td_cow++;
1229 prot &= ~VM_PROT_WRITE;
1234 * We must verify that the maps have not changed since our last
1237 if (!fs.lookup_still_valid) {
1238 if (!vm_map_trylock_read(fs.map)) {
1240 unlock_and_deallocate(&fs);
1243 fs.lookup_still_valid = true;
1244 if (fs.map->timestamp != fs.map_generation) {
1245 result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
1246 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
1249 * If we don't need the page any longer, put it on the inactive
1250 * list (the easiest thing to do here). If no one needs it,
1251 * pageout will grab it eventually.
1253 if (result != KERN_SUCCESS) {
1255 unlock_and_deallocate(&fs);
1258 * If retry of map lookup would have blocked then
1259 * retry fault from start.
1261 if (result == KERN_FAILURE)
1265 if ((retry_object != fs.first_object) ||
1266 (retry_pindex != fs.first_pindex)) {
1268 unlock_and_deallocate(&fs);
1273 * Check whether the protection has changed or the object has
1274 * been copied while we left the map unlocked. Changing from
1275 * read to write permission is OK - we leave the page
1276 * write-protected, and catch the write fault. Changing from
1277 * write to read permission means that we can't mark the page
1278 * write-enabled after all.
1281 fault_type &= retry_prot;
1284 unlock_and_deallocate(&fs);
1288 /* Reassert because wired may have changed. */
1289 KASSERT(wired || (fault_flags & VM_FAULT_WIRE) == 0,
1290 ("!wired && VM_FAULT_WIRE"));
1295 * If the page was filled by a pager, save the virtual address that
1296 * should be faulted on next under a sequential access pattern to the
1297 * map entry. A read lock on the map suffices to update this address
1301 fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE;
1303 vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags, true);
1304 vm_page_assert_xbusied(fs.m);
1307 * Page must be completely valid or it is not fit to
1308 * map into user space. vm_pager_get_pages() ensures this.
1310 KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
1311 ("vm_fault: page %p partially invalid", fs.m));
1312 VM_OBJECT_WUNLOCK(fs.object);
1315 * Put this page into the physical map. We had to do the unlock above
1316 * because pmap_enter() may sleep. We don't put the page
1317 * back on the active queue until later so that the pageout daemon
1318 * won't find it (yet).
1320 pmap_enter(fs.map->pmap, vaddr, fs.m, prot,
1321 fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0);
1322 if (faultcount != 1 && (fault_flags & VM_FAULT_WIRE) == 0 &&
1324 vm_fault_prefault(&fs, vaddr,
1325 faultcount > 0 ? behind : PFBAK,
1326 faultcount > 0 ? ahead : PFFOR, false);
1327 VM_OBJECT_WLOCK(fs.object);
1331 * If the page is not wired down, then put it where the pageout daemon
1334 if ((fault_flags & VM_FAULT_WIRE) != 0)
1337 vm_page_activate(fs.m);
1338 if (m_hold != NULL) {
1342 vm_page_unlock(fs.m);
1343 vm_page_xunbusy(fs.m);
1346 * Unlock everything, and return
1348 unlock_and_deallocate(&fs);
1350 VM_CNT_INC(v_io_faults);
1351 curthread->td_ru.ru_majflt++;
1353 if (racct_enable && fs.object->type == OBJT_VNODE) {
1355 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1356 racct_add_force(curproc, RACCT_WRITEBPS,
1357 PAGE_SIZE + behind * PAGE_SIZE);
1358 racct_add_force(curproc, RACCT_WRITEIOPS, 1);
1360 racct_add_force(curproc, RACCT_READBPS,
1361 PAGE_SIZE + ahead * PAGE_SIZE);
1362 racct_add_force(curproc, RACCT_READIOPS, 1);
1364 PROC_UNLOCK(curproc);
1368 curthread->td_ru.ru_minflt++;
1370 return (KERN_SUCCESS);
1374 * Speed up the reclamation of pages that precede the faulting pindex within
1375 * the first object of the shadow chain. Essentially, perform the equivalent
1376 * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes
1377 * the faulting pindex by the cluster size when the pages read by vm_fault()
1378 * cross a cluster-size boundary. The cluster size is the greater of the
1379 * smallest superpage size and VM_FAULT_DONTNEED_MIN.
1381 * When "fs->first_object" is a shadow object, the pages in the backing object
1382 * that precede the faulting pindex are deactivated by vm_fault(). So, this
1383 * function must only be concerned with pages in the first object.
1386 vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead)
1388 vm_map_entry_t entry;
1389 vm_object_t first_object, object;
1390 vm_offset_t end, start;
1391 vm_page_t m, m_next;
1392 vm_pindex_t pend, pstart;
1395 object = fs->object;
1396 VM_OBJECT_ASSERT_WLOCKED(object);
1397 first_object = fs->first_object;
1398 if (first_object != object) {
1399 if (!VM_OBJECT_TRYWLOCK(first_object)) {
1400 VM_OBJECT_WUNLOCK(object);
1401 VM_OBJECT_WLOCK(first_object);
1402 VM_OBJECT_WLOCK(object);
1405 /* Neither fictitious nor unmanaged pages can be reclaimed. */
1406 if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
1407 size = VM_FAULT_DONTNEED_MIN;
1408 if (MAXPAGESIZES > 1 && size < pagesizes[1])
1409 size = pagesizes[1];
1410 end = rounddown2(vaddr, size);
1411 if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) &&
1412 (entry = fs->entry)->start < end) {
1413 if (end - entry->start < size)
1414 start = entry->start;
1417 pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED);
1418 pstart = OFF_TO_IDX(entry->offset) + atop(start -
1420 m_next = vm_page_find_least(first_object, pstart);
1421 pend = OFF_TO_IDX(entry->offset) + atop(end -
1423 while ((m = m_next) != NULL && m->pindex < pend) {
1424 m_next = TAILQ_NEXT(m, listq);
1425 if (m->valid != VM_PAGE_BITS_ALL ||
1430 * Don't clear PGA_REFERENCED, since it would
1431 * likely represent a reference by a different
1434 * Typically, at this point, prefetched pages
1435 * are still in the inactive queue. Only
1436 * pages that triggered page faults are in the
1440 if (!vm_page_inactive(m))
1441 vm_page_deactivate(m);
1446 if (first_object != object)
1447 VM_OBJECT_WUNLOCK(first_object);
1451 * vm_fault_prefault provides a quick way of clustering
1452 * pagefaults into a processes address space. It is a "cousin"
1453 * of vm_map_pmap_enter, except it runs at page fault time instead
1457 vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
1458 int backward, int forward, bool obj_locked)
1461 vm_map_entry_t entry;
1462 vm_object_t backing_object, lobject;
1463 vm_offset_t addr, starta;
1468 pmap = fs->map->pmap;
1469 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1474 if (addra < backward * PAGE_SIZE) {
1475 starta = entry->start;
1477 starta = addra - backward * PAGE_SIZE;
1478 if (starta < entry->start)
1479 starta = entry->start;
1483 * Generate the sequence of virtual addresses that are candidates for
1484 * prefaulting in an outward spiral from the faulting virtual address,
1485 * "addra". Specifically, the sequence is "addra - PAGE_SIZE", "addra
1486 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
1487 * If the candidate address doesn't have a backing physical page, then
1488 * the loop immediately terminates.
1490 for (i = 0; i < 2 * imax(backward, forward); i++) {
1491 addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
1493 if (addr > addra + forward * PAGE_SIZE)
1496 if (addr < starta || addr >= entry->end)
1499 if (!pmap_is_prefaultable(pmap, addr))
1502 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1503 lobject = entry->object.vm_object;
1505 VM_OBJECT_RLOCK(lobject);
1506 while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1507 lobject->type == OBJT_DEFAULT &&
1508 (backing_object = lobject->backing_object) != NULL) {
1509 KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
1510 0, ("vm_fault_prefault: unaligned object offset"));
1511 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1512 VM_OBJECT_RLOCK(backing_object);
1513 if (!obj_locked || lobject != entry->object.vm_object)
1514 VM_OBJECT_RUNLOCK(lobject);
1515 lobject = backing_object;
1518 if (!obj_locked || lobject != entry->object.vm_object)
1519 VM_OBJECT_RUNLOCK(lobject);
1522 if (m->valid == VM_PAGE_BITS_ALL &&
1523 (m->flags & PG_FICTITIOUS) == 0)
1524 pmap_enter_quick(pmap, addr, m, entry->protection);
1525 if (!obj_locked || lobject != entry->object.vm_object)
1526 VM_OBJECT_RUNLOCK(lobject);
1531 * Hold each of the physical pages that are mapped by the specified range of
1532 * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
1533 * and allow the specified types of access, "prot". If all of the implied
1534 * pages are successfully held, then the number of held pages is returned
1535 * together with pointers to those pages in the array "ma". However, if any
1536 * of the pages cannot be held, -1 is returned.
1539 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
1540 vm_prot_t prot, vm_page_t *ma, int max_count)
1542 vm_offset_t end, va;
1545 boolean_t pmap_failed;
1549 end = round_page(addr + len);
1550 addr = trunc_page(addr);
1553 * Check for illegal addresses.
1555 if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
1558 if (atop(end - addr) > max_count)
1559 panic("vm_fault_quick_hold_pages: count > max_count");
1560 count = atop(end - addr);
1563 * Most likely, the physical pages are resident in the pmap, so it is
1564 * faster to try pmap_extract_and_hold() first.
1566 pmap_failed = FALSE;
1567 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
1568 *mp = pmap_extract_and_hold(map->pmap, va, prot);
1571 else if ((prot & VM_PROT_WRITE) != 0 &&
1572 (*mp)->dirty != VM_PAGE_BITS_ALL) {
1574 * Explicitly dirty the physical page. Otherwise, the
1575 * caller's changes may go unnoticed because they are
1576 * performed through an unmanaged mapping or by a DMA
1579 * The object lock is not held here.
1580 * See vm_page_clear_dirty_mask().
1587 * One or more pages could not be held by the pmap. Either no
1588 * page was mapped at the specified virtual address or that
1589 * mapping had insufficient permissions. Attempt to fault in
1590 * and hold these pages.
1592 * If vm_fault_disable_pagefaults() was called,
1593 * i.e., TDP_NOFAULTING is set, we must not sleep nor
1594 * acquire MD VM locks, which means we must not call
1595 * vm_fault_hold(). Some (out of tree) callers mark
1596 * too wide a code area with vm_fault_disable_pagefaults()
1597 * already, use the VM_PROT_QUICK_NOFAULT flag to request
1598 * the proper behaviour explicitly.
1600 if ((prot & VM_PROT_QUICK_NOFAULT) != 0 &&
1601 (curthread->td_pflags & TDP_NOFAULTING) != 0)
1603 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1604 if (*mp == NULL && vm_fault_hold(map, va, prot,
1605 VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
1610 for (mp = ma; mp < ma + count; mp++)
1613 if (vm_page_unwire(*mp, PQ_INACTIVE) &&
1614 (*mp)->object == NULL)
1616 vm_page_unlock(*mp);
1623 * vm_fault_copy_entry
1625 * Create new shadow object backing dst_entry with private copy of
1626 * all underlying pages. When src_entry is equal to dst_entry,
1627 * function implements COW for wired-down map entry. Otherwise,
1628 * it forks wired entry into dst_map.
1630 * In/out conditions:
1631 * The source and destination maps must be locked for write.
1632 * The source map entry must be wired down (or be a sharing map
1633 * entry corresponding to a main map entry that is wired down).
1636 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1637 vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1638 vm_ooffset_t *fork_charge)
1640 vm_object_t backing_object, dst_object, object, src_object;
1641 vm_pindex_t dst_pindex, pindex, src_pindex;
1642 vm_prot_t access, prot;
1652 upgrade = src_entry == dst_entry;
1653 access = prot = dst_entry->protection;
1655 src_object = src_entry->object.vm_object;
1656 src_pindex = OFF_TO_IDX(src_entry->offset);
1658 if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
1659 dst_object = src_object;
1660 vm_object_reference(dst_object);
1663 * Create the top-level object for the destination entry. (Doesn't
1664 * actually shadow anything - we copy the pages directly.)
1666 dst_object = vm_object_allocate(OBJT_DEFAULT,
1667 atop(dst_entry->end - dst_entry->start));
1668 #if VM_NRESERVLEVEL > 0
1669 dst_object->flags |= OBJ_COLORED;
1670 dst_object->pg_color = atop(dst_entry->start);
1672 dst_object->domain = src_object->domain;
1673 dst_object->charge = dst_entry->end - dst_entry->start;
1676 VM_OBJECT_WLOCK(dst_object);
1677 KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1678 ("vm_fault_copy_entry: vm_object not NULL"));
1679 if (src_object != dst_object) {
1680 dst_entry->object.vm_object = dst_object;
1681 dst_entry->offset = 0;
1682 dst_entry->eflags &= ~MAP_ENTRY_VN_EXEC;
1684 if (fork_charge != NULL) {
1685 KASSERT(dst_entry->cred == NULL,
1686 ("vm_fault_copy_entry: leaked swp charge"));
1687 dst_object->cred = curthread->td_ucred;
1688 crhold(dst_object->cred);
1689 *fork_charge += dst_object->charge;
1690 } else if ((dst_object->type == OBJT_DEFAULT ||
1691 dst_object->type == OBJT_SWAP) &&
1692 dst_object->cred == NULL) {
1693 KASSERT(dst_entry->cred != NULL, ("no cred for entry %p",
1695 dst_object->cred = dst_entry->cred;
1696 dst_entry->cred = NULL;
1700 * If not an upgrade, then enter the mappings in the pmap as
1701 * read and/or execute accesses. Otherwise, enter them as
1704 * A writeable large page mapping is only created if all of
1705 * the constituent small page mappings are modified. Marking
1706 * PTEs as modified on inception allows promotion to happen
1707 * without taking potentially large number of soft faults.
1710 access &= ~VM_PROT_WRITE;
1713 * Loop through all of the virtual pages within the entry's
1714 * range, copying each page from the source object to the
1715 * destination object. Since the source is wired, those pages
1716 * must exist. In contrast, the destination is pageable.
1717 * Since the destination object doesn't share any backing storage
1718 * with the source object, all of its pages must be dirtied,
1719 * regardless of whether they can be written.
1721 for (vaddr = dst_entry->start, dst_pindex = 0;
1722 vaddr < dst_entry->end;
1723 vaddr += PAGE_SIZE, dst_pindex++) {
1726 * Find the page in the source object, and copy it in.
1727 * Because the source is wired down, the page will be
1730 if (src_object != dst_object)
1731 VM_OBJECT_RLOCK(src_object);
1732 object = src_object;
1733 pindex = src_pindex + dst_pindex;
1734 while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1735 (backing_object = object->backing_object) != NULL) {
1737 * Unless the source mapping is read-only or
1738 * it is presently being upgraded from
1739 * read-only, the first object in the shadow
1740 * chain should provide all of the pages. In
1741 * other words, this loop body should never be
1742 * executed when the source mapping is already
1745 KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 ||
1747 ("vm_fault_copy_entry: main object missing page"));
1749 VM_OBJECT_RLOCK(backing_object);
1750 pindex += OFF_TO_IDX(object->backing_object_offset);
1751 if (object != dst_object)
1752 VM_OBJECT_RUNLOCK(object);
1753 object = backing_object;
1755 KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing"));
1757 if (object != dst_object) {
1759 * Allocate a page in the destination object.
1761 dst_m = vm_page_alloc(dst_object, (src_object ==
1762 dst_object ? src_pindex : 0) + dst_pindex,
1764 if (dst_m == NULL) {
1765 VM_OBJECT_WUNLOCK(dst_object);
1766 VM_OBJECT_RUNLOCK(object);
1767 vm_wait(dst_object);
1768 VM_OBJECT_WLOCK(dst_object);
1771 pmap_copy_page(src_m, dst_m);
1772 VM_OBJECT_RUNLOCK(object);
1773 dst_m->dirty = dst_m->valid = src_m->valid;
1776 if (vm_page_sleep_if_busy(dst_m, "fltupg"))
1778 if (dst_m->pindex >= dst_object->size)
1780 * We are upgrading. Index can occur
1781 * out of bounds if the object type is
1782 * vnode and the file was truncated.
1785 vm_page_xbusy(dst_m);
1787 VM_OBJECT_WUNLOCK(dst_object);
1790 * Enter it in the pmap. If a wired, copy-on-write
1791 * mapping is being replaced by a write-enabled
1792 * mapping, then wire that new mapping.
1794 * The page can be invalid if the user called
1795 * msync(MS_INVALIDATE) or truncated the backing vnode
1796 * or shared memory object. In this case, do not
1797 * insert it into pmap, but still do the copy so that
1798 * all copies of the wired map entry have similar
1801 if (dst_m->valid == VM_PAGE_BITS_ALL) {
1802 pmap_enter(dst_map->pmap, vaddr, dst_m, prot,
1803 access | (upgrade ? PMAP_ENTER_WIRED : 0), 0);
1807 * Mark it no longer busy, and put it on the active list.
1809 VM_OBJECT_WLOCK(dst_object);
1812 if (src_m != dst_m) {
1813 vm_page_lock(src_m);
1814 vm_page_unwire(src_m, PQ_INACTIVE);
1815 vm_page_unlock(src_m);
1816 vm_page_lock(dst_m);
1817 vm_page_wire(dst_m);
1818 vm_page_unlock(dst_m);
1820 KASSERT(vm_page_wired(dst_m),
1821 ("dst_m %p is not wired", dst_m));
1824 vm_page_lock(dst_m);
1825 vm_page_activate(dst_m);
1826 vm_page_unlock(dst_m);
1828 vm_page_xunbusy(dst_m);
1830 VM_OBJECT_WUNLOCK(dst_object);
1832 dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1833 vm_object_deallocate(src_object);
1838 * Block entry into the machine-independent layer's page fault handler by
1839 * the calling thread. Subsequent calls to vm_fault() by that thread will
1840 * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of
1841 * spurious page faults.
1844 vm_fault_disable_pagefaults(void)
1847 return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
1851 vm_fault_enable_pagefaults(int save)
1854 curthread_pflags_restore(save);