2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
10 * This code is derived from software contributed to Berkeley by
11 * The Mach Operating System project at Carnegie-Mellon University.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * from: @(#)vm_fault.c 8.4 (Berkeley) 1/12/94
44 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
45 * All rights reserved.
47 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
49 * Permission to use, copy, modify and distribute this software and
50 * its documentation is hereby granted, provided that both the copyright
51 * notice and this permission notice appear in all copies of the
52 * software, derivative works or modified versions, and any portions
53 * thereof, and that both notices appear in supporting documentation.
55 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
57 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
59 * Carnegie Mellon requests users of this software to return to
61 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
62 * School of Computer Science
63 * Carnegie Mellon University
64 * Pittsburgh PA 15213-3890
66 * any improvements or extensions that they make and grant Carnegie the
67 * rights to redistribute these changes.
71 * Page fault handling module.
74 #include <sys/cdefs.h>
75 __FBSDID("$FreeBSD$");
77 #include "opt_ktrace.h"
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
86 #include <sys/racct.h>
87 #include <sys/resourcevar.h>
88 #include <sys/rwlock.h>
89 #include <sys/sysctl.h>
90 #include <sys/vmmeter.h>
91 #include <sys/vnode.h>
93 #include <sys/ktrace.h>
97 #include <vm/vm_param.h>
99 #include <vm/vm_map.h>
100 #include <vm/vm_object.h>
101 #include <vm/vm_page.h>
102 #include <vm/vm_pageout.h>
103 #include <vm/vm_kern.h>
104 #include <vm/vm_pager.h>
105 #include <vm/vm_extern.h>
106 #include <vm/vm_reserv.h>
111 #define VM_FAULT_READ_DEFAULT (1 + VM_FAULT_READ_AHEAD_INIT)
112 #define VM_FAULT_READ_MAX (1 + VM_FAULT_READ_AHEAD_MAX)
114 #define VM_FAULT_DONTNEED_MIN 1048576
121 vm_object_t first_object;
122 vm_pindex_t first_pindex;
124 vm_map_entry_t entry;
125 int lookup_still_valid;
129 static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr,
131 static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
132 int backward, int forward);
135 release_page(struct faultstate *fs)
138 vm_page_xunbusy(fs->m);
140 vm_page_deactivate(fs->m);
141 vm_page_unlock(fs->m);
146 unlock_map(struct faultstate *fs)
149 if (fs->lookup_still_valid) {
150 vm_map_lookup_done(fs->map, fs->entry);
151 fs->lookup_still_valid = FALSE;
156 unlock_and_deallocate(struct faultstate *fs)
159 vm_object_pip_wakeup(fs->object);
160 VM_OBJECT_WUNLOCK(fs->object);
161 if (fs->object != fs->first_object) {
162 VM_OBJECT_WLOCK(fs->first_object);
163 vm_page_lock(fs->first_m);
164 vm_page_free(fs->first_m);
165 vm_page_unlock(fs->first_m);
166 vm_object_pip_wakeup(fs->first_object);
167 VM_OBJECT_WUNLOCK(fs->first_object);
170 vm_object_deallocate(fs->first_object);
172 if (fs->vp != NULL) {
179 vm_fault_dirty(vm_map_entry_t entry, vm_page_t m, vm_prot_t prot,
180 vm_prot_t fault_type, int fault_flags, boolean_t set_wd)
182 boolean_t need_dirty;
184 if (((prot & VM_PROT_WRITE) == 0 &&
185 (fault_flags & VM_FAULT_DIRTY) == 0) ||
186 (m->oflags & VPO_UNMANAGED) != 0)
189 VM_OBJECT_ASSERT_LOCKED(m->object);
191 need_dirty = ((fault_type & VM_PROT_WRITE) != 0 &&
192 (fault_flags & VM_FAULT_WIRE) == 0) ||
193 (fault_flags & VM_FAULT_DIRTY) != 0;
196 vm_object_set_writeable_dirty(m->object);
199 * If two callers of vm_fault_dirty() with set_wd ==
200 * FALSE, one for the map entry with MAP_ENTRY_NOSYNC
201 * flag set, other with flag clear, race, it is
202 * possible for the no-NOSYNC thread to see m->dirty
203 * != 0 and not clear VPO_NOSYNC. Take vm_page lock
204 * around manipulation of VPO_NOSYNC and
205 * vm_page_dirty() call, to avoid the race and keep
206 * m->oflags consistent.
211 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
212 * if the page is already dirty to prevent data written with
213 * the expectation of being synced from not being synced.
214 * Likewise if this entry does not request NOSYNC then make
215 * sure the page isn't marked NOSYNC. Applications sharing
216 * data should use the same flags to avoid ping ponging.
218 if ((entry->eflags & MAP_ENTRY_NOSYNC) != 0) {
220 m->oflags |= VPO_NOSYNC;
223 m->oflags &= ~VPO_NOSYNC;
227 * If the fault is a write, we know that this page is being
228 * written NOW so dirty it explicitly to save on
229 * pmap_is_modified() calls later.
231 * Also tell the backing pager, if any, that it should remove
232 * any swap backing since the page is now dirty.
239 vm_pager_page_unswapped(m);
245 * Handle a page fault occurring at the given address,
246 * requiring the given permissions, in the map specified.
247 * If successful, the page is inserted into the
248 * associated physical map.
250 * NOTE: the given address should be truncated to the
251 * proper page address.
253 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
254 * a standard error specifying why the fault is fatal is returned.
256 * The map in question must be referenced, and remains so.
257 * Caller may hold no locks.
260 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
267 if ((td->td_pflags & TDP_NOFAULTING) != 0)
268 return (KERN_PROTECTION_FAILURE);
270 if (map != kernel_map && KTRPOINT(td, KTR_FAULT))
271 ktrfault(vaddr, fault_type);
273 result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags,
276 if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND))
283 vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
284 int fault_flags, vm_page_t *m_hold)
287 int alloc_req, era, faultcount, nera, result;
288 boolean_t dead, growstack, is_first_object_locked, wired;
290 vm_object_t next_object;
292 struct faultstate fs;
295 int ahead, behind, cluster_offset, error, locked;
299 PCPU_INC(cnt.v_vm_faults);
306 * Find the backing store object and offset into it to begin the
310 result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry,
311 &fs.first_object, &fs.first_pindex, &prot, &wired);
312 if (result != KERN_SUCCESS) {
313 if (growstack && result == KERN_INVALID_ADDRESS &&
315 result = vm_map_growstack(curproc, vaddr);
316 if (result != KERN_SUCCESS)
317 return (KERN_FAILURE);
324 map_generation = fs.map->timestamp;
326 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
327 panic("vm_fault: fault on nofault entry, addr: %lx",
331 if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
332 fs.entry->wiring_thread != curthread) {
333 vm_map_unlock_read(fs.map);
335 if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
336 (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
341 fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
342 vm_map_unlock_and_wait(fs.map, 0);
344 vm_map_unlock(fs.map);
349 fault_type = prot | (fault_type & VM_PROT_COPY);
351 KASSERT((fault_flags & VM_FAULT_WIRE) == 0,
352 ("!wired && VM_FAULT_WIRE"));
354 if (fs.vp == NULL /* avoid locked vnode leak */ &&
355 (fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0 &&
356 /* avoid calling vm_object_set_writeable_dirty() */
357 ((prot & VM_PROT_WRITE) == 0 ||
358 (fs.first_object->type != OBJT_VNODE &&
359 (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
360 (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0)) {
361 VM_OBJECT_RLOCK(fs.first_object);
362 if ((prot & VM_PROT_WRITE) != 0 &&
363 (fs.first_object->type == OBJT_VNODE ||
364 (fs.first_object->flags & OBJ_TMPFS_NODE) != 0) &&
365 (fs.first_object->flags & OBJ_MIGHTBEDIRTY) == 0)
367 m = vm_page_lookup(fs.first_object, fs.first_pindex);
368 /* A busy page can be mapped for read|execute access. */
369 if (m == NULL || ((prot & VM_PROT_WRITE) != 0 &&
370 vm_page_busied(m)) || m->valid != VM_PAGE_BITS_ALL)
372 result = pmap_enter(fs.map->pmap, vaddr, m, prot,
373 fault_type | PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED :
375 if (result != KERN_SUCCESS)
377 if (m_hold != NULL) {
383 vm_fault_dirty(fs.entry, m, prot, fault_type, fault_flags,
385 VM_OBJECT_RUNLOCK(fs.first_object);
387 vm_fault_prefault(&fs, vaddr, PFBAK, PFFOR);
388 vm_map_lookup_done(fs.map, fs.entry);
389 curthread->td_ru.ru_minflt++;
390 return (KERN_SUCCESS);
392 if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) {
393 VM_OBJECT_RUNLOCK(fs.first_object);
394 VM_OBJECT_WLOCK(fs.first_object);
397 VM_OBJECT_WLOCK(fs.first_object);
401 * Make a reference to this object to prevent its disposal while we
402 * are messing with it. Once we have the reference, the map is free
403 * to be diddled. Since objects reference their shadows (and copies),
404 * they will stay around as well.
406 * Bump the paging-in-progress count to prevent size changes (e.g.
407 * truncation operations) during I/O. This must be done after
408 * obtaining the vnode lock in order to avoid possible deadlocks.
410 vm_object_reference_locked(fs.first_object);
411 vm_object_pip_add(fs.first_object, 1);
413 fs.lookup_still_valid = TRUE;
418 * Search for the page at object/offset.
420 fs.object = fs.first_object;
421 fs.pindex = fs.first_pindex;
424 * If the object is marked for imminent termination,
425 * we retry here, since the collapse pass has raced
426 * with us. Otherwise, if we see terminally dead
427 * object, return fail.
429 if ((fs.object->flags & OBJ_DEAD) != 0) {
430 dead = fs.object->type == OBJT_DEAD;
431 unlock_and_deallocate(&fs);
433 return (KERN_PROTECTION_FAILURE);
439 * See if page is resident
441 fs.m = vm_page_lookup(fs.object, fs.pindex);
444 * Wait/Retry if the page is busy. We have to do this
445 * if the page is either exclusive or shared busy
446 * because the vm_pager may be using read busy for
447 * pageouts (and even pageins if it is the vnode
448 * pager), and we could end up trying to pagein and
449 * pageout the same page simultaneously.
451 * We can theoretically allow the busy case on a read
452 * fault if the page is marked valid, but since such
453 * pages are typically already pmap'd, putting that
454 * special case in might be more effort then it is
455 * worth. We cannot under any circumstances mess
456 * around with a shared busied page except, perhaps,
459 if (vm_page_busied(fs.m)) {
461 * Reference the page before unlocking and
462 * sleeping so that the page daemon is less
463 * likely to reclaim it.
465 vm_page_aflag_set(fs.m, PGA_REFERENCED);
466 if (fs.object != fs.first_object) {
467 if (!VM_OBJECT_TRYWLOCK(
469 VM_OBJECT_WUNLOCK(fs.object);
470 VM_OBJECT_WLOCK(fs.first_object);
471 VM_OBJECT_WLOCK(fs.object);
473 vm_page_lock(fs.first_m);
474 vm_page_free(fs.first_m);
475 vm_page_unlock(fs.first_m);
476 vm_object_pip_wakeup(fs.first_object);
477 VM_OBJECT_WUNLOCK(fs.first_object);
481 if (fs.m == vm_page_lookup(fs.object,
483 vm_page_sleep_if_busy(fs.m, "vmpfw");
485 vm_object_pip_wakeup(fs.object);
486 VM_OBJECT_WUNLOCK(fs.object);
487 PCPU_INC(cnt.v_intrans);
488 vm_object_deallocate(fs.first_object);
492 vm_page_remque(fs.m);
493 vm_page_unlock(fs.m);
496 * Mark page busy for other processes, and the
497 * pagedaemon. If it still isn't completely valid
498 * (readable), jump to readrest, else break-out ( we
502 if (fs.m->valid != VM_PAGE_BITS_ALL)
506 KASSERT(fs.m == NULL, ("fs.m should be NULL, not %p", fs.m));
509 * Page is not resident. If the pager might contain the page
510 * or this is the beginning of the search, allocate a new
511 * page. (Default objects are zero-fill, so there is no real
514 if (fs.object->type != OBJT_DEFAULT ||
515 fs.object == fs.first_object) {
516 if (fs.pindex >= fs.object->size) {
517 unlock_and_deallocate(&fs);
518 return (KERN_PROTECTION_FAILURE);
522 * Allocate a new page for this object/offset pair.
524 * Unlocked read of the p_flag is harmless. At
525 * worst, the P_KILLED might be not observed
526 * there, and allocation can fail, causing
527 * restart and new reading of the p_flag.
529 if (!vm_page_count_severe() || P_KILLED(curproc)) {
530 #if VM_NRESERVLEVEL > 0
531 vm_object_color(fs.object, atop(vaddr) -
534 alloc_req = P_KILLED(curproc) ?
535 VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
536 if (fs.object->type != OBJT_VNODE &&
537 fs.object->backing_object == NULL)
538 alloc_req |= VM_ALLOC_ZERO;
539 fs.m = vm_page_alloc(fs.object, fs.pindex,
543 unlock_and_deallocate(&fs);
546 } else if (fs.m->valid == VM_PAGE_BITS_ALL)
552 * We have either allocated a new page or found an existing
553 * page that is only partially valid.
555 * Attempt to fault-in the page if there is a chance that the
556 * pager has it, and potentially fault in additional pages
559 if (fs.object->type != OBJT_DEFAULT) {
561 u_char behavior = vm_map_entry_behavior(fs.entry);
563 era = fs.entry->read_ahead;
564 if (behavior == MAP_ENTRY_BEHAV_RANDOM ||
569 } else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
571 nera = VM_FAULT_READ_AHEAD_MAX;
573 if (vaddr == fs.entry->next_read)
574 vm_fault_dontneed(&fs, vaddr, ahead);
575 } else if (vaddr == fs.entry->next_read) {
577 * This is a sequential fault. Arithmetically
578 * increase the requested number of pages in
579 * the read-ahead window. The requested
580 * number of pages is "# of sequential faults
581 * x (read ahead min + 1) + read ahead min"
584 nera = VM_FAULT_READ_AHEAD_MIN;
587 if (nera > VM_FAULT_READ_AHEAD_MAX)
588 nera = VM_FAULT_READ_AHEAD_MAX;
591 if (era == VM_FAULT_READ_AHEAD_MAX)
592 vm_fault_dontneed(&fs, vaddr, ahead);
595 * This is a non-sequential fault. Request a
596 * cluster of pages that is aligned to a
597 * VM_FAULT_READ_DEFAULT page offset boundary
598 * within the object. Alignment to a page
599 * offset boundary is more likely to coincide
600 * with the underlying file system block than
601 * alignment to a virtual address boundary.
603 cluster_offset = fs.pindex %
604 VM_FAULT_READ_DEFAULT;
605 behind = ulmin(cluster_offset,
606 atop(vaddr - fs.entry->start));
608 ahead = VM_FAULT_READ_DEFAULT - 1 -
611 ahead = ulmin(ahead, atop(fs.entry->end - vaddr) - 1);
613 fs.entry->read_ahead = nera;
616 * Call the pager to retrieve the data, if any, after
617 * releasing the lock on the map. We hold a ref on
618 * fs.object and the pages are exclusive busied.
622 if (fs.object->type == OBJT_VNODE) {
623 vp = fs.object->handle;
626 else if (fs.vp != NULL) {
630 locked = VOP_ISLOCKED(vp);
632 if (locked != LK_EXCLUSIVE)
634 /* Do not sleep for vnode lock while fs.m is busy */
635 error = vget(vp, locked | LK_CANRECURSE |
636 LK_NOWAIT, curthread);
640 unlock_and_deallocate(&fs);
641 error = vget(vp, locked | LK_RETRY |
642 LK_CANRECURSE, curthread);
646 ("vm_fault: vget failed"));
652 KASSERT(fs.vp == NULL || !fs.map->system_map,
653 ("vm_fault: vnode-backed object mapped by system map"));
656 * Page in the requested page and hint the pager,
657 * that it may bring up surrounding pages.
659 rv = vm_pager_get_pages(fs.object, &fs.m, 1,
661 if (rv == VM_PAGER_OK) {
662 faultcount = behind + 1 + ahead;
664 break; /* break to PAGE HAS BEEN FOUND */
666 if (rv == VM_PAGER_ERROR)
667 printf("vm_fault: pager read error, pid %d (%s)\n",
668 curproc->p_pid, curproc->p_comm);
671 * If an I/O error occurred or the requested page was
672 * outside the range of the pager, clean up and return
675 if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) {
678 vm_page_unlock(fs.m);
680 unlock_and_deallocate(&fs);
681 return (rv == VM_PAGER_ERROR ? KERN_FAILURE :
682 KERN_PROTECTION_FAILURE);
686 * The requested page does not exist at this object/
687 * offset. Remove the invalid page from the object,
688 * waking up anyone waiting for it, and continue on to
689 * the next object. However, if this is the top-level
690 * object, we must leave the busy page in place to
691 * prevent another process from rushing past us, and
692 * inserting the page in that object at the same time
695 if (fs.object != fs.first_object) {
698 vm_page_unlock(fs.m);
704 * We get here if the object has default pager (or unwiring)
705 * or the pager doesn't have the page.
707 if (fs.object == fs.first_object)
711 * Move on to the next object. Lock the next object before
712 * unlocking the current one.
714 next_object = fs.object->backing_object;
715 if (next_object == NULL) {
717 * If there's no object left, fill the page in the top
720 if (fs.object != fs.first_object) {
721 vm_object_pip_wakeup(fs.object);
722 VM_OBJECT_WUNLOCK(fs.object);
724 fs.object = fs.first_object;
725 fs.pindex = fs.first_pindex;
727 VM_OBJECT_WLOCK(fs.object);
732 * Zero the page if necessary and mark it valid.
734 if ((fs.m->flags & PG_ZERO) == 0) {
735 pmap_zero_page(fs.m);
737 PCPU_INC(cnt.v_ozfod);
739 PCPU_INC(cnt.v_zfod);
740 fs.m->valid = VM_PAGE_BITS_ALL;
741 /* Don't try to prefault neighboring pages. */
743 break; /* break to PAGE HAS BEEN FOUND */
745 KASSERT(fs.object != next_object,
746 ("object loop %p", next_object));
747 VM_OBJECT_WLOCK(next_object);
748 vm_object_pip_add(next_object, 1);
749 if (fs.object != fs.first_object)
750 vm_object_pip_wakeup(fs.object);
752 OFF_TO_IDX(fs.object->backing_object_offset);
753 VM_OBJECT_WUNLOCK(fs.object);
754 fs.object = next_object;
758 vm_page_assert_xbusied(fs.m);
761 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
766 * If the page is being written, but isn't already owned by the
767 * top-level object, we have to copy it into a new page owned by the
770 if (fs.object != fs.first_object) {
772 * We only really need to copy if we want to write it.
774 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
776 * This allows pages to be virtually copied from a
777 * backing_object into the first_object, where the
778 * backing object has no other refs to it, and cannot
779 * gain any more refs. Instead of a bcopy, we just
780 * move the page from the backing object to the
781 * first object. Note that we must mark the page
782 * dirty in the first object so that it will go out
783 * to swap when needed.
785 is_first_object_locked = FALSE;
788 * Only one shadow object
790 (fs.object->shadow_count == 1) &&
792 * No COW refs, except us
794 (fs.object->ref_count == 1) &&
796 * No one else can look this object up
798 (fs.object->handle == NULL) &&
800 * No other ways to look the object up
802 ((fs.object->type == OBJT_DEFAULT) ||
803 (fs.object->type == OBJT_SWAP)) &&
804 (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
806 * We don't chase down the shadow chain
808 fs.object == fs.first_object->backing_object) {
810 vm_page_remove(fs.m);
811 vm_page_unlock(fs.m);
812 vm_page_lock(fs.first_m);
813 vm_page_replace_checked(fs.m, fs.first_object,
814 fs.first_pindex, fs.first_m);
815 vm_page_free(fs.first_m);
816 vm_page_unlock(fs.first_m);
818 #if VM_NRESERVLEVEL > 0
820 * Rename the reservation.
822 vm_reserv_rename(fs.m, fs.first_object,
823 fs.object, OFF_TO_IDX(
824 fs.first_object->backing_object_offset));
827 * Removing the page from the backing object
833 PCPU_INC(cnt.v_cow_optim);
836 * Oh, well, lets copy it.
838 pmap_copy_page(fs.m, fs.first_m);
839 fs.first_m->valid = VM_PAGE_BITS_ALL;
840 if (wired && (fault_flags &
841 VM_FAULT_WIRE) == 0) {
842 vm_page_lock(fs.first_m);
843 vm_page_wire(fs.first_m);
844 vm_page_unlock(fs.first_m);
847 vm_page_unwire(fs.m, PQ_INACTIVE);
848 vm_page_unlock(fs.m);
851 * We no longer need the old page or object.
856 * fs.object != fs.first_object due to above
859 vm_object_pip_wakeup(fs.object);
860 VM_OBJECT_WUNLOCK(fs.object);
862 * Only use the new page below...
864 fs.object = fs.first_object;
865 fs.pindex = fs.first_pindex;
867 if (!is_first_object_locked)
868 VM_OBJECT_WLOCK(fs.object);
869 PCPU_INC(cnt.v_cow_faults);
872 prot &= ~VM_PROT_WRITE;
877 * We must verify that the maps have not changed since our last
880 if (!fs.lookup_still_valid) {
881 vm_object_t retry_object;
882 vm_pindex_t retry_pindex;
883 vm_prot_t retry_prot;
885 if (!vm_map_trylock_read(fs.map)) {
887 unlock_and_deallocate(&fs);
890 fs.lookup_still_valid = TRUE;
891 if (fs.map->timestamp != map_generation) {
892 result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
893 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
896 * If we don't need the page any longer, put it on the inactive
897 * list (the easiest thing to do here). If no one needs it,
898 * pageout will grab it eventually.
900 if (result != KERN_SUCCESS) {
902 unlock_and_deallocate(&fs);
905 * If retry of map lookup would have blocked then
906 * retry fault from start.
908 if (result == KERN_FAILURE)
912 if ((retry_object != fs.first_object) ||
913 (retry_pindex != fs.first_pindex)) {
915 unlock_and_deallocate(&fs);
920 * Check whether the protection has changed or the object has
921 * been copied while we left the map unlocked. Changing from
922 * read to write permission is OK - we leave the page
923 * write-protected, and catch the write fault. Changing from
924 * write to read permission means that we can't mark the page
925 * write-enabled after all.
932 * If the page was filled by a pager, save the virtual address that
933 * should be faulted on next under a sequential access pattern to the
934 * map entry. A read lock on the map suffices to update this address
938 fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE;
940 vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags, TRUE);
941 vm_page_assert_xbusied(fs.m);
944 * Page must be completely valid or it is not fit to
945 * map into user space. vm_pager_get_pages() ensures this.
947 KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
948 ("vm_fault: page %p partially invalid", fs.m));
949 VM_OBJECT_WUNLOCK(fs.object);
952 * Put this page into the physical map. We had to do the unlock above
953 * because pmap_enter() may sleep. We don't put the page
954 * back on the active queue until later so that the pageout daemon
955 * won't find it (yet).
957 pmap_enter(fs.map->pmap, vaddr, fs.m, prot,
958 fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0);
959 if (faultcount != 1 && (fault_flags & VM_FAULT_WIRE) == 0 &&
961 vm_fault_prefault(&fs, vaddr,
962 faultcount > 0 ? behind : PFBAK,
963 faultcount > 0 ? ahead : PFFOR);
964 VM_OBJECT_WLOCK(fs.object);
968 * If the page is not wired down, then put it where the pageout daemon
971 if ((fault_flags & VM_FAULT_WIRE) != 0) {
972 KASSERT(wired, ("VM_FAULT_WIRE && !wired"));
975 vm_page_activate(fs.m);
976 if (m_hold != NULL) {
980 vm_page_unlock(fs.m);
981 vm_page_xunbusy(fs.m);
984 * Unlock everything, and return
986 unlock_and_deallocate(&fs);
988 PCPU_INC(cnt.v_io_faults);
989 curthread->td_ru.ru_majflt++;
991 if (racct_enable && fs.object->type == OBJT_VNODE) {
993 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
994 racct_add_force(curproc, RACCT_WRITEBPS,
995 PAGE_SIZE + behind * PAGE_SIZE);
996 racct_add_force(curproc, RACCT_WRITEIOPS, 1);
998 racct_add_force(curproc, RACCT_READBPS,
999 PAGE_SIZE + ahead * PAGE_SIZE);
1000 racct_add_force(curproc, RACCT_READIOPS, 1);
1002 PROC_UNLOCK(curproc);
1006 curthread->td_ru.ru_minflt++;
1008 return (KERN_SUCCESS);
1012 * Speed up the reclamation of pages that precede the faulting pindex within
1013 * the first object of the shadow chain. Essentially, perform the equivalent
1014 * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes
1015 * the faulting pindex by the cluster size when the pages read by vm_fault()
1016 * cross a cluster-size boundary. The cluster size is the greater of the
1017 * smallest superpage size and VM_FAULT_DONTNEED_MIN.
1019 * When "fs->first_object" is a shadow object, the pages in the backing object
1020 * that precede the faulting pindex are deactivated by vm_fault(). So, this
1021 * function must only be concerned with pages in the first object.
1024 vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead)
1026 vm_map_entry_t entry;
1027 vm_object_t first_object, object;
1028 vm_offset_t end, start;
1029 vm_page_t m, m_next;
1030 vm_pindex_t pend, pstart;
1033 object = fs->object;
1034 VM_OBJECT_ASSERT_WLOCKED(object);
1035 first_object = fs->first_object;
1036 if (first_object != object) {
1037 if (!VM_OBJECT_TRYWLOCK(first_object)) {
1038 VM_OBJECT_WUNLOCK(object);
1039 VM_OBJECT_WLOCK(first_object);
1040 VM_OBJECT_WLOCK(object);
1043 /* Neither fictitious nor unmanaged pages can be reclaimed. */
1044 if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
1045 size = VM_FAULT_DONTNEED_MIN;
1046 if (MAXPAGESIZES > 1 && size < pagesizes[1])
1047 size = pagesizes[1];
1048 end = rounddown2(vaddr, size);
1049 if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) &&
1050 (entry = fs->entry)->start < end) {
1051 if (end - entry->start < size)
1052 start = entry->start;
1055 pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED);
1056 pstart = OFF_TO_IDX(entry->offset) + atop(start -
1058 m_next = vm_page_find_least(first_object, pstart);
1059 pend = OFF_TO_IDX(entry->offset) + atop(end -
1061 while ((m = m_next) != NULL && m->pindex < pend) {
1062 m_next = TAILQ_NEXT(m, listq);
1063 if (m->valid != VM_PAGE_BITS_ALL ||
1068 * Don't clear PGA_REFERENCED, since it would
1069 * likely represent a reference by a different
1072 * Typically, at this point, prefetched pages
1073 * are still in the inactive queue. Only
1074 * pages that triggered page faults are in the
1078 vm_page_deactivate(m);
1083 if (first_object != object)
1084 VM_OBJECT_WUNLOCK(first_object);
1088 * vm_fault_prefault provides a quick way of clustering
1089 * pagefaults into a processes address space. It is a "cousin"
1090 * of vm_map_pmap_enter, except it runs at page fault time instead
1094 vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
1095 int backward, int forward)
1098 vm_map_entry_t entry;
1099 vm_object_t backing_object, lobject;
1100 vm_offset_t addr, starta;
1105 pmap = fs->map->pmap;
1106 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1111 starta = addra - backward * PAGE_SIZE;
1112 if (starta < entry->start) {
1113 starta = entry->start;
1114 } else if (starta > addra) {
1119 * Generate the sequence of virtual addresses that are candidates for
1120 * prefaulting in an outward spiral from the faulting virtual address,
1121 * "addra". Specifically, the sequence is "addra - PAGE_SIZE", "addra
1122 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
1123 * If the candidate address doesn't have a backing physical page, then
1124 * the loop immediately terminates.
1126 for (i = 0; i < 2 * imax(backward, forward); i++) {
1127 addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
1129 if (addr > addra + forward * PAGE_SIZE)
1132 if (addr < starta || addr >= entry->end)
1135 if (!pmap_is_prefaultable(pmap, addr))
1138 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1139 lobject = entry->object.vm_object;
1140 VM_OBJECT_RLOCK(lobject);
1141 while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1142 lobject->type == OBJT_DEFAULT &&
1143 (backing_object = lobject->backing_object) != NULL) {
1144 KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
1145 0, ("vm_fault_prefault: unaligned object offset"));
1146 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1147 VM_OBJECT_RLOCK(backing_object);
1148 VM_OBJECT_RUNLOCK(lobject);
1149 lobject = backing_object;
1152 VM_OBJECT_RUNLOCK(lobject);
1155 if (m->valid == VM_PAGE_BITS_ALL &&
1156 (m->flags & PG_FICTITIOUS) == 0)
1157 pmap_enter_quick(pmap, addr, m, entry->protection);
1158 VM_OBJECT_RUNLOCK(lobject);
1163 * Hold each of the physical pages that are mapped by the specified range of
1164 * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
1165 * and allow the specified types of access, "prot". If all of the implied
1166 * pages are successfully held, then the number of held pages is returned
1167 * together with pointers to those pages in the array "ma". However, if any
1168 * of the pages cannot be held, -1 is returned.
1171 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
1172 vm_prot_t prot, vm_page_t *ma, int max_count)
1174 vm_offset_t end, va;
1177 boolean_t pmap_failed;
1181 end = round_page(addr + len);
1182 addr = trunc_page(addr);
1185 * Check for illegal addresses.
1187 if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
1190 if (atop(end - addr) > max_count)
1191 panic("vm_fault_quick_hold_pages: count > max_count");
1192 count = atop(end - addr);
1195 * Most likely, the physical pages are resident in the pmap, so it is
1196 * faster to try pmap_extract_and_hold() first.
1198 pmap_failed = FALSE;
1199 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
1200 *mp = pmap_extract_and_hold(map->pmap, va, prot);
1203 else if ((prot & VM_PROT_WRITE) != 0 &&
1204 (*mp)->dirty != VM_PAGE_BITS_ALL) {
1206 * Explicitly dirty the physical page. Otherwise, the
1207 * caller's changes may go unnoticed because they are
1208 * performed through an unmanaged mapping or by a DMA
1211 * The object lock is not held here.
1212 * See vm_page_clear_dirty_mask().
1219 * One or more pages could not be held by the pmap. Either no
1220 * page was mapped at the specified virtual address or that
1221 * mapping had insufficient permissions. Attempt to fault in
1222 * and hold these pages.
1224 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1225 if (*mp == NULL && vm_fault_hold(map, va, prot,
1226 VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
1231 for (mp = ma; mp < ma + count; mp++)
1234 vm_page_unhold(*mp);
1235 vm_page_unlock(*mp);
1242 * vm_fault_copy_entry
1244 * Create new shadow object backing dst_entry with private copy of
1245 * all underlying pages. When src_entry is equal to dst_entry,
1246 * function implements COW for wired-down map entry. Otherwise,
1247 * it forks wired entry into dst_map.
1249 * In/out conditions:
1250 * The source and destination maps must be locked for write.
1251 * The source map entry must be wired down (or be a sharing map
1252 * entry corresponding to a main map entry that is wired down).
1255 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1256 vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1257 vm_ooffset_t *fork_charge)
1259 vm_object_t backing_object, dst_object, object, src_object;
1260 vm_pindex_t dst_pindex, pindex, src_pindex;
1261 vm_prot_t access, prot;
1271 upgrade = src_entry == dst_entry;
1272 access = prot = dst_entry->protection;
1274 src_object = src_entry->object.vm_object;
1275 src_pindex = OFF_TO_IDX(src_entry->offset);
1277 if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
1278 dst_object = src_object;
1279 vm_object_reference(dst_object);
1282 * Create the top-level object for the destination entry. (Doesn't
1283 * actually shadow anything - we copy the pages directly.)
1285 dst_object = vm_object_allocate(OBJT_DEFAULT,
1286 OFF_TO_IDX(dst_entry->end - dst_entry->start));
1287 #if VM_NRESERVLEVEL > 0
1288 dst_object->flags |= OBJ_COLORED;
1289 dst_object->pg_color = atop(dst_entry->start);
1293 VM_OBJECT_WLOCK(dst_object);
1294 KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1295 ("vm_fault_copy_entry: vm_object not NULL"));
1296 if (src_object != dst_object) {
1297 dst_entry->object.vm_object = dst_object;
1298 dst_entry->offset = 0;
1299 dst_object->charge = dst_entry->end - dst_entry->start;
1301 if (fork_charge != NULL) {
1302 KASSERT(dst_entry->cred == NULL,
1303 ("vm_fault_copy_entry: leaked swp charge"));
1304 dst_object->cred = curthread->td_ucred;
1305 crhold(dst_object->cred);
1306 *fork_charge += dst_object->charge;
1307 } else if (dst_object->cred == NULL) {
1308 KASSERT(dst_entry->cred != NULL, ("no cred for entry %p",
1310 dst_object->cred = dst_entry->cred;
1311 dst_entry->cred = NULL;
1315 * If not an upgrade, then enter the mappings in the pmap as
1316 * read and/or execute accesses. Otherwise, enter them as
1319 * A writeable large page mapping is only created if all of
1320 * the constituent small page mappings are modified. Marking
1321 * PTEs as modified on inception allows promotion to happen
1322 * without taking potentially large number of soft faults.
1325 access &= ~VM_PROT_WRITE;
1328 * Loop through all of the virtual pages within the entry's
1329 * range, copying each page from the source object to the
1330 * destination object. Since the source is wired, those pages
1331 * must exist. In contrast, the destination is pageable.
1332 * Since the destination object does share any backing storage
1333 * with the source object, all of its pages must be dirtied,
1334 * regardless of whether they can be written.
1336 for (vaddr = dst_entry->start, dst_pindex = 0;
1337 vaddr < dst_entry->end;
1338 vaddr += PAGE_SIZE, dst_pindex++) {
1341 * Find the page in the source object, and copy it in.
1342 * Because the source is wired down, the page will be
1345 if (src_object != dst_object)
1346 VM_OBJECT_RLOCK(src_object);
1347 object = src_object;
1348 pindex = src_pindex + dst_pindex;
1349 while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1350 (backing_object = object->backing_object) != NULL) {
1352 * Unless the source mapping is read-only or
1353 * it is presently being upgraded from
1354 * read-only, the first object in the shadow
1355 * chain should provide all of the pages. In
1356 * other words, this loop body should never be
1357 * executed when the source mapping is already
1360 KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 ||
1362 ("vm_fault_copy_entry: main object missing page"));
1364 VM_OBJECT_RLOCK(backing_object);
1365 pindex += OFF_TO_IDX(object->backing_object_offset);
1366 if (object != dst_object)
1367 VM_OBJECT_RUNLOCK(object);
1368 object = backing_object;
1370 KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing"));
1372 if (object != dst_object) {
1374 * Allocate a page in the destination object.
1376 dst_m = vm_page_alloc(dst_object, (src_object ==
1377 dst_object ? src_pindex : 0) + dst_pindex,
1379 if (dst_m == NULL) {
1380 VM_OBJECT_WUNLOCK(dst_object);
1381 VM_OBJECT_RUNLOCK(object);
1383 VM_OBJECT_WLOCK(dst_object);
1386 pmap_copy_page(src_m, dst_m);
1387 VM_OBJECT_RUNLOCK(object);
1388 dst_m->valid = VM_PAGE_BITS_ALL;
1389 dst_m->dirty = VM_PAGE_BITS_ALL;
1392 if (vm_page_sleep_if_busy(dst_m, "fltupg"))
1394 vm_page_xbusy(dst_m);
1395 KASSERT(dst_m->valid == VM_PAGE_BITS_ALL,
1396 ("invalid dst page %p", dst_m));
1398 VM_OBJECT_WUNLOCK(dst_object);
1401 * Enter it in the pmap. If a wired, copy-on-write
1402 * mapping is being replaced by a write-enabled
1403 * mapping, then wire that new mapping.
1405 pmap_enter(dst_map->pmap, vaddr, dst_m, prot,
1406 access | (upgrade ? PMAP_ENTER_WIRED : 0), 0);
1409 * Mark it no longer busy, and put it on the active list.
1411 VM_OBJECT_WLOCK(dst_object);
1414 if (src_m != dst_m) {
1415 vm_page_lock(src_m);
1416 vm_page_unwire(src_m, PQ_INACTIVE);
1417 vm_page_unlock(src_m);
1418 vm_page_lock(dst_m);
1419 vm_page_wire(dst_m);
1420 vm_page_unlock(dst_m);
1422 KASSERT(dst_m->wire_count > 0,
1423 ("dst_m %p is not wired", dst_m));
1426 vm_page_lock(dst_m);
1427 vm_page_activate(dst_m);
1428 vm_page_unlock(dst_m);
1430 vm_page_xunbusy(dst_m);
1432 VM_OBJECT_WUNLOCK(dst_object);
1434 dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1435 vm_object_deallocate(src_object);
1440 * Block entry into the machine-independent layer's page fault handler by
1441 * the calling thread. Subsequent calls to vm_fault() by that thread will
1442 * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of
1443 * spurious page faults.
1446 vm_fault_disable_pagefaults(void)
1449 return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
1453 vm_fault_enable_pagefaults(int save)
1456 curthread_pflags_restore(save);