2 * SPDX-License-Identifier: (BSD-4-Clause AND MIT-CMU)
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1994 John S. Dyson
8 * Copyright (c) 1994 David Greenman
12 * This code is derived from software contributed to Berkeley by
13 * The Mach Operating System project at Carnegie-Mellon University.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the University of
26 * California, Berkeley and its contributors.
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * from: @(#)vm_fault.c 8.4 (Berkeley) 1/12/94
46 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
47 * All rights reserved.
49 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
51 * Permission to use, copy, modify and distribute this software and
52 * its documentation is hereby granted, provided that both the copyright
53 * notice and this permission notice appear in all copies of the
54 * software, derivative works or modified versions, and any portions
55 * thereof, and that both notices appear in supporting documentation.
57 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
58 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
59 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
61 * Carnegie Mellon requests users of this software to return to
63 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
64 * School of Computer Science
65 * Carnegie Mellon University
66 * Pittsburgh PA 15213-3890
68 * any improvements or extensions that they make and grant Carnegie the
69 * rights to redistribute these changes.
73 * Page fault handling module.
76 #include <sys/cdefs.h>
77 __FBSDID("$FreeBSD$");
79 #include "opt_ktrace.h"
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/kernel.h>
87 #include <sys/mutex.h>
89 #include <sys/racct.h>
90 #include <sys/resourcevar.h>
91 #include <sys/rwlock.h>
92 #include <sys/sysctl.h>
93 #include <sys/vmmeter.h>
94 #include <sys/vnode.h>
96 #include <sys/ktrace.h>
100 #include <vm/vm_param.h>
102 #include <vm/vm_map.h>
103 #include <vm/vm_object.h>
104 #include <vm/vm_page.h>
105 #include <vm/vm_pageout.h>
106 #include <vm/vm_kern.h>
107 #include <vm/vm_pager.h>
108 #include <vm/vm_extern.h>
109 #include <vm/vm_reserv.h>
114 #define VM_FAULT_READ_DEFAULT (1 + VM_FAULT_READ_AHEAD_INIT)
115 #define VM_FAULT_READ_MAX (1 + VM_FAULT_READ_AHEAD_MAX)
117 #define VM_FAULT_DONTNEED_MIN 1048576
124 vm_object_t first_object;
125 vm_pindex_t first_pindex;
127 vm_map_entry_t entry;
129 bool lookup_still_valid;
133 static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr,
135 static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
136 int backward, int forward, bool obj_locked);
138 static int vm_pfault_oom_attempts = 3;
139 SYSCTL_INT(_vm, OID_AUTO, pfault_oom_attempts, CTLFLAG_RWTUN,
140 &vm_pfault_oom_attempts, 0,
141 "Number of page allocation attempts in page fault handler before it "
142 "triggers OOM handling");
144 static int vm_pfault_oom_wait = 10;
145 SYSCTL_INT(_vm, OID_AUTO, pfault_oom_wait, CTLFLAG_RWTUN,
146 &vm_pfault_oom_wait, 0,
147 "Number of seconds to wait for free pages before retrying "
148 "the page fault handler");
151 release_page(struct faultstate *fs)
154 vm_page_xunbusy(fs->m);
156 vm_page_deactivate(fs->m);
157 vm_page_unlock(fs->m);
162 unlock_map(struct faultstate *fs)
165 if (fs->lookup_still_valid) {
166 vm_map_lookup_done(fs->map, fs->entry);
167 fs->lookup_still_valid = false;
172 unlock_vp(struct faultstate *fs)
175 if (fs->vp != NULL) {
182 unlock_and_deallocate(struct faultstate *fs)
185 vm_object_pip_wakeup(fs->object);
186 VM_OBJECT_WUNLOCK(fs->object);
187 if (fs->object != fs->first_object) {
188 VM_OBJECT_WLOCK(fs->first_object);
189 vm_page_lock(fs->first_m);
190 vm_page_free(fs->first_m);
191 vm_page_unlock(fs->first_m);
192 vm_object_pip_wakeup(fs->first_object);
193 VM_OBJECT_WUNLOCK(fs->first_object);
196 vm_object_deallocate(fs->first_object);
202 vm_fault_dirty(vm_map_entry_t entry, vm_page_t m, vm_prot_t prot,
203 vm_prot_t fault_type, int fault_flags, bool set_wd)
207 if (((prot & VM_PROT_WRITE) == 0 &&
208 (fault_flags & VM_FAULT_DIRTY) == 0) ||
209 (m->oflags & VPO_UNMANAGED) != 0)
212 VM_OBJECT_ASSERT_LOCKED(m->object);
214 need_dirty = ((fault_type & VM_PROT_WRITE) != 0 &&
215 (fault_flags & VM_FAULT_WIRE) == 0) ||
216 (fault_flags & VM_FAULT_DIRTY) != 0;
219 vm_object_set_writeable_dirty(m->object);
222 * If two callers of vm_fault_dirty() with set_wd ==
223 * FALSE, one for the map entry with MAP_ENTRY_NOSYNC
224 * flag set, other with flag clear, race, it is
225 * possible for the no-NOSYNC thread to see m->dirty
226 * != 0 and not clear VPO_NOSYNC. Take vm_page lock
227 * around manipulation of VPO_NOSYNC and
228 * vm_page_dirty() call, to avoid the race and keep
229 * m->oflags consistent.
234 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
235 * if the page is already dirty to prevent data written with
236 * the expectation of being synced from not being synced.
237 * Likewise if this entry does not request NOSYNC then make
238 * sure the page isn't marked NOSYNC. Applications sharing
239 * data should use the same flags to avoid ping ponging.
241 if ((entry->eflags & MAP_ENTRY_NOSYNC) != 0) {
243 m->oflags |= VPO_NOSYNC;
246 m->oflags &= ~VPO_NOSYNC;
250 * If the fault is a write, we know that this page is being
251 * written NOW so dirty it explicitly to save on
252 * pmap_is_modified() calls later.
254 * Also, since the page is now dirty, we can possibly tell
255 * the pager to release any swap backing the page. Calling
256 * the pager requires a write lock on the object.
263 vm_pager_page_unswapped(m);
267 vm_fault_fill_hold(vm_page_t *m_hold, vm_page_t m)
270 if (m_hold != NULL) {
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 m = vm_page_lookup(fs->first_object, fs->first_pindex);
296 /* A busy page can be mapped for read|execute access. */
297 if (m == NULL || ((prot & VM_PROT_WRITE) != 0 &&
298 vm_page_busied(m)) || m->valid != VM_PAGE_BITS_ALL)
299 return (KERN_FAILURE);
302 #if (defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
303 __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)) && \
305 if ((m->flags & PG_FICTITIOUS) == 0 &&
306 (m_super = vm_reserv_to_superpage(m)) != NULL &&
307 rounddown2(vaddr, pagesizes[m_super->psind]) >= fs->entry->start &&
308 roundup2(vaddr + 1, pagesizes[m_super->psind]) <= fs->entry->end &&
309 (vaddr & (pagesizes[m_super->psind] - 1)) == (VM_PAGE_TO_PHYS(m) &
310 (pagesizes[m_super->psind] - 1)) && !wired &&
311 pmap_ps_enabled(fs->map->pmap)) {
312 flags = PS_ALL_VALID;
313 if ((prot & VM_PROT_WRITE) != 0) {
315 * Create a superpage mapping allowing write access
316 * only if none of the constituent pages are busy and
317 * all of them are already dirty (except possibly for
318 * the page that was faulted on).
320 flags |= PS_NONE_BUSY;
321 if ((fs->first_object->flags & OBJ_UNMANAGED) == 0)
322 flags |= PS_ALL_DIRTY;
324 if (vm_page_ps_test(m_super, flags, m)) {
326 psind = m_super->psind;
327 vaddr = rounddown2(vaddr, pagesizes[psind]);
328 /* Preset the modified bit for dirty superpages. */
329 if ((flags & PS_ALL_DIRTY) != 0)
330 fault_type |= VM_PROT_WRITE;
334 rv = pmap_enter(fs->map->pmap, vaddr, m_map, prot, fault_type |
335 PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED : 0), psind);
336 if (rv != KERN_SUCCESS)
338 vm_fault_fill_hold(m_hold, m);
339 vm_fault_dirty(fs->entry, m, prot, fault_type, fault_flags, false);
340 if (psind == 0 && !wired)
341 vm_fault_prefault(fs, vaddr, PFBAK, PFFOR, true);
342 VM_OBJECT_RUNLOCK(fs->first_object);
343 vm_map_lookup_done(fs->map, fs->entry);
344 curthread->td_ru.ru_minflt++;
345 return (KERN_SUCCESS);
349 vm_fault_restore_map_lock(struct faultstate *fs)
352 VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
353 MPASS(fs->first_object->paging_in_progress > 0);
355 if (!vm_map_trylock_read(fs->map)) {
356 VM_OBJECT_WUNLOCK(fs->first_object);
357 vm_map_lock_read(fs->map);
358 VM_OBJECT_WLOCK(fs->first_object);
360 fs->lookup_still_valid = true;
364 vm_fault_populate_check_page(vm_page_t m)
368 * Check each page to ensure that the pager is obeying the
369 * interface: the page must be installed in the object, fully
370 * valid, and exclusively busied.
373 MPASS(m->valid == VM_PAGE_BITS_ALL);
374 MPASS(vm_page_xbusied(m));
378 vm_fault_populate_cleanup(vm_object_t object, vm_pindex_t first,
384 VM_OBJECT_ASSERT_WLOCKED(object);
385 MPASS(first <= last);
386 for (pidx = first, m = vm_page_lookup(object, pidx);
387 pidx <= last; pidx++, m = vm_page_next(m)) {
388 vm_fault_populate_check_page(m);
390 vm_page_deactivate(m);
397 vm_fault_populate(struct faultstate *fs, vm_prot_t prot, int fault_type,
398 int fault_flags, boolean_t wired, vm_page_t *m_hold)
403 vm_pindex_t map_first, map_last, pager_first, pager_last, pidx;
404 int i, npages, psind, rv;
406 MPASS(fs->object == fs->first_object);
407 VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
408 MPASS(fs->first_object->paging_in_progress > 0);
409 MPASS(fs->first_object->backing_object == NULL);
410 MPASS(fs->lookup_still_valid);
412 pager_first = OFF_TO_IDX(fs->entry->offset);
413 pager_last = pager_first + atop(fs->entry->end - fs->entry->start) - 1;
418 * Call the pager (driver) populate() method.
420 * There is no guarantee that the method will be called again
421 * if the current fault is for read, and a future fault is
422 * for write. Report the entry's maximum allowed protection
425 rv = vm_pager_populate(fs->first_object, fs->first_pindex,
426 fault_type, fs->entry->max_protection, &pager_first, &pager_last);
428 VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
429 if (rv == VM_PAGER_BAD) {
431 * VM_PAGER_BAD is the backdoor for a pager to request
432 * normal fault handling.
434 vm_fault_restore_map_lock(fs);
435 if (fs->map->timestamp != fs->map_generation)
436 return (KERN_RESOURCE_SHORTAGE); /* RetryFault */
437 return (KERN_NOT_RECEIVER);
439 if (rv != VM_PAGER_OK)
440 return (KERN_FAILURE); /* AKA SIGSEGV */
442 /* Ensure that the driver is obeying the interface. */
443 MPASS(pager_first <= pager_last);
444 MPASS(fs->first_pindex <= pager_last);
445 MPASS(fs->first_pindex >= pager_first);
446 MPASS(pager_last < fs->first_object->size);
448 vm_fault_restore_map_lock(fs);
449 if (fs->map->timestamp != fs->map_generation) {
450 vm_fault_populate_cleanup(fs->first_object, pager_first,
452 return (KERN_RESOURCE_SHORTAGE); /* RetryFault */
456 * The map is unchanged after our last unlock. Process the fault.
458 * The range [pager_first, pager_last] that is given to the
459 * pager is only a hint. The pager may populate any range
460 * within the object that includes the requested page index.
461 * In case the pager expanded the range, clip it to fit into
464 map_first = OFF_TO_IDX(fs->entry->offset);
465 if (map_first > pager_first) {
466 vm_fault_populate_cleanup(fs->first_object, pager_first,
468 pager_first = map_first;
470 map_last = map_first + atop(fs->entry->end - fs->entry->start) - 1;
471 if (map_last < pager_last) {
472 vm_fault_populate_cleanup(fs->first_object, map_last + 1,
474 pager_last = map_last;
476 for (pidx = pager_first, m = vm_page_lookup(fs->first_object, pidx);
478 pidx += npages, m = vm_page_next(&m[npages - 1])) {
479 vaddr = fs->entry->start + IDX_TO_OFF(pidx) - fs->entry->offset;
480 #if defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
481 __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)
483 if (psind > 0 && ((vaddr & (pagesizes[psind] - 1)) != 0 ||
484 pidx + OFF_TO_IDX(pagesizes[psind]) - 1 > pager_last ||
485 !pmap_ps_enabled(fs->map->pmap) || wired))
490 npages = atop(pagesizes[psind]);
491 for (i = 0; i < npages; i++) {
492 vm_fault_populate_check_page(&m[i]);
493 vm_fault_dirty(fs->entry, &m[i], prot, fault_type,
496 VM_OBJECT_WUNLOCK(fs->first_object);
497 rv = pmap_enter(fs->map->pmap, vaddr, m, prot, fault_type |
498 (wired ? PMAP_ENTER_WIRED : 0), psind);
499 #if defined(__amd64__)
500 if (psind > 0 && rv == KERN_FAILURE) {
501 for (i = 0; i < npages; i++) {
502 rv = pmap_enter(fs->map->pmap, vaddr + ptoa(i),
503 &m[i], prot, fault_type |
504 (wired ? PMAP_ENTER_WIRED : 0), 0);
505 MPASS(rv == KERN_SUCCESS);
509 MPASS(rv == KERN_SUCCESS);
511 VM_OBJECT_WLOCK(fs->first_object);
513 for (i = 0; i < npages; i++) {
514 vm_page_change_lock(&m[i], &m_mtx);
515 if ((fault_flags & VM_FAULT_WIRE) != 0)
518 vm_page_activate(&m[i]);
519 if (m_hold != NULL && m[i].pindex == fs->first_pindex) {
523 vm_page_xunbusy_maybelocked(&m[i]);
528 curthread->td_ru.ru_majflt++;
529 return (KERN_SUCCESS);
535 * Handle a page fault occurring at the given address,
536 * requiring the given permissions, in the map specified.
537 * If successful, the page is inserted into the
538 * associated physical map.
540 * NOTE: the given address should be truncated to the
541 * proper page address.
543 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
544 * a standard error specifying why the fault is fatal is returned.
546 * The map in question must be referenced, and remains so.
547 * Caller may hold no locks.
550 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
557 if ((td->td_pflags & TDP_NOFAULTING) != 0)
558 return (KERN_PROTECTION_FAILURE);
560 if (map != kernel_map && KTRPOINT(td, KTR_FAULT))
561 ktrfault(vaddr, fault_type);
563 result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags,
566 if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND))
573 vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
574 int fault_flags, vm_page_t *m_hold)
576 struct faultstate fs;
578 struct domainset *dset;
580 vm_object_t next_object, retry_object;
581 vm_offset_t e_end, e_start;
582 vm_pindex_t retry_pindex;
583 vm_prot_t prot, retry_prot;
584 int ahead, alloc_req, behind, cluster_offset, error, era, faultcount;
585 int locked, nera, oom, result, rv;
587 boolean_t wired; /* Passed by reference. */
588 bool dead, hardfault, is_first_object_locked;
590 VM_CNT_INC(v_vm_faults);
601 * Find the backing store object and offset into it to begin the
605 result = vm_map_lookup(&fs.map, vaddr, fault_type |
606 VM_PROT_FAULT_LOOKUP, &fs.entry, &fs.first_object,
607 &fs.first_pindex, &prot, &wired);
608 if (result != KERN_SUCCESS) {
613 fs.map_generation = fs.map->timestamp;
615 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
616 panic("%s: fault on nofault entry, addr: %#lx",
617 __func__, (u_long)vaddr);
620 if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
621 fs.entry->wiring_thread != curthread) {
622 vm_map_unlock_read(fs.map);
624 if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
625 (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
627 fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
628 vm_map_unlock_and_wait(fs.map, 0);
630 vm_map_unlock(fs.map);
634 MPASS((fs.entry->eflags & MAP_ENTRY_GUARD) == 0);
637 fault_type = prot | (fault_type & VM_PROT_COPY);
639 KASSERT((fault_flags & VM_FAULT_WIRE) == 0,
640 ("!wired && VM_FAULT_WIRE"));
643 * Try to avoid lock contention on the top-level object through
644 * special-case handling of some types of page faults, specifically,
645 * those that are both (1) mapping an existing page from the top-
646 * level object and (2) not having to mark that object as containing
647 * dirty pages. Under these conditions, a read lock on the top-level
648 * object suffices, allowing multiple page faults of a similar type to
649 * run in parallel on the same top-level object.
651 if (fs.vp == NULL /* avoid locked vnode leak */ &&
652 (fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0 &&
653 /* avoid calling vm_object_set_writeable_dirty() */
654 ((prot & VM_PROT_WRITE) == 0 ||
655 (fs.first_object->type != OBJT_VNODE &&
656 (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
657 (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0)) {
658 VM_OBJECT_RLOCK(fs.first_object);
659 if ((prot & VM_PROT_WRITE) == 0 ||
660 (fs.first_object->type != OBJT_VNODE &&
661 (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
662 (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0) {
663 rv = vm_fault_soft_fast(&fs, vaddr, prot, fault_type,
664 fault_flags, wired, m_hold);
665 if (rv == KERN_SUCCESS)
668 if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) {
669 VM_OBJECT_RUNLOCK(fs.first_object);
670 VM_OBJECT_WLOCK(fs.first_object);
673 VM_OBJECT_WLOCK(fs.first_object);
677 * Make a reference to this object to prevent its disposal while we
678 * are messing with it. Once we have the reference, the map is free
679 * to be diddled. Since objects reference their shadows (and copies),
680 * they will stay around as well.
682 * Bump the paging-in-progress count to prevent size changes (e.g.
683 * truncation operations) during I/O.
685 vm_object_reference_locked(fs.first_object);
686 vm_object_pip_add(fs.first_object, 1);
688 fs.lookup_still_valid = true;
693 * Search for the page at object/offset.
695 fs.object = fs.first_object;
696 fs.pindex = fs.first_pindex;
699 * If the object is marked for imminent termination,
700 * we retry here, since the collapse pass has raced
701 * with us. Otherwise, if we see terminally dead
702 * object, return fail.
704 if ((fs.object->flags & OBJ_DEAD) != 0) {
705 dead = fs.object->type == OBJT_DEAD;
706 unlock_and_deallocate(&fs);
708 return (KERN_PROTECTION_FAILURE);
714 * See if page is resident
716 fs.m = vm_page_lookup(fs.object, fs.pindex);
719 * Wait/Retry if the page is busy. We have to do this
720 * if the page is either exclusive or shared busy
721 * because the vm_pager may be using read busy for
722 * pageouts (and even pageins if it is the vnode
723 * pager), and we could end up trying to pagein and
724 * pageout the same page simultaneously.
726 * We can theoretically allow the busy case on a read
727 * fault if the page is marked valid, but since such
728 * pages are typically already pmap'd, putting that
729 * special case in might be more effort then it is
730 * worth. We cannot under any circumstances mess
731 * around with a shared busied page except, perhaps,
734 if (vm_page_busied(fs.m)) {
736 * Reference the page before unlocking and
737 * sleeping so that the page daemon is less
738 * likely to reclaim it.
740 vm_page_aflag_set(fs.m, PGA_REFERENCED);
741 if (fs.object != fs.first_object) {
742 if (!VM_OBJECT_TRYWLOCK(
744 VM_OBJECT_WUNLOCK(fs.object);
745 VM_OBJECT_WLOCK(fs.first_object);
746 VM_OBJECT_WLOCK(fs.object);
748 vm_page_lock(fs.first_m);
749 vm_page_free(fs.first_m);
750 vm_page_unlock(fs.first_m);
751 vm_object_pip_wakeup(fs.first_object);
752 VM_OBJECT_WUNLOCK(fs.first_object);
756 if (fs.m == vm_page_lookup(fs.object,
758 vm_page_sleep_if_busy(fs.m, "vmpfw");
760 vm_object_pip_wakeup(fs.object);
761 VM_OBJECT_WUNLOCK(fs.object);
762 VM_CNT_INC(v_intrans);
763 vm_object_deallocate(fs.first_object);
768 * Mark page busy for other processes, and the
769 * pagedaemon. If it still isn't completely valid
770 * (readable), jump to readrest, else break-out ( we
774 if (fs.m->valid != VM_PAGE_BITS_ALL)
776 break; /* break to PAGE HAS BEEN FOUND */
778 KASSERT(fs.m == NULL, ("fs.m should be NULL, not %p", fs.m));
781 * Page is not resident. If the pager might contain the page
782 * or this is the beginning of the search, allocate a new
783 * page. (Default objects are zero-fill, so there is no real
786 if (fs.object->type != OBJT_DEFAULT ||
787 fs.object == fs.first_object) {
788 if (fs.pindex >= fs.object->size) {
789 unlock_and_deallocate(&fs);
790 return (KERN_PROTECTION_FAILURE);
793 if (fs.object == fs.first_object &&
794 (fs.first_object->flags & OBJ_POPULATE) != 0 &&
795 fs.first_object->shadow_count == 0) {
796 rv = vm_fault_populate(&fs, prot, fault_type,
797 fault_flags, wired, m_hold);
801 unlock_and_deallocate(&fs);
803 case KERN_RESOURCE_SHORTAGE:
804 unlock_and_deallocate(&fs);
806 case KERN_NOT_RECEIVER:
808 * Pager's populate() method
809 * returned VM_PAGER_BAD.
813 panic("inconsistent return codes");
818 * Allocate a new page for this object/offset pair.
820 * Unlocked read of the p_flag is harmless. At
821 * worst, the P_KILLED might be not observed
822 * there, and allocation can fail, causing
823 * restart and new reading of the p_flag.
825 dset = fs.object->domain.dr_policy;
827 dset = curthread->td_domain.dr_policy;
828 if (!vm_page_count_severe_set(&dset->ds_mask) ||
830 #if VM_NRESERVLEVEL > 0
831 vm_object_color(fs.object, atop(vaddr) -
834 alloc_req = P_KILLED(curproc) ?
835 VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
836 if (fs.object->type != OBJT_VNODE &&
837 fs.object->backing_object == NULL)
838 alloc_req |= VM_ALLOC_ZERO;
839 fs.m = vm_page_alloc(fs.object, fs.pindex,
843 unlock_and_deallocate(&fs);
844 if (vm_pfault_oom_attempts < 0 ||
845 oom < vm_pfault_oom_attempts) {
848 vm_pfault_oom_wait * hz);
853 "proc %d (%s) failed to alloc page on fault, starting OOM\n",
854 curproc->p_pid, curproc->p_comm);
855 vm_pageout_oom(VM_OOM_MEM_PF);
862 * At this point, we have either allocated a new page or found
863 * an existing page that is only partially valid.
865 * We hold a reference on the current object and the page is
870 * If the pager for the current object might have the page,
871 * then determine the number of additional pages to read and
872 * potentially reprioritize previously read pages for earlier
873 * reclamation. These operations should only be performed
874 * once per page fault. Even if the current pager doesn't
875 * have the page, the number of additional pages to read will
876 * apply to subsequent objects in the shadow chain.
878 if (fs.object->type != OBJT_DEFAULT && nera == -1 &&
879 !P_KILLED(curproc)) {
880 KASSERT(fs.lookup_still_valid, ("map unlocked"));
881 era = fs.entry->read_ahead;
882 behavior = vm_map_entry_behavior(fs.entry);
883 if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
885 } else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
886 nera = VM_FAULT_READ_AHEAD_MAX;
887 if (vaddr == fs.entry->next_read)
888 vm_fault_dontneed(&fs, vaddr, nera);
889 } else if (vaddr == fs.entry->next_read) {
891 * This is a sequential fault. Arithmetically
892 * increase the requested number of pages in
893 * the read-ahead window. The requested
894 * number of pages is "# of sequential faults
895 * x (read ahead min + 1) + read ahead min"
897 nera = VM_FAULT_READ_AHEAD_MIN;
900 if (nera > VM_FAULT_READ_AHEAD_MAX)
901 nera = VM_FAULT_READ_AHEAD_MAX;
903 if (era == VM_FAULT_READ_AHEAD_MAX)
904 vm_fault_dontneed(&fs, vaddr, nera);
907 * This is a non-sequential fault.
913 * A read lock on the map suffices to update
914 * the read ahead count safely.
916 fs.entry->read_ahead = nera;
920 * Prepare for unlocking the map. Save the map
921 * entry's start and end addresses, which are used to
922 * optimize the size of the pager operation below.
923 * Even if the map entry's addresses change after
924 * unlocking the map, using the saved addresses is
927 e_start = fs.entry->start;
928 e_end = fs.entry->end;
932 * Call the pager to retrieve the page if there is a chance
933 * that the pager has it, and potentially retrieve additional
934 * pages at the same time.
936 if (fs.object->type != OBJT_DEFAULT) {
938 * Release the map lock before locking the vnode or
939 * sleeping in the pager. (If the current object has
940 * a shadow, then an earlier iteration of this loop
941 * may have already unlocked the map.)
945 if (fs.object->type == OBJT_VNODE &&
946 (vp = fs.object->handle) != fs.vp) {
948 * Perform an unlock in case the desired vnode
949 * changed while the map was unlocked during a
954 locked = VOP_ISLOCKED(vp);
955 if (locked != LK_EXCLUSIVE)
959 * We must not sleep acquiring the vnode lock
960 * while we have the page exclusive busied or
961 * the object's paging-in-progress count
962 * incremented. Otherwise, we could deadlock.
964 error = vget(vp, locked | LK_CANRECURSE |
965 LK_NOWAIT, curthread);
969 unlock_and_deallocate(&fs);
970 error = vget(vp, locked | LK_RETRY |
971 LK_CANRECURSE, curthread);
975 ("vm_fault: vget failed"));
980 KASSERT(fs.vp == NULL || !fs.map->system_map,
981 ("vm_fault: vnode-backed object mapped by system map"));
984 * Page in the requested page and hint the pager,
985 * that it may bring up surrounding pages.
987 if (nera == -1 || behavior == MAP_ENTRY_BEHAV_RANDOM ||
992 /* Is this a sequential fault? */
998 * Request a cluster of pages that is
999 * aligned to a VM_FAULT_READ_DEFAULT
1000 * page offset boundary within the
1001 * object. Alignment to a page offset
1002 * boundary is more likely to coincide
1003 * with the underlying file system
1004 * block than alignment to a virtual
1007 cluster_offset = fs.pindex %
1008 VM_FAULT_READ_DEFAULT;
1009 behind = ulmin(cluster_offset,
1010 atop(vaddr - e_start));
1011 ahead = VM_FAULT_READ_DEFAULT - 1 -
1014 ahead = ulmin(ahead, atop(e_end - vaddr) - 1);
1016 rv = vm_pager_get_pages(fs.object, &fs.m, 1,
1018 if (rv == VM_PAGER_OK) {
1019 faultcount = behind + 1 + ahead;
1021 break; /* break to PAGE HAS BEEN FOUND */
1023 if (rv == VM_PAGER_ERROR)
1024 printf("vm_fault: pager read error, pid %d (%s)\n",
1025 curproc->p_pid, curproc->p_comm);
1028 * If an I/O error occurred or the requested page was
1029 * outside the range of the pager, clean up and return
1032 if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) {
1034 if (!vm_page_wired(fs.m))
1037 vm_page_xunbusy_maybelocked(fs.m);
1038 vm_page_unlock(fs.m);
1040 unlock_and_deallocate(&fs);
1041 return (rv == VM_PAGER_ERROR ? KERN_FAILURE :
1042 KERN_PROTECTION_FAILURE);
1046 * The requested page does not exist at this object/
1047 * offset. Remove the invalid page from the object,
1048 * waking up anyone waiting for it, and continue on to
1049 * the next object. However, if this is the top-level
1050 * object, we must leave the busy page in place to
1051 * prevent another process from rushing past us, and
1052 * inserting the page in that object at the same time
1055 if (fs.object != fs.first_object) {
1057 if (!vm_page_wired(fs.m))
1060 vm_page_xunbusy_maybelocked(fs.m);
1061 vm_page_unlock(fs.m);
1067 * We get here if the object has default pager (or unwiring)
1068 * or the pager doesn't have the page.
1070 if (fs.object == fs.first_object)
1074 * Move on to the next object. Lock the next object before
1075 * unlocking the current one.
1077 next_object = fs.object->backing_object;
1078 if (next_object == NULL) {
1080 * If there's no object left, fill the page in the top
1081 * object with zeros.
1083 if (fs.object != fs.first_object) {
1084 vm_object_pip_wakeup(fs.object);
1085 VM_OBJECT_WUNLOCK(fs.object);
1087 fs.object = fs.first_object;
1088 fs.pindex = fs.first_pindex;
1090 VM_OBJECT_WLOCK(fs.object);
1095 * Zero the page if necessary and mark it valid.
1097 if ((fs.m->flags & PG_ZERO) == 0) {
1098 pmap_zero_page(fs.m);
1100 VM_CNT_INC(v_ozfod);
1103 fs.m->valid = VM_PAGE_BITS_ALL;
1104 /* Don't try to prefault neighboring pages. */
1106 break; /* break to PAGE HAS BEEN FOUND */
1108 KASSERT(fs.object != next_object,
1109 ("object loop %p", next_object));
1110 VM_OBJECT_WLOCK(next_object);
1111 vm_object_pip_add(next_object, 1);
1112 if (fs.object != fs.first_object)
1113 vm_object_pip_wakeup(fs.object);
1115 OFF_TO_IDX(fs.object->backing_object_offset);
1116 VM_OBJECT_WUNLOCK(fs.object);
1117 fs.object = next_object;
1121 vm_page_assert_xbusied(fs.m);
1124 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1129 * If the page is being written, but isn't already owned by the
1130 * top-level object, we have to copy it into a new page owned by the
1133 if (fs.object != fs.first_object) {
1135 * We only really need to copy if we want to write it.
1137 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1139 * This allows pages to be virtually copied from a
1140 * backing_object into the first_object, where the
1141 * backing object has no other refs to it, and cannot
1142 * gain any more refs. Instead of a bcopy, we just
1143 * move the page from the backing object to the
1144 * first object. Note that we must mark the page
1145 * dirty in the first object so that it will go out
1146 * to swap when needed.
1148 is_first_object_locked = false;
1151 * Only one shadow object
1153 (fs.object->shadow_count == 1) &&
1155 * No COW refs, except us
1157 (fs.object->ref_count == 1) &&
1159 * No one else can look this object up
1161 (fs.object->handle == NULL) &&
1163 * No other ways to look the object up
1165 ((fs.object->type == OBJT_DEFAULT) ||
1166 (fs.object->type == OBJT_SWAP)) &&
1167 (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
1169 * We don't chase down the shadow chain
1171 fs.object == fs.first_object->backing_object) {
1173 * Keep the page wired to ensure that it is not
1174 * freed by another thread, such as the page
1175 * daemon, while it is disassociated from an
1179 vm_page_change_lock(fs.m, &mtx);
1181 (void)vm_page_remove(fs.m);
1182 vm_page_change_lock(fs.first_m, &mtx);
1183 vm_page_replace_checked(fs.m, fs.first_object,
1184 fs.first_pindex, fs.first_m);
1185 vm_page_free(fs.first_m);
1186 vm_page_change_lock(fs.m, &mtx);
1187 vm_page_unwire(fs.m, PQ_ACTIVE);
1189 vm_page_dirty(fs.m);
1190 #if VM_NRESERVLEVEL > 0
1192 * Rename the reservation.
1194 vm_reserv_rename(fs.m, fs.first_object,
1195 fs.object, OFF_TO_IDX(
1196 fs.first_object->backing_object_offset));
1199 * Removing the page from the backing object
1202 vm_page_xbusy(fs.m);
1205 VM_CNT_INC(v_cow_optim);
1208 * Oh, well, lets copy it.
1210 pmap_copy_page(fs.m, fs.first_m);
1211 fs.first_m->valid = VM_PAGE_BITS_ALL;
1212 if (wired && (fault_flags &
1213 VM_FAULT_WIRE) == 0) {
1214 vm_page_lock(fs.first_m);
1215 vm_page_wire(fs.first_m);
1216 vm_page_unlock(fs.first_m);
1219 vm_page_unwire(fs.m, PQ_INACTIVE);
1220 vm_page_unlock(fs.m);
1223 * We no longer need the old page or object.
1228 * fs.object != fs.first_object due to above
1231 vm_object_pip_wakeup(fs.object);
1232 VM_OBJECT_WUNLOCK(fs.object);
1235 * We only try to prefault read-only mappings to the
1236 * neighboring pages when this copy-on-write fault is
1237 * a hard fault. In other cases, trying to prefault
1238 * is typically wasted effort.
1240 if (faultcount == 0)
1244 * Only use the new page below...
1246 fs.object = fs.first_object;
1247 fs.pindex = fs.first_pindex;
1249 if (!is_first_object_locked)
1250 VM_OBJECT_WLOCK(fs.object);
1251 VM_CNT_INC(v_cow_faults);
1252 curthread->td_cow++;
1254 prot &= ~VM_PROT_WRITE;
1259 * We must verify that the maps have not changed since our last
1262 if (!fs.lookup_still_valid) {
1263 if (!vm_map_trylock_read(fs.map)) {
1265 unlock_and_deallocate(&fs);
1268 fs.lookup_still_valid = true;
1269 if (fs.map->timestamp != fs.map_generation) {
1270 result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
1271 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
1274 * If we don't need the page any longer, put it on the inactive
1275 * list (the easiest thing to do here). If no one needs it,
1276 * pageout will grab it eventually.
1278 if (result != KERN_SUCCESS) {
1280 unlock_and_deallocate(&fs);
1283 * If retry of map lookup would have blocked then
1284 * retry fault from start.
1286 if (result == KERN_FAILURE)
1290 if ((retry_object != fs.first_object) ||
1291 (retry_pindex != fs.first_pindex)) {
1293 unlock_and_deallocate(&fs);
1298 * Check whether the protection has changed or the object has
1299 * been copied while we left the map unlocked. Changing from
1300 * read to write permission is OK - we leave the page
1301 * write-protected, and catch the write fault. Changing from
1302 * write to read permission means that we can't mark the page
1303 * write-enabled after all.
1306 fault_type &= retry_prot;
1309 unlock_and_deallocate(&fs);
1313 /* Reassert because wired may have changed. */
1314 KASSERT(wired || (fault_flags & VM_FAULT_WIRE) == 0,
1315 ("!wired && VM_FAULT_WIRE"));
1320 * If the page was filled by a pager, save the virtual address that
1321 * should be faulted on next under a sequential access pattern to the
1322 * map entry. A read lock on the map suffices to update this address
1326 fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE;
1328 vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags, true);
1329 vm_page_assert_xbusied(fs.m);
1332 * Page must be completely valid or it is not fit to
1333 * map into user space. vm_pager_get_pages() ensures this.
1335 KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
1336 ("vm_fault: page %p partially invalid", fs.m));
1337 VM_OBJECT_WUNLOCK(fs.object);
1340 * Put this page into the physical map. We had to do the unlock above
1341 * because pmap_enter() may sleep. We don't put the page
1342 * back on the active queue until later so that the pageout daemon
1343 * won't find it (yet).
1345 pmap_enter(fs.map->pmap, vaddr, fs.m, prot,
1346 fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0);
1347 if (faultcount != 1 && (fault_flags & VM_FAULT_WIRE) == 0 &&
1349 vm_fault_prefault(&fs, vaddr,
1350 faultcount > 0 ? behind : PFBAK,
1351 faultcount > 0 ? ahead : PFFOR, false);
1352 VM_OBJECT_WLOCK(fs.object);
1356 * If the page is not wired down, then put it where the pageout daemon
1359 if ((fault_flags & VM_FAULT_WIRE) != 0)
1362 vm_page_activate(fs.m);
1363 if (m_hold != NULL) {
1367 vm_page_unlock(fs.m);
1368 vm_page_xunbusy(fs.m);
1371 * Unlock everything, and return
1373 unlock_and_deallocate(&fs);
1375 VM_CNT_INC(v_io_faults);
1376 curthread->td_ru.ru_majflt++;
1378 if (racct_enable && fs.object->type == OBJT_VNODE) {
1380 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1381 racct_add_force(curproc, RACCT_WRITEBPS,
1382 PAGE_SIZE + behind * PAGE_SIZE);
1383 racct_add_force(curproc, RACCT_WRITEIOPS, 1);
1385 racct_add_force(curproc, RACCT_READBPS,
1386 PAGE_SIZE + ahead * PAGE_SIZE);
1387 racct_add_force(curproc, RACCT_READIOPS, 1);
1389 PROC_UNLOCK(curproc);
1393 curthread->td_ru.ru_minflt++;
1395 return (KERN_SUCCESS);
1399 * Speed up the reclamation of pages that precede the faulting pindex within
1400 * the first object of the shadow chain. Essentially, perform the equivalent
1401 * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes
1402 * the faulting pindex by the cluster size when the pages read by vm_fault()
1403 * cross a cluster-size boundary. The cluster size is the greater of the
1404 * smallest superpage size and VM_FAULT_DONTNEED_MIN.
1406 * When "fs->first_object" is a shadow object, the pages in the backing object
1407 * that precede the faulting pindex are deactivated by vm_fault(). So, this
1408 * function must only be concerned with pages in the first object.
1411 vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead)
1413 vm_map_entry_t entry;
1414 vm_object_t first_object, object;
1415 vm_offset_t end, start;
1416 vm_page_t m, m_next;
1417 vm_pindex_t pend, pstart;
1420 object = fs->object;
1421 VM_OBJECT_ASSERT_WLOCKED(object);
1422 first_object = fs->first_object;
1423 if (first_object != object) {
1424 if (!VM_OBJECT_TRYWLOCK(first_object)) {
1425 VM_OBJECT_WUNLOCK(object);
1426 VM_OBJECT_WLOCK(first_object);
1427 VM_OBJECT_WLOCK(object);
1430 /* Neither fictitious nor unmanaged pages can be reclaimed. */
1431 if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
1432 size = VM_FAULT_DONTNEED_MIN;
1433 if (MAXPAGESIZES > 1 && size < pagesizes[1])
1434 size = pagesizes[1];
1435 end = rounddown2(vaddr, size);
1436 if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) &&
1437 (entry = fs->entry)->start < end) {
1438 if (end - entry->start < size)
1439 start = entry->start;
1442 pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED);
1443 pstart = OFF_TO_IDX(entry->offset) + atop(start -
1445 m_next = vm_page_find_least(first_object, pstart);
1446 pend = OFF_TO_IDX(entry->offset) + atop(end -
1448 while ((m = m_next) != NULL && m->pindex < pend) {
1449 m_next = TAILQ_NEXT(m, listq);
1450 if (m->valid != VM_PAGE_BITS_ALL ||
1455 * Don't clear PGA_REFERENCED, since it would
1456 * likely represent a reference by a different
1459 * Typically, at this point, prefetched pages
1460 * are still in the inactive queue. Only
1461 * pages that triggered page faults are in the
1465 if (!vm_page_inactive(m))
1466 vm_page_deactivate(m);
1471 if (first_object != object)
1472 VM_OBJECT_WUNLOCK(first_object);
1476 * vm_fault_prefault provides a quick way of clustering
1477 * pagefaults into a processes address space. It is a "cousin"
1478 * of vm_map_pmap_enter, except it runs at page fault time instead
1482 vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
1483 int backward, int forward, bool obj_locked)
1486 vm_map_entry_t entry;
1487 vm_object_t backing_object, lobject;
1488 vm_offset_t addr, starta;
1493 pmap = fs->map->pmap;
1494 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1499 if (addra < backward * PAGE_SIZE) {
1500 starta = entry->start;
1502 starta = addra - backward * PAGE_SIZE;
1503 if (starta < entry->start)
1504 starta = entry->start;
1508 * Generate the sequence of virtual addresses that are candidates for
1509 * prefaulting in an outward spiral from the faulting virtual address,
1510 * "addra". Specifically, the sequence is "addra - PAGE_SIZE", "addra
1511 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
1512 * If the candidate address doesn't have a backing physical page, then
1513 * the loop immediately terminates.
1515 for (i = 0; i < 2 * imax(backward, forward); i++) {
1516 addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
1518 if (addr > addra + forward * PAGE_SIZE)
1521 if (addr < starta || addr >= entry->end)
1524 if (!pmap_is_prefaultable(pmap, addr))
1527 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1528 lobject = entry->object.vm_object;
1530 VM_OBJECT_RLOCK(lobject);
1531 while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1532 lobject->type == OBJT_DEFAULT &&
1533 (backing_object = lobject->backing_object) != NULL) {
1534 KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
1535 0, ("vm_fault_prefault: unaligned object offset"));
1536 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1537 VM_OBJECT_RLOCK(backing_object);
1538 if (!obj_locked || lobject != entry->object.vm_object)
1539 VM_OBJECT_RUNLOCK(lobject);
1540 lobject = backing_object;
1543 if (!obj_locked || lobject != entry->object.vm_object)
1544 VM_OBJECT_RUNLOCK(lobject);
1547 if (m->valid == VM_PAGE_BITS_ALL &&
1548 (m->flags & PG_FICTITIOUS) == 0)
1549 pmap_enter_quick(pmap, addr, m, entry->protection);
1550 if (!obj_locked || lobject != entry->object.vm_object)
1551 VM_OBJECT_RUNLOCK(lobject);
1556 * Hold each of the physical pages that are mapped by the specified range of
1557 * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
1558 * and allow the specified types of access, "prot". If all of the implied
1559 * pages are successfully held, then the number of held pages is returned
1560 * together with pointers to those pages in the array "ma". However, if any
1561 * of the pages cannot be held, -1 is returned.
1564 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
1565 vm_prot_t prot, vm_page_t *ma, int max_count)
1567 vm_offset_t end, va;
1570 boolean_t pmap_failed;
1574 end = round_page(addr + len);
1575 addr = trunc_page(addr);
1578 * Check for illegal addresses.
1580 if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
1583 if (atop(end - addr) > max_count)
1584 panic("vm_fault_quick_hold_pages: count > max_count");
1585 count = atop(end - addr);
1588 * Most likely, the physical pages are resident in the pmap, so it is
1589 * faster to try pmap_extract_and_hold() first.
1591 pmap_failed = FALSE;
1592 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
1593 *mp = pmap_extract_and_hold(map->pmap, va, prot);
1596 else if ((prot & VM_PROT_WRITE) != 0 &&
1597 (*mp)->dirty != VM_PAGE_BITS_ALL) {
1599 * Explicitly dirty the physical page. Otherwise, the
1600 * caller's changes may go unnoticed because they are
1601 * performed through an unmanaged mapping or by a DMA
1604 * The object lock is not held here.
1605 * See vm_page_clear_dirty_mask().
1612 * One or more pages could not be held by the pmap. Either no
1613 * page was mapped at the specified virtual address or that
1614 * mapping had insufficient permissions. Attempt to fault in
1615 * and hold these pages.
1617 * If vm_fault_disable_pagefaults() was called,
1618 * i.e., TDP_NOFAULTING is set, we must not sleep nor
1619 * acquire MD VM locks, which means we must not call
1620 * vm_fault_hold(). Some (out of tree) callers mark
1621 * too wide a code area with vm_fault_disable_pagefaults()
1622 * already, use the VM_PROT_QUICK_NOFAULT flag to request
1623 * the proper behaviour explicitly.
1625 if ((prot & VM_PROT_QUICK_NOFAULT) != 0 &&
1626 (curthread->td_pflags & TDP_NOFAULTING) != 0)
1628 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1629 if (*mp == NULL && vm_fault_hold(map, va, prot,
1630 VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
1635 for (mp = ma; mp < ma + count; mp++)
1638 if (vm_page_unwire(*mp, PQ_INACTIVE) &&
1639 (*mp)->object == NULL)
1641 vm_page_unlock(*mp);
1648 * vm_fault_copy_entry
1650 * Create new shadow object backing dst_entry with private copy of
1651 * all underlying pages. When src_entry is equal to dst_entry,
1652 * function implements COW for wired-down map entry. Otherwise,
1653 * it forks wired entry into dst_map.
1655 * In/out conditions:
1656 * The source and destination maps must be locked for write.
1657 * The source map entry must be wired down (or be a sharing map
1658 * entry corresponding to a main map entry that is wired down).
1661 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1662 vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1663 vm_ooffset_t *fork_charge)
1665 vm_object_t backing_object, dst_object, object, src_object;
1666 vm_pindex_t dst_pindex, pindex, src_pindex;
1667 vm_prot_t access, prot;
1677 upgrade = src_entry == dst_entry;
1678 access = prot = dst_entry->protection;
1680 src_object = src_entry->object.vm_object;
1681 src_pindex = OFF_TO_IDX(src_entry->offset);
1683 if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
1684 dst_object = src_object;
1685 vm_object_reference(dst_object);
1688 * Create the top-level object for the destination entry. (Doesn't
1689 * actually shadow anything - we copy the pages directly.)
1691 dst_object = vm_object_allocate(OBJT_DEFAULT,
1692 atop(dst_entry->end - dst_entry->start));
1693 #if VM_NRESERVLEVEL > 0
1694 dst_object->flags |= OBJ_COLORED;
1695 dst_object->pg_color = atop(dst_entry->start);
1697 dst_object->domain = src_object->domain;
1698 dst_object->charge = dst_entry->end - dst_entry->start;
1701 VM_OBJECT_WLOCK(dst_object);
1702 KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1703 ("vm_fault_copy_entry: vm_object not NULL"));
1704 if (src_object != dst_object) {
1705 dst_entry->object.vm_object = dst_object;
1706 dst_entry->offset = 0;
1707 dst_entry->eflags &= ~MAP_ENTRY_VN_EXEC;
1709 if (fork_charge != NULL) {
1710 KASSERT(dst_entry->cred == NULL,
1711 ("vm_fault_copy_entry: leaked swp charge"));
1712 dst_object->cred = curthread->td_ucred;
1713 crhold(dst_object->cred);
1714 *fork_charge += dst_object->charge;
1715 } else if ((dst_object->type == OBJT_DEFAULT ||
1716 dst_object->type == OBJT_SWAP) &&
1717 dst_object->cred == NULL) {
1718 KASSERT(dst_entry->cred != NULL, ("no cred for entry %p",
1720 dst_object->cred = dst_entry->cred;
1721 dst_entry->cred = NULL;
1725 * If not an upgrade, then enter the mappings in the pmap as
1726 * read and/or execute accesses. Otherwise, enter them as
1729 * A writeable large page mapping is only created if all of
1730 * the constituent small page mappings are modified. Marking
1731 * PTEs as modified on inception allows promotion to happen
1732 * without taking potentially large number of soft faults.
1735 access &= ~VM_PROT_WRITE;
1738 * Loop through all of the virtual pages within the entry's
1739 * range, copying each page from the source object to the
1740 * destination object. Since the source is wired, those pages
1741 * must exist. In contrast, the destination is pageable.
1742 * Since the destination object doesn't share any backing storage
1743 * with the source object, all of its pages must be dirtied,
1744 * regardless of whether they can be written.
1746 for (vaddr = dst_entry->start, dst_pindex = 0;
1747 vaddr < dst_entry->end;
1748 vaddr += PAGE_SIZE, dst_pindex++) {
1751 * Find the page in the source object, and copy it in.
1752 * Because the source is wired down, the page will be
1755 if (src_object != dst_object)
1756 VM_OBJECT_RLOCK(src_object);
1757 object = src_object;
1758 pindex = src_pindex + dst_pindex;
1759 while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1760 (backing_object = object->backing_object) != NULL) {
1762 * Unless the source mapping is read-only or
1763 * it is presently being upgraded from
1764 * read-only, the first object in the shadow
1765 * chain should provide all of the pages. In
1766 * other words, this loop body should never be
1767 * executed when the source mapping is already
1770 KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 ||
1772 ("vm_fault_copy_entry: main object missing page"));
1774 VM_OBJECT_RLOCK(backing_object);
1775 pindex += OFF_TO_IDX(object->backing_object_offset);
1776 if (object != dst_object)
1777 VM_OBJECT_RUNLOCK(object);
1778 object = backing_object;
1780 KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing"));
1782 if (object != dst_object) {
1784 * Allocate a page in the destination object.
1786 dst_m = vm_page_alloc(dst_object, (src_object ==
1787 dst_object ? src_pindex : 0) + dst_pindex,
1789 if (dst_m == NULL) {
1790 VM_OBJECT_WUNLOCK(dst_object);
1791 VM_OBJECT_RUNLOCK(object);
1792 vm_wait(dst_object);
1793 VM_OBJECT_WLOCK(dst_object);
1796 pmap_copy_page(src_m, dst_m);
1797 VM_OBJECT_RUNLOCK(object);
1798 dst_m->dirty = dst_m->valid = src_m->valid;
1801 if (vm_page_sleep_if_busy(dst_m, "fltupg"))
1803 if (dst_m->pindex >= dst_object->size)
1805 * We are upgrading. Index can occur
1806 * out of bounds if the object type is
1807 * vnode and the file was truncated.
1810 vm_page_xbusy(dst_m);
1812 VM_OBJECT_WUNLOCK(dst_object);
1815 * Enter it in the pmap. If a wired, copy-on-write
1816 * mapping is being replaced by a write-enabled
1817 * mapping, then wire that new mapping.
1819 * The page can be invalid if the user called
1820 * msync(MS_INVALIDATE) or truncated the backing vnode
1821 * or shared memory object. In this case, do not
1822 * insert it into pmap, but still do the copy so that
1823 * all copies of the wired map entry have similar
1826 if (dst_m->valid == VM_PAGE_BITS_ALL) {
1827 pmap_enter(dst_map->pmap, vaddr, dst_m, prot,
1828 access | (upgrade ? PMAP_ENTER_WIRED : 0), 0);
1832 * Mark it no longer busy, and put it on the active list.
1834 VM_OBJECT_WLOCK(dst_object);
1837 if (src_m != dst_m) {
1838 vm_page_lock(src_m);
1839 vm_page_unwire(src_m, PQ_INACTIVE);
1840 vm_page_unlock(src_m);
1841 vm_page_lock(dst_m);
1842 vm_page_wire(dst_m);
1843 vm_page_unlock(dst_m);
1845 KASSERT(vm_page_wired(dst_m),
1846 ("dst_m %p is not wired", dst_m));
1849 vm_page_lock(dst_m);
1850 vm_page_activate(dst_m);
1851 vm_page_unlock(dst_m);
1853 vm_page_xunbusy(dst_m);
1855 VM_OBJECT_WUNLOCK(dst_object);
1857 dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1858 vm_object_deallocate(src_object);
1863 * Block entry into the machine-independent layer's page fault handler by
1864 * the calling thread. Subsequent calls to vm_fault() by that thread will
1865 * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of
1866 * spurious page faults.
1869 vm_fault_disable_pagefaults(void)
1872 return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
1876 vm_fault_enable_pagefaults(int save)
1879 curthread_pflags_restore(save);