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/refcount.h>
91 #include <sys/resourcevar.h>
92 #include <sys/rwlock.h>
93 #include <sys/signalvar.h>
94 #include <sys/sysctl.h>
95 #include <sys/sysent.h>
96 #include <sys/vmmeter.h>
97 #include <sys/vnode.h>
99 #include <sys/ktrace.h>
103 #include <vm/vm_param.h>
105 #include <vm/vm_map.h>
106 #include <vm/vm_object.h>
107 #include <vm/vm_page.h>
108 #include <vm/vm_pageout.h>
109 #include <vm/vm_kern.h>
110 #include <vm/vm_pager.h>
111 #include <vm/vm_extern.h>
112 #include <vm/vm_reserv.h>
117 #define VM_FAULT_READ_DEFAULT (1 + VM_FAULT_READ_AHEAD_INIT)
118 #define VM_FAULT_READ_MAX (1 + VM_FAULT_READ_AHEAD_MAX)
120 #define VM_FAULT_DONTNEED_MIN 1048576
128 vm_object_t first_object;
129 vm_pindex_t first_pindex;
131 vm_map_entry_t entry;
133 bool lookup_still_valid;
137 static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr,
139 static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
140 int backward, int forward, bool obj_locked);
142 static int vm_pfault_oom_attempts = 3;
143 SYSCTL_INT(_vm, OID_AUTO, pfault_oom_attempts, CTLFLAG_RWTUN,
144 &vm_pfault_oom_attempts, 0,
145 "Number of page allocation attempts in page fault handler before it "
146 "triggers OOM handling");
148 static int vm_pfault_oom_wait = 10;
149 SYSCTL_INT(_vm, OID_AUTO, pfault_oom_wait, CTLFLAG_RWTUN,
150 &vm_pfault_oom_wait, 0,
151 "Number of seconds to wait for free pages before retrying "
152 "the page fault handler");
155 fault_page_release(vm_page_t *mp)
162 * We are likely to loop around again and attempt to busy
163 * this page. Deactivating it leaves it available for
164 * pageout while optimizing fault restarts.
166 vm_page_deactivate(m);
173 fault_page_free(vm_page_t *mp)
179 VM_OBJECT_ASSERT_WLOCKED(m->object);
180 if (!vm_page_wired(m))
189 unlock_map(struct faultstate *fs)
192 if (fs->lookup_still_valid) {
193 vm_map_lookup_done(fs->map, fs->entry);
194 fs->lookup_still_valid = false;
199 unlock_vp(struct faultstate *fs)
202 if (fs->vp != NULL) {
209 fault_deallocate(struct faultstate *fs)
212 fault_page_release(&fs->m_cow);
213 fault_page_release(&fs->m);
214 vm_object_pip_wakeup(fs->object);
215 if (fs->object != fs->first_object) {
216 VM_OBJECT_WLOCK(fs->first_object);
217 fault_page_free(&fs->first_m);
218 VM_OBJECT_WUNLOCK(fs->first_object);
219 vm_object_pip_wakeup(fs->first_object);
221 vm_object_deallocate(fs->first_object);
227 unlock_and_deallocate(struct faultstate *fs)
230 VM_OBJECT_WUNLOCK(fs->object);
231 fault_deallocate(fs);
235 vm_fault_dirty(vm_map_entry_t entry, vm_page_t m, vm_prot_t prot,
236 vm_prot_t fault_type, int fault_flags)
240 if (((prot & VM_PROT_WRITE) == 0 &&
241 (fault_flags & VM_FAULT_DIRTY) == 0) ||
242 (m->oflags & VPO_UNMANAGED) != 0)
245 VM_PAGE_OBJECT_BUSY_ASSERT(m);
247 need_dirty = ((fault_type & VM_PROT_WRITE) != 0 &&
248 (fault_flags & VM_FAULT_WIRE) == 0) ||
249 (fault_flags & VM_FAULT_DIRTY) != 0;
251 vm_object_set_writeable_dirty(m->object);
254 * If the fault is a write, we know that this page is being
255 * written NOW so dirty it explicitly to save on
256 * pmap_is_modified() calls later.
258 * Also, since the page is now dirty, we can possibly tell
259 * the pager to release any swap backing the page.
261 if (need_dirty && vm_page_set_dirty(m) == 0) {
263 * If this is a NOSYNC mmap we do not want to set PGA_NOSYNC
264 * if the page is already dirty to prevent data written with
265 * the expectation of being synced from not being synced.
266 * Likewise if this entry does not request NOSYNC then make
267 * sure the page isn't marked NOSYNC. Applications sharing
268 * data should use the same flags to avoid ping ponging.
270 if ((entry->eflags & MAP_ENTRY_NOSYNC) != 0)
271 vm_page_aflag_set(m, PGA_NOSYNC);
273 vm_page_aflag_clear(m, PGA_NOSYNC);
279 * Unlocks fs.first_object and fs.map on success.
282 vm_fault_soft_fast(struct faultstate *fs, vm_offset_t vaddr, vm_prot_t prot,
283 int fault_type, int fault_flags, boolean_t wired, vm_page_t *m_hold)
286 #if (defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
287 __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)) && \
294 MPASS(fs->vp == NULL);
295 vm_object_busy(fs->first_object);
296 m = vm_page_lookup(fs->first_object, fs->first_pindex);
297 /* A busy page can be mapped for read|execute access. */
298 if (m == NULL || ((prot & VM_PROT_WRITE) != 0 &&
299 vm_page_busied(m)) || !vm_page_all_valid(m)) {
305 #if (defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
306 __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)) && \
308 if ((m->flags & PG_FICTITIOUS) == 0 &&
309 (m_super = vm_reserv_to_superpage(m)) != NULL &&
310 rounddown2(vaddr, pagesizes[m_super->psind]) >= fs->entry->start &&
311 roundup2(vaddr + 1, pagesizes[m_super->psind]) <= fs->entry->end &&
312 (vaddr & (pagesizes[m_super->psind] - 1)) == (VM_PAGE_TO_PHYS(m) &
313 (pagesizes[m_super->psind] - 1)) && !wired &&
314 pmap_ps_enabled(fs->map->pmap)) {
315 flags = PS_ALL_VALID;
316 if ((prot & VM_PROT_WRITE) != 0) {
318 * Create a superpage mapping allowing write access
319 * only if none of the constituent pages are busy and
320 * all of them are already dirty (except possibly for
321 * the page that was faulted on).
323 flags |= PS_NONE_BUSY;
324 if ((fs->first_object->flags & OBJ_UNMANAGED) == 0)
325 flags |= PS_ALL_DIRTY;
327 if (vm_page_ps_test(m_super, flags, m)) {
329 psind = m_super->psind;
330 vaddr = rounddown2(vaddr, pagesizes[psind]);
331 /* Preset the modified bit for dirty superpages. */
332 if ((flags & PS_ALL_DIRTY) != 0)
333 fault_type |= VM_PROT_WRITE;
337 rv = pmap_enter(fs->map->pmap, vaddr, m_map, prot, fault_type |
338 PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED : 0), psind);
339 if (rv != KERN_SUCCESS)
341 if (m_hold != NULL) {
345 vm_fault_dirty(fs->entry, m, prot, fault_type, fault_flags);
346 if (psind == 0 && !wired)
347 vm_fault_prefault(fs, vaddr, PFBAK, PFFOR, true);
348 VM_OBJECT_RUNLOCK(fs->first_object);
349 vm_map_lookup_done(fs->map, fs->entry);
350 curthread->td_ru.ru_minflt++;
353 vm_object_unbusy(fs->first_object);
358 vm_fault_restore_map_lock(struct faultstate *fs)
361 VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
362 MPASS(REFCOUNT_COUNT(fs->first_object->paging_in_progress) > 0);
364 if (!vm_map_trylock_read(fs->map)) {
365 VM_OBJECT_WUNLOCK(fs->first_object);
366 vm_map_lock_read(fs->map);
367 VM_OBJECT_WLOCK(fs->first_object);
369 fs->lookup_still_valid = true;
373 vm_fault_populate_check_page(vm_page_t m)
377 * Check each page to ensure that the pager is obeying the
378 * interface: the page must be installed in the object, fully
379 * valid, and exclusively busied.
382 MPASS(vm_page_all_valid(m));
383 MPASS(vm_page_xbusied(m));
387 vm_fault_populate_cleanup(vm_object_t object, vm_pindex_t first,
393 VM_OBJECT_ASSERT_WLOCKED(object);
394 MPASS(first <= last);
395 for (pidx = first, m = vm_page_lookup(object, pidx);
396 pidx <= last; pidx++, m = vm_page_next(m)) {
397 vm_fault_populate_check_page(m);
398 vm_page_deactivate(m);
404 vm_fault_populate(struct faultstate *fs, vm_prot_t prot, int fault_type,
405 int fault_flags, boolean_t wired, vm_page_t *m_hold)
409 vm_pindex_t map_first, map_last, pager_first, pager_last, pidx;
410 int i, npages, psind, rv;
412 MPASS(fs->object == fs->first_object);
413 VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
414 MPASS(REFCOUNT_COUNT(fs->first_object->paging_in_progress) > 0);
415 MPASS(fs->first_object->backing_object == NULL);
416 MPASS(fs->lookup_still_valid);
418 pager_first = OFF_TO_IDX(fs->entry->offset);
419 pager_last = pager_first + atop(fs->entry->end - fs->entry->start) - 1;
424 * Call the pager (driver) populate() method.
426 * There is no guarantee that the method will be called again
427 * if the current fault is for read, and a future fault is
428 * for write. Report the entry's maximum allowed protection
431 rv = vm_pager_populate(fs->first_object, fs->first_pindex,
432 fault_type, fs->entry->max_protection, &pager_first, &pager_last);
434 VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
435 if (rv == VM_PAGER_BAD) {
437 * VM_PAGER_BAD is the backdoor for a pager to request
438 * normal fault handling.
440 vm_fault_restore_map_lock(fs);
441 if (fs->map->timestamp != fs->map_generation)
442 return (KERN_RESOURCE_SHORTAGE); /* RetryFault */
443 return (KERN_NOT_RECEIVER);
445 if (rv != VM_PAGER_OK)
446 return (KERN_FAILURE); /* AKA SIGSEGV */
448 /* Ensure that the driver is obeying the interface. */
449 MPASS(pager_first <= pager_last);
450 MPASS(fs->first_pindex <= pager_last);
451 MPASS(fs->first_pindex >= pager_first);
452 MPASS(pager_last < fs->first_object->size);
454 vm_fault_restore_map_lock(fs);
455 if (fs->map->timestamp != fs->map_generation) {
456 vm_fault_populate_cleanup(fs->first_object, pager_first,
458 return (KERN_RESOURCE_SHORTAGE); /* RetryFault */
462 * The map is unchanged after our last unlock. Process the fault.
464 * The range [pager_first, pager_last] that is given to the
465 * pager is only a hint. The pager may populate any range
466 * within the object that includes the requested page index.
467 * In case the pager expanded the range, clip it to fit into
470 map_first = OFF_TO_IDX(fs->entry->offset);
471 if (map_first > pager_first) {
472 vm_fault_populate_cleanup(fs->first_object, pager_first,
474 pager_first = map_first;
476 map_last = map_first + atop(fs->entry->end - fs->entry->start) - 1;
477 if (map_last < pager_last) {
478 vm_fault_populate_cleanup(fs->first_object, map_last + 1,
480 pager_last = map_last;
482 for (pidx = pager_first, m = vm_page_lookup(fs->first_object, pidx);
484 pidx += npages, m = vm_page_next(&m[npages - 1])) {
485 vaddr = fs->entry->start + IDX_TO_OFF(pidx) - fs->entry->offset;
486 #if defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
487 __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)
489 if (psind > 0 && ((vaddr & (pagesizes[psind] - 1)) != 0 ||
490 pidx + OFF_TO_IDX(pagesizes[psind]) - 1 > pager_last ||
491 !pmap_ps_enabled(fs->map->pmap) || wired))
496 npages = atop(pagesizes[psind]);
497 for (i = 0; i < npages; i++) {
498 vm_fault_populate_check_page(&m[i]);
499 vm_fault_dirty(fs->entry, &m[i], prot, fault_type,
502 VM_OBJECT_WUNLOCK(fs->first_object);
503 rv = pmap_enter(fs->map->pmap, vaddr, m, prot, fault_type |
504 (wired ? PMAP_ENTER_WIRED : 0), psind);
505 #if defined(__amd64__)
506 if (psind > 0 && rv == KERN_FAILURE) {
507 for (i = 0; i < npages; i++) {
508 rv = pmap_enter(fs->map->pmap, vaddr + ptoa(i),
509 &m[i], prot, fault_type |
510 (wired ? PMAP_ENTER_WIRED : 0), 0);
511 MPASS(rv == KERN_SUCCESS);
515 MPASS(rv == KERN_SUCCESS);
517 VM_OBJECT_WLOCK(fs->first_object);
518 for (i = 0; i < npages; i++) {
519 if ((fault_flags & VM_FAULT_WIRE) != 0)
522 vm_page_activate(&m[i]);
523 if (m_hold != NULL && m[i].pindex == fs->first_pindex) {
527 vm_page_xunbusy(&m[i]);
530 curthread->td_ru.ru_majflt++;
531 return (KERN_SUCCESS);
534 static int prot_fault_translation;
535 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RWTUN,
536 &prot_fault_translation, 0,
537 "Control signal to deliver on protection fault");
539 /* compat definition to keep common code for signal translation */
540 #define UCODE_PAGEFLT 12
542 _Static_assert(UCODE_PAGEFLT == T_PAGEFLT, "T_PAGEFLT");
548 * Handle a page fault occurring at the given address,
549 * requiring the given permissions, in the map specified.
550 * If successful, the page is inserted into the
551 * associated physical map.
553 * NOTE: the given address should be truncated to the
554 * proper page address.
556 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
557 * a standard error specifying why the fault is fatal is returned.
559 * The map in question must be referenced, and remains so.
560 * Caller may hold no locks.
563 vm_fault_trap(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
564 int fault_flags, int *signo, int *ucode)
568 MPASS(signo == NULL || ucode != NULL);
570 if (map != kernel_map && KTRPOINT(curthread, KTR_FAULT))
571 ktrfault(vaddr, fault_type);
573 result = vm_fault(map, trunc_page(vaddr), fault_type, fault_flags,
575 KASSERT(result == KERN_SUCCESS || result == KERN_FAILURE ||
576 result == KERN_INVALID_ADDRESS ||
577 result == KERN_RESOURCE_SHORTAGE ||
578 result == KERN_PROTECTION_FAILURE ||
579 result == KERN_OUT_OF_BOUNDS,
580 ("Unexpected Mach error %d from vm_fault()", result));
582 if (map != kernel_map && KTRPOINT(curthread, KTR_FAULTEND))
585 if (result != KERN_SUCCESS && signo != NULL) {
588 case KERN_INVALID_ADDRESS:
590 *ucode = SEGV_MAPERR;
592 case KERN_RESOURCE_SHORTAGE:
596 case KERN_OUT_OF_BOUNDS:
600 case KERN_PROTECTION_FAILURE:
601 if (prot_fault_translation == 0) {
603 * Autodetect. This check also covers
604 * the images without the ABI-tag ELF
607 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD &&
608 curproc->p_osrel >= P_OSREL_SIGSEGV) {
610 *ucode = SEGV_ACCERR;
613 *ucode = UCODE_PAGEFLT;
615 } else if (prot_fault_translation == 1) {
616 /* Always compat mode. */
618 *ucode = UCODE_PAGEFLT;
620 /* Always SIGSEGV mode. */
622 *ucode = SEGV_ACCERR;
626 KASSERT(0, ("Unexpected Mach error %d from vm_fault()",
635 vm_fault_lock_vnode(struct faultstate *fs)
640 if (fs->object->type != OBJT_VNODE)
641 return (KERN_SUCCESS);
642 vp = fs->object->handle;
644 ASSERT_VOP_LOCKED(vp, "saved vnode is not locked");
645 return (KERN_SUCCESS);
649 * Perform an unlock in case the desired vnode changed while
650 * the map was unlocked during a retry.
654 locked = VOP_ISLOCKED(vp);
655 if (locked != LK_EXCLUSIVE)
659 * We must not sleep acquiring the vnode lock while we have
660 * the page exclusive busied or the object's
661 * paging-in-progress count incremented. Otherwise, we could
664 error = vget(vp, locked | LK_CANRECURSE | LK_NOWAIT, curthread);
667 return (KERN_SUCCESS);
671 unlock_and_deallocate(fs);
672 error = vget(vp, locked | LK_RETRY | LK_CANRECURSE, curthread);
675 KASSERT(error == 0, ("vm_fault: vget failed %d", error));
676 return (KERN_RESOURCE_SHORTAGE);
680 * Wait/Retry if the page is busy. We have to do this if the page is
681 * either exclusive or shared busy because the vm_pager may be using
682 * read busy for pageouts (and even pageins if it is the vnode pager),
683 * and we could end up trying to pagein and pageout the same page
686 * We can theoretically allow the busy case on a read fault if the page
687 * is marked valid, but since such pages are typically already pmap'd,
688 * putting that special case in might be more effort then it is worth.
689 * We cannot under any circumstances mess around with a shared busied
690 * page except, perhaps, to pmap it.
693 vm_fault_busy_sleep(struct faultstate *fs)
696 * Reference the page before unlocking and
697 * sleeping so that the page daemon is less
698 * likely to reclaim it.
700 vm_page_aflag_set(fs->m, PGA_REFERENCED);
701 if (fs->object != fs->first_object) {
702 fault_page_release(&fs->first_m);
703 vm_object_pip_wakeup(fs->first_object);
705 vm_object_pip_wakeup(fs->object);
707 if (fs->m == vm_page_lookup(fs->object, fs->pindex))
708 vm_page_busy_sleep(fs->m, "vmpfw", false);
710 VM_OBJECT_WUNLOCK(fs->object);
711 VM_CNT_INC(v_intrans);
712 vm_object_deallocate(fs->first_object);
716 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
717 int fault_flags, vm_page_t *m_hold)
719 struct faultstate fs;
720 struct domainset *dset;
721 vm_object_t next_object, retry_object;
722 vm_offset_t e_end, e_start;
723 vm_pindex_t retry_pindex;
724 vm_prot_t prot, retry_prot;
725 int ahead, alloc_req, behind, cluster_offset, era, faultcount;
726 int nera, oom, result, rv;
728 boolean_t wired; /* Passed by reference. */
729 bool dead, hardfault, is_first_object_locked;
731 VM_CNT_INC(v_vm_faults);
733 if ((curthread->td_pflags & TDP_NOFAULTING) != 0)
734 return (KERN_PROTECTION_FAILURE);
746 * Find the backing store object and offset into it to begin the
750 result = vm_map_lookup(&fs.map, vaddr, fault_type |
751 VM_PROT_FAULT_LOOKUP, &fs.entry, &fs.first_object,
752 &fs.first_pindex, &prot, &wired);
753 if (result != KERN_SUCCESS) {
758 fs.map_generation = fs.map->timestamp;
760 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
761 panic("%s: fault on nofault entry, addr: %#lx",
762 __func__, (u_long)vaddr);
765 if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
766 fs.entry->wiring_thread != curthread) {
767 vm_map_unlock_read(fs.map);
769 if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
770 (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
772 fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
773 vm_map_unlock_and_wait(fs.map, 0);
775 vm_map_unlock(fs.map);
779 MPASS((fs.entry->eflags & MAP_ENTRY_GUARD) == 0);
782 fault_type = prot | (fault_type & VM_PROT_COPY);
784 KASSERT((fault_flags & VM_FAULT_WIRE) == 0,
785 ("!wired && VM_FAULT_WIRE"));
788 * Try to avoid lock contention on the top-level object through
789 * special-case handling of some types of page faults, specifically,
790 * those that are mapping an existing page from the top-level object.
791 * Under this condition, a read lock on the object suffices, allowing
792 * multiple page faults of a similar type to run in parallel.
794 if (fs.vp == NULL /* avoid locked vnode leak */ &&
795 (fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0) {
796 VM_OBJECT_RLOCK(fs.first_object);
797 rv = vm_fault_soft_fast(&fs, vaddr, prot, fault_type,
798 fault_flags, wired, m_hold);
799 if (rv == KERN_SUCCESS)
801 if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) {
802 VM_OBJECT_RUNLOCK(fs.first_object);
803 VM_OBJECT_WLOCK(fs.first_object);
806 VM_OBJECT_WLOCK(fs.first_object);
810 * Make a reference to this object to prevent its disposal while we
811 * are messing with it. Once we have the reference, the map is free
812 * to be diddled. Since objects reference their shadows (and copies),
813 * they will stay around as well.
815 * Bump the paging-in-progress count to prevent size changes (e.g.
816 * truncation operations) during I/O.
818 vm_object_reference_locked(fs.first_object);
819 vm_object_pip_add(fs.first_object, 1);
821 fs.lookup_still_valid = true;
823 fs.m_cow = fs.m = fs.first_m = NULL;
826 * Search for the page at object/offset.
828 fs.object = fs.first_object;
829 fs.pindex = fs.first_pindex;
831 KASSERT(fs.m == NULL,
832 ("page still set %p at loop start", fs.m));
834 * If the object is marked for imminent termination,
835 * we retry here, since the collapse pass has raced
836 * with us. Otherwise, if we see terminally dead
837 * object, return fail.
839 if ((fs.object->flags & OBJ_DEAD) != 0) {
840 dead = fs.object->type == OBJT_DEAD;
841 unlock_and_deallocate(&fs);
843 return (KERN_PROTECTION_FAILURE);
849 * See if page is resident
851 fs.m = vm_page_lookup(fs.object, fs.pindex);
853 if (vm_page_tryxbusy(fs.m) == 0) {
854 vm_fault_busy_sleep(&fs);
859 * The page is marked busy for other processes and the
860 * pagedaemon. If it still isn't completely valid
861 * (readable), jump to readrest, else break-out ( we
864 if (!vm_page_all_valid(fs.m))
866 break; /* break to PAGE HAS BEEN FOUND */
868 KASSERT(fs.m == NULL, ("fs.m should be NULL, not %p", fs.m));
871 * Page is not resident. If the pager might contain the page
872 * or this is the beginning of the search, allocate a new
873 * page. (Default objects are zero-fill, so there is no real
876 if (fs.object->type != OBJT_DEFAULT ||
877 fs.object == fs.first_object) {
878 if ((fs.object->flags & OBJ_SIZEVNLOCK) != 0) {
879 rv = vm_fault_lock_vnode(&fs);
880 MPASS(rv == KERN_SUCCESS ||
881 rv == KERN_RESOURCE_SHORTAGE);
882 if (rv == KERN_RESOURCE_SHORTAGE)
885 if (fs.pindex >= fs.object->size) {
886 unlock_and_deallocate(&fs);
887 return (KERN_OUT_OF_BOUNDS);
890 if (fs.object == fs.first_object &&
891 (fs.first_object->flags & OBJ_POPULATE) != 0 &&
892 fs.first_object->shadow_count == 0) {
893 rv = vm_fault_populate(&fs, prot, fault_type,
894 fault_flags, wired, m_hold);
898 unlock_and_deallocate(&fs);
900 case KERN_RESOURCE_SHORTAGE:
901 unlock_and_deallocate(&fs);
903 case KERN_NOT_RECEIVER:
905 * Pager's populate() method
906 * returned VM_PAGER_BAD.
910 panic("inconsistent return codes");
915 * Allocate a new page for this object/offset pair.
917 * Unlocked read of the p_flag is harmless. At
918 * worst, the P_KILLED might be not observed
919 * there, and allocation can fail, causing
920 * restart and new reading of the p_flag.
922 dset = fs.object->domain.dr_policy;
924 dset = curthread->td_domain.dr_policy;
925 if (!vm_page_count_severe_set(&dset->ds_mask) ||
927 #if VM_NRESERVLEVEL > 0
928 vm_object_color(fs.object, atop(vaddr) -
931 alloc_req = P_KILLED(curproc) ?
932 VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
933 if (fs.object->type != OBJT_VNODE &&
934 fs.object->backing_object == NULL)
935 alloc_req |= VM_ALLOC_ZERO;
936 fs.m = vm_page_alloc(fs.object, fs.pindex,
940 unlock_and_deallocate(&fs);
941 if (vm_pfault_oom_attempts < 0 ||
942 oom < vm_pfault_oom_attempts) {
945 vm_pfault_oom_wait * hz);
950 "proc %d (%s) failed to alloc page on fault, starting OOM\n",
951 curproc->p_pid, curproc->p_comm);
952 vm_pageout_oom(VM_OOM_MEM_PF);
959 * At this point, we have either allocated a new page or found
960 * an existing page that is only partially valid.
962 * We hold a reference on the current object and the page is
967 * If the pager for the current object might have the page,
968 * then determine the number of additional pages to read and
969 * potentially reprioritize previously read pages for earlier
970 * reclamation. These operations should only be performed
971 * once per page fault. Even if the current pager doesn't
972 * have the page, the number of additional pages to read will
973 * apply to subsequent objects in the shadow chain.
975 if (fs.object->type != OBJT_DEFAULT && nera == -1 &&
976 !P_KILLED(curproc)) {
977 KASSERT(fs.lookup_still_valid, ("map unlocked"));
978 era = fs.entry->read_ahead;
979 behavior = vm_map_entry_behavior(fs.entry);
980 if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
982 } else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
983 nera = VM_FAULT_READ_AHEAD_MAX;
984 if (vaddr == fs.entry->next_read)
985 vm_fault_dontneed(&fs, vaddr, nera);
986 } else if (vaddr == fs.entry->next_read) {
988 * This is a sequential fault. Arithmetically
989 * increase the requested number of pages in
990 * the read-ahead window. The requested
991 * number of pages is "# of sequential faults
992 * x (read ahead min + 1) + read ahead min"
994 nera = VM_FAULT_READ_AHEAD_MIN;
997 if (nera > VM_FAULT_READ_AHEAD_MAX)
998 nera = VM_FAULT_READ_AHEAD_MAX;
1000 if (era == VM_FAULT_READ_AHEAD_MAX)
1001 vm_fault_dontneed(&fs, vaddr, nera);
1004 * This is a non-sequential fault.
1010 * A read lock on the map suffices to update
1011 * the read ahead count safely.
1013 fs.entry->read_ahead = nera;
1017 * Prepare for unlocking the map. Save the map
1018 * entry's start and end addresses, which are used to
1019 * optimize the size of the pager operation below.
1020 * Even if the map entry's addresses change after
1021 * unlocking the map, using the saved addresses is
1024 e_start = fs.entry->start;
1025 e_end = fs.entry->end;
1029 * Call the pager to retrieve the page if there is a chance
1030 * that the pager has it, and potentially retrieve additional
1031 * pages at the same time.
1033 if (fs.object->type != OBJT_DEFAULT) {
1035 * Release the map lock before locking the vnode or
1036 * sleeping in the pager. (If the current object has
1037 * a shadow, then an earlier iteration of this loop
1038 * may have already unlocked the map.)
1042 rv = vm_fault_lock_vnode(&fs);
1043 MPASS(rv == KERN_SUCCESS ||
1044 rv == KERN_RESOURCE_SHORTAGE);
1045 if (rv == KERN_RESOURCE_SHORTAGE)
1047 KASSERT(fs.vp == NULL || !fs.map->system_map,
1048 ("vm_fault: vnode-backed object mapped by system map"));
1051 * Page in the requested page and hint the pager,
1052 * that it may bring up surrounding pages.
1054 if (nera == -1 || behavior == MAP_ENTRY_BEHAV_RANDOM ||
1055 P_KILLED(curproc)) {
1059 /* Is this a sequential fault? */
1065 * Request a cluster of pages that is
1066 * aligned to a VM_FAULT_READ_DEFAULT
1067 * page offset boundary within the
1068 * object. Alignment to a page offset
1069 * boundary is more likely to coincide
1070 * with the underlying file system
1071 * block than alignment to a virtual
1074 cluster_offset = fs.pindex %
1075 VM_FAULT_READ_DEFAULT;
1076 behind = ulmin(cluster_offset,
1077 atop(vaddr - e_start));
1078 ahead = VM_FAULT_READ_DEFAULT - 1 -
1081 ahead = ulmin(ahead, atop(e_end - vaddr) - 1);
1083 VM_OBJECT_WUNLOCK(fs.object);
1084 rv = vm_pager_get_pages(fs.object, &fs.m, 1,
1086 VM_OBJECT_WLOCK(fs.object);
1087 if (rv == VM_PAGER_OK) {
1088 faultcount = behind + 1 + ahead;
1090 break; /* break to PAGE HAS BEEN FOUND */
1092 if (rv == VM_PAGER_ERROR)
1093 printf("vm_fault: pager read error, pid %d (%s)\n",
1094 curproc->p_pid, curproc->p_comm);
1097 * If an I/O error occurred or the requested page was
1098 * outside the range of the pager, clean up and return
1101 if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) {
1102 fault_page_free(&fs.m);
1103 unlock_and_deallocate(&fs);
1104 return (KERN_OUT_OF_BOUNDS);
1110 * The requested page does not exist at this object/
1111 * offset. Remove the invalid page from the object,
1112 * waking up anyone waiting for it, and continue on to
1113 * the next object. However, if this is the top-level
1114 * object, we must leave the busy page in place to
1115 * prevent another process from rushing past us, and
1116 * inserting the page in that object at the same time
1119 if (fs.object == fs.first_object) {
1123 fault_page_free(&fs.m);
1126 * Move on to the next object. Lock the next object before
1127 * unlocking the current one.
1129 next_object = fs.object->backing_object;
1130 if (next_object == NULL) {
1132 * If there's no object left, fill the page in the top
1133 * object with zeros.
1135 if (fs.object != fs.first_object) {
1136 vm_object_pip_wakeup(fs.object);
1137 VM_OBJECT_WUNLOCK(fs.object);
1139 fs.object = fs.first_object;
1140 fs.pindex = fs.first_pindex;
1141 VM_OBJECT_WLOCK(fs.object);
1143 MPASS(fs.first_m != NULL);
1144 MPASS(fs.m == NULL);
1149 * Zero the page if necessary and mark it valid.
1151 if ((fs.m->flags & PG_ZERO) == 0) {
1152 pmap_zero_page(fs.m);
1154 VM_CNT_INC(v_ozfod);
1157 vm_page_valid(fs.m);
1158 /* Don't try to prefault neighboring pages. */
1160 break; /* break to PAGE HAS BEEN FOUND */
1162 MPASS(fs.first_m != NULL);
1163 KASSERT(fs.object != next_object,
1164 ("object loop %p", next_object));
1165 VM_OBJECT_WLOCK(next_object);
1166 vm_object_pip_add(next_object, 1);
1167 if (fs.object != fs.first_object)
1168 vm_object_pip_wakeup(fs.object);
1170 OFF_TO_IDX(fs.object->backing_object_offset);
1171 VM_OBJECT_WUNLOCK(fs.object);
1172 fs.object = next_object;
1176 vm_page_assert_xbusied(fs.m);
1179 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1184 * If the page is being written, but isn't already owned by the
1185 * top-level object, we have to copy it into a new page owned by the
1188 if (fs.object != fs.first_object) {
1190 * We only really need to copy if we want to write it.
1192 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1194 * This allows pages to be virtually copied from a
1195 * backing_object into the first_object, where the
1196 * backing object has no other refs to it, and cannot
1197 * gain any more refs. Instead of a bcopy, we just
1198 * move the page from the backing object to the
1199 * first object. Note that we must mark the page
1200 * dirty in the first object so that it will go out
1201 * to swap when needed.
1203 is_first_object_locked = false;
1206 * Only one shadow object
1208 (fs.object->shadow_count == 1) &&
1210 * No COW refs, except us
1212 (fs.object->ref_count == 1) &&
1214 * No one else can look this object up
1216 (fs.object->handle == NULL) &&
1218 * No other ways to look the object up
1220 ((fs.object->flags & OBJ_ANON) != 0) &&
1221 (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
1223 * We don't chase down the shadow chain
1225 fs.object == fs.first_object->backing_object) {
1228 * Remove but keep xbusy for replace. fs.m is
1229 * moved into fs.first_object and left busy
1230 * while fs.first_m is conditionally freed.
1232 vm_page_remove_xbusy(fs.m);
1233 vm_page_replace(fs.m, fs.first_object,
1234 fs.first_pindex, fs.first_m);
1235 vm_page_dirty(fs.m);
1236 #if VM_NRESERVLEVEL > 0
1238 * Rename the reservation.
1240 vm_reserv_rename(fs.m, fs.first_object,
1241 fs.object, OFF_TO_IDX(
1242 fs.first_object->backing_object_offset));
1244 VM_OBJECT_WUNLOCK(fs.object);
1247 VM_CNT_INC(v_cow_optim);
1249 VM_OBJECT_WUNLOCK(fs.object);
1251 * Oh, well, lets copy it.
1253 pmap_copy_page(fs.m, fs.first_m);
1254 vm_page_valid(fs.first_m);
1255 if (wired && (fault_flags &
1256 VM_FAULT_WIRE) == 0) {
1257 vm_page_wire(fs.first_m);
1258 vm_page_unwire(fs.m, PQ_INACTIVE);
1261 * Save the cow page to be released after
1262 * pmap_enter is complete.
1268 * fs.object != fs.first_object due to above
1271 vm_object_pip_wakeup(fs.object);
1274 * We only try to prefault read-only mappings to the
1275 * neighboring pages when this copy-on-write fault is
1276 * a hard fault. In other cases, trying to prefault
1277 * is typically wasted effort.
1279 if (faultcount == 0)
1283 * Only use the new page below...
1285 fs.object = fs.first_object;
1286 fs.pindex = fs.first_pindex;
1288 if (!is_first_object_locked)
1289 VM_OBJECT_WLOCK(fs.object);
1290 VM_CNT_INC(v_cow_faults);
1291 curthread->td_cow++;
1293 prot &= ~VM_PROT_WRITE;
1298 * We must verify that the maps have not changed since our last
1301 if (!fs.lookup_still_valid) {
1302 if (!vm_map_trylock_read(fs.map)) {
1303 unlock_and_deallocate(&fs);
1306 fs.lookup_still_valid = true;
1307 if (fs.map->timestamp != fs.map_generation) {
1308 result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
1309 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
1312 * If we don't need the page any longer, put it on the inactive
1313 * list (the easiest thing to do here). If no one needs it,
1314 * pageout will grab it eventually.
1316 if (result != KERN_SUCCESS) {
1317 unlock_and_deallocate(&fs);
1320 * If retry of map lookup would have blocked then
1321 * retry fault from start.
1323 if (result == KERN_FAILURE)
1327 if ((retry_object != fs.first_object) ||
1328 (retry_pindex != fs.first_pindex)) {
1329 unlock_and_deallocate(&fs);
1334 * Check whether the protection has changed or the object has
1335 * been copied while we left the map unlocked. Changing from
1336 * read to write permission is OK - we leave the page
1337 * write-protected, and catch the write fault. Changing from
1338 * write to read permission means that we can't mark the page
1339 * write-enabled after all.
1342 fault_type &= retry_prot;
1344 unlock_and_deallocate(&fs);
1348 /* Reassert because wired may have changed. */
1349 KASSERT(wired || (fault_flags & VM_FAULT_WIRE) == 0,
1350 ("!wired && VM_FAULT_WIRE"));
1355 * If the page was filled by a pager, save the virtual address that
1356 * should be faulted on next under a sequential access pattern to the
1357 * map entry. A read lock on the map suffices to update this address
1361 fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE;
1363 vm_page_assert_xbusied(fs.m);
1364 vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags);
1367 * Page must be completely valid or it is not fit to
1368 * map into user space. vm_pager_get_pages() ensures this.
1370 KASSERT(vm_page_all_valid(fs.m),
1371 ("vm_fault: page %p partially invalid", fs.m));
1372 VM_OBJECT_WUNLOCK(fs.object);
1375 * Put this page into the physical map. We had to do the unlock above
1376 * because pmap_enter() may sleep. We don't put the page
1377 * back on the active queue until later so that the pageout daemon
1378 * won't find it (yet).
1380 pmap_enter(fs.map->pmap, vaddr, fs.m, prot,
1381 fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0);
1382 if (faultcount != 1 && (fault_flags & VM_FAULT_WIRE) == 0 &&
1384 vm_fault_prefault(&fs, vaddr,
1385 faultcount > 0 ? behind : PFBAK,
1386 faultcount > 0 ? ahead : PFFOR, false);
1389 * If the page is not wired down, then put it where the pageout daemon
1392 if ((fault_flags & VM_FAULT_WIRE) != 0)
1395 vm_page_activate(fs.m);
1396 if (m_hold != NULL) {
1400 vm_page_xunbusy(fs.m);
1404 * Unlock everything, and return
1406 fault_deallocate(&fs);
1408 VM_CNT_INC(v_io_faults);
1409 curthread->td_ru.ru_majflt++;
1411 if (racct_enable && fs.object->type == OBJT_VNODE) {
1413 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1414 racct_add_force(curproc, RACCT_WRITEBPS,
1415 PAGE_SIZE + behind * PAGE_SIZE);
1416 racct_add_force(curproc, RACCT_WRITEIOPS, 1);
1418 racct_add_force(curproc, RACCT_READBPS,
1419 PAGE_SIZE + ahead * PAGE_SIZE);
1420 racct_add_force(curproc, RACCT_READIOPS, 1);
1422 PROC_UNLOCK(curproc);
1426 curthread->td_ru.ru_minflt++;
1428 return (KERN_SUCCESS);
1432 * Speed up the reclamation of pages that precede the faulting pindex within
1433 * the first object of the shadow chain. Essentially, perform the equivalent
1434 * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes
1435 * the faulting pindex by the cluster size when the pages read by vm_fault()
1436 * cross a cluster-size boundary. The cluster size is the greater of the
1437 * smallest superpage size and VM_FAULT_DONTNEED_MIN.
1439 * When "fs->first_object" is a shadow object, the pages in the backing object
1440 * that precede the faulting pindex are deactivated by vm_fault(). So, this
1441 * function must only be concerned with pages in the first object.
1444 vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead)
1446 vm_map_entry_t entry;
1447 vm_object_t first_object, object;
1448 vm_offset_t end, start;
1449 vm_page_t m, m_next;
1450 vm_pindex_t pend, pstart;
1453 object = fs->object;
1454 VM_OBJECT_ASSERT_WLOCKED(object);
1455 first_object = fs->first_object;
1456 if (first_object != object) {
1457 if (!VM_OBJECT_TRYWLOCK(first_object)) {
1458 VM_OBJECT_WUNLOCK(object);
1459 VM_OBJECT_WLOCK(first_object);
1460 VM_OBJECT_WLOCK(object);
1463 /* Neither fictitious nor unmanaged pages can be reclaimed. */
1464 if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
1465 size = VM_FAULT_DONTNEED_MIN;
1466 if (MAXPAGESIZES > 1 && size < pagesizes[1])
1467 size = pagesizes[1];
1468 end = rounddown2(vaddr, size);
1469 if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) &&
1470 (entry = fs->entry)->start < end) {
1471 if (end - entry->start < size)
1472 start = entry->start;
1475 pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED);
1476 pstart = OFF_TO_IDX(entry->offset) + atop(start -
1478 m_next = vm_page_find_least(first_object, pstart);
1479 pend = OFF_TO_IDX(entry->offset) + atop(end -
1481 while ((m = m_next) != NULL && m->pindex < pend) {
1482 m_next = TAILQ_NEXT(m, listq);
1483 if (!vm_page_all_valid(m) ||
1488 * Don't clear PGA_REFERENCED, since it would
1489 * likely represent a reference by a different
1492 * Typically, at this point, prefetched pages
1493 * are still in the inactive queue. Only
1494 * pages that triggered page faults are in the
1495 * active queue. The test for whether the page
1496 * is in the inactive queue is racy; in the
1497 * worst case we will requeue the page
1500 if (!vm_page_inactive(m))
1501 vm_page_deactivate(m);
1505 if (first_object != object)
1506 VM_OBJECT_WUNLOCK(first_object);
1510 * vm_fault_prefault provides a quick way of clustering
1511 * pagefaults into a processes address space. It is a "cousin"
1512 * of vm_map_pmap_enter, except it runs at page fault time instead
1516 vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
1517 int backward, int forward, bool obj_locked)
1520 vm_map_entry_t entry;
1521 vm_object_t backing_object, lobject;
1522 vm_offset_t addr, starta;
1527 pmap = fs->map->pmap;
1528 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1533 if (addra < backward * PAGE_SIZE) {
1534 starta = entry->start;
1536 starta = addra - backward * PAGE_SIZE;
1537 if (starta < entry->start)
1538 starta = entry->start;
1542 * Generate the sequence of virtual addresses that are candidates for
1543 * prefaulting in an outward spiral from the faulting virtual address,
1544 * "addra". Specifically, the sequence is "addra - PAGE_SIZE", "addra
1545 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
1546 * If the candidate address doesn't have a backing physical page, then
1547 * the loop immediately terminates.
1549 for (i = 0; i < 2 * imax(backward, forward); i++) {
1550 addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
1552 if (addr > addra + forward * PAGE_SIZE)
1555 if (addr < starta || addr >= entry->end)
1558 if (!pmap_is_prefaultable(pmap, addr))
1561 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1562 lobject = entry->object.vm_object;
1564 VM_OBJECT_RLOCK(lobject);
1565 while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1566 lobject->type == OBJT_DEFAULT &&
1567 (backing_object = lobject->backing_object) != NULL) {
1568 KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
1569 0, ("vm_fault_prefault: unaligned object offset"));
1570 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1571 VM_OBJECT_RLOCK(backing_object);
1572 if (!obj_locked || lobject != entry->object.vm_object)
1573 VM_OBJECT_RUNLOCK(lobject);
1574 lobject = backing_object;
1577 if (!obj_locked || lobject != entry->object.vm_object)
1578 VM_OBJECT_RUNLOCK(lobject);
1581 if (vm_page_all_valid(m) &&
1582 (m->flags & PG_FICTITIOUS) == 0)
1583 pmap_enter_quick(pmap, addr, m, entry->protection);
1584 if (!obj_locked || lobject != entry->object.vm_object)
1585 VM_OBJECT_RUNLOCK(lobject);
1590 * Hold each of the physical pages that are mapped by the specified range of
1591 * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
1592 * and allow the specified types of access, "prot". If all of the implied
1593 * pages are successfully held, then the number of held pages is returned
1594 * together with pointers to those pages in the array "ma". However, if any
1595 * of the pages cannot be held, -1 is returned.
1598 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
1599 vm_prot_t prot, vm_page_t *ma, int max_count)
1601 vm_offset_t end, va;
1604 boolean_t pmap_failed;
1608 end = round_page(addr + len);
1609 addr = trunc_page(addr);
1612 * Check for illegal addresses.
1614 if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
1617 if (atop(end - addr) > max_count)
1618 panic("vm_fault_quick_hold_pages: count > max_count");
1619 count = atop(end - addr);
1622 * Most likely, the physical pages are resident in the pmap, so it is
1623 * faster to try pmap_extract_and_hold() first.
1625 pmap_failed = FALSE;
1626 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
1627 *mp = pmap_extract_and_hold(map->pmap, va, prot);
1630 else if ((prot & VM_PROT_WRITE) != 0 &&
1631 (*mp)->dirty != VM_PAGE_BITS_ALL) {
1633 * Explicitly dirty the physical page. Otherwise, the
1634 * caller's changes may go unnoticed because they are
1635 * performed through an unmanaged mapping or by a DMA
1638 * The object lock is not held here.
1639 * See vm_page_clear_dirty_mask().
1646 * One or more pages could not be held by the pmap. Either no
1647 * page was mapped at the specified virtual address or that
1648 * mapping had insufficient permissions. Attempt to fault in
1649 * and hold these pages.
1651 * If vm_fault_disable_pagefaults() was called,
1652 * i.e., TDP_NOFAULTING is set, we must not sleep nor
1653 * acquire MD VM locks, which means we must not call
1654 * vm_fault(). Some (out of tree) callers mark
1655 * too wide a code area with vm_fault_disable_pagefaults()
1656 * already, use the VM_PROT_QUICK_NOFAULT flag to request
1657 * the proper behaviour explicitly.
1659 if ((prot & VM_PROT_QUICK_NOFAULT) != 0 &&
1660 (curthread->td_pflags & TDP_NOFAULTING) != 0)
1662 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1663 if (*mp == NULL && vm_fault(map, va, prot,
1664 VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
1669 for (mp = ma; mp < ma + count; mp++)
1671 vm_page_unwire(*mp, PQ_INACTIVE);
1677 * vm_fault_copy_entry
1679 * Create new shadow object backing dst_entry with private copy of
1680 * all underlying pages. When src_entry is equal to dst_entry,
1681 * function implements COW for wired-down map entry. Otherwise,
1682 * it forks wired entry into dst_map.
1684 * In/out conditions:
1685 * The source and destination maps must be locked for write.
1686 * The source map entry must be wired down (or be a sharing map
1687 * entry corresponding to a main map entry that is wired down).
1690 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1691 vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1692 vm_ooffset_t *fork_charge)
1694 vm_object_t backing_object, dst_object, object, src_object;
1695 vm_pindex_t dst_pindex, pindex, src_pindex;
1696 vm_prot_t access, prot;
1706 upgrade = src_entry == dst_entry;
1707 access = prot = dst_entry->protection;
1709 src_object = src_entry->object.vm_object;
1710 src_pindex = OFF_TO_IDX(src_entry->offset);
1712 if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
1713 dst_object = src_object;
1714 vm_object_reference(dst_object);
1717 * Create the top-level object for the destination entry.
1718 * Doesn't actually shadow anything - we copy the pages
1721 dst_object = vm_object_allocate_anon(atop(dst_entry->end -
1722 dst_entry->start), NULL, NULL, 0);
1723 #if VM_NRESERVLEVEL > 0
1724 dst_object->flags |= OBJ_COLORED;
1725 dst_object->pg_color = atop(dst_entry->start);
1727 dst_object->domain = src_object->domain;
1728 dst_object->charge = dst_entry->end - dst_entry->start;
1731 VM_OBJECT_WLOCK(dst_object);
1732 KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1733 ("vm_fault_copy_entry: vm_object not NULL"));
1734 if (src_object != dst_object) {
1735 dst_entry->object.vm_object = dst_object;
1736 dst_entry->offset = 0;
1737 dst_entry->eflags &= ~MAP_ENTRY_VN_EXEC;
1739 if (fork_charge != NULL) {
1740 KASSERT(dst_entry->cred == NULL,
1741 ("vm_fault_copy_entry: leaked swp charge"));
1742 dst_object->cred = curthread->td_ucred;
1743 crhold(dst_object->cred);
1744 *fork_charge += dst_object->charge;
1745 } else if ((dst_object->type == OBJT_DEFAULT ||
1746 dst_object->type == OBJT_SWAP) &&
1747 dst_object->cred == NULL) {
1748 KASSERT(dst_entry->cred != NULL, ("no cred for entry %p",
1750 dst_object->cred = dst_entry->cred;
1751 dst_entry->cred = NULL;
1755 * If not an upgrade, then enter the mappings in the pmap as
1756 * read and/or execute accesses. Otherwise, enter them as
1759 * A writeable large page mapping is only created if all of
1760 * the constituent small page mappings are modified. Marking
1761 * PTEs as modified on inception allows promotion to happen
1762 * without taking potentially large number of soft faults.
1765 access &= ~VM_PROT_WRITE;
1768 * Loop through all of the virtual pages within the entry's
1769 * range, copying each page from the source object to the
1770 * destination object. Since the source is wired, those pages
1771 * must exist. In contrast, the destination is pageable.
1772 * Since the destination object doesn't share any backing storage
1773 * with the source object, all of its pages must be dirtied,
1774 * regardless of whether they can be written.
1776 for (vaddr = dst_entry->start, dst_pindex = 0;
1777 vaddr < dst_entry->end;
1778 vaddr += PAGE_SIZE, dst_pindex++) {
1781 * Find the page in the source object, and copy it in.
1782 * Because the source is wired down, the page will be
1785 if (src_object != dst_object)
1786 VM_OBJECT_RLOCK(src_object);
1787 object = src_object;
1788 pindex = src_pindex + dst_pindex;
1789 while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1790 (backing_object = object->backing_object) != NULL) {
1792 * Unless the source mapping is read-only or
1793 * it is presently being upgraded from
1794 * read-only, the first object in the shadow
1795 * chain should provide all of the pages. In
1796 * other words, this loop body should never be
1797 * executed when the source mapping is already
1800 KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 ||
1802 ("vm_fault_copy_entry: main object missing page"));
1804 VM_OBJECT_RLOCK(backing_object);
1805 pindex += OFF_TO_IDX(object->backing_object_offset);
1806 if (object != dst_object)
1807 VM_OBJECT_RUNLOCK(object);
1808 object = backing_object;
1810 KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing"));
1812 if (object != dst_object) {
1814 * Allocate a page in the destination object.
1816 dst_m = vm_page_alloc(dst_object, (src_object ==
1817 dst_object ? src_pindex : 0) + dst_pindex,
1819 if (dst_m == NULL) {
1820 VM_OBJECT_WUNLOCK(dst_object);
1821 VM_OBJECT_RUNLOCK(object);
1822 vm_wait(dst_object);
1823 VM_OBJECT_WLOCK(dst_object);
1826 pmap_copy_page(src_m, dst_m);
1827 VM_OBJECT_RUNLOCK(object);
1828 dst_m->dirty = dst_m->valid = src_m->valid;
1831 if (vm_page_busy_acquire(dst_m, VM_ALLOC_WAITFAIL) == 0)
1833 if (dst_m->pindex >= dst_object->size) {
1835 * We are upgrading. Index can occur
1836 * out of bounds if the object type is
1837 * vnode and the file was truncated.
1839 vm_page_xunbusy(dst_m);
1843 VM_OBJECT_WUNLOCK(dst_object);
1846 * Enter it in the pmap. If a wired, copy-on-write
1847 * mapping is being replaced by a write-enabled
1848 * mapping, then wire that new mapping.
1850 * The page can be invalid if the user called
1851 * msync(MS_INVALIDATE) or truncated the backing vnode
1852 * or shared memory object. In this case, do not
1853 * insert it into pmap, but still do the copy so that
1854 * all copies of the wired map entry have similar
1857 if (vm_page_all_valid(dst_m)) {
1858 pmap_enter(dst_map->pmap, vaddr, dst_m, prot,
1859 access | (upgrade ? PMAP_ENTER_WIRED : 0), 0);
1863 * Mark it no longer busy, and put it on the active list.
1865 VM_OBJECT_WLOCK(dst_object);
1868 if (src_m != dst_m) {
1869 vm_page_unwire(src_m, PQ_INACTIVE);
1870 vm_page_wire(dst_m);
1872 KASSERT(vm_page_wired(dst_m),
1873 ("dst_m %p is not wired", dst_m));
1876 vm_page_activate(dst_m);
1878 vm_page_xunbusy(dst_m);
1880 VM_OBJECT_WUNLOCK(dst_object);
1882 dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1883 vm_object_deallocate(src_object);
1888 * Block entry into the machine-independent layer's page fault handler by
1889 * the calling thread. Subsequent calls to vm_fault() by that thread will
1890 * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of
1891 * spurious page faults.
1894 vm_fault_disable_pagefaults(void)
1897 return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
1901 vm_fault_enable_pagefaults(int save)
1904 curthread_pflags_restore(save);