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>
85 #include <sys/resourcevar.h>
86 #include <sys/rwlock.h>
87 #include <sys/sysctl.h>
88 #include <sys/vmmeter.h>
89 #include <sys/vnode.h>
91 #include <sys/ktrace.h>
95 #include <vm/vm_param.h>
97 #include <vm/vm_map.h>
98 #include <vm/vm_object.h>
99 #include <vm/vm_page.h>
100 #include <vm/vm_pageout.h>
101 #include <vm/vm_kern.h>
102 #include <vm/vm_pager.h>
103 #include <vm/vm_extern.h>
108 static int vm_fault_additional_pages(vm_page_t, int, int, vm_page_t *, int *);
110 #define VM_FAULT_READ_BEHIND 8
111 #define VM_FAULT_READ_DEFAULT (1 + VM_FAULT_READ_AHEAD_INIT)
112 #define VM_FAULT_READ_MAX (1 + VM_FAULT_READ_AHEAD_MAX)
113 #define VM_FAULT_NINCR (VM_FAULT_READ_MAX / VM_FAULT_READ_BEHIND)
114 #define VM_FAULT_SUM (VM_FAULT_NINCR * (VM_FAULT_NINCR + 1) / 2)
115 #define VM_FAULT_CACHE_BEHIND (VM_FAULT_READ_BEHIND * VM_FAULT_SUM)
122 vm_object_t first_object;
123 vm_pindex_t first_pindex;
125 vm_map_entry_t entry;
126 int lookup_still_valid;
130 static void vm_fault_cache_behind(const struct faultstate *fs, int distance);
131 static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
132 int faultcount, int reqpage);
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_CHANGE_WIRING) == 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);
243 * TRYPAGER - used by vm_fault to calculate whether the pager for the
244 * current object *might* contain the page.
246 * default objects are zero-fill, there is no real pager.
248 #define TRYPAGER (fs.object->type != OBJT_DEFAULT && \
249 ((fault_flags & VM_FAULT_CHANGE_WIRING) == 0 || wired))
254 * Handle a page fault occurring at the given address,
255 * requiring the given permissions, in the map specified.
256 * If successful, the page is inserted into the
257 * associated physical map.
259 * NOTE: the given address should be truncated to the
260 * proper page address.
262 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
263 * a standard error specifying why the fault is fatal is returned.
265 * The map in question must be referenced, and remains so.
266 * Caller may hold no locks.
269 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
276 if ((td->td_pflags & TDP_NOFAULTING) != 0)
277 return (KERN_PROTECTION_FAILURE);
279 if (map != kernel_map && KTRPOINT(td, KTR_FAULT))
280 ktrfault(vaddr, fault_type);
282 result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags,
285 if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND))
292 vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
293 int fault_flags, vm_page_t *m_hold)
296 int alloc_req, era, faultcount, nera, reqpage, result;
297 boolean_t growstack, is_first_object_locked, wired;
299 vm_object_t next_object;
300 vm_page_t marray[VM_FAULT_READ_MAX];
302 struct faultstate fs;
305 int ahead, behind, cluster_offset, error, locked;
309 PCPU_INC(cnt.v_vm_faults);
311 faultcount = reqpage = 0;
316 * Find the backing store object and offset into it to begin the
320 result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry,
321 &fs.first_object, &fs.first_pindex, &prot, &wired);
322 if (result != KERN_SUCCESS) {
323 if (growstack && result == KERN_INVALID_ADDRESS &&
325 result = vm_map_growstack(curproc, vaddr);
326 if (result != KERN_SUCCESS)
327 return (KERN_FAILURE);
334 map_generation = fs.map->timestamp;
336 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
337 panic("vm_fault: fault on nofault entry, addr: %lx",
341 if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
342 fs.entry->wiring_thread != curthread) {
343 vm_map_unlock_read(fs.map);
345 if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
346 (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
347 fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
348 vm_map_unlock_and_wait(fs.map, 0);
350 vm_map_unlock(fs.map);
355 fault_type = prot | (fault_type & VM_PROT_COPY);
357 if (fs.vp == NULL /* avoid locked vnode leak */ &&
358 (fault_flags & (VM_FAULT_CHANGE_WIRING | VM_FAULT_DIRTY)) == 0 &&
359 /* avoid calling vm_object_set_writeable_dirty() */
360 ((prot & VM_PROT_WRITE) == 0 ||
361 (fs.first_object->type != OBJT_VNODE &&
362 (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
363 (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0)) {
364 VM_OBJECT_RLOCK(fs.first_object);
365 if ((prot & VM_PROT_WRITE) != 0 &&
366 (fs.first_object->type == OBJT_VNODE ||
367 (fs.first_object->flags & OBJ_TMPFS_NODE) != 0) &&
368 (fs.first_object->flags & OBJ_MIGHTBEDIRTY) == 0)
370 m = vm_page_lookup(fs.first_object, fs.first_pindex);
371 /* A busy page can be mapped for read|execute access. */
372 if (m == NULL || ((prot & VM_PROT_WRITE) != 0 &&
373 vm_page_busied(m)) || m->valid != VM_PAGE_BITS_ALL)
375 result = pmap_enter(fs.map->pmap, vaddr, m, prot,
376 fault_type | PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED :
378 if (result != KERN_SUCCESS)
380 if (m_hold != NULL) {
386 vm_fault_dirty(fs.entry, m, prot, fault_type, fault_flags,
388 VM_OBJECT_RUNLOCK(fs.first_object);
390 vm_fault_prefault(&fs, vaddr, 0, 0);
391 vm_map_lookup_done(fs.map, fs.entry);
392 curthread->td_ru.ru_minflt++;
393 return (KERN_SUCCESS);
395 if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) {
396 VM_OBJECT_RUNLOCK(fs.first_object);
397 VM_OBJECT_WLOCK(fs.first_object);
400 VM_OBJECT_WLOCK(fs.first_object);
404 * Make a reference to this object to prevent its disposal while we
405 * are messing with it. Once we have the reference, the map is free
406 * to be diddled. Since objects reference their shadows (and copies),
407 * they will stay around as well.
409 * Bump the paging-in-progress count to prevent size changes (e.g.
410 * truncation operations) during I/O. This must be done after
411 * obtaining the vnode lock in order to avoid possible deadlocks.
413 vm_object_reference_locked(fs.first_object);
414 vm_object_pip_add(fs.first_object, 1);
416 fs.lookup_still_valid = TRUE;
421 * Search for the page at object/offset.
423 fs.object = fs.first_object;
424 fs.pindex = fs.first_pindex;
427 * If the object is dead, we stop here
429 if (fs.object->flags & OBJ_DEAD) {
430 unlock_and_deallocate(&fs);
431 return (KERN_PROTECTION_FAILURE);
435 * See if page is resident
437 fs.m = vm_page_lookup(fs.object, fs.pindex);
440 * Wait/Retry if the page is busy. We have to do this
441 * if the page is either exclusive or shared busy
442 * because the vm_pager may be using read busy for
443 * pageouts (and even pageins if it is the vnode
444 * pager), and we could end up trying to pagein and
445 * pageout the same page simultaneously.
447 * We can theoretically allow the busy case on a read
448 * fault if the page is marked valid, but since such
449 * pages are typically already pmap'd, putting that
450 * special case in might be more effort then it is
451 * worth. We cannot under any circumstances mess
452 * around with a shared busied page except, perhaps,
455 if (vm_page_busied(fs.m)) {
457 * Reference the page before unlocking and
458 * sleeping so that the page daemon is less
459 * likely to reclaim it.
461 vm_page_aflag_set(fs.m, PGA_REFERENCED);
462 if (fs.object != fs.first_object) {
463 if (!VM_OBJECT_TRYWLOCK(
465 VM_OBJECT_WUNLOCK(fs.object);
466 VM_OBJECT_WLOCK(fs.first_object);
467 VM_OBJECT_WLOCK(fs.object);
469 vm_page_lock(fs.first_m);
470 vm_page_free(fs.first_m);
471 vm_page_unlock(fs.first_m);
472 vm_object_pip_wakeup(fs.first_object);
473 VM_OBJECT_WUNLOCK(fs.first_object);
477 if (fs.m == vm_page_lookup(fs.object,
479 vm_page_sleep_if_busy(fs.m, "vmpfw");
481 vm_object_pip_wakeup(fs.object);
482 VM_OBJECT_WUNLOCK(fs.object);
483 PCPU_INC(cnt.v_intrans);
484 vm_object_deallocate(fs.first_object);
488 vm_page_remque(fs.m);
489 vm_page_unlock(fs.m);
492 * Mark page busy for other processes, and the
493 * pagedaemon. If it still isn't completely valid
494 * (readable), jump to readrest, else break-out ( we
498 if (fs.m->valid != VM_PAGE_BITS_ALL)
504 * Page is not resident, If this is the search termination
505 * or the pager might contain the page, allocate a new page.
507 if (TRYPAGER || fs.object == fs.first_object) {
508 if (fs.pindex >= fs.object->size) {
509 unlock_and_deallocate(&fs);
510 return (KERN_PROTECTION_FAILURE);
514 * Allocate a new page for this object/offset pair.
516 * Unlocked read of the p_flag is harmless. At
517 * worst, the P_KILLED might be not observed
518 * there, and allocation can fail, causing
519 * restart and new reading of the p_flag.
522 if (!vm_page_count_severe() || P_KILLED(curproc)) {
523 #if VM_NRESERVLEVEL > 0
524 if ((fs.object->flags & OBJ_COLORED) == 0) {
525 fs.object->flags |= OBJ_COLORED;
526 fs.object->pg_color = atop(vaddr) -
530 alloc_req = P_KILLED(curproc) ?
531 VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
532 if (fs.object->type != OBJT_VNODE &&
533 fs.object->backing_object == NULL)
534 alloc_req |= VM_ALLOC_ZERO;
535 fs.m = vm_page_alloc(fs.object, fs.pindex,
539 unlock_and_deallocate(&fs);
542 } else if (fs.m->valid == VM_PAGE_BITS_ALL)
548 * We have found a valid page or we have allocated a new page.
549 * The page thus may not be valid or may not be entirely
552 * Attempt to fault-in the page if there is a chance that the
553 * pager has it, and potentially fault in additional pages
558 u_char behavior = vm_map_entry_behavior(fs.entry);
560 era = fs.entry->read_ahead;
561 if (behavior == MAP_ENTRY_BEHAV_RANDOM ||
566 } else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
568 nera = VM_FAULT_READ_AHEAD_MAX;
570 if (fs.pindex == fs.entry->next_read)
571 vm_fault_cache_behind(&fs,
573 } else if (fs.pindex == fs.entry->next_read) {
575 * This is a sequential fault. Arithmetically
576 * increase the requested number of pages in
577 * the read-ahead window. The requested
578 * number of pages is "# of sequential faults
579 * x (read ahead min + 1) + read ahead min"
582 nera = VM_FAULT_READ_AHEAD_MIN;
585 if (nera > VM_FAULT_READ_AHEAD_MAX)
586 nera = VM_FAULT_READ_AHEAD_MAX;
589 if (era == VM_FAULT_READ_AHEAD_MAX)
590 vm_fault_cache_behind(&fs,
591 VM_FAULT_CACHE_BEHIND);
594 * This is a non-sequential fault. Request a
595 * cluster of pages that is aligned to a
596 * VM_FAULT_READ_DEFAULT page offset boundary
597 * within the object. Alignment to a page
598 * offset boundary is more likely to coincide
599 * with the underlying file system block than
600 * alignment to a virtual address boundary.
602 cluster_offset = fs.pindex %
603 VM_FAULT_READ_DEFAULT;
604 behind = ulmin(cluster_offset,
605 atop(vaddr - fs.entry->start));
607 ahead = VM_FAULT_READ_DEFAULT - 1 -
610 ahead = ulmin(ahead, atop(fs.entry->end - vaddr) - 1);
612 fs.entry->read_ahead = nera;
615 * Call the pager to retrieve the data, if any, after
616 * releasing the lock on the map. We hold a ref on
617 * fs.object and the pages are exclusive busied.
621 if (fs.object->type == OBJT_VNODE) {
622 vp = fs.object->handle;
625 else if (fs.vp != NULL) {
629 locked = VOP_ISLOCKED(vp);
631 if (locked != LK_EXCLUSIVE)
633 /* Do not sleep for vnode lock while fs.m is busy */
634 error = vget(vp, locked | LK_CANRECURSE |
635 LK_NOWAIT, curthread);
639 unlock_and_deallocate(&fs);
640 error = vget(vp, locked | LK_RETRY |
641 LK_CANRECURSE, curthread);
645 ("vm_fault: vget failed"));
651 KASSERT(fs.vp == NULL || !fs.map->system_map,
652 ("vm_fault: vnode-backed object mapped by system map"));
655 * now we find out if any other pages should be paged
656 * in at this time this routine checks to see if the
657 * pages surrounding this fault reside in the same
658 * object as the page for this fault. If they do,
659 * then they are faulted in also into the object. The
660 * array "marray" returned contains an array of
661 * vm_page_t structs where one of them is the
662 * vm_page_t passed to the routine. The reqpage
663 * return value is the index into the marray for the
664 * vm_page_t passed to the routine.
666 * fs.m plus the additional pages are exclusive busied.
668 faultcount = vm_fault_additional_pages(
669 fs.m, behind, ahead, marray, &reqpage);
672 vm_pager_get_pages(fs.object, marray, faultcount,
673 reqpage) : VM_PAGER_FAIL;
675 if (rv == VM_PAGER_OK) {
677 * Found the page. Leave it busy while we play
682 * Relookup in case pager changed page. Pager
683 * is responsible for disposition of old page
686 fs.m = vm_page_lookup(fs.object, fs.pindex);
688 unlock_and_deallocate(&fs);
693 break; /* break to PAGE HAS BEEN FOUND */
696 * Remove the bogus page (which does not exist at this
697 * object/offset); before doing so, we must get back
698 * our object lock to preserve our invariant.
700 * Also wake up any other process that may want to bring
703 * If this is the top-level object, we must leave the
704 * busy page to prevent another process from rushing
705 * past us, and inserting the page in that object at
706 * the same time that we are.
708 if (rv == VM_PAGER_ERROR)
709 printf("vm_fault: pager read error, pid %d (%s)\n",
710 curproc->p_pid, curproc->p_comm);
712 * Data outside the range of the pager or an I/O error
715 * XXX - the check for kernel_map is a kludge to work
716 * around having the machine panic on a kernel space
717 * fault w/ I/O error.
719 if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
720 (rv == VM_PAGER_BAD)) {
723 vm_page_unlock(fs.m);
725 unlock_and_deallocate(&fs);
726 return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
728 if (fs.object != fs.first_object) {
731 vm_page_unlock(fs.m);
734 * XXX - we cannot just fall out at this
735 * point, m has been freed and is invalid!
741 * We get here if the object has default pager (or unwiring)
742 * or the pager doesn't have the page.
744 if (fs.object == fs.first_object)
748 * Move on to the next object. Lock the next object before
749 * unlocking the current one.
751 fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
752 next_object = fs.object->backing_object;
753 if (next_object == NULL) {
755 * If there's no object left, fill the page in the top
758 if (fs.object != fs.first_object) {
759 vm_object_pip_wakeup(fs.object);
760 VM_OBJECT_WUNLOCK(fs.object);
762 fs.object = fs.first_object;
763 fs.pindex = fs.first_pindex;
765 VM_OBJECT_WLOCK(fs.object);
770 * Zero the page if necessary and mark it valid.
772 if ((fs.m->flags & PG_ZERO) == 0) {
773 pmap_zero_page(fs.m);
775 PCPU_INC(cnt.v_ozfod);
777 PCPU_INC(cnt.v_zfod);
778 fs.m->valid = VM_PAGE_BITS_ALL;
779 /* Don't try to prefault neighboring pages. */
781 break; /* break to PAGE HAS BEEN FOUND */
783 KASSERT(fs.object != next_object,
784 ("object loop %p", next_object));
785 VM_OBJECT_WLOCK(next_object);
786 vm_object_pip_add(next_object, 1);
787 if (fs.object != fs.first_object)
788 vm_object_pip_wakeup(fs.object);
789 VM_OBJECT_WUNLOCK(fs.object);
790 fs.object = next_object;
794 vm_page_assert_xbusied(fs.m);
797 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
802 * If the page is being written, but isn't already owned by the
803 * top-level object, we have to copy it into a new page owned by the
806 if (fs.object != fs.first_object) {
808 * We only really need to copy if we want to write it.
810 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
812 * This allows pages to be virtually copied from a
813 * backing_object into the first_object, where the
814 * backing object has no other refs to it, and cannot
815 * gain any more refs. Instead of a bcopy, we just
816 * move the page from the backing object to the
817 * first object. Note that we must mark the page
818 * dirty in the first object so that it will go out
819 * to swap when needed.
821 is_first_object_locked = FALSE;
824 * Only one shadow object
826 (fs.object->shadow_count == 1) &&
828 * No COW refs, except us
830 (fs.object->ref_count == 1) &&
832 * No one else can look this object up
834 (fs.object->handle == NULL) &&
836 * No other ways to look the object up
838 ((fs.object->type == OBJT_DEFAULT) ||
839 (fs.object->type == OBJT_SWAP)) &&
840 (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
842 * We don't chase down the shadow chain
844 fs.object == fs.first_object->backing_object) {
846 * get rid of the unnecessary page
848 vm_page_lock(fs.first_m);
849 vm_page_free(fs.first_m);
850 vm_page_unlock(fs.first_m);
852 * grab the page and put it into the
853 * process'es object. The page is
854 * automatically made dirty.
856 if (vm_page_rename(fs.m, fs.first_object,
858 unlock_and_deallocate(&fs);
864 PCPU_INC(cnt.v_cow_optim);
867 * Oh, well, lets copy it.
869 pmap_copy_page(fs.m, fs.first_m);
870 fs.first_m->valid = VM_PAGE_BITS_ALL;
871 if (wired && (fault_flags &
872 VM_FAULT_CHANGE_WIRING) == 0) {
873 vm_page_lock(fs.first_m);
874 vm_page_wire(fs.first_m);
875 vm_page_unlock(fs.first_m);
878 vm_page_unwire(fs.m, PQ_INACTIVE);
879 vm_page_unlock(fs.m);
882 * We no longer need the old page or object.
887 * fs.object != fs.first_object due to above
890 vm_object_pip_wakeup(fs.object);
891 VM_OBJECT_WUNLOCK(fs.object);
893 * Only use the new page below...
895 fs.object = fs.first_object;
896 fs.pindex = fs.first_pindex;
898 if (!is_first_object_locked)
899 VM_OBJECT_WLOCK(fs.object);
900 PCPU_INC(cnt.v_cow_faults);
903 prot &= ~VM_PROT_WRITE;
908 * We must verify that the maps have not changed since our last
911 if (!fs.lookup_still_valid) {
912 vm_object_t retry_object;
913 vm_pindex_t retry_pindex;
914 vm_prot_t retry_prot;
916 if (!vm_map_trylock_read(fs.map)) {
918 unlock_and_deallocate(&fs);
921 fs.lookup_still_valid = TRUE;
922 if (fs.map->timestamp != map_generation) {
923 result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
924 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
927 * If we don't need the page any longer, put it on the inactive
928 * list (the easiest thing to do here). If no one needs it,
929 * pageout will grab it eventually.
931 if (result != KERN_SUCCESS) {
933 unlock_and_deallocate(&fs);
936 * If retry of map lookup would have blocked then
937 * retry fault from start.
939 if (result == KERN_FAILURE)
943 if ((retry_object != fs.first_object) ||
944 (retry_pindex != fs.first_pindex)) {
946 unlock_and_deallocate(&fs);
951 * Check whether the protection has changed or the object has
952 * been copied while we left the map unlocked. Changing from
953 * read to write permission is OK - we leave the page
954 * write-protected, and catch the write fault. Changing from
955 * write to read permission means that we can't mark the page
956 * write-enabled after all.
962 * If the page was filled by a pager, update the map entry's
963 * last read offset. Since the pager does not return the
964 * actual set of pages that it read, this update is based on
965 * the requested set. Typically, the requested and actual
968 * XXX The following assignment modifies the map
969 * without holding a write lock on it.
972 fs.entry->next_read = fs.pindex + faultcount - reqpage;
974 vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags, TRUE);
975 vm_page_assert_xbusied(fs.m);
978 * Page must be completely valid or it is not fit to
979 * map into user space. vm_pager_get_pages() ensures this.
981 KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
982 ("vm_fault: page %p partially invalid", fs.m));
983 VM_OBJECT_WUNLOCK(fs.object);
986 * Put this page into the physical map. We had to do the unlock above
987 * because pmap_enter() may sleep. We don't put the page
988 * back on the active queue until later so that the pageout daemon
989 * won't find it (yet).
991 pmap_enter(fs.map->pmap, vaddr, fs.m, prot,
992 fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0);
993 if (faultcount != 1 && (fault_flags & VM_FAULT_CHANGE_WIRING) == 0 &&
995 vm_fault_prefault(&fs, vaddr, faultcount, reqpage);
996 VM_OBJECT_WLOCK(fs.object);
1000 * If the page is not wired down, then put it where the pageout daemon
1003 if (fault_flags & VM_FAULT_CHANGE_WIRING) {
1007 vm_page_unwire(fs.m, PQ_ACTIVE);
1009 vm_page_activate(fs.m);
1010 if (m_hold != NULL) {
1014 vm_page_unlock(fs.m);
1015 vm_page_xunbusy(fs.m);
1018 * Unlock everything, and return
1020 unlock_and_deallocate(&fs);
1022 PCPU_INC(cnt.v_io_faults);
1023 curthread->td_ru.ru_majflt++;
1025 curthread->td_ru.ru_minflt++;
1027 return (KERN_SUCCESS);
1031 * Speed up the reclamation of up to "distance" pages that precede the
1032 * faulting pindex within the first object of the shadow chain.
1035 vm_fault_cache_behind(const struct faultstate *fs, int distance)
1037 vm_object_t first_object, object;
1038 vm_page_t m, m_prev;
1041 object = fs->object;
1042 VM_OBJECT_ASSERT_WLOCKED(object);
1043 first_object = fs->first_object;
1044 if (first_object != object) {
1045 if (!VM_OBJECT_TRYWLOCK(first_object)) {
1046 VM_OBJECT_WUNLOCK(object);
1047 VM_OBJECT_WLOCK(first_object);
1048 VM_OBJECT_WLOCK(object);
1051 /* Neither fictitious nor unmanaged pages can be cached. */
1052 if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
1053 if (fs->first_pindex < distance)
1056 pindex = fs->first_pindex - distance;
1057 if (pindex < OFF_TO_IDX(fs->entry->offset))
1058 pindex = OFF_TO_IDX(fs->entry->offset);
1059 m = first_object != object ? fs->first_m : fs->m;
1060 vm_page_assert_xbusied(m);
1061 m_prev = vm_page_prev(m);
1062 while ((m = m_prev) != NULL && m->pindex >= pindex &&
1063 m->valid == VM_PAGE_BITS_ALL) {
1064 m_prev = vm_page_prev(m);
1065 if (vm_page_busied(m))
1068 if (m->hold_count == 0 && m->wire_count == 0) {
1070 vm_page_aflag_clear(m, PGA_REFERENCED);
1072 vm_page_deactivate(m);
1079 if (first_object != object)
1080 VM_OBJECT_WUNLOCK(first_object);
1084 * vm_fault_prefault provides a quick way of clustering
1085 * pagefaults into a processes address space. It is a "cousin"
1086 * of vm_map_pmap_enter, except it runs at page fault time instead
1090 vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
1091 int faultcount, int reqpage)
1094 vm_map_entry_t entry;
1095 vm_object_t backing_object, lobject;
1096 vm_offset_t addr, starta;
1099 int backward, forward, i;
1101 pmap = fs->map->pmap;
1102 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1105 if (faultcount > 0) {
1107 forward = faultcount - reqpage - 1;
1114 starta = addra - backward * PAGE_SIZE;
1115 if (starta < entry->start) {
1116 starta = entry->start;
1117 } else if (starta > addra) {
1122 * Generate the sequence of virtual addresses that are candidates for
1123 * prefaulting in an outward spiral from the faulting virtual address,
1124 * "addra". Specifically, the sequence is "addra - PAGE_SIZE", "addra
1125 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
1126 * If the candidate address doesn't have a backing physical page, then
1127 * the loop immediately terminates.
1129 for (i = 0; i < 2 * imax(backward, forward); i++) {
1130 addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
1132 if (addr > addra + forward * PAGE_SIZE)
1135 if (addr < starta || addr >= entry->end)
1138 if (!pmap_is_prefaultable(pmap, addr))
1141 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1142 lobject = entry->object.vm_object;
1143 VM_OBJECT_RLOCK(lobject);
1144 while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1145 lobject->type == OBJT_DEFAULT &&
1146 (backing_object = lobject->backing_object) != NULL) {
1147 KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
1148 0, ("vm_fault_prefault: unaligned object offset"));
1149 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1150 VM_OBJECT_RLOCK(backing_object);
1151 VM_OBJECT_RUNLOCK(lobject);
1152 lobject = backing_object;
1155 VM_OBJECT_RUNLOCK(lobject);
1158 if (m->valid == VM_PAGE_BITS_ALL &&
1159 (m->flags & PG_FICTITIOUS) == 0)
1160 pmap_enter_quick(pmap, addr, m, entry->protection);
1161 VM_OBJECT_RUNLOCK(lobject);
1166 * Hold each of the physical pages that are mapped by the specified range of
1167 * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
1168 * and allow the specified types of access, "prot". If all of the implied
1169 * pages are successfully held, then the number of held pages is returned
1170 * together with pointers to those pages in the array "ma". However, if any
1171 * of the pages cannot be held, -1 is returned.
1174 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
1175 vm_prot_t prot, vm_page_t *ma, int max_count)
1177 vm_offset_t end, va;
1180 boolean_t pmap_failed;
1184 end = round_page(addr + len);
1185 addr = trunc_page(addr);
1188 * Check for illegal addresses.
1190 if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
1193 if (atop(end - addr) > max_count)
1194 panic("vm_fault_quick_hold_pages: count > max_count");
1195 count = atop(end - addr);
1198 * Most likely, the physical pages are resident in the pmap, so it is
1199 * faster to try pmap_extract_and_hold() first.
1201 pmap_failed = FALSE;
1202 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
1203 *mp = pmap_extract_and_hold(map->pmap, va, prot);
1206 else if ((prot & VM_PROT_WRITE) != 0 &&
1207 (*mp)->dirty != VM_PAGE_BITS_ALL) {
1209 * Explicitly dirty the physical page. Otherwise, the
1210 * caller's changes may go unnoticed because they are
1211 * performed through an unmanaged mapping or by a DMA
1214 * The object lock is not held here.
1215 * See vm_page_clear_dirty_mask().
1222 * One or more pages could not be held by the pmap. Either no
1223 * page was mapped at the specified virtual address or that
1224 * mapping had insufficient permissions. Attempt to fault in
1225 * and hold these pages.
1227 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1228 if (*mp == NULL && vm_fault_hold(map, va, prot,
1229 VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
1234 for (mp = ma; mp < ma + count; mp++)
1237 vm_page_unhold(*mp);
1238 vm_page_unlock(*mp);
1245 * vm_fault_copy_entry
1247 * Create new shadow object backing dst_entry with private copy of
1248 * all underlying pages. When src_entry is equal to dst_entry,
1249 * function implements COW for wired-down map entry. Otherwise,
1250 * it forks wired entry into dst_map.
1252 * In/out conditions:
1253 * The source and destination maps must be locked for write.
1254 * The source map entry must be wired down (or be a sharing map
1255 * entry corresponding to a main map entry that is wired down).
1258 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1259 vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1260 vm_ooffset_t *fork_charge)
1262 vm_object_t backing_object, dst_object, object, src_object;
1263 vm_pindex_t dst_pindex, pindex, src_pindex;
1264 vm_prot_t access, prot;
1274 upgrade = src_entry == dst_entry;
1275 access = prot = dst_entry->protection;
1277 src_object = src_entry->object.vm_object;
1278 src_pindex = OFF_TO_IDX(src_entry->offset);
1280 if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
1281 dst_object = src_object;
1282 vm_object_reference(dst_object);
1285 * Create the top-level object for the destination entry. (Doesn't
1286 * actually shadow anything - we copy the pages directly.)
1288 dst_object = vm_object_allocate(OBJT_DEFAULT,
1289 OFF_TO_IDX(dst_entry->end - dst_entry->start));
1290 #if VM_NRESERVLEVEL > 0
1291 dst_object->flags |= OBJ_COLORED;
1292 dst_object->pg_color = atop(dst_entry->start);
1296 VM_OBJECT_WLOCK(dst_object);
1297 KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1298 ("vm_fault_copy_entry: vm_object not NULL"));
1299 if (src_object != dst_object) {
1300 dst_entry->object.vm_object = dst_object;
1301 dst_entry->offset = 0;
1302 dst_object->charge = dst_entry->end - dst_entry->start;
1304 if (fork_charge != NULL) {
1305 KASSERT(dst_entry->cred == NULL,
1306 ("vm_fault_copy_entry: leaked swp charge"));
1307 dst_object->cred = curthread->td_ucred;
1308 crhold(dst_object->cred);
1309 *fork_charge += dst_object->charge;
1310 } else if (dst_object->cred == NULL) {
1311 KASSERT(dst_entry->cred != NULL, ("no cred for entry %p",
1313 dst_object->cred = dst_entry->cred;
1314 dst_entry->cred = NULL;
1318 * If not an upgrade, then enter the mappings in the pmap as
1319 * read and/or execute accesses. Otherwise, enter them as
1322 * A writeable large page mapping is only created if all of
1323 * the constituent small page mappings are modified. Marking
1324 * PTEs as modified on inception allows promotion to happen
1325 * without taking potentially large number of soft faults.
1328 access &= ~VM_PROT_WRITE;
1331 * Loop through all of the virtual pages within the entry's
1332 * range, copying each page from the source object to the
1333 * destination object. Since the source is wired, those pages
1334 * must exist. In contrast, the destination is pageable.
1335 * Since the destination object does share any backing storage
1336 * with the source object, all of its pages must be dirtied,
1337 * regardless of whether they can be written.
1339 for (vaddr = dst_entry->start, dst_pindex = 0;
1340 vaddr < dst_entry->end;
1341 vaddr += PAGE_SIZE, dst_pindex++) {
1344 * Find the page in the source object, and copy it in.
1345 * Because the source is wired down, the page will be
1348 if (src_object != dst_object)
1349 VM_OBJECT_RLOCK(src_object);
1350 object = src_object;
1351 pindex = src_pindex + dst_pindex;
1352 while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1353 (backing_object = object->backing_object) != NULL) {
1355 * Unless the source mapping is read-only or
1356 * it is presently being upgraded from
1357 * read-only, the first object in the shadow
1358 * chain should provide all of the pages. In
1359 * other words, this loop body should never be
1360 * executed when the source mapping is already
1363 KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 ||
1365 ("vm_fault_copy_entry: main object missing page"));
1367 VM_OBJECT_RLOCK(backing_object);
1368 pindex += OFF_TO_IDX(object->backing_object_offset);
1369 if (object != dst_object)
1370 VM_OBJECT_RUNLOCK(object);
1371 object = backing_object;
1373 KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing"));
1375 if (object != dst_object) {
1377 * Allocate a page in the destination object.
1379 dst_m = vm_page_alloc(dst_object, (src_object ==
1380 dst_object ? src_pindex : 0) + dst_pindex,
1382 if (dst_m == NULL) {
1383 VM_OBJECT_WUNLOCK(dst_object);
1384 VM_OBJECT_RUNLOCK(object);
1386 VM_OBJECT_WLOCK(dst_object);
1389 pmap_copy_page(src_m, dst_m);
1390 VM_OBJECT_RUNLOCK(object);
1391 dst_m->valid = VM_PAGE_BITS_ALL;
1392 dst_m->dirty = VM_PAGE_BITS_ALL;
1395 if (vm_page_sleep_if_busy(dst_m, "fltupg"))
1397 vm_page_xbusy(dst_m);
1398 KASSERT(dst_m->valid == VM_PAGE_BITS_ALL,
1399 ("invalid dst page %p", dst_m));
1401 VM_OBJECT_WUNLOCK(dst_object);
1404 * Enter it in the pmap. If a wired, copy-on-write
1405 * mapping is being replaced by a write-enabled
1406 * mapping, then wire that new mapping.
1408 pmap_enter(dst_map->pmap, vaddr, dst_m, prot,
1409 access | (upgrade ? PMAP_ENTER_WIRED : 0), 0);
1412 * Mark it no longer busy, and put it on the active list.
1414 VM_OBJECT_WLOCK(dst_object);
1417 if (src_m != dst_m) {
1418 vm_page_lock(src_m);
1419 vm_page_unwire(src_m, PQ_INACTIVE);
1420 vm_page_unlock(src_m);
1421 vm_page_lock(dst_m);
1422 vm_page_wire(dst_m);
1423 vm_page_unlock(dst_m);
1425 KASSERT(dst_m->wire_count > 0,
1426 ("dst_m %p is not wired", dst_m));
1429 vm_page_lock(dst_m);
1430 vm_page_activate(dst_m);
1431 vm_page_unlock(dst_m);
1433 vm_page_xunbusy(dst_m);
1435 VM_OBJECT_WUNLOCK(dst_object);
1437 dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1438 vm_object_deallocate(src_object);
1444 * This routine checks around the requested page for other pages that
1445 * might be able to be faulted in. This routine brackets the viable
1446 * pages for the pages to be paged in.
1449 * m, rbehind, rahead
1452 * marray (array of vm_page_t), reqpage (index of requested page)
1455 * number of pages in marray
1458 vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
1467 vm_pindex_t pindex, startpindex, endpindex, tpindex;
1469 int cbehind, cahead;
1471 VM_OBJECT_ASSERT_WLOCKED(m->object);
1475 cbehind = cahead = 0;
1478 * if the requested page is not available, then give up now
1480 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1484 if ((cbehind == 0) && (cahead == 0)) {
1490 if (rahead > cahead) {
1494 if (rbehind > cbehind) {
1499 * scan backward for the read behind pages -- in memory
1502 if (rbehind > pindex) {
1506 startpindex = pindex - rbehind;
1509 if ((rtm = TAILQ_PREV(m, pglist, listq)) != NULL &&
1510 rtm->pindex >= startpindex)
1511 startpindex = rtm->pindex + 1;
1513 /* tpindex is unsigned; beware of numeric underflow. */
1514 for (i = 0, tpindex = pindex - 1; tpindex >= startpindex &&
1515 tpindex < pindex; i++, tpindex--) {
1517 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
1518 VM_ALLOC_IFNOTCACHED);
1521 * Shift the allocated pages to the
1522 * beginning of the array.
1524 for (j = 0; j < i; j++) {
1525 marray[j] = marray[j + tpindex + 1 -
1531 marray[tpindex - startpindex] = rtm;
1539 /* page offset of the required page */
1542 tpindex = pindex + 1;
1546 * scan forward for the read ahead pages
1548 endpindex = tpindex + rahead;
1549 if ((rtm = TAILQ_NEXT(m, listq)) != NULL && rtm->pindex < endpindex)
1550 endpindex = rtm->pindex;
1551 if (endpindex > object->size)
1552 endpindex = object->size;
1554 for (; tpindex < endpindex; i++, tpindex++) {
1556 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
1557 VM_ALLOC_IFNOTCACHED);
1565 /* return number of pages */
1570 * Block entry into the machine-independent layer's page fault handler by
1571 * the calling thread. Subsequent calls to vm_fault() by that thread will
1572 * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of
1573 * spurious page faults.
1576 vm_fault_disable_pagefaults(void)
1579 return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
1583 vm_fault_enable_pagefaults(int save)
1586 curthread_pflags_restore(save);