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>
88 #include <sys/racct.h>
89 #include <sys/resourcevar.h>
90 #include <sys/rwlock.h>
91 #include <sys/sysctl.h>
92 #include <sys/vmmeter.h>
93 #include <sys/vnode.h>
95 #include <sys/ktrace.h>
99 #include <vm/vm_param.h>
101 #include <vm/vm_map.h>
102 #include <vm/vm_object.h>
103 #include <vm/vm_page.h>
104 #include <vm/vm_pageout.h>
105 #include <vm/vm_kern.h>
106 #include <vm/vm_pager.h>
107 #include <vm/vm_extern.h>
108 #include <vm/vm_reserv.h>
113 #define VM_FAULT_READ_DEFAULT (1 + VM_FAULT_READ_AHEAD_INIT)
114 #define VM_FAULT_READ_MAX (1 + VM_FAULT_READ_AHEAD_MAX)
116 #define VM_FAULT_DONTNEED_MIN 1048576
123 vm_object_t first_object;
124 vm_pindex_t first_pindex;
126 vm_map_entry_t entry;
128 bool lookup_still_valid;
132 static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr,
134 static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
135 int backward, int forward, bool obj_locked);
138 release_page(struct faultstate *fs)
141 vm_page_xunbusy(fs->m);
143 vm_page_deactivate(fs->m);
144 vm_page_unlock(fs->m);
149 unlock_map(struct faultstate *fs)
152 if (fs->lookup_still_valid) {
153 vm_map_lookup_done(fs->map, fs->entry);
154 fs->lookup_still_valid = false;
159 unlock_vp(struct faultstate *fs)
162 if (fs->vp != NULL) {
169 unlock_and_deallocate(struct faultstate *fs)
172 vm_object_pip_wakeup(fs->object);
173 VM_OBJECT_WUNLOCK(fs->object);
174 if (fs->object != fs->first_object) {
175 VM_OBJECT_WLOCK(fs->first_object);
176 vm_page_lock(fs->first_m);
177 vm_page_free(fs->first_m);
178 vm_page_unlock(fs->first_m);
179 vm_object_pip_wakeup(fs->first_object);
180 VM_OBJECT_WUNLOCK(fs->first_object);
183 vm_object_deallocate(fs->first_object);
189 vm_fault_dirty(vm_map_entry_t entry, vm_page_t m, vm_prot_t prot,
190 vm_prot_t fault_type, int fault_flags, bool set_wd)
194 if (((prot & VM_PROT_WRITE) == 0 &&
195 (fault_flags & VM_FAULT_DIRTY) == 0) ||
196 (m->oflags & VPO_UNMANAGED) != 0)
199 VM_OBJECT_ASSERT_LOCKED(m->object);
201 need_dirty = ((fault_type & VM_PROT_WRITE) != 0 &&
202 (fault_flags & VM_FAULT_WIRE) == 0) ||
203 (fault_flags & VM_FAULT_DIRTY) != 0;
206 vm_object_set_writeable_dirty(m->object);
209 * If two callers of vm_fault_dirty() with set_wd ==
210 * FALSE, one for the map entry with MAP_ENTRY_NOSYNC
211 * flag set, other with flag clear, race, it is
212 * possible for the no-NOSYNC thread to see m->dirty
213 * != 0 and not clear VPO_NOSYNC. Take vm_page lock
214 * around manipulation of VPO_NOSYNC and
215 * vm_page_dirty() call, to avoid the race and keep
216 * m->oflags consistent.
221 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
222 * if the page is already dirty to prevent data written with
223 * the expectation of being synced from not being synced.
224 * Likewise if this entry does not request NOSYNC then make
225 * sure the page isn't marked NOSYNC. Applications sharing
226 * data should use the same flags to avoid ping ponging.
228 if ((entry->eflags & MAP_ENTRY_NOSYNC) != 0) {
230 m->oflags |= VPO_NOSYNC;
233 m->oflags &= ~VPO_NOSYNC;
237 * If the fault is a write, we know that this page is being
238 * written NOW so dirty it explicitly to save on
239 * pmap_is_modified() calls later.
241 * Also, since the page is now dirty, we can possibly tell
242 * the pager to release any swap backing the page. Calling
243 * the pager requires a write lock on the object.
250 vm_pager_page_unswapped(m);
254 vm_fault_fill_hold(vm_page_t *m_hold, vm_page_t m)
257 if (m_hold != NULL) {
266 * Unlocks fs.first_object and fs.map on success.
269 vm_fault_soft_fast(struct faultstate *fs, vm_offset_t vaddr, vm_prot_t prot,
270 int fault_type, int fault_flags, boolean_t wired, vm_page_t *m_hold)
273 #if (defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
274 __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)) && \
281 MPASS(fs->vp == NULL);
282 m = vm_page_lookup(fs->first_object, fs->first_pindex);
283 /* A busy page can be mapped for read|execute access. */
284 if (m == NULL || ((prot & VM_PROT_WRITE) != 0 &&
285 vm_page_busied(m)) || m->valid != VM_PAGE_BITS_ALL)
286 return (KERN_FAILURE);
289 #if (defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
290 __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)) && \
292 if ((m->flags & PG_FICTITIOUS) == 0 &&
293 (m_super = vm_reserv_to_superpage(m)) != NULL &&
294 rounddown2(vaddr, pagesizes[m_super->psind]) >= fs->entry->start &&
295 roundup2(vaddr + 1, pagesizes[m_super->psind]) <= fs->entry->end &&
296 (vaddr & (pagesizes[m_super->psind] - 1)) == (VM_PAGE_TO_PHYS(m) &
297 (pagesizes[m_super->psind] - 1)) &&
298 pmap_ps_enabled(fs->map->pmap)) {
299 flags = PS_ALL_VALID;
300 if ((prot & VM_PROT_WRITE) != 0) {
302 * Create a superpage mapping allowing write access
303 * only if none of the constituent pages are busy and
304 * all of them are already dirty (except possibly for
305 * the page that was faulted on).
307 flags |= PS_NONE_BUSY;
308 if ((fs->first_object->flags & OBJ_UNMANAGED) == 0)
309 flags |= PS_ALL_DIRTY;
311 if (vm_page_ps_test(m_super, flags, m)) {
313 psind = m_super->psind;
314 vaddr = rounddown2(vaddr, pagesizes[psind]);
315 /* Preset the modified bit for dirty superpages. */
316 if ((flags & PS_ALL_DIRTY) != 0)
317 fault_type |= VM_PROT_WRITE;
321 rv = pmap_enter(fs->map->pmap, vaddr, m_map, prot, fault_type |
322 PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED : 0), psind);
323 if (rv != KERN_SUCCESS)
325 vm_fault_fill_hold(m_hold, m);
326 vm_fault_dirty(fs->entry, m, prot, fault_type, fault_flags, false);
327 if (psind == 0 && !wired)
328 vm_fault_prefault(fs, vaddr, PFBAK, PFFOR, true);
329 VM_OBJECT_RUNLOCK(fs->first_object);
330 vm_map_lookup_done(fs->map, fs->entry);
331 curthread->td_ru.ru_minflt++;
332 return (KERN_SUCCESS);
336 vm_fault_restore_map_lock(struct faultstate *fs)
339 VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
340 MPASS(fs->first_object->paging_in_progress > 0);
342 if (!vm_map_trylock_read(fs->map)) {
343 VM_OBJECT_WUNLOCK(fs->first_object);
344 vm_map_lock_read(fs->map);
345 VM_OBJECT_WLOCK(fs->first_object);
347 fs->lookup_still_valid = true;
351 vm_fault_populate_check_page(vm_page_t m)
355 * Check each page to ensure that the pager is obeying the
356 * interface: the page must be installed in the object, fully
357 * valid, and exclusively busied.
360 MPASS(m->valid == VM_PAGE_BITS_ALL);
361 MPASS(vm_page_xbusied(m));
365 vm_fault_populate_cleanup(vm_object_t object, vm_pindex_t first,
371 VM_OBJECT_ASSERT_WLOCKED(object);
372 MPASS(first <= last);
373 for (pidx = first, m = vm_page_lookup(object, pidx);
374 pidx <= last; pidx++, m = vm_page_next(m)) {
375 vm_fault_populate_check_page(m);
377 vm_page_deactivate(m);
384 vm_fault_populate(struct faultstate *fs, vm_prot_t prot, int fault_type,
385 int fault_flags, boolean_t wired, vm_page_t *m_hold)
390 vm_pindex_t map_first, map_last, pager_first, pager_last, pidx;
391 int i, npages, psind, rv;
393 MPASS(fs->object == fs->first_object);
394 VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
395 MPASS(fs->first_object->paging_in_progress > 0);
396 MPASS(fs->first_object->backing_object == NULL);
397 MPASS(fs->lookup_still_valid);
399 pager_first = OFF_TO_IDX(fs->entry->offset);
400 pager_last = pager_first + atop(fs->entry->end - fs->entry->start) - 1;
405 * Call the pager (driver) populate() method.
407 * There is no guarantee that the method will be called again
408 * if the current fault is for read, and a future fault is
409 * for write. Report the entry's maximum allowed protection
412 rv = vm_pager_populate(fs->first_object, fs->first_pindex,
413 fault_type, fs->entry->max_protection, &pager_first, &pager_last);
415 VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
416 if (rv == VM_PAGER_BAD) {
418 * VM_PAGER_BAD is the backdoor for a pager to request
419 * normal fault handling.
421 vm_fault_restore_map_lock(fs);
422 if (fs->map->timestamp != fs->map_generation)
423 return (KERN_RESOURCE_SHORTAGE); /* RetryFault */
424 return (KERN_NOT_RECEIVER);
426 if (rv != VM_PAGER_OK)
427 return (KERN_FAILURE); /* AKA SIGSEGV */
429 /* Ensure that the driver is obeying the interface. */
430 MPASS(pager_first <= pager_last);
431 MPASS(fs->first_pindex <= pager_last);
432 MPASS(fs->first_pindex >= pager_first);
433 MPASS(pager_last < fs->first_object->size);
435 vm_fault_restore_map_lock(fs);
436 if (fs->map->timestamp != fs->map_generation) {
437 vm_fault_populate_cleanup(fs->first_object, pager_first,
439 return (KERN_RESOURCE_SHORTAGE); /* RetryFault */
443 * The map is unchanged after our last unlock. Process the fault.
445 * The range [pager_first, pager_last] that is given to the
446 * pager is only a hint. The pager may populate any range
447 * within the object that includes the requested page index.
448 * In case the pager expanded the range, clip it to fit into
451 map_first = OFF_TO_IDX(fs->entry->offset);
452 if (map_first > pager_first) {
453 vm_fault_populate_cleanup(fs->first_object, pager_first,
455 pager_first = map_first;
457 map_last = map_first + atop(fs->entry->end - fs->entry->start) - 1;
458 if (map_last < pager_last) {
459 vm_fault_populate_cleanup(fs->first_object, map_last + 1,
461 pager_last = map_last;
463 for (pidx = pager_first, m = vm_page_lookup(fs->first_object, pidx);
465 pidx += npages, m = vm_page_next(&m[npages - 1])) {
466 vaddr = fs->entry->start + IDX_TO_OFF(pidx) - fs->entry->offset;
467 #if defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
468 __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)
470 if (psind > 0 && ((vaddr & (pagesizes[psind] - 1)) != 0 ||
471 pidx + OFF_TO_IDX(pagesizes[psind]) - 1 > pager_last ||
472 !pmap_ps_enabled(fs->map->pmap)))
477 npages = atop(pagesizes[psind]);
478 for (i = 0; i < npages; i++) {
479 vm_fault_populate_check_page(&m[i]);
480 vm_fault_dirty(fs->entry, &m[i], prot, fault_type,
483 VM_OBJECT_WUNLOCK(fs->first_object);
484 pmap_enter(fs->map->pmap, vaddr, m, prot, fault_type | (wired ?
485 PMAP_ENTER_WIRED : 0), psind);
486 VM_OBJECT_WLOCK(fs->first_object);
488 for (i = 0; i < npages; i++) {
489 vm_page_change_lock(&m[i], &m_mtx);
490 if ((fault_flags & VM_FAULT_WIRE) != 0)
493 vm_page_activate(&m[i]);
494 if (m_hold != NULL && m[i].pindex == fs->first_pindex) {
498 vm_page_xunbusy_maybelocked(&m[i]);
503 curthread->td_ru.ru_majflt++;
504 return (KERN_SUCCESS);
510 * Handle a page fault occurring at the given address,
511 * requiring the given permissions, in the map specified.
512 * If successful, the page is inserted into the
513 * associated physical map.
515 * NOTE: the given address should be truncated to the
516 * proper page address.
518 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
519 * a standard error specifying why the fault is fatal is returned.
521 * The map in question must be referenced, and remains so.
522 * Caller may hold no locks.
525 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
532 if ((td->td_pflags & TDP_NOFAULTING) != 0)
533 return (KERN_PROTECTION_FAILURE);
535 if (map != kernel_map && KTRPOINT(td, KTR_FAULT))
536 ktrfault(vaddr, fault_type);
538 result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags,
541 if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND))
548 vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
549 int fault_flags, vm_page_t *m_hold)
551 struct faultstate fs;
553 struct domainset *dset;
554 vm_object_t next_object, retry_object;
555 vm_offset_t e_end, e_start;
556 vm_pindex_t retry_pindex;
557 vm_prot_t prot, retry_prot;
558 int ahead, alloc_req, behind, cluster_offset, error, era, faultcount;
559 int locked, nera, result, rv;
561 boolean_t wired; /* Passed by reference. */
562 bool dead, hardfault, is_first_object_locked;
564 VM_CNT_INC(v_vm_faults);
573 * Find the backing store object and offset into it to begin the
577 result = vm_map_lookup(&fs.map, vaddr, fault_type |
578 VM_PROT_FAULT_LOOKUP, &fs.entry, &fs.first_object,
579 &fs.first_pindex, &prot, &wired);
580 if (result != KERN_SUCCESS) {
585 fs.map_generation = fs.map->timestamp;
587 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
588 panic("%s: fault on nofault entry, addr: %#lx",
589 __func__, (u_long)vaddr);
592 if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
593 fs.entry->wiring_thread != curthread) {
594 vm_map_unlock_read(fs.map);
596 if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
597 (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
599 fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
600 vm_map_unlock_and_wait(fs.map, 0);
602 vm_map_unlock(fs.map);
606 MPASS((fs.entry->eflags & MAP_ENTRY_GUARD) == 0);
609 fault_type = prot | (fault_type & VM_PROT_COPY);
611 KASSERT((fault_flags & VM_FAULT_WIRE) == 0,
612 ("!wired && VM_FAULT_WIRE"));
615 * Try to avoid lock contention on the top-level object through
616 * special-case handling of some types of page faults, specifically,
617 * those that are both (1) mapping an existing page from the top-
618 * level object and (2) not having to mark that object as containing
619 * dirty pages. Under these conditions, a read lock on the top-level
620 * object suffices, allowing multiple page faults of a similar type to
621 * run in parallel on the same top-level object.
623 if (fs.vp == NULL /* avoid locked vnode leak */ &&
624 (fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0 &&
625 /* avoid calling vm_object_set_writeable_dirty() */
626 ((prot & VM_PROT_WRITE) == 0 ||
627 (fs.first_object->type != OBJT_VNODE &&
628 (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
629 (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0)) {
630 VM_OBJECT_RLOCK(fs.first_object);
631 if ((prot & VM_PROT_WRITE) == 0 ||
632 (fs.first_object->type != OBJT_VNODE &&
633 (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
634 (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0) {
635 rv = vm_fault_soft_fast(&fs, vaddr, prot, fault_type,
636 fault_flags, wired, m_hold);
637 if (rv == KERN_SUCCESS)
640 if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) {
641 VM_OBJECT_RUNLOCK(fs.first_object);
642 VM_OBJECT_WLOCK(fs.first_object);
645 VM_OBJECT_WLOCK(fs.first_object);
649 * Make a reference to this object to prevent its disposal while we
650 * are messing with it. Once we have the reference, the map is free
651 * to be diddled. Since objects reference their shadows (and copies),
652 * they will stay around as well.
654 * Bump the paging-in-progress count to prevent size changes (e.g.
655 * truncation operations) during I/O.
657 vm_object_reference_locked(fs.first_object);
658 vm_object_pip_add(fs.first_object, 1);
660 fs.lookup_still_valid = true;
665 * Search for the page at object/offset.
667 fs.object = fs.first_object;
668 fs.pindex = fs.first_pindex;
671 * If the object is marked for imminent termination,
672 * we retry here, since the collapse pass has raced
673 * with us. Otherwise, if we see terminally dead
674 * object, return fail.
676 if ((fs.object->flags & OBJ_DEAD) != 0) {
677 dead = fs.object->type == OBJT_DEAD;
678 unlock_and_deallocate(&fs);
680 return (KERN_PROTECTION_FAILURE);
686 * See if page is resident
688 fs.m = vm_page_lookup(fs.object, fs.pindex);
691 * Wait/Retry if the page is busy. We have to do this
692 * if the page is either exclusive or shared busy
693 * because the vm_pager may be using read busy for
694 * pageouts (and even pageins if it is the vnode
695 * pager), and we could end up trying to pagein and
696 * pageout the same page simultaneously.
698 * We can theoretically allow the busy case on a read
699 * fault if the page is marked valid, but since such
700 * pages are typically already pmap'd, putting that
701 * special case in might be more effort then it is
702 * worth. We cannot under any circumstances mess
703 * around with a shared busied page except, perhaps,
706 if (vm_page_busied(fs.m)) {
708 * Reference the page before unlocking and
709 * sleeping so that the page daemon is less
710 * likely to reclaim it.
712 vm_page_aflag_set(fs.m, PGA_REFERENCED);
713 if (fs.object != fs.first_object) {
714 if (!VM_OBJECT_TRYWLOCK(
716 VM_OBJECT_WUNLOCK(fs.object);
717 VM_OBJECT_WLOCK(fs.first_object);
718 VM_OBJECT_WLOCK(fs.object);
720 vm_page_lock(fs.first_m);
721 vm_page_free(fs.first_m);
722 vm_page_unlock(fs.first_m);
723 vm_object_pip_wakeup(fs.first_object);
724 VM_OBJECT_WUNLOCK(fs.first_object);
728 if (fs.m == vm_page_lookup(fs.object,
730 vm_page_sleep_if_busy(fs.m, "vmpfw");
732 vm_object_pip_wakeup(fs.object);
733 VM_OBJECT_WUNLOCK(fs.object);
734 VM_CNT_INC(v_intrans);
735 vm_object_deallocate(fs.first_object);
740 * Mark page busy for other processes, and the
741 * pagedaemon. If it still isn't completely valid
742 * (readable), jump to readrest, else break-out ( we
746 if (fs.m->valid != VM_PAGE_BITS_ALL)
748 break; /* break to PAGE HAS BEEN FOUND */
750 KASSERT(fs.m == NULL, ("fs.m should be NULL, not %p", fs.m));
753 * Page is not resident. If the pager might contain the page
754 * or this is the beginning of the search, allocate a new
755 * page. (Default objects are zero-fill, so there is no real
758 if (fs.object->type != OBJT_DEFAULT ||
759 fs.object == fs.first_object) {
760 if (fs.pindex >= fs.object->size) {
761 unlock_and_deallocate(&fs);
762 return (KERN_PROTECTION_FAILURE);
765 if (fs.object == fs.first_object &&
766 (fs.first_object->flags & OBJ_POPULATE) != 0 &&
767 fs.first_object->shadow_count == 0) {
768 rv = vm_fault_populate(&fs, prot, fault_type,
769 fault_flags, wired, m_hold);
773 unlock_and_deallocate(&fs);
775 case KERN_RESOURCE_SHORTAGE:
776 unlock_and_deallocate(&fs);
778 case KERN_NOT_RECEIVER:
780 * Pager's populate() method
781 * returned VM_PAGER_BAD.
785 panic("inconsistent return codes");
790 * Allocate a new page for this object/offset pair.
792 * Unlocked read of the p_flag is harmless. At
793 * worst, the P_KILLED might be not observed
794 * there, and allocation can fail, causing
795 * restart and new reading of the p_flag.
797 dset = fs.object->domain.dr_policy;
799 dset = curthread->td_domain.dr_policy;
800 if (!vm_page_count_severe_set(&dset->ds_mask) ||
802 #if VM_NRESERVLEVEL > 0
803 vm_object_color(fs.object, atop(vaddr) -
806 alloc_req = P_KILLED(curproc) ?
807 VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
808 if (fs.object->type != OBJT_VNODE &&
809 fs.object->backing_object == NULL)
810 alloc_req |= VM_ALLOC_ZERO;
811 fs.m = vm_page_alloc(fs.object, fs.pindex,
815 unlock_and_deallocate(&fs);
823 * At this point, we have either allocated a new page or found
824 * an existing page that is only partially valid.
826 * We hold a reference on the current object and the page is
831 * If the pager for the current object might have the page,
832 * then determine the number of additional pages to read and
833 * potentially reprioritize previously read pages for earlier
834 * reclamation. These operations should only be performed
835 * once per page fault. Even if the current pager doesn't
836 * have the page, the number of additional pages to read will
837 * apply to subsequent objects in the shadow chain.
839 if (fs.object->type != OBJT_DEFAULT && nera == -1 &&
840 !P_KILLED(curproc)) {
841 KASSERT(fs.lookup_still_valid, ("map unlocked"));
842 era = fs.entry->read_ahead;
843 behavior = vm_map_entry_behavior(fs.entry);
844 if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
846 } else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
847 nera = VM_FAULT_READ_AHEAD_MAX;
848 if (vaddr == fs.entry->next_read)
849 vm_fault_dontneed(&fs, vaddr, nera);
850 } else if (vaddr == fs.entry->next_read) {
852 * This is a sequential fault. Arithmetically
853 * increase the requested number of pages in
854 * the read-ahead window. The requested
855 * number of pages is "# of sequential faults
856 * x (read ahead min + 1) + read ahead min"
858 nera = VM_FAULT_READ_AHEAD_MIN;
861 if (nera > VM_FAULT_READ_AHEAD_MAX)
862 nera = VM_FAULT_READ_AHEAD_MAX;
864 if (era == VM_FAULT_READ_AHEAD_MAX)
865 vm_fault_dontneed(&fs, vaddr, nera);
868 * This is a non-sequential fault.
874 * A read lock on the map suffices to update
875 * the read ahead count safely.
877 fs.entry->read_ahead = nera;
881 * Prepare for unlocking the map. Save the map
882 * entry's start and end addresses, which are used to
883 * optimize the size of the pager operation below.
884 * Even if the map entry's addresses change after
885 * unlocking the map, using the saved addresses is
888 e_start = fs.entry->start;
889 e_end = fs.entry->end;
893 * Call the pager to retrieve the page if there is a chance
894 * that the pager has it, and potentially retrieve additional
895 * pages at the same time.
897 if (fs.object->type != OBJT_DEFAULT) {
899 * Release the map lock before locking the vnode or
900 * sleeping in the pager. (If the current object has
901 * a shadow, then an earlier iteration of this loop
902 * may have already unlocked the map.)
906 if (fs.object->type == OBJT_VNODE &&
907 (vp = fs.object->handle) != fs.vp) {
909 * Perform an unlock in case the desired vnode
910 * changed while the map was unlocked during a
915 locked = VOP_ISLOCKED(vp);
916 if (locked != LK_EXCLUSIVE)
920 * We must not sleep acquiring the vnode lock
921 * while we have the page exclusive busied or
922 * the object's paging-in-progress count
923 * incremented. Otherwise, we could deadlock.
925 error = vget(vp, locked | LK_CANRECURSE |
926 LK_NOWAIT, curthread);
930 unlock_and_deallocate(&fs);
931 error = vget(vp, locked | LK_RETRY |
932 LK_CANRECURSE, curthread);
936 ("vm_fault: vget failed"));
941 KASSERT(fs.vp == NULL || !fs.map->system_map,
942 ("vm_fault: vnode-backed object mapped by system map"));
945 * Page in the requested page and hint the pager,
946 * that it may bring up surrounding pages.
948 if (nera == -1 || behavior == MAP_ENTRY_BEHAV_RANDOM ||
953 /* Is this a sequential fault? */
959 * Request a cluster of pages that is
960 * aligned to a VM_FAULT_READ_DEFAULT
961 * page offset boundary within the
962 * object. Alignment to a page offset
963 * boundary is more likely to coincide
964 * with the underlying file system
965 * block than alignment to a virtual
968 cluster_offset = fs.pindex %
969 VM_FAULT_READ_DEFAULT;
970 behind = ulmin(cluster_offset,
971 atop(vaddr - e_start));
972 ahead = VM_FAULT_READ_DEFAULT - 1 -
975 ahead = ulmin(ahead, atop(e_end - vaddr) - 1);
977 rv = vm_pager_get_pages(fs.object, &fs.m, 1,
979 if (rv == VM_PAGER_OK) {
980 faultcount = behind + 1 + ahead;
982 break; /* break to PAGE HAS BEEN FOUND */
984 if (rv == VM_PAGER_ERROR)
985 printf("vm_fault: pager read error, pid %d (%s)\n",
986 curproc->p_pid, curproc->p_comm);
989 * If an I/O error occurred or the requested page was
990 * outside the range of the pager, clean up and return
993 if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) {
995 if (fs.m->wire_count == 0)
998 vm_page_xunbusy_maybelocked(fs.m);
999 vm_page_unlock(fs.m);
1001 unlock_and_deallocate(&fs);
1002 return (rv == VM_PAGER_ERROR ? KERN_FAILURE :
1003 KERN_PROTECTION_FAILURE);
1007 * The requested page does not exist at this object/
1008 * offset. Remove the invalid page from the object,
1009 * waking up anyone waiting for it, and continue on to
1010 * the next object. However, if this is the top-level
1011 * object, we must leave the busy page in place to
1012 * prevent another process from rushing past us, and
1013 * inserting the page in that object at the same time
1016 if (fs.object != fs.first_object) {
1018 if (fs.m->wire_count == 0)
1021 vm_page_xunbusy_maybelocked(fs.m);
1022 vm_page_unlock(fs.m);
1028 * We get here if the object has default pager (or unwiring)
1029 * or the pager doesn't have the page.
1031 if (fs.object == fs.first_object)
1035 * Move on to the next object. Lock the next object before
1036 * unlocking the current one.
1038 next_object = fs.object->backing_object;
1039 if (next_object == NULL) {
1041 * If there's no object left, fill the page in the top
1042 * object with zeros.
1044 if (fs.object != fs.first_object) {
1045 vm_object_pip_wakeup(fs.object);
1046 VM_OBJECT_WUNLOCK(fs.object);
1048 fs.object = fs.first_object;
1049 fs.pindex = fs.first_pindex;
1051 VM_OBJECT_WLOCK(fs.object);
1056 * Zero the page if necessary and mark it valid.
1058 if ((fs.m->flags & PG_ZERO) == 0) {
1059 pmap_zero_page(fs.m);
1061 VM_CNT_INC(v_ozfod);
1064 fs.m->valid = VM_PAGE_BITS_ALL;
1065 /* Don't try to prefault neighboring pages. */
1067 break; /* break to PAGE HAS BEEN FOUND */
1069 KASSERT(fs.object != next_object,
1070 ("object loop %p", next_object));
1071 VM_OBJECT_WLOCK(next_object);
1072 vm_object_pip_add(next_object, 1);
1073 if (fs.object != fs.first_object)
1074 vm_object_pip_wakeup(fs.object);
1076 OFF_TO_IDX(fs.object->backing_object_offset);
1077 VM_OBJECT_WUNLOCK(fs.object);
1078 fs.object = next_object;
1082 vm_page_assert_xbusied(fs.m);
1085 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1090 * If the page is being written, but isn't already owned by the
1091 * top-level object, we have to copy it into a new page owned by the
1094 if (fs.object != fs.first_object) {
1096 * We only really need to copy if we want to write it.
1098 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1100 * This allows pages to be virtually copied from a
1101 * backing_object into the first_object, where the
1102 * backing object has no other refs to it, and cannot
1103 * gain any more refs. Instead of a bcopy, we just
1104 * move the page from the backing object to the
1105 * first object. Note that we must mark the page
1106 * dirty in the first object so that it will go out
1107 * to swap when needed.
1109 is_first_object_locked = false;
1112 * Only one shadow object
1114 (fs.object->shadow_count == 1) &&
1116 * No COW refs, except us
1118 (fs.object->ref_count == 1) &&
1120 * No one else can look this object up
1122 (fs.object->handle == NULL) &&
1124 * No other ways to look the object up
1126 ((fs.object->type == OBJT_DEFAULT) ||
1127 (fs.object->type == OBJT_SWAP)) &&
1128 (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
1130 * We don't chase down the shadow chain
1132 fs.object == fs.first_object->backing_object) {
1134 vm_page_dequeue(fs.m);
1135 vm_page_remove(fs.m);
1136 vm_page_unlock(fs.m);
1137 vm_page_lock(fs.first_m);
1138 vm_page_replace_checked(fs.m, fs.first_object,
1139 fs.first_pindex, fs.first_m);
1140 vm_page_free(fs.first_m);
1141 vm_page_unlock(fs.first_m);
1142 vm_page_dirty(fs.m);
1143 #if VM_NRESERVLEVEL > 0
1145 * Rename the reservation.
1147 vm_reserv_rename(fs.m, fs.first_object,
1148 fs.object, OFF_TO_IDX(
1149 fs.first_object->backing_object_offset));
1152 * Removing the page from the backing object
1155 vm_page_xbusy(fs.m);
1158 VM_CNT_INC(v_cow_optim);
1161 * Oh, well, lets copy it.
1163 pmap_copy_page(fs.m, fs.first_m);
1164 fs.first_m->valid = VM_PAGE_BITS_ALL;
1165 if (wired && (fault_flags &
1166 VM_FAULT_WIRE) == 0) {
1167 vm_page_lock(fs.first_m);
1168 vm_page_wire(fs.first_m);
1169 vm_page_unlock(fs.first_m);
1172 vm_page_unwire(fs.m, PQ_INACTIVE);
1173 vm_page_unlock(fs.m);
1176 * We no longer need the old page or object.
1181 * fs.object != fs.first_object due to above
1184 vm_object_pip_wakeup(fs.object);
1185 VM_OBJECT_WUNLOCK(fs.object);
1188 * We only try to prefault read-only mappings to the
1189 * neighboring pages when this copy-on-write fault is
1190 * a hard fault. In other cases, trying to prefault
1191 * is typically wasted effort.
1193 if (faultcount == 0)
1197 * Only use the new page below...
1199 fs.object = fs.first_object;
1200 fs.pindex = fs.first_pindex;
1202 if (!is_first_object_locked)
1203 VM_OBJECT_WLOCK(fs.object);
1204 VM_CNT_INC(v_cow_faults);
1205 curthread->td_cow++;
1207 prot &= ~VM_PROT_WRITE;
1212 * We must verify that the maps have not changed since our last
1215 if (!fs.lookup_still_valid) {
1216 if (!vm_map_trylock_read(fs.map)) {
1218 unlock_and_deallocate(&fs);
1221 fs.lookup_still_valid = true;
1222 if (fs.map->timestamp != fs.map_generation) {
1223 result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
1224 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
1227 * If we don't need the page any longer, put it on the inactive
1228 * list (the easiest thing to do here). If no one needs it,
1229 * pageout will grab it eventually.
1231 if (result != KERN_SUCCESS) {
1233 unlock_and_deallocate(&fs);
1236 * If retry of map lookup would have blocked then
1237 * retry fault from start.
1239 if (result == KERN_FAILURE)
1243 if ((retry_object != fs.first_object) ||
1244 (retry_pindex != fs.first_pindex)) {
1246 unlock_and_deallocate(&fs);
1251 * Check whether the protection has changed or the object has
1252 * been copied while we left the map unlocked. Changing from
1253 * read to write permission is OK - we leave the page
1254 * write-protected, and catch the write fault. Changing from
1255 * write to read permission means that we can't mark the page
1256 * write-enabled after all.
1259 fault_type &= retry_prot;
1262 unlock_and_deallocate(&fs);
1266 /* Reassert because wired may have changed. */
1267 KASSERT(wired || (fault_flags & VM_FAULT_WIRE) == 0,
1268 ("!wired && VM_FAULT_WIRE"));
1273 * If the page was filled by a pager, save the virtual address that
1274 * should be faulted on next under a sequential access pattern to the
1275 * map entry. A read lock on the map suffices to update this address
1279 fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE;
1281 vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags, true);
1282 vm_page_assert_xbusied(fs.m);
1285 * Page must be completely valid or it is not fit to
1286 * map into user space. vm_pager_get_pages() ensures this.
1288 KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
1289 ("vm_fault: page %p partially invalid", fs.m));
1290 VM_OBJECT_WUNLOCK(fs.object);
1293 * Put this page into the physical map. We had to do the unlock above
1294 * because pmap_enter() may sleep. We don't put the page
1295 * back on the active queue until later so that the pageout daemon
1296 * won't find it (yet).
1298 pmap_enter(fs.map->pmap, vaddr, fs.m, prot,
1299 fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0);
1300 if (faultcount != 1 && (fault_flags & VM_FAULT_WIRE) == 0 &&
1302 vm_fault_prefault(&fs, vaddr,
1303 faultcount > 0 ? behind : PFBAK,
1304 faultcount > 0 ? ahead : PFFOR, false);
1305 VM_OBJECT_WLOCK(fs.object);
1309 * If the page is not wired down, then put it where the pageout daemon
1312 if ((fault_flags & VM_FAULT_WIRE) != 0)
1315 vm_page_activate(fs.m);
1316 if (m_hold != NULL) {
1320 vm_page_unlock(fs.m);
1321 vm_page_xunbusy(fs.m);
1324 * Unlock everything, and return
1326 unlock_and_deallocate(&fs);
1328 VM_CNT_INC(v_io_faults);
1329 curthread->td_ru.ru_majflt++;
1331 if (racct_enable && fs.object->type == OBJT_VNODE) {
1333 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1334 racct_add_force(curproc, RACCT_WRITEBPS,
1335 PAGE_SIZE + behind * PAGE_SIZE);
1336 racct_add_force(curproc, RACCT_WRITEIOPS, 1);
1338 racct_add_force(curproc, RACCT_READBPS,
1339 PAGE_SIZE + ahead * PAGE_SIZE);
1340 racct_add_force(curproc, RACCT_READIOPS, 1);
1342 PROC_UNLOCK(curproc);
1346 curthread->td_ru.ru_minflt++;
1348 return (KERN_SUCCESS);
1352 * Speed up the reclamation of pages that precede the faulting pindex within
1353 * the first object of the shadow chain. Essentially, perform the equivalent
1354 * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes
1355 * the faulting pindex by the cluster size when the pages read by vm_fault()
1356 * cross a cluster-size boundary. The cluster size is the greater of the
1357 * smallest superpage size and VM_FAULT_DONTNEED_MIN.
1359 * When "fs->first_object" is a shadow object, the pages in the backing object
1360 * that precede the faulting pindex are deactivated by vm_fault(). So, this
1361 * function must only be concerned with pages in the first object.
1364 vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead)
1366 vm_map_entry_t entry;
1367 vm_object_t first_object, object;
1368 vm_offset_t end, start;
1369 vm_page_t m, m_next;
1370 vm_pindex_t pend, pstart;
1373 object = fs->object;
1374 VM_OBJECT_ASSERT_WLOCKED(object);
1375 first_object = fs->first_object;
1376 if (first_object != object) {
1377 if (!VM_OBJECT_TRYWLOCK(first_object)) {
1378 VM_OBJECT_WUNLOCK(object);
1379 VM_OBJECT_WLOCK(first_object);
1380 VM_OBJECT_WLOCK(object);
1383 /* Neither fictitious nor unmanaged pages can be reclaimed. */
1384 if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
1385 size = VM_FAULT_DONTNEED_MIN;
1386 if (MAXPAGESIZES > 1 && size < pagesizes[1])
1387 size = pagesizes[1];
1388 end = rounddown2(vaddr, size);
1389 if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) &&
1390 (entry = fs->entry)->start < end) {
1391 if (end - entry->start < size)
1392 start = entry->start;
1395 pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED);
1396 pstart = OFF_TO_IDX(entry->offset) + atop(start -
1398 m_next = vm_page_find_least(first_object, pstart);
1399 pend = OFF_TO_IDX(entry->offset) + atop(end -
1401 while ((m = m_next) != NULL && m->pindex < pend) {
1402 m_next = TAILQ_NEXT(m, listq);
1403 if (m->valid != VM_PAGE_BITS_ALL ||
1408 * Don't clear PGA_REFERENCED, since it would
1409 * likely represent a reference by a different
1412 * Typically, at this point, prefetched pages
1413 * are still in the inactive queue. Only
1414 * pages that triggered page faults are in the
1418 if (!vm_page_inactive(m))
1419 vm_page_deactivate(m);
1424 if (first_object != object)
1425 VM_OBJECT_WUNLOCK(first_object);
1429 * vm_fault_prefault provides a quick way of clustering
1430 * pagefaults into a processes address space. It is a "cousin"
1431 * of vm_map_pmap_enter, except it runs at page fault time instead
1435 vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
1436 int backward, int forward, bool obj_locked)
1439 vm_map_entry_t entry;
1440 vm_object_t backing_object, lobject;
1441 vm_offset_t addr, starta;
1446 pmap = fs->map->pmap;
1447 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1452 if (addra < backward * PAGE_SIZE) {
1453 starta = entry->start;
1455 starta = addra - backward * PAGE_SIZE;
1456 if (starta < entry->start)
1457 starta = entry->start;
1461 * Generate the sequence of virtual addresses that are candidates for
1462 * prefaulting in an outward spiral from the faulting virtual address,
1463 * "addra". Specifically, the sequence is "addra - PAGE_SIZE", "addra
1464 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
1465 * If the candidate address doesn't have a backing physical page, then
1466 * the loop immediately terminates.
1468 for (i = 0; i < 2 * imax(backward, forward); i++) {
1469 addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
1471 if (addr > addra + forward * PAGE_SIZE)
1474 if (addr < starta || addr >= entry->end)
1477 if (!pmap_is_prefaultable(pmap, addr))
1480 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1481 lobject = entry->object.vm_object;
1483 VM_OBJECT_RLOCK(lobject);
1484 while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1485 lobject->type == OBJT_DEFAULT &&
1486 (backing_object = lobject->backing_object) != NULL) {
1487 KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
1488 0, ("vm_fault_prefault: unaligned object offset"));
1489 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1490 VM_OBJECT_RLOCK(backing_object);
1491 if (!obj_locked || lobject != entry->object.vm_object)
1492 VM_OBJECT_RUNLOCK(lobject);
1493 lobject = backing_object;
1496 if (!obj_locked || lobject != entry->object.vm_object)
1497 VM_OBJECT_RUNLOCK(lobject);
1500 if (m->valid == VM_PAGE_BITS_ALL &&
1501 (m->flags & PG_FICTITIOUS) == 0)
1502 pmap_enter_quick(pmap, addr, m, entry->protection);
1503 if (!obj_locked || lobject != entry->object.vm_object)
1504 VM_OBJECT_RUNLOCK(lobject);
1509 * Hold each of the physical pages that are mapped by the specified range of
1510 * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
1511 * and allow the specified types of access, "prot". If all of the implied
1512 * pages are successfully held, then the number of held pages is returned
1513 * together with pointers to those pages in the array "ma". However, if any
1514 * of the pages cannot be held, -1 is returned.
1517 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
1518 vm_prot_t prot, vm_page_t *ma, int max_count)
1520 vm_offset_t end, va;
1523 boolean_t pmap_failed;
1527 end = round_page(addr + len);
1528 addr = trunc_page(addr);
1531 * Check for illegal addresses.
1533 if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
1536 if (atop(end - addr) > max_count)
1537 panic("vm_fault_quick_hold_pages: count > max_count");
1538 count = atop(end - addr);
1541 * Most likely, the physical pages are resident in the pmap, so it is
1542 * faster to try pmap_extract_and_hold() first.
1544 pmap_failed = FALSE;
1545 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
1546 *mp = pmap_extract_and_hold(map->pmap, va, prot);
1549 else if ((prot & VM_PROT_WRITE) != 0 &&
1550 (*mp)->dirty != VM_PAGE_BITS_ALL) {
1552 * Explicitly dirty the physical page. Otherwise, the
1553 * caller's changes may go unnoticed because they are
1554 * performed through an unmanaged mapping or by a DMA
1557 * The object lock is not held here.
1558 * See vm_page_clear_dirty_mask().
1565 * One or more pages could not be held by the pmap. Either no
1566 * page was mapped at the specified virtual address or that
1567 * mapping had insufficient permissions. Attempt to fault in
1568 * and hold these pages.
1570 * If vm_fault_disable_pagefaults() was called,
1571 * i.e., TDP_NOFAULTING is set, we must not sleep nor
1572 * acquire MD VM locks, which means we must not call
1573 * vm_fault_hold(). Some (out of tree) callers mark
1574 * too wide a code area with vm_fault_disable_pagefaults()
1575 * already, use the VM_PROT_QUICK_NOFAULT flag to request
1576 * the proper behaviour explicitly.
1578 if ((prot & VM_PROT_QUICK_NOFAULT) != 0 &&
1579 (curthread->td_pflags & TDP_NOFAULTING) != 0)
1581 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1582 if (*mp == NULL && vm_fault_hold(map, va, prot,
1583 VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
1588 for (mp = ma; mp < ma + count; mp++)
1591 vm_page_unhold(*mp);
1592 vm_page_unlock(*mp);
1599 * vm_fault_copy_entry
1601 * Create new shadow object backing dst_entry with private copy of
1602 * all underlying pages. When src_entry is equal to dst_entry,
1603 * function implements COW for wired-down map entry. Otherwise,
1604 * it forks wired entry into dst_map.
1606 * In/out conditions:
1607 * The source and destination maps must be locked for write.
1608 * The source map entry must be wired down (or be a sharing map
1609 * entry corresponding to a main map entry that is wired down).
1612 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1613 vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1614 vm_ooffset_t *fork_charge)
1616 vm_object_t backing_object, dst_object, object, src_object;
1617 vm_pindex_t dst_pindex, pindex, src_pindex;
1618 vm_prot_t access, prot;
1628 upgrade = src_entry == dst_entry;
1629 access = prot = dst_entry->protection;
1631 src_object = src_entry->object.vm_object;
1632 src_pindex = OFF_TO_IDX(src_entry->offset);
1634 if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
1635 dst_object = src_object;
1636 vm_object_reference(dst_object);
1639 * Create the top-level object for the destination entry. (Doesn't
1640 * actually shadow anything - we copy the pages directly.)
1642 dst_object = vm_object_allocate(OBJT_DEFAULT,
1643 atop(dst_entry->end - dst_entry->start));
1644 #if VM_NRESERVLEVEL > 0
1645 dst_object->flags |= OBJ_COLORED;
1646 dst_object->pg_color = atop(dst_entry->start);
1648 dst_object->domain = src_object->domain;
1649 dst_object->charge = dst_entry->end - dst_entry->start;
1652 VM_OBJECT_WLOCK(dst_object);
1653 KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1654 ("vm_fault_copy_entry: vm_object not NULL"));
1655 if (src_object != dst_object) {
1656 dst_entry->object.vm_object = dst_object;
1657 dst_entry->offset = 0;
1659 if (fork_charge != NULL) {
1660 KASSERT(dst_entry->cred == NULL,
1661 ("vm_fault_copy_entry: leaked swp charge"));
1662 dst_object->cred = curthread->td_ucred;
1663 crhold(dst_object->cred);
1664 *fork_charge += dst_object->charge;
1665 } else if ((dst_object->type == OBJT_DEFAULT ||
1666 dst_object->type == OBJT_SWAP) &&
1667 dst_object->cred == NULL) {
1668 KASSERT(dst_entry->cred != NULL, ("no cred for entry %p",
1670 dst_object->cred = dst_entry->cred;
1671 dst_entry->cred = NULL;
1675 * If not an upgrade, then enter the mappings in the pmap as
1676 * read and/or execute accesses. Otherwise, enter them as
1679 * A writeable large page mapping is only created if all of
1680 * the constituent small page mappings are modified. Marking
1681 * PTEs as modified on inception allows promotion to happen
1682 * without taking potentially large number of soft faults.
1685 access &= ~VM_PROT_WRITE;
1688 * Loop through all of the virtual pages within the entry's
1689 * range, copying each page from the source object to the
1690 * destination object. Since the source is wired, those pages
1691 * must exist. In contrast, the destination is pageable.
1692 * Since the destination object doesn't share any backing storage
1693 * with the source object, all of its pages must be dirtied,
1694 * regardless of whether they can be written.
1696 for (vaddr = dst_entry->start, dst_pindex = 0;
1697 vaddr < dst_entry->end;
1698 vaddr += PAGE_SIZE, dst_pindex++) {
1701 * Find the page in the source object, and copy it in.
1702 * Because the source is wired down, the page will be
1705 if (src_object != dst_object)
1706 VM_OBJECT_RLOCK(src_object);
1707 object = src_object;
1708 pindex = src_pindex + dst_pindex;
1709 while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1710 (backing_object = object->backing_object) != NULL) {
1712 * Unless the source mapping is read-only or
1713 * it is presently being upgraded from
1714 * read-only, the first object in the shadow
1715 * chain should provide all of the pages. In
1716 * other words, this loop body should never be
1717 * executed when the source mapping is already
1720 KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 ||
1722 ("vm_fault_copy_entry: main object missing page"));
1724 VM_OBJECT_RLOCK(backing_object);
1725 pindex += OFF_TO_IDX(object->backing_object_offset);
1726 if (object != dst_object)
1727 VM_OBJECT_RUNLOCK(object);
1728 object = backing_object;
1730 KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing"));
1732 if (object != dst_object) {
1734 * Allocate a page in the destination object.
1736 dst_m = vm_page_alloc(dst_object, (src_object ==
1737 dst_object ? src_pindex : 0) + dst_pindex,
1739 if (dst_m == NULL) {
1740 VM_OBJECT_WUNLOCK(dst_object);
1741 VM_OBJECT_RUNLOCK(object);
1742 vm_wait(dst_object);
1743 VM_OBJECT_WLOCK(dst_object);
1746 pmap_copy_page(src_m, dst_m);
1747 VM_OBJECT_RUNLOCK(object);
1748 dst_m->valid = VM_PAGE_BITS_ALL;
1749 dst_m->dirty = VM_PAGE_BITS_ALL;
1752 if (vm_page_sleep_if_busy(dst_m, "fltupg"))
1754 if (dst_m->pindex >= dst_object->size)
1756 * We are upgrading. Index can occur
1757 * out of bounds if the object type is
1758 * vnode and the file was truncated.
1761 vm_page_xbusy(dst_m);
1762 KASSERT(dst_m->valid == VM_PAGE_BITS_ALL,
1763 ("invalid dst page %p", dst_m));
1765 VM_OBJECT_WUNLOCK(dst_object);
1768 * Enter it in the pmap. If a wired, copy-on-write
1769 * mapping is being replaced by a write-enabled
1770 * mapping, then wire that new mapping.
1772 pmap_enter(dst_map->pmap, vaddr, dst_m, prot,
1773 access | (upgrade ? PMAP_ENTER_WIRED : 0), 0);
1776 * Mark it no longer busy, and put it on the active list.
1778 VM_OBJECT_WLOCK(dst_object);
1781 if (src_m != dst_m) {
1782 vm_page_lock(src_m);
1783 vm_page_unwire(src_m, PQ_INACTIVE);
1784 vm_page_unlock(src_m);
1785 vm_page_lock(dst_m);
1786 vm_page_wire(dst_m);
1787 vm_page_unlock(dst_m);
1789 KASSERT(dst_m->wire_count > 0,
1790 ("dst_m %p is not wired", dst_m));
1793 vm_page_lock(dst_m);
1794 vm_page_activate(dst_m);
1795 vm_page_unlock(dst_m);
1797 vm_page_xunbusy(dst_m);
1799 VM_OBJECT_WUNLOCK(dst_object);
1801 dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1802 vm_object_deallocate(src_object);
1807 * Block entry into the machine-independent layer's page fault handler by
1808 * the calling thread. Subsequent calls to vm_fault() by that thread will
1809 * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of
1810 * spurious page faults.
1813 vm_fault_disable_pagefaults(void)
1816 return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
1820 vm_fault_enable_pagefaults(int save)
1823 curthread_pflags_restore(save);