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)
119 #define VM_FAULT_DONTNEED_MIN 1048576
122 /* Fault parameters. */
125 vm_prot_t fault_type;
131 /* Page reference for cow. */
134 /* Current object. */
139 /* Top-level map object. */
140 vm_object_t first_object;
141 vm_pindex_t first_pindex;
146 vm_map_entry_t entry;
148 bool lookup_still_valid;
150 /* Vnode if locked. */
154 static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr,
156 static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
157 int backward, int forward, bool obj_locked);
159 static int vm_pfault_oom_attempts = 3;
160 SYSCTL_INT(_vm, OID_AUTO, pfault_oom_attempts, CTLFLAG_RWTUN,
161 &vm_pfault_oom_attempts, 0,
162 "Number of page allocation attempts in page fault handler before it "
163 "triggers OOM handling");
165 static int vm_pfault_oom_wait = 10;
166 SYSCTL_INT(_vm, OID_AUTO, pfault_oom_wait, CTLFLAG_RWTUN,
167 &vm_pfault_oom_wait, 0,
168 "Number of seconds to wait for free pages before retrying "
169 "the page fault handler");
172 fault_page_release(vm_page_t *mp)
179 * We are likely to loop around again and attempt to busy
180 * this page. Deactivating it leaves it available for
181 * pageout while optimizing fault restarts.
183 vm_page_deactivate(m);
190 fault_page_free(vm_page_t *mp)
196 VM_OBJECT_ASSERT_WLOCKED(m->object);
197 if (!vm_page_wired(m))
206 unlock_map(struct faultstate *fs)
209 if (fs->lookup_still_valid) {
210 vm_map_lookup_done(fs->map, fs->entry);
211 fs->lookup_still_valid = false;
216 unlock_vp(struct faultstate *fs)
219 if (fs->vp != NULL) {
226 fault_deallocate(struct faultstate *fs)
229 fault_page_release(&fs->m_cow);
230 fault_page_release(&fs->m);
231 vm_object_pip_wakeup(fs->object);
232 if (fs->object != fs->first_object) {
233 VM_OBJECT_WLOCK(fs->first_object);
234 fault_page_free(&fs->first_m);
235 VM_OBJECT_WUNLOCK(fs->first_object);
236 vm_object_pip_wakeup(fs->first_object);
238 vm_object_deallocate(fs->first_object);
244 unlock_and_deallocate(struct faultstate *fs)
247 VM_OBJECT_WUNLOCK(fs->object);
248 fault_deallocate(fs);
252 vm_fault_dirty(struct faultstate *fs, vm_page_t m)
256 if (((fs->prot & VM_PROT_WRITE) == 0 &&
257 (fs->fault_flags & VM_FAULT_DIRTY) == 0) ||
258 (m->oflags & VPO_UNMANAGED) != 0)
261 VM_PAGE_OBJECT_BUSY_ASSERT(m);
263 need_dirty = ((fs->fault_type & VM_PROT_WRITE) != 0 &&
264 (fs->fault_flags & VM_FAULT_WIRE) == 0) ||
265 (fs->fault_flags & VM_FAULT_DIRTY) != 0;
267 vm_object_set_writeable_dirty(m->object);
270 * If the fault is a write, we know that this page is being
271 * written NOW so dirty it explicitly to save on
272 * pmap_is_modified() calls later.
274 * Also, since the page is now dirty, we can possibly tell
275 * the pager to release any swap backing the page.
277 if (need_dirty && vm_page_set_dirty(m) == 0) {
279 * If this is a NOSYNC mmap we do not want to set PGA_NOSYNC
280 * if the page is already dirty to prevent data written with
281 * the expectation of being synced from not being synced.
282 * Likewise if this entry does not request NOSYNC then make
283 * sure the page isn't marked NOSYNC. Applications sharing
284 * data should use the same flags to avoid ping ponging.
286 if ((fs->entry->eflags & MAP_ENTRY_NOSYNC) != 0)
287 vm_page_aflag_set(m, PGA_NOSYNC);
289 vm_page_aflag_clear(m, PGA_NOSYNC);
295 * Unlocks fs.first_object and fs.map on success.
298 vm_fault_soft_fast(struct faultstate *fs)
301 #if VM_NRESERVLEVEL > 0
308 MPASS(fs->vp == NULL);
310 vm_object_busy(fs->first_object);
311 m = vm_page_lookup(fs->first_object, fs->first_pindex);
312 /* A busy page can be mapped for read|execute access. */
313 if (m == NULL || ((fs->prot & VM_PROT_WRITE) != 0 &&
314 vm_page_busied(m)) || !vm_page_all_valid(m)) {
320 #if VM_NRESERVLEVEL > 0
321 if ((m->flags & PG_FICTITIOUS) == 0 &&
322 (m_super = vm_reserv_to_superpage(m)) != NULL &&
323 rounddown2(vaddr, pagesizes[m_super->psind]) >= fs->entry->start &&
324 roundup2(vaddr + 1, pagesizes[m_super->psind]) <= fs->entry->end &&
325 (vaddr & (pagesizes[m_super->psind] - 1)) == (VM_PAGE_TO_PHYS(m) &
326 (pagesizes[m_super->psind] - 1)) && !fs->wired &&
327 pmap_ps_enabled(fs->map->pmap)) {
328 flags = PS_ALL_VALID;
329 if ((fs->prot & VM_PROT_WRITE) != 0) {
331 * Create a superpage mapping allowing write access
332 * only if none of the constituent pages are busy and
333 * all of them are already dirty (except possibly for
334 * the page that was faulted on).
336 flags |= PS_NONE_BUSY;
337 if ((fs->first_object->flags & OBJ_UNMANAGED) == 0)
338 flags |= PS_ALL_DIRTY;
340 if (vm_page_ps_test(m_super, flags, m)) {
342 psind = m_super->psind;
343 vaddr = rounddown2(vaddr, pagesizes[psind]);
344 /* Preset the modified bit for dirty superpages. */
345 if ((flags & PS_ALL_DIRTY) != 0)
346 fs->fault_type |= VM_PROT_WRITE;
350 rv = pmap_enter(fs->map->pmap, vaddr, m_map, fs->prot, fs->fault_type |
351 PMAP_ENTER_NOSLEEP | (fs->wired ? PMAP_ENTER_WIRED : 0), psind);
352 if (rv != KERN_SUCCESS)
354 if (fs->m_hold != NULL) {
358 if (psind == 0 && !fs->wired)
359 vm_fault_prefault(fs, vaddr, PFBAK, PFFOR, true);
360 VM_OBJECT_RUNLOCK(fs->first_object);
361 vm_fault_dirty(fs, m);
362 vm_map_lookup_done(fs->map, fs->entry);
363 curthread->td_ru.ru_minflt++;
366 vm_object_unbusy(fs->first_object);
371 vm_fault_restore_map_lock(struct faultstate *fs)
374 VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
375 MPASS(blockcount_read(&fs->first_object->paging_in_progress) > 0);
377 if (!vm_map_trylock_read(fs->map)) {
378 VM_OBJECT_WUNLOCK(fs->first_object);
379 vm_map_lock_read(fs->map);
380 VM_OBJECT_WLOCK(fs->first_object);
382 fs->lookup_still_valid = true;
386 vm_fault_populate_check_page(vm_page_t m)
390 * Check each page to ensure that the pager is obeying the
391 * interface: the page must be installed in the object, fully
392 * valid, and exclusively busied.
395 MPASS(vm_page_all_valid(m));
396 MPASS(vm_page_xbusied(m));
400 vm_fault_populate_cleanup(vm_object_t object, vm_pindex_t first,
406 VM_OBJECT_ASSERT_WLOCKED(object);
407 MPASS(first <= last);
408 for (pidx = first, m = vm_page_lookup(object, pidx);
409 pidx <= last; pidx++, m = vm_page_next(m)) {
410 vm_fault_populate_check_page(m);
411 vm_page_deactivate(m);
417 vm_fault_populate(struct faultstate *fs)
421 vm_pindex_t map_first, map_last, pager_first, pager_last, pidx;
422 int bdry_idx, i, npages, psind, rv;
424 MPASS(fs->object == fs->first_object);
425 VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
426 MPASS(blockcount_read(&fs->first_object->paging_in_progress) > 0);
427 MPASS(fs->first_object->backing_object == NULL);
428 MPASS(fs->lookup_still_valid);
430 pager_first = OFF_TO_IDX(fs->entry->offset);
431 pager_last = pager_first + atop(fs->entry->end - fs->entry->start) - 1;
436 * Call the pager (driver) populate() method.
438 * There is no guarantee that the method will be called again
439 * if the current fault is for read, and a future fault is
440 * for write. Report the entry's maximum allowed protection
443 rv = vm_pager_populate(fs->first_object, fs->first_pindex,
444 fs->fault_type, fs->entry->max_protection, &pager_first,
447 VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
448 if (rv == VM_PAGER_BAD) {
450 * VM_PAGER_BAD is the backdoor for a pager to request
451 * normal fault handling.
453 vm_fault_restore_map_lock(fs);
454 if (fs->map->timestamp != fs->map_generation)
455 return (KERN_RESTART);
456 return (KERN_NOT_RECEIVER);
458 if (rv != VM_PAGER_OK)
459 return (KERN_FAILURE); /* AKA SIGSEGV */
461 /* Ensure that the driver is obeying the interface. */
462 MPASS(pager_first <= pager_last);
463 MPASS(fs->first_pindex <= pager_last);
464 MPASS(fs->first_pindex >= pager_first);
465 MPASS(pager_last < fs->first_object->size);
467 vm_fault_restore_map_lock(fs);
468 bdry_idx = (fs->entry->eflags & MAP_ENTRY_SPLIT_BOUNDARY_MASK) >>
469 MAP_ENTRY_SPLIT_BOUNDARY_SHIFT;
470 if (fs->map->timestamp != fs->map_generation) {
472 vm_fault_populate_cleanup(fs->first_object, pager_first,
475 m = vm_page_lookup(fs->first_object, pager_first);
479 return (KERN_RESTART);
483 * The map is unchanged after our last unlock. Process the fault.
485 * First, the special case of largepage mappings, where
486 * populate only busies the first page in superpage run.
489 KASSERT(PMAP_HAS_LARGEPAGES,
490 ("missing pmap support for large pages"));
491 m = vm_page_lookup(fs->first_object, pager_first);
492 vm_fault_populate_check_page(m);
493 VM_OBJECT_WUNLOCK(fs->first_object);
494 vaddr = fs->entry->start + IDX_TO_OFF(pager_first) -
496 /* assert alignment for entry */
497 KASSERT((vaddr & (pagesizes[bdry_idx] - 1)) == 0,
498 ("unaligned superpage start %#jx pager_first %#jx offset %#jx vaddr %#jx",
499 (uintmax_t)fs->entry->start, (uintmax_t)pager_first,
500 (uintmax_t)fs->entry->offset, (uintmax_t)vaddr));
501 KASSERT((VM_PAGE_TO_PHYS(m) & (pagesizes[bdry_idx] - 1)) == 0,
502 ("unaligned superpage m %p %#jx", m,
503 (uintmax_t)VM_PAGE_TO_PHYS(m)));
504 rv = pmap_enter(fs->map->pmap, vaddr, m, fs->prot,
505 fs->fault_type | (fs->wired ? PMAP_ENTER_WIRED : 0) |
506 PMAP_ENTER_LARGEPAGE, bdry_idx);
507 VM_OBJECT_WLOCK(fs->first_object);
509 if (rv != KERN_SUCCESS)
511 if ((fs->fault_flags & VM_FAULT_WIRE) != 0) {
512 for (i = 0; i < atop(pagesizes[bdry_idx]); i++)
515 if (fs->m_hold != NULL) {
516 *fs->m_hold = m + (fs->first_pindex - pager_first);
517 vm_page_wire(*fs->m_hold);
523 * The range [pager_first, pager_last] that is given to the
524 * pager is only a hint. The pager may populate any range
525 * within the object that includes the requested page index.
526 * In case the pager expanded the range, clip it to fit into
529 map_first = OFF_TO_IDX(fs->entry->offset);
530 if (map_first > pager_first) {
531 vm_fault_populate_cleanup(fs->first_object, pager_first,
533 pager_first = map_first;
535 map_last = map_first + atop(fs->entry->end - fs->entry->start) - 1;
536 if (map_last < pager_last) {
537 vm_fault_populate_cleanup(fs->first_object, map_last + 1,
539 pager_last = map_last;
541 for (pidx = pager_first, m = vm_page_lookup(fs->first_object, pidx);
543 pidx += npages, m = vm_page_next(&m[npages - 1])) {
544 vaddr = fs->entry->start + IDX_TO_OFF(pidx) - fs->entry->offset;
547 if (psind > 0 && ((vaddr & (pagesizes[psind] - 1)) != 0 ||
548 pidx + OFF_TO_IDX(pagesizes[psind]) - 1 > pager_last ||
549 !pmap_ps_enabled(fs->map->pmap) || fs->wired))
552 npages = atop(pagesizes[psind]);
553 for (i = 0; i < npages; i++) {
554 vm_fault_populate_check_page(&m[i]);
555 vm_fault_dirty(fs, &m[i]);
557 VM_OBJECT_WUNLOCK(fs->first_object);
558 rv = pmap_enter(fs->map->pmap, vaddr, m, fs->prot, fs->fault_type |
559 (fs->wired ? PMAP_ENTER_WIRED : 0), psind);
562 * pmap_enter() may fail for a superpage mapping if additional
563 * protection policies prevent the full mapping.
564 * For example, this will happen on amd64 if the entire
565 * address range does not share the same userspace protection
566 * key. Revert to single-page mappings if this happens.
568 MPASS(rv == KERN_SUCCESS ||
569 (psind > 0 && rv == KERN_PROTECTION_FAILURE));
570 if (__predict_false(psind > 0 &&
571 rv == KERN_PROTECTION_FAILURE)) {
572 for (i = 0; i < npages; i++) {
573 rv = pmap_enter(fs->map->pmap, vaddr + ptoa(i),
574 &m[i], fs->prot, fs->fault_type |
575 (fs->wired ? PMAP_ENTER_WIRED : 0), 0);
576 MPASS(rv == KERN_SUCCESS);
580 VM_OBJECT_WLOCK(fs->first_object);
581 for (i = 0; i < npages; i++) {
582 if ((fs->fault_flags & VM_FAULT_WIRE) != 0)
585 vm_page_activate(&m[i]);
586 if (fs->m_hold != NULL && m[i].pindex == fs->first_pindex) {
587 (*fs->m_hold) = &m[i];
590 vm_page_xunbusy(&m[i]);
594 curthread->td_ru.ru_majflt++;
598 static int prot_fault_translation;
599 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RWTUN,
600 &prot_fault_translation, 0,
601 "Control signal to deliver on protection fault");
603 /* compat definition to keep common code for signal translation */
604 #define UCODE_PAGEFLT 12
606 _Static_assert(UCODE_PAGEFLT == T_PAGEFLT, "T_PAGEFLT");
612 * Handle a page fault occurring at the given address,
613 * requiring the given permissions, in the map specified.
614 * If successful, the page is inserted into the
615 * associated physical map.
617 * NOTE: the given address should be truncated to the
618 * proper page address.
620 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
621 * a standard error specifying why the fault is fatal is returned.
623 * The map in question must be referenced, and remains so.
624 * Caller may hold no locks.
627 vm_fault_trap(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
628 int fault_flags, int *signo, int *ucode)
632 MPASS(signo == NULL || ucode != NULL);
634 if (map != kernel_map && KTRPOINT(curthread, KTR_FAULT))
635 ktrfault(vaddr, fault_type);
637 result = vm_fault(map, trunc_page(vaddr), fault_type, fault_flags,
639 KASSERT(result == KERN_SUCCESS || result == KERN_FAILURE ||
640 result == KERN_INVALID_ADDRESS ||
641 result == KERN_RESOURCE_SHORTAGE ||
642 result == KERN_PROTECTION_FAILURE ||
643 result == KERN_OUT_OF_BOUNDS,
644 ("Unexpected Mach error %d from vm_fault()", result));
646 if (map != kernel_map && KTRPOINT(curthread, KTR_FAULTEND))
649 if (result != KERN_SUCCESS && signo != NULL) {
652 case KERN_INVALID_ADDRESS:
654 *ucode = SEGV_MAPERR;
656 case KERN_RESOURCE_SHORTAGE:
660 case KERN_OUT_OF_BOUNDS:
664 case KERN_PROTECTION_FAILURE:
665 if (prot_fault_translation == 0) {
667 * Autodetect. This check also covers
668 * the images without the ABI-tag ELF
671 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD &&
672 curproc->p_osrel >= P_OSREL_SIGSEGV) {
674 *ucode = SEGV_ACCERR;
677 *ucode = UCODE_PAGEFLT;
679 } else if (prot_fault_translation == 1) {
680 /* Always compat mode. */
682 *ucode = UCODE_PAGEFLT;
684 /* Always SIGSEGV mode. */
686 *ucode = SEGV_ACCERR;
690 KASSERT(0, ("Unexpected Mach error %d from vm_fault()",
699 vm_fault_lock_vnode(struct faultstate *fs, bool objlocked)
704 if (fs->object->type != OBJT_VNODE)
705 return (KERN_SUCCESS);
706 vp = fs->object->handle;
708 ASSERT_VOP_LOCKED(vp, "saved vnode is not locked");
709 return (KERN_SUCCESS);
713 * Perform an unlock in case the desired vnode changed while
714 * the map was unlocked during a retry.
718 locked = VOP_ISLOCKED(vp);
719 if (locked != LK_EXCLUSIVE)
723 * We must not sleep acquiring the vnode lock while we have
724 * the page exclusive busied or the object's
725 * paging-in-progress count incremented. Otherwise, we could
728 error = vget(vp, locked | LK_CANRECURSE | LK_NOWAIT);
731 return (KERN_SUCCESS);
736 unlock_and_deallocate(fs);
738 fault_deallocate(fs);
739 error = vget(vp, locked | LK_RETRY | LK_CANRECURSE);
742 KASSERT(error == 0, ("vm_fault: vget failed %d", error));
743 return (KERN_RESOURCE_SHORTAGE);
747 * Calculate the desired readahead. Handle drop-behind.
749 * Returns the number of readahead blocks to pass to the pager.
752 vm_fault_readahead(struct faultstate *fs)
757 KASSERT(fs->lookup_still_valid, ("map unlocked"));
758 era = fs->entry->read_ahead;
759 behavior = vm_map_entry_behavior(fs->entry);
760 if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
762 } else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
763 nera = VM_FAULT_READ_AHEAD_MAX;
764 if (fs->vaddr == fs->entry->next_read)
765 vm_fault_dontneed(fs, fs->vaddr, nera);
766 } else if (fs->vaddr == fs->entry->next_read) {
768 * This is a sequential fault. Arithmetically
769 * increase the requested number of pages in
770 * the read-ahead window. The requested
771 * number of pages is "# of sequential faults
772 * x (read ahead min + 1) + read ahead min"
774 nera = VM_FAULT_READ_AHEAD_MIN;
777 if (nera > VM_FAULT_READ_AHEAD_MAX)
778 nera = VM_FAULT_READ_AHEAD_MAX;
780 if (era == VM_FAULT_READ_AHEAD_MAX)
781 vm_fault_dontneed(fs, fs->vaddr, nera);
784 * This is a non-sequential fault.
790 * A read lock on the map suffices to update
791 * the read ahead count safely.
793 fs->entry->read_ahead = nera;
800 vm_fault_lookup(struct faultstate *fs)
804 KASSERT(!fs->lookup_still_valid,
805 ("vm_fault_lookup: Map already locked."));
806 result = vm_map_lookup(&fs->map, fs->vaddr, fs->fault_type |
807 VM_PROT_FAULT_LOOKUP, &fs->entry, &fs->first_object,
808 &fs->first_pindex, &fs->prot, &fs->wired);
809 if (result != KERN_SUCCESS) {
814 fs->map_generation = fs->map->timestamp;
816 if (fs->entry->eflags & MAP_ENTRY_NOFAULT) {
817 panic("%s: fault on nofault entry, addr: %#lx",
818 __func__, (u_long)fs->vaddr);
821 if (fs->entry->eflags & MAP_ENTRY_IN_TRANSITION &&
822 fs->entry->wiring_thread != curthread) {
823 vm_map_unlock_read(fs->map);
824 vm_map_lock(fs->map);
825 if (vm_map_lookup_entry(fs->map, fs->vaddr, &fs->entry) &&
826 (fs->entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
828 fs->entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
829 vm_map_unlock_and_wait(fs->map, 0);
831 vm_map_unlock(fs->map);
832 return (KERN_RESOURCE_SHORTAGE);
835 MPASS((fs->entry->eflags & MAP_ENTRY_GUARD) == 0);
838 fs->fault_type = fs->prot | (fs->fault_type & VM_PROT_COPY);
840 KASSERT((fs->fault_flags & VM_FAULT_WIRE) == 0,
841 ("!fs->wired && VM_FAULT_WIRE"));
842 fs->lookup_still_valid = true;
844 return (KERN_SUCCESS);
848 vm_fault_relookup(struct faultstate *fs)
850 vm_object_t retry_object;
851 vm_pindex_t retry_pindex;
852 vm_prot_t retry_prot;
855 if (!vm_map_trylock_read(fs->map))
856 return (KERN_RESTART);
858 fs->lookup_still_valid = true;
859 if (fs->map->timestamp == fs->map_generation)
860 return (KERN_SUCCESS);
862 result = vm_map_lookup_locked(&fs->map, fs->vaddr, fs->fault_type,
863 &fs->entry, &retry_object, &retry_pindex, &retry_prot,
865 if (result != KERN_SUCCESS) {
867 * If retry of map lookup would have blocked then
868 * retry fault from start.
870 if (result == KERN_FAILURE)
871 return (KERN_RESTART);
874 if (retry_object != fs->first_object ||
875 retry_pindex != fs->first_pindex)
876 return (KERN_RESTART);
879 * Check whether the protection has changed or the object has
880 * been copied while we left the map unlocked. Changing from
881 * read to write permission is OK - we leave the page
882 * write-protected, and catch the write fault. Changing from
883 * write to read permission means that we can't mark the page
884 * write-enabled after all.
886 fs->prot &= retry_prot;
887 fs->fault_type &= retry_prot;
889 return (KERN_RESTART);
891 /* Reassert because wired may have changed. */
892 KASSERT(fs->wired || (fs->fault_flags & VM_FAULT_WIRE) == 0,
893 ("!wired && VM_FAULT_WIRE"));
895 return (KERN_SUCCESS);
899 vm_fault_cow(struct faultstate *fs)
901 bool is_first_object_locked;
903 KASSERT(fs->object != fs->first_object,
904 ("source and target COW objects are identical"));
907 * This allows pages to be virtually copied from a backing_object
908 * into the first_object, where the backing object has no other
909 * refs to it, and cannot gain any more refs. Instead of a bcopy,
910 * we just move the page from the backing object to the first
911 * object. Note that we must mark the page dirty in the first
912 * object so that it will go out to swap when needed.
914 is_first_object_locked = false;
917 * Only one shadow object and no other refs.
919 fs->object->shadow_count == 1 && fs->object->ref_count == 1 &&
921 * No other ways to look the object up
923 fs->object->handle == NULL && (fs->object->flags & OBJ_ANON) != 0 &&
925 * We don't chase down the shadow chain and we can acquire locks.
927 (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs->first_object)) &&
928 fs->object == fs->first_object->backing_object &&
929 VM_OBJECT_TRYWLOCK(fs->object)) {
931 * Remove but keep xbusy for replace. fs->m is moved into
932 * fs->first_object and left busy while fs->first_m is
933 * conditionally freed.
935 vm_page_remove_xbusy(fs->m);
936 vm_page_replace(fs->m, fs->first_object, fs->first_pindex,
938 vm_page_dirty(fs->m);
939 #if VM_NRESERVLEVEL > 0
941 * Rename the reservation.
943 vm_reserv_rename(fs->m, fs->first_object, fs->object,
944 OFF_TO_IDX(fs->first_object->backing_object_offset));
946 VM_OBJECT_WUNLOCK(fs->object);
947 VM_OBJECT_WUNLOCK(fs->first_object);
950 VM_CNT_INC(v_cow_optim);
952 if (is_first_object_locked)
953 VM_OBJECT_WUNLOCK(fs->first_object);
955 * Oh, well, lets copy it.
957 pmap_copy_page(fs->m, fs->first_m);
958 vm_page_valid(fs->first_m);
959 if (fs->wired && (fs->fault_flags & VM_FAULT_WIRE) == 0) {
960 vm_page_wire(fs->first_m);
961 vm_page_unwire(fs->m, PQ_INACTIVE);
964 * Save the cow page to be released after
965 * pmap_enter is complete.
971 * Typically, the shadow object is either private to this
972 * address space (OBJ_ONEMAPPING) or its pages are read only.
973 * In the highly unusual case where the pages of a shadow object
974 * are read/write shared between this and other address spaces,
975 * we need to ensure that any pmap-level mappings to the
976 * original, copy-on-write page from the backing object are
977 * removed from those other address spaces.
979 * The flag check is racy, but this is tolerable: if
980 * OBJ_ONEMAPPING is cleared after the check, the busy state
981 * ensures that new mappings of m_cow can't be created.
982 * pmap_enter() will replace an existing mapping in the current
983 * address space. If OBJ_ONEMAPPING is set after the check,
984 * removing mappings will at worse trigger some unnecessary page
987 vm_page_assert_xbusied(fs->m_cow);
988 if ((fs->first_object->flags & OBJ_ONEMAPPING) == 0)
989 pmap_remove_all(fs->m_cow);
992 vm_object_pip_wakeup(fs->object);
995 * Only use the new page below...
997 fs->object = fs->first_object;
998 fs->pindex = fs->first_pindex;
1000 VM_CNT_INC(v_cow_faults);
1001 curthread->td_cow++;
1005 vm_fault_next(struct faultstate *fs)
1007 vm_object_t next_object;
1010 * The requested page does not exist at this object/
1011 * offset. Remove the invalid page from the object,
1012 * waking up anyone waiting for it, and continue on to
1013 * the next object. However, if this is the top-level
1014 * object, we must leave the busy page in place to
1015 * prevent another process from rushing past us, and
1016 * inserting the page in that object at the same time
1019 if (fs->object == fs->first_object) {
1020 fs->first_m = fs->m;
1023 fault_page_free(&fs->m);
1026 * Move on to the next object. Lock the next object before
1027 * unlocking the current one.
1029 VM_OBJECT_ASSERT_WLOCKED(fs->object);
1030 next_object = fs->object->backing_object;
1031 if (next_object == NULL)
1033 MPASS(fs->first_m != NULL);
1034 KASSERT(fs->object != next_object, ("object loop %p", next_object));
1035 VM_OBJECT_WLOCK(next_object);
1036 vm_object_pip_add(next_object, 1);
1037 if (fs->object != fs->first_object)
1038 vm_object_pip_wakeup(fs->object);
1039 fs->pindex += OFF_TO_IDX(fs->object->backing_object_offset);
1040 VM_OBJECT_WUNLOCK(fs->object);
1041 fs->object = next_object;
1047 vm_fault_zerofill(struct faultstate *fs)
1051 * If there's no object left, fill the page in the top
1052 * object with zeros.
1054 if (fs->object != fs->first_object) {
1055 vm_object_pip_wakeup(fs->object);
1056 fs->object = fs->first_object;
1057 fs->pindex = fs->first_pindex;
1059 MPASS(fs->first_m != NULL);
1060 MPASS(fs->m == NULL);
1061 fs->m = fs->first_m;
1065 * Zero the page if necessary and mark it valid.
1067 if ((fs->m->flags & PG_ZERO) == 0) {
1068 pmap_zero_page(fs->m);
1070 VM_CNT_INC(v_ozfod);
1073 vm_page_valid(fs->m);
1077 * Allocate a page directly or via the object populate method.
1080 vm_fault_allocate(struct faultstate *fs)
1082 struct domainset *dset;
1086 if ((fs->object->flags & OBJ_SIZEVNLOCK) != 0) {
1087 rv = vm_fault_lock_vnode(fs, true);
1088 MPASS(rv == KERN_SUCCESS || rv == KERN_RESOURCE_SHORTAGE);
1089 if (rv == KERN_RESOURCE_SHORTAGE)
1093 if (fs->pindex >= fs->object->size)
1094 return (KERN_OUT_OF_BOUNDS);
1096 if (fs->object == fs->first_object &&
1097 (fs->first_object->flags & OBJ_POPULATE) != 0 &&
1098 fs->first_object->shadow_count == 0) {
1099 rv = vm_fault_populate(fs);
1103 case KERN_PROTECTION_FAILURE:
1106 case KERN_NOT_RECEIVER:
1108 * Pager's populate() method
1109 * returned VM_PAGER_BAD.
1113 panic("inconsistent return codes");
1118 * Allocate a new page for this object/offset pair.
1120 * Unlocked read of the p_flag is harmless. At worst, the P_KILLED
1121 * might be not observed there, and allocation can fail, causing
1122 * restart and new reading of the p_flag.
1124 dset = fs->object->domain.dr_policy;
1126 dset = curthread->td_domain.dr_policy;
1127 if (!vm_page_count_severe_set(&dset->ds_mask) || P_KILLED(curproc)) {
1128 #if VM_NRESERVLEVEL > 0
1129 vm_object_color(fs->object, atop(fs->vaddr) - fs->pindex);
1131 alloc_req = P_KILLED(curproc) ?
1132 VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
1133 if (fs->object->type != OBJT_VNODE &&
1134 fs->object->backing_object == NULL)
1135 alloc_req |= VM_ALLOC_ZERO;
1136 fs->m = vm_page_alloc(fs->object, fs->pindex, alloc_req);
1138 if (fs->m == NULL) {
1139 unlock_and_deallocate(fs);
1140 if (vm_pfault_oom_attempts < 0 ||
1141 fs->oom < vm_pfault_oom_attempts) {
1143 vm_waitpfault(dset, vm_pfault_oom_wait * hz);
1147 "proc %d (%s) failed to alloc page on fault, starting OOM\n",
1148 curproc->p_pid, curproc->p_comm);
1149 vm_pageout_oom(VM_OOM_MEM_PF);
1152 return (KERN_RESOURCE_SHORTAGE);
1156 return (KERN_NOT_RECEIVER);
1160 * Call the pager to retrieve the page if there is a chance
1161 * that the pager has it, and potentially retrieve additional
1162 * pages at the same time.
1165 vm_fault_getpages(struct faultstate *fs, int nera, int *behindp, int *aheadp)
1167 vm_offset_t e_end, e_start;
1168 int ahead, behind, cluster_offset, rv;
1172 * Prepare for unlocking the map. Save the map
1173 * entry's start and end addresses, which are used to
1174 * optimize the size of the pager operation below.
1175 * Even if the map entry's addresses change after
1176 * unlocking the map, using the saved addresses is
1179 e_start = fs->entry->start;
1180 e_end = fs->entry->end;
1181 behavior = vm_map_entry_behavior(fs->entry);
1184 * Release the map lock before locking the vnode or
1185 * sleeping in the pager. (If the current object has
1186 * a shadow, then an earlier iteration of this loop
1187 * may have already unlocked the map.)
1191 rv = vm_fault_lock_vnode(fs, false);
1192 MPASS(rv == KERN_SUCCESS || rv == KERN_RESOURCE_SHORTAGE);
1193 if (rv == KERN_RESOURCE_SHORTAGE)
1195 KASSERT(fs->vp == NULL || !fs->map->system_map,
1196 ("vm_fault: vnode-backed object mapped by system map"));
1199 * Page in the requested page and hint the pager,
1200 * that it may bring up surrounding pages.
1202 if (nera == -1 || behavior == MAP_ENTRY_BEHAV_RANDOM ||
1203 P_KILLED(curproc)) {
1207 /* Is this a sequential fault? */
1213 * Request a cluster of pages that is
1214 * aligned to a VM_FAULT_READ_DEFAULT
1215 * page offset boundary within the
1216 * object. Alignment to a page offset
1217 * boundary is more likely to coincide
1218 * with the underlying file system
1219 * block than alignment to a virtual
1222 cluster_offset = fs->pindex % VM_FAULT_READ_DEFAULT;
1223 behind = ulmin(cluster_offset,
1224 atop(fs->vaddr - e_start));
1225 ahead = VM_FAULT_READ_DEFAULT - 1 - cluster_offset;
1227 ahead = ulmin(ahead, atop(e_end - fs->vaddr) - 1);
1231 rv = vm_pager_get_pages(fs->object, &fs->m, 1, behindp, aheadp);
1232 if (rv == VM_PAGER_OK)
1233 return (KERN_SUCCESS);
1234 if (rv == VM_PAGER_ERROR)
1235 printf("vm_fault: pager read error, pid %d (%s)\n",
1236 curproc->p_pid, curproc->p_comm);
1238 * If an I/O error occurred or the requested page was
1239 * outside the range of the pager, clean up and return
1242 if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD)
1243 return (KERN_OUT_OF_BOUNDS);
1244 return (KERN_NOT_RECEIVER);
1248 * Wait/Retry if the page is busy. We have to do this if the page is
1249 * either exclusive or shared busy because the vm_pager may be using
1250 * read busy for pageouts (and even pageins if it is the vnode pager),
1251 * and we could end up trying to pagein and pageout the same page
1254 * We can theoretically allow the busy case on a read fault if the page
1255 * is marked valid, but since such pages are typically already pmap'd,
1256 * putting that special case in might be more effort then it is worth.
1257 * We cannot under any circumstances mess around with a shared busied
1258 * page except, perhaps, to pmap it.
1261 vm_fault_busy_sleep(struct faultstate *fs)
1264 * Reference the page before unlocking and
1265 * sleeping so that the page daemon is less
1266 * likely to reclaim it.
1268 vm_page_aflag_set(fs->m, PGA_REFERENCED);
1269 if (fs->object != fs->first_object) {
1270 fault_page_release(&fs->first_m);
1271 vm_object_pip_wakeup(fs->first_object);
1273 vm_object_pip_wakeup(fs->object);
1275 if (fs->m == vm_page_lookup(fs->object, fs->pindex))
1276 vm_page_busy_sleep(fs->m, "vmpfw", false);
1278 VM_OBJECT_WUNLOCK(fs->object);
1279 VM_CNT_INC(v_intrans);
1280 vm_object_deallocate(fs->first_object);
1284 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
1285 int fault_flags, vm_page_t *m_hold)
1287 struct faultstate fs;
1288 int ahead, behind, faultcount;
1289 int nera, result, rv;
1290 bool dead, hardfault;
1292 VM_CNT_INC(v_vm_faults);
1294 if ((curthread->td_pflags & TDP_NOFAULTING) != 0)
1295 return (KERN_PROTECTION_FAILURE);
1300 fs.fault_flags = fault_flags;
1302 fs.lookup_still_valid = false;
1309 fs.fault_type = fault_type;
1312 * Find the backing store object and offset into it to begin the
1315 result = vm_fault_lookup(&fs);
1316 if (result != KERN_SUCCESS) {
1317 if (result == KERN_RESOURCE_SHORTAGE)
1323 * Try to avoid lock contention on the top-level object through
1324 * special-case handling of some types of page faults, specifically,
1325 * those that are mapping an existing page from the top-level object.
1326 * Under this condition, a read lock on the object suffices, allowing
1327 * multiple page faults of a similar type to run in parallel.
1329 if (fs.vp == NULL /* avoid locked vnode leak */ &&
1330 (fs.entry->eflags & MAP_ENTRY_SPLIT_BOUNDARY_MASK) == 0 &&
1331 (fs.fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0) {
1332 VM_OBJECT_RLOCK(fs.first_object);
1333 rv = vm_fault_soft_fast(&fs);
1334 if (rv == KERN_SUCCESS)
1336 if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) {
1337 VM_OBJECT_RUNLOCK(fs.first_object);
1338 VM_OBJECT_WLOCK(fs.first_object);
1341 VM_OBJECT_WLOCK(fs.first_object);
1345 * Make a reference to this object to prevent its disposal while we
1346 * are messing with it. Once we have the reference, the map is free
1347 * to be diddled. Since objects reference their shadows (and copies),
1348 * they will stay around as well.
1350 * Bump the paging-in-progress count to prevent size changes (e.g.
1351 * truncation operations) during I/O.
1353 vm_object_reference_locked(fs.first_object);
1354 vm_object_pip_add(fs.first_object, 1);
1356 fs.m_cow = fs.m = fs.first_m = NULL;
1359 * Search for the page at object/offset.
1361 fs.object = fs.first_object;
1362 fs.pindex = fs.first_pindex;
1364 if ((fs.entry->eflags & MAP_ENTRY_SPLIT_BOUNDARY_MASK) != 0) {
1365 rv = vm_fault_allocate(&fs);
1368 unlock_and_deallocate(&fs);
1370 case KERN_RESOURCE_SHORTAGE:
1374 case KERN_PROTECTION_FAILURE:
1375 case KERN_OUT_OF_BOUNDS:
1376 unlock_and_deallocate(&fs);
1378 case KERN_NOT_RECEIVER:
1381 panic("vm_fault: Unhandled rv %d", rv);
1386 KASSERT(fs.m == NULL,
1387 ("page still set %p at loop start", fs.m));
1389 * If the object is marked for imminent termination,
1390 * we retry here, since the collapse pass has raced
1391 * with us. Otherwise, if we see terminally dead
1392 * object, return fail.
1394 if ((fs.object->flags & OBJ_DEAD) != 0) {
1395 dead = fs.object->type == OBJT_DEAD;
1396 unlock_and_deallocate(&fs);
1398 return (KERN_PROTECTION_FAILURE);
1404 * See if page is resident
1406 fs.m = vm_page_lookup(fs.object, fs.pindex);
1408 if (vm_page_tryxbusy(fs.m) == 0) {
1409 vm_fault_busy_sleep(&fs);
1414 * The page is marked busy for other processes and the
1415 * pagedaemon. If it still is completely valid we
1418 if (vm_page_all_valid(fs.m)) {
1419 VM_OBJECT_WUNLOCK(fs.object);
1420 break; /* break to PAGE HAS BEEN FOUND. */
1423 VM_OBJECT_ASSERT_WLOCKED(fs.object);
1426 * Page is not resident. If the pager might contain the page
1427 * or this is the beginning of the search, allocate a new
1428 * page. (Default objects are zero-fill, so there is no real
1431 if (fs.m == NULL && (fs.object->type != OBJT_DEFAULT ||
1432 fs.object == fs.first_object)) {
1433 rv = vm_fault_allocate(&fs);
1436 unlock_and_deallocate(&fs);
1438 case KERN_RESOURCE_SHORTAGE:
1442 case KERN_PROTECTION_FAILURE:
1443 case KERN_OUT_OF_BOUNDS:
1444 unlock_and_deallocate(&fs);
1446 case KERN_NOT_RECEIVER:
1449 panic("vm_fault: Unhandled rv %d", rv);
1454 * Default objects have no pager so no exclusive busy exists
1455 * to protect this page in the chain. Skip to the next
1456 * object without dropping the lock to preserve atomicity of
1459 if (fs.object->type != OBJT_DEFAULT) {
1461 * At this point, we have either allocated a new page
1462 * or found an existing page that is only partially
1465 * We hold a reference on the current object and the
1466 * page is exclusive busied. The exclusive busy
1467 * prevents simultaneous faults and collapses while
1468 * the object lock is dropped.
1470 VM_OBJECT_WUNLOCK(fs.object);
1473 * If the pager for the current object might have
1474 * the page, then determine the number of additional
1475 * pages to read and potentially reprioritize
1476 * previously read pages for earlier reclamation.
1477 * These operations should only be performed once per
1478 * page fault. Even if the current pager doesn't
1479 * have the page, the number of additional pages to
1480 * read will apply to subsequent objects in the
1483 if (nera == -1 && !P_KILLED(curproc))
1484 nera = vm_fault_readahead(&fs);
1486 rv = vm_fault_getpages(&fs, nera, &behind, &ahead);
1487 if (rv == KERN_SUCCESS) {
1488 faultcount = behind + 1 + ahead;
1490 break; /* break to PAGE HAS BEEN FOUND. */
1492 if (rv == KERN_RESOURCE_SHORTAGE)
1494 VM_OBJECT_WLOCK(fs.object);
1495 if (rv == KERN_OUT_OF_BOUNDS) {
1496 fault_page_free(&fs.m);
1497 unlock_and_deallocate(&fs);
1503 * The page was not found in the current object. Try to
1504 * traverse into a backing object or zero fill if none is
1507 if (vm_fault_next(&fs))
1509 if ((fs.fault_flags & VM_FAULT_NOFILL) != 0) {
1510 if (fs.first_object == fs.object)
1511 fault_page_free(&fs.first_m);
1512 unlock_and_deallocate(&fs);
1513 return (KERN_OUT_OF_BOUNDS);
1515 VM_OBJECT_WUNLOCK(fs.object);
1516 vm_fault_zerofill(&fs);
1517 /* Don't try to prefault neighboring pages. */
1519 break; /* break to PAGE HAS BEEN FOUND. */
1523 * PAGE HAS BEEN FOUND. A valid page has been found and exclusively
1524 * busied. The object lock must no longer be held.
1526 vm_page_assert_xbusied(fs.m);
1527 VM_OBJECT_ASSERT_UNLOCKED(fs.object);
1530 * If the page is being written, but isn't already owned by the
1531 * top-level object, we have to copy it into a new page owned by the
1534 if (fs.object != fs.first_object) {
1536 * We only really need to copy if we want to write it.
1538 if ((fs.fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1541 * We only try to prefault read-only mappings to the
1542 * neighboring pages when this copy-on-write fault is
1543 * a hard fault. In other cases, trying to prefault
1544 * is typically wasted effort.
1546 if (faultcount == 0)
1550 fs.prot &= ~VM_PROT_WRITE;
1555 * We must verify that the maps have not changed since our last
1558 if (!fs.lookup_still_valid) {
1559 result = vm_fault_relookup(&fs);
1560 if (result != KERN_SUCCESS) {
1561 fault_deallocate(&fs);
1562 if (result == KERN_RESTART)
1567 VM_OBJECT_ASSERT_UNLOCKED(fs.object);
1570 * If the page was filled by a pager, save the virtual address that
1571 * should be faulted on next under a sequential access pattern to the
1572 * map entry. A read lock on the map suffices to update this address
1576 fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE;
1579 * Page must be completely valid or it is not fit to
1580 * map into user space. vm_pager_get_pages() ensures this.
1582 vm_page_assert_xbusied(fs.m);
1583 KASSERT(vm_page_all_valid(fs.m),
1584 ("vm_fault: page %p partially invalid", fs.m));
1586 vm_fault_dirty(&fs, fs.m);
1589 * Put this page into the physical map. We had to do the unlock above
1590 * because pmap_enter() may sleep. We don't put the page
1591 * back on the active queue until later so that the pageout daemon
1592 * won't find it (yet).
1594 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot,
1595 fs.fault_type | (fs.wired ? PMAP_ENTER_WIRED : 0), 0);
1596 if (faultcount != 1 && (fs.fault_flags & VM_FAULT_WIRE) == 0 &&
1598 vm_fault_prefault(&fs, vaddr,
1599 faultcount > 0 ? behind : PFBAK,
1600 faultcount > 0 ? ahead : PFFOR, false);
1603 * If the page is not wired down, then put it where the pageout daemon
1606 if ((fs.fault_flags & VM_FAULT_WIRE) != 0)
1609 vm_page_activate(fs.m);
1610 if (fs.m_hold != NULL) {
1611 (*fs.m_hold) = fs.m;
1614 vm_page_xunbusy(fs.m);
1618 * Unlock everything, and return
1620 fault_deallocate(&fs);
1622 VM_CNT_INC(v_io_faults);
1623 curthread->td_ru.ru_majflt++;
1625 if (racct_enable && fs.object->type == OBJT_VNODE) {
1627 if ((fs.fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1628 racct_add_force(curproc, RACCT_WRITEBPS,
1629 PAGE_SIZE + behind * PAGE_SIZE);
1630 racct_add_force(curproc, RACCT_WRITEIOPS, 1);
1632 racct_add_force(curproc, RACCT_READBPS,
1633 PAGE_SIZE + ahead * PAGE_SIZE);
1634 racct_add_force(curproc, RACCT_READIOPS, 1);
1636 PROC_UNLOCK(curproc);
1640 curthread->td_ru.ru_minflt++;
1642 return (KERN_SUCCESS);
1646 * Speed up the reclamation of pages that precede the faulting pindex within
1647 * the first object of the shadow chain. Essentially, perform the equivalent
1648 * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes
1649 * the faulting pindex by the cluster size when the pages read by vm_fault()
1650 * cross a cluster-size boundary. The cluster size is the greater of the
1651 * smallest superpage size and VM_FAULT_DONTNEED_MIN.
1653 * When "fs->first_object" is a shadow object, the pages in the backing object
1654 * that precede the faulting pindex are deactivated by vm_fault(). So, this
1655 * function must only be concerned with pages in the first object.
1658 vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead)
1660 vm_map_entry_t entry;
1661 vm_object_t first_object, object;
1662 vm_offset_t end, start;
1663 vm_page_t m, m_next;
1664 vm_pindex_t pend, pstart;
1667 object = fs->object;
1668 VM_OBJECT_ASSERT_UNLOCKED(object);
1669 first_object = fs->first_object;
1670 /* Neither fictitious nor unmanaged pages can be reclaimed. */
1671 if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
1672 VM_OBJECT_RLOCK(first_object);
1673 size = VM_FAULT_DONTNEED_MIN;
1674 if (MAXPAGESIZES > 1 && size < pagesizes[1])
1675 size = pagesizes[1];
1676 end = rounddown2(vaddr, size);
1677 if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) &&
1678 (entry = fs->entry)->start < end) {
1679 if (end - entry->start < size)
1680 start = entry->start;
1683 pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED);
1684 pstart = OFF_TO_IDX(entry->offset) + atop(start -
1686 m_next = vm_page_find_least(first_object, pstart);
1687 pend = OFF_TO_IDX(entry->offset) + atop(end -
1689 while ((m = m_next) != NULL && m->pindex < pend) {
1690 m_next = TAILQ_NEXT(m, listq);
1691 if (!vm_page_all_valid(m) ||
1696 * Don't clear PGA_REFERENCED, since it would
1697 * likely represent a reference by a different
1700 * Typically, at this point, prefetched pages
1701 * are still in the inactive queue. Only
1702 * pages that triggered page faults are in the
1703 * active queue. The test for whether the page
1704 * is in the inactive queue is racy; in the
1705 * worst case we will requeue the page
1708 if (!vm_page_inactive(m))
1709 vm_page_deactivate(m);
1712 VM_OBJECT_RUNLOCK(first_object);
1717 * vm_fault_prefault provides a quick way of clustering
1718 * pagefaults into a processes address space. It is a "cousin"
1719 * of vm_map_pmap_enter, except it runs at page fault time instead
1723 vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
1724 int backward, int forward, bool obj_locked)
1727 vm_map_entry_t entry;
1728 vm_object_t backing_object, lobject;
1729 vm_offset_t addr, starta;
1734 pmap = fs->map->pmap;
1735 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1740 if (addra < backward * PAGE_SIZE) {
1741 starta = entry->start;
1743 starta = addra - backward * PAGE_SIZE;
1744 if (starta < entry->start)
1745 starta = entry->start;
1749 * Generate the sequence of virtual addresses that are candidates for
1750 * prefaulting in an outward spiral from the faulting virtual address,
1751 * "addra". Specifically, the sequence is "addra - PAGE_SIZE", "addra
1752 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
1753 * If the candidate address doesn't have a backing physical page, then
1754 * the loop immediately terminates.
1756 for (i = 0; i < 2 * imax(backward, forward); i++) {
1757 addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
1759 if (addr > addra + forward * PAGE_SIZE)
1762 if (addr < starta || addr >= entry->end)
1765 if (!pmap_is_prefaultable(pmap, addr))
1768 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1769 lobject = entry->object.vm_object;
1771 VM_OBJECT_RLOCK(lobject);
1772 while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1773 lobject->type == OBJT_DEFAULT &&
1774 (backing_object = lobject->backing_object) != NULL) {
1775 KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
1776 0, ("vm_fault_prefault: unaligned object offset"));
1777 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1778 VM_OBJECT_RLOCK(backing_object);
1779 if (!obj_locked || lobject != entry->object.vm_object)
1780 VM_OBJECT_RUNLOCK(lobject);
1781 lobject = backing_object;
1784 if (!obj_locked || lobject != entry->object.vm_object)
1785 VM_OBJECT_RUNLOCK(lobject);
1788 if (vm_page_all_valid(m) &&
1789 (m->flags & PG_FICTITIOUS) == 0)
1790 pmap_enter_quick(pmap, addr, m, entry->protection);
1791 if (!obj_locked || lobject != entry->object.vm_object)
1792 VM_OBJECT_RUNLOCK(lobject);
1797 * Hold each of the physical pages that are mapped by the specified range of
1798 * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
1799 * and allow the specified types of access, "prot". If all of the implied
1800 * pages are successfully held, then the number of held pages is returned
1801 * together with pointers to those pages in the array "ma". However, if any
1802 * of the pages cannot be held, -1 is returned.
1805 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
1806 vm_prot_t prot, vm_page_t *ma, int max_count)
1808 vm_offset_t end, va;
1811 boolean_t pmap_failed;
1815 end = round_page(addr + len);
1816 addr = trunc_page(addr);
1818 if (!vm_map_range_valid(map, addr, end))
1821 if (atop(end - addr) > max_count)
1822 panic("vm_fault_quick_hold_pages: count > max_count");
1823 count = atop(end - addr);
1826 * Most likely, the physical pages are resident in the pmap, so it is
1827 * faster to try pmap_extract_and_hold() first.
1829 pmap_failed = FALSE;
1830 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
1831 *mp = pmap_extract_and_hold(map->pmap, va, prot);
1834 else if ((prot & VM_PROT_WRITE) != 0 &&
1835 (*mp)->dirty != VM_PAGE_BITS_ALL) {
1837 * Explicitly dirty the physical page. Otherwise, the
1838 * caller's changes may go unnoticed because they are
1839 * performed through an unmanaged mapping or by a DMA
1842 * The object lock is not held here.
1843 * See vm_page_clear_dirty_mask().
1850 * One or more pages could not be held by the pmap. Either no
1851 * page was mapped at the specified virtual address or that
1852 * mapping had insufficient permissions. Attempt to fault in
1853 * and hold these pages.
1855 * If vm_fault_disable_pagefaults() was called,
1856 * i.e., TDP_NOFAULTING is set, we must not sleep nor
1857 * acquire MD VM locks, which means we must not call
1858 * vm_fault(). Some (out of tree) callers mark
1859 * too wide a code area with vm_fault_disable_pagefaults()
1860 * already, use the VM_PROT_QUICK_NOFAULT flag to request
1861 * the proper behaviour explicitly.
1863 if ((prot & VM_PROT_QUICK_NOFAULT) != 0 &&
1864 (curthread->td_pflags & TDP_NOFAULTING) != 0)
1866 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1867 if (*mp == NULL && vm_fault(map, va, prot,
1868 VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
1873 for (mp = ma; mp < ma + count; mp++)
1875 vm_page_unwire(*mp, PQ_INACTIVE);
1881 * vm_fault_copy_entry
1883 * Create new shadow object backing dst_entry with private copy of
1884 * all underlying pages. When src_entry is equal to dst_entry,
1885 * function implements COW for wired-down map entry. Otherwise,
1886 * it forks wired entry into dst_map.
1888 * In/out conditions:
1889 * The source and destination maps must be locked for write.
1890 * The source map entry must be wired down (or be a sharing map
1891 * entry corresponding to a main map entry that is wired down).
1894 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1895 vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1896 vm_ooffset_t *fork_charge)
1898 vm_object_t backing_object, dst_object, object, src_object;
1899 vm_pindex_t dst_pindex, pindex, src_pindex;
1900 vm_prot_t access, prot;
1910 upgrade = src_entry == dst_entry;
1911 access = prot = dst_entry->protection;
1913 src_object = src_entry->object.vm_object;
1914 src_pindex = OFF_TO_IDX(src_entry->offset);
1916 if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
1917 dst_object = src_object;
1918 vm_object_reference(dst_object);
1921 * Create the top-level object for the destination entry.
1922 * Doesn't actually shadow anything - we copy the pages
1925 dst_object = vm_object_allocate_anon(atop(dst_entry->end -
1926 dst_entry->start), NULL, NULL, 0);
1927 #if VM_NRESERVLEVEL > 0
1928 dst_object->flags |= OBJ_COLORED;
1929 dst_object->pg_color = atop(dst_entry->start);
1931 dst_object->domain = src_object->domain;
1932 dst_object->charge = dst_entry->end - dst_entry->start;
1935 VM_OBJECT_WLOCK(dst_object);
1936 KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1937 ("vm_fault_copy_entry: vm_object not NULL"));
1938 if (src_object != dst_object) {
1939 dst_entry->object.vm_object = dst_object;
1940 dst_entry->offset = 0;
1941 dst_entry->eflags &= ~MAP_ENTRY_VN_EXEC;
1943 if (fork_charge != NULL) {
1944 KASSERT(dst_entry->cred == NULL,
1945 ("vm_fault_copy_entry: leaked swp charge"));
1946 dst_object->cred = curthread->td_ucred;
1947 crhold(dst_object->cred);
1948 *fork_charge += dst_object->charge;
1949 } else if ((dst_object->type == OBJT_DEFAULT ||
1950 (dst_object->flags & OBJ_SWAP) != 0) &&
1951 dst_object->cred == NULL) {
1952 KASSERT(dst_entry->cred != NULL, ("no cred for entry %p",
1954 dst_object->cred = dst_entry->cred;
1955 dst_entry->cred = NULL;
1959 * If not an upgrade, then enter the mappings in the pmap as
1960 * read and/or execute accesses. Otherwise, enter them as
1963 * A writeable large page mapping is only created if all of
1964 * the constituent small page mappings are modified. Marking
1965 * PTEs as modified on inception allows promotion to happen
1966 * without taking potentially large number of soft faults.
1969 access &= ~VM_PROT_WRITE;
1972 * Loop through all of the virtual pages within the entry's
1973 * range, copying each page from the source object to the
1974 * destination object. Since the source is wired, those pages
1975 * must exist. In contrast, the destination is pageable.
1976 * Since the destination object doesn't share any backing storage
1977 * with the source object, all of its pages must be dirtied,
1978 * regardless of whether they can be written.
1980 for (vaddr = dst_entry->start, dst_pindex = 0;
1981 vaddr < dst_entry->end;
1982 vaddr += PAGE_SIZE, dst_pindex++) {
1985 * Find the page in the source object, and copy it in.
1986 * Because the source is wired down, the page will be
1989 if (src_object != dst_object)
1990 VM_OBJECT_RLOCK(src_object);
1991 object = src_object;
1992 pindex = src_pindex + dst_pindex;
1993 while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1994 (backing_object = object->backing_object) != NULL) {
1996 * Unless the source mapping is read-only or
1997 * it is presently being upgraded from
1998 * read-only, the first object in the shadow
1999 * chain should provide all of the pages. In
2000 * other words, this loop body should never be
2001 * executed when the source mapping is already
2004 KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 ||
2006 ("vm_fault_copy_entry: main object missing page"));
2008 VM_OBJECT_RLOCK(backing_object);
2009 pindex += OFF_TO_IDX(object->backing_object_offset);
2010 if (object != dst_object)
2011 VM_OBJECT_RUNLOCK(object);
2012 object = backing_object;
2014 KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing"));
2016 if (object != dst_object) {
2018 * Allocate a page in the destination object.
2020 dst_m = vm_page_alloc(dst_object, (src_object ==
2021 dst_object ? src_pindex : 0) + dst_pindex,
2023 if (dst_m == NULL) {
2024 VM_OBJECT_WUNLOCK(dst_object);
2025 VM_OBJECT_RUNLOCK(object);
2026 vm_wait(dst_object);
2027 VM_OBJECT_WLOCK(dst_object);
2030 pmap_copy_page(src_m, dst_m);
2031 VM_OBJECT_RUNLOCK(object);
2032 dst_m->dirty = dst_m->valid = src_m->valid;
2035 if (vm_page_busy_acquire(dst_m, VM_ALLOC_WAITFAIL) == 0)
2037 if (dst_m->pindex >= dst_object->size) {
2039 * We are upgrading. Index can occur
2040 * out of bounds if the object type is
2041 * vnode and the file was truncated.
2043 vm_page_xunbusy(dst_m);
2047 VM_OBJECT_WUNLOCK(dst_object);
2050 * Enter it in the pmap. If a wired, copy-on-write
2051 * mapping is being replaced by a write-enabled
2052 * mapping, then wire that new mapping.
2054 * The page can be invalid if the user called
2055 * msync(MS_INVALIDATE) or truncated the backing vnode
2056 * or shared memory object. In this case, do not
2057 * insert it into pmap, but still do the copy so that
2058 * all copies of the wired map entry have similar
2061 if (vm_page_all_valid(dst_m)) {
2062 pmap_enter(dst_map->pmap, vaddr, dst_m, prot,
2063 access | (upgrade ? PMAP_ENTER_WIRED : 0), 0);
2067 * Mark it no longer busy, and put it on the active list.
2069 VM_OBJECT_WLOCK(dst_object);
2072 if (src_m != dst_m) {
2073 vm_page_unwire(src_m, PQ_INACTIVE);
2074 vm_page_wire(dst_m);
2076 KASSERT(vm_page_wired(dst_m),
2077 ("dst_m %p is not wired", dst_m));
2080 vm_page_activate(dst_m);
2082 vm_page_xunbusy(dst_m);
2084 VM_OBJECT_WUNLOCK(dst_object);
2086 dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
2087 vm_object_deallocate(src_object);
2092 * Block entry into the machine-independent layer's page fault handler by
2093 * the calling thread. Subsequent calls to vm_fault() by that thread will
2094 * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of
2095 * spurious page faults.
2098 vm_fault_disable_pagefaults(void)
2101 return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
2105 vm_fault_enable_pagefaults(int save)
2108 curthread_pflags_restore(save);