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>
104 #include <vm/vm_reserv.h>
109 static int vm_fault_additional_pages(vm_page_t, int, int, vm_page_t *, int *);
111 #define VM_FAULT_READ_BEHIND 8
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_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)
288 int alloc_req, era, faultcount, nera, reqpage, result;
289 boolean_t dead, growstack, is_first_object_locked, wired;
291 vm_object_t next_object;
292 vm_page_t marray[VM_FAULT_READ_MAX];
294 struct faultstate fs;
301 PCPU_INC(cnt.v_vm_faults);
303 faultcount = reqpage = 0;
308 * Find the backing store object and offset into it to begin the
312 result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry,
313 &fs.first_object, &fs.first_pindex, &prot, &wired);
314 if (result != KERN_SUCCESS) {
315 if (growstack && result == KERN_INVALID_ADDRESS &&
317 result = vm_map_growstack(curproc, vaddr);
318 if (result != KERN_SUCCESS)
319 return (KERN_FAILURE);
326 map_generation = fs.map->timestamp;
328 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
329 panic("vm_fault: fault on nofault entry, addr: %lx",
333 if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
334 fs.entry->wiring_thread != curthread) {
335 vm_map_unlock_read(fs.map);
337 if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
338 (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
343 fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
344 vm_map_unlock_and_wait(fs.map, 0);
346 vm_map_unlock(fs.map);
351 fault_type = prot | (fault_type & VM_PROT_COPY);
353 KASSERT((fault_flags & VM_FAULT_WIRE) == 0,
354 ("!wired && VM_FAULT_WIRE"));
356 if (fs.vp == NULL /* avoid locked vnode leak */ &&
357 (fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0 &&
358 /* avoid calling vm_object_set_writeable_dirty() */
359 ((prot & VM_PROT_WRITE) == 0 ||
360 (fs.first_object->type != OBJT_VNODE &&
361 (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
362 (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0)) {
363 VM_OBJECT_RLOCK(fs.first_object);
364 if ((prot & VM_PROT_WRITE) != 0 &&
365 (fs.first_object->type == OBJT_VNODE ||
366 (fs.first_object->flags & OBJ_TMPFS_NODE) != 0) &&
367 (fs.first_object->flags & OBJ_MIGHTBEDIRTY) == 0)
369 m = vm_page_lookup(fs.first_object, fs.first_pindex);
370 /* A busy page can be mapped for read|execute access. */
371 if (m == NULL || ((prot & VM_PROT_WRITE) != 0 &&
372 vm_page_busied(m)) || m->valid != VM_PAGE_BITS_ALL)
374 result = pmap_enter(fs.map->pmap, vaddr, m, prot,
375 fault_type | PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED :
377 if (result != KERN_SUCCESS)
379 if (m_hold != NULL) {
385 vm_fault_dirty(fs.entry, m, prot, fault_type, fault_flags,
387 VM_OBJECT_RUNLOCK(fs.first_object);
389 vm_fault_prefault(&fs, vaddr, 0, 0);
390 vm_map_lookup_done(fs.map, fs.entry);
391 curthread->td_ru.ru_minflt++;
392 return (KERN_SUCCESS);
394 if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) {
395 VM_OBJECT_RUNLOCK(fs.first_object);
396 VM_OBJECT_WLOCK(fs.first_object);
399 VM_OBJECT_WLOCK(fs.first_object);
403 * Make a reference to this object to prevent its disposal while we
404 * are messing with it. Once we have the reference, the map is free
405 * to be diddled. Since objects reference their shadows (and copies),
406 * they will stay around as well.
408 * Bump the paging-in-progress count to prevent size changes (e.g.
409 * truncation operations) during I/O. This must be done after
410 * obtaining the vnode lock in order to avoid possible deadlocks.
412 vm_object_reference_locked(fs.first_object);
413 vm_object_pip_add(fs.first_object, 1);
415 fs.lookup_still_valid = TRUE;
420 * Search for the page at object/offset.
422 fs.object = fs.first_object;
423 fs.pindex = fs.first_pindex;
426 * If the object is marked for imminent termination,
427 * we retry here, since the collapse pass has raced
428 * with us. Otherwise, if we see terminally dead
429 * object, return fail.
431 if ((fs.object->flags & OBJ_DEAD) != 0) {
432 dead = fs.object->type == OBJT_DEAD;
433 unlock_and_deallocate(&fs);
435 return (KERN_PROTECTION_FAILURE);
441 * See if page is resident
443 fs.m = vm_page_lookup(fs.object, fs.pindex);
446 * Wait/Retry if the page is busy. We have to do this
447 * if the page is either exclusive or shared busy
448 * because the vm_pager may be using read busy for
449 * pageouts (and even pageins if it is the vnode
450 * pager), and we could end up trying to pagein and
451 * pageout the same page simultaneously.
453 * We can theoretically allow the busy case on a read
454 * fault if the page is marked valid, but since such
455 * pages are typically already pmap'd, putting that
456 * special case in might be more effort then it is
457 * worth. We cannot under any circumstances mess
458 * around with a shared busied page except, perhaps,
461 if (vm_page_busied(fs.m)) {
463 * Reference the page before unlocking and
464 * sleeping so that the page daemon is less
465 * likely to reclaim it.
467 vm_page_aflag_set(fs.m, PGA_REFERENCED);
468 if (fs.object != fs.first_object) {
469 if (!VM_OBJECT_TRYWLOCK(
471 VM_OBJECT_WUNLOCK(fs.object);
472 VM_OBJECT_WLOCK(fs.first_object);
473 VM_OBJECT_WLOCK(fs.object);
475 vm_page_lock(fs.first_m);
476 vm_page_free(fs.first_m);
477 vm_page_unlock(fs.first_m);
478 vm_object_pip_wakeup(fs.first_object);
479 VM_OBJECT_WUNLOCK(fs.first_object);
483 if (fs.m == vm_page_lookup(fs.object,
485 vm_page_sleep_if_busy(fs.m, "vmpfw");
487 vm_object_pip_wakeup(fs.object);
488 VM_OBJECT_WUNLOCK(fs.object);
489 PCPU_INC(cnt.v_intrans);
490 vm_object_deallocate(fs.first_object);
494 vm_page_remque(fs.m);
495 vm_page_unlock(fs.m);
498 * Mark page busy for other processes, and the
499 * pagedaemon. If it still isn't completely valid
500 * (readable), jump to readrest, else break-out ( we
504 if (fs.m->valid != VM_PAGE_BITS_ALL)
510 * Page is not resident. If this is the search termination
511 * or the pager might contain the page, allocate a new page.
512 * Default objects are zero-fill, there is no real pager.
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.
530 if (!vm_page_count_severe() || P_KILLED(curproc)) {
531 #if VM_NRESERVLEVEL > 0
532 if ((fs.object->flags & OBJ_COLORED) == 0) {
533 fs.object->flags |= OBJ_COLORED;
534 fs.object->pg_color = atop(vaddr) -
538 alloc_req = P_KILLED(curproc) ?
539 VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
540 if (fs.object->type != OBJT_VNODE &&
541 fs.object->backing_object == NULL)
542 alloc_req |= VM_ALLOC_ZERO;
543 fs.m = vm_page_alloc(fs.object, fs.pindex,
547 unlock_and_deallocate(&fs);
550 } else if (fs.m->valid == VM_PAGE_BITS_ALL)
556 * We have found a valid page or we have allocated a new page.
557 * The page thus may not be valid or may not be entirely
560 * Attempt to fault-in the page if there is a chance that the
561 * pager has it, and potentially fault in additional pages
562 * at the same time. For default objects simply provide
565 if (fs.object->type != OBJT_DEFAULT) {
567 u_char behavior = vm_map_entry_behavior(fs.entry);
569 if (behavior == MAP_ENTRY_BEHAV_RANDOM ||
573 } else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
575 ahead = atop(fs.entry->end - vaddr) - 1;
576 if (ahead > VM_FAULT_READ_AHEAD_MAX)
577 ahead = VM_FAULT_READ_AHEAD_MAX;
578 if (fs.pindex == fs.entry->next_read)
579 vm_fault_cache_behind(&fs,
583 * If this is a sequential page fault, then
584 * arithmetically increase the number of pages
585 * in the read-ahead window. Otherwise, reset
586 * the read-ahead window to its smallest size.
588 behind = atop(vaddr - fs.entry->start);
589 if (behind > VM_FAULT_READ_BEHIND)
590 behind = VM_FAULT_READ_BEHIND;
591 ahead = atop(fs.entry->end - vaddr) - 1;
592 era = fs.entry->read_ahead;
593 if (fs.pindex == fs.entry->next_read) {
595 if (nera > VM_FAULT_READ_AHEAD_MAX)
596 nera = VM_FAULT_READ_AHEAD_MAX;
600 if (era == VM_FAULT_READ_AHEAD_MAX)
601 vm_fault_cache_behind(&fs,
602 VM_FAULT_CACHE_BEHIND);
603 } else if (ahead > VM_FAULT_READ_AHEAD_MIN)
604 ahead = VM_FAULT_READ_AHEAD_MIN;
606 fs.entry->read_ahead = ahead;
610 * Call the pager to retrieve the data, if any, after
611 * releasing the lock on the map. We hold a ref on
612 * fs.object and the pages are exclusive busied.
616 if (fs.object->type == OBJT_VNODE) {
617 vp = fs.object->handle;
620 else if (fs.vp != NULL) {
624 locked = VOP_ISLOCKED(vp);
626 if (locked != LK_EXCLUSIVE)
628 /* Do not sleep for vnode lock while fs.m is busy */
629 error = vget(vp, locked | LK_CANRECURSE |
630 LK_NOWAIT, curthread);
634 unlock_and_deallocate(&fs);
635 error = vget(vp, locked | LK_RETRY |
636 LK_CANRECURSE, curthread);
640 ("vm_fault: vget failed"));
646 KASSERT(fs.vp == NULL || !fs.map->system_map,
647 ("vm_fault: vnode-backed object mapped by system map"));
650 * now we find out if any other pages should be paged
651 * in at this time this routine checks to see if the
652 * pages surrounding this fault reside in the same
653 * object as the page for this fault. If they do,
654 * then they are faulted in also into the object. The
655 * array "marray" returned contains an array of
656 * vm_page_t structs where one of them is the
657 * vm_page_t passed to the routine. The reqpage
658 * return value is the index into the marray for the
659 * vm_page_t passed to the routine.
661 * fs.m plus the additional pages are exclusive busied.
663 faultcount = vm_fault_additional_pages(
664 fs.m, behind, ahead, marray, &reqpage);
667 vm_pager_get_pages(fs.object, marray, faultcount,
668 reqpage) : VM_PAGER_FAIL;
670 if (rv == VM_PAGER_OK) {
672 * Found the page. Leave it busy while we play
677 * Relookup in case pager changed page. Pager
678 * is responsible for disposition of old page
681 fs.m = vm_page_lookup(fs.object, fs.pindex);
683 unlock_and_deallocate(&fs);
688 break; /* break to PAGE HAS BEEN FOUND */
691 * Remove the bogus page (which does not exist at this
692 * object/offset); before doing so, we must get back
693 * our object lock to preserve our invariant.
695 * Also wake up any other process that may want to bring
698 * If this is the top-level object, we must leave the
699 * busy page to prevent another process from rushing
700 * past us, and inserting the page in that object at
701 * the same time that we are.
703 if (rv == VM_PAGER_ERROR)
704 printf("vm_fault: pager read error, pid %d (%s)\n",
705 curproc->p_pid, curproc->p_comm);
707 * Data outside the range of the pager or an I/O error
710 * XXX - the check for kernel_map is a kludge to work
711 * around having the machine panic on a kernel space
712 * fault w/ I/O error.
714 if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
715 (rv == VM_PAGER_BAD)) {
718 vm_page_unlock(fs.m);
720 unlock_and_deallocate(&fs);
721 return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
723 if (fs.object != fs.first_object) {
726 vm_page_unlock(fs.m);
729 * XXX - we cannot just fall out at this
730 * point, m has been freed and is invalid!
736 * We get here if the object has default pager (or unwiring)
737 * or the pager doesn't have the page.
739 if (fs.object == fs.first_object)
743 * Move on to the next object. Lock the next object before
744 * unlocking the current one.
746 fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
747 next_object = fs.object->backing_object;
748 if (next_object == NULL) {
750 * If there's no object left, fill the page in the top
753 if (fs.object != fs.first_object) {
754 vm_object_pip_wakeup(fs.object);
755 VM_OBJECT_WUNLOCK(fs.object);
757 fs.object = fs.first_object;
758 fs.pindex = fs.first_pindex;
760 VM_OBJECT_WLOCK(fs.object);
765 * Zero the page if necessary and mark it valid.
767 if ((fs.m->flags & PG_ZERO) == 0) {
768 pmap_zero_page(fs.m);
770 PCPU_INC(cnt.v_ozfod);
772 PCPU_INC(cnt.v_zfod);
773 fs.m->valid = VM_PAGE_BITS_ALL;
774 /* Don't try to prefault neighboring pages. */
776 break; /* break to PAGE HAS BEEN FOUND */
778 KASSERT(fs.object != next_object,
779 ("object loop %p", next_object));
780 VM_OBJECT_WLOCK(next_object);
781 vm_object_pip_add(next_object, 1);
782 if (fs.object != fs.first_object)
783 vm_object_pip_wakeup(fs.object);
784 VM_OBJECT_WUNLOCK(fs.object);
785 fs.object = next_object;
789 vm_page_assert_xbusied(fs.m);
792 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
797 * If the page is being written, but isn't already owned by the
798 * top-level object, we have to copy it into a new page owned by the
801 if (fs.object != fs.first_object) {
803 * We only really need to copy if we want to write it.
805 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
807 * This allows pages to be virtually copied from a
808 * backing_object into the first_object, where the
809 * backing object has no other refs to it, and cannot
810 * gain any more refs. Instead of a bcopy, we just
811 * move the page from the backing object to the
812 * first object. Note that we must mark the page
813 * dirty in the first object so that it will go out
814 * to swap when needed.
816 is_first_object_locked = FALSE;
819 * Only one shadow object
821 (fs.object->shadow_count == 1) &&
823 * No COW refs, except us
825 (fs.object->ref_count == 1) &&
827 * No one else can look this object up
829 (fs.object->handle == NULL) &&
831 * No other ways to look the object up
833 ((fs.object->type == OBJT_DEFAULT) ||
834 (fs.object->type == OBJT_SWAP)) &&
835 (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
837 * We don't chase down the shadow chain
839 fs.object == fs.first_object->backing_object) {
841 * get rid of the unnecessary page
843 vm_page_lock(fs.first_m);
844 vm_page_free(fs.first_m);
845 vm_page_unlock(fs.first_m);
847 * grab the page and put it into the
848 * process'es object. The page is
849 * automatically made dirty.
851 if (vm_page_rename(fs.m, fs.first_object,
853 unlock_and_deallocate(&fs);
856 #if VM_NRESERVLEVEL > 0
858 * Rename the reservation.
860 vm_reserv_rename(fs.m, fs.first_object,
861 fs.object, OFF_TO_IDX(
862 fs.first_object->backing_object_offset));
867 PCPU_INC(cnt.v_cow_optim);
870 * Oh, well, lets copy it.
872 pmap_copy_page(fs.m, fs.first_m);
873 fs.first_m->valid = VM_PAGE_BITS_ALL;
874 if (wired && (fault_flags &
875 VM_FAULT_WIRE) == 0) {
876 vm_page_lock(fs.first_m);
877 vm_page_wire(fs.first_m);
878 vm_page_unlock(fs.first_m);
881 vm_page_unwire(fs.m, FALSE);
882 vm_page_unlock(fs.m);
885 * We no longer need the old page or object.
890 * fs.object != fs.first_object due to above
893 vm_object_pip_wakeup(fs.object);
894 VM_OBJECT_WUNLOCK(fs.object);
896 * Only use the new page below...
898 fs.object = fs.first_object;
899 fs.pindex = fs.first_pindex;
901 if (!is_first_object_locked)
902 VM_OBJECT_WLOCK(fs.object);
903 PCPU_INC(cnt.v_cow_faults);
906 prot &= ~VM_PROT_WRITE;
911 * We must verify that the maps have not changed since our last
914 if (!fs.lookup_still_valid) {
915 vm_object_t retry_object;
916 vm_pindex_t retry_pindex;
917 vm_prot_t retry_prot;
919 if (!vm_map_trylock_read(fs.map)) {
921 unlock_and_deallocate(&fs);
924 fs.lookup_still_valid = TRUE;
925 if (fs.map->timestamp != map_generation) {
926 result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
927 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
930 * If we don't need the page any longer, put it on the inactive
931 * list (the easiest thing to do here). If no one needs it,
932 * pageout will grab it eventually.
934 if (result != KERN_SUCCESS) {
936 unlock_and_deallocate(&fs);
939 * If retry of map lookup would have blocked then
940 * retry fault from start.
942 if (result == KERN_FAILURE)
946 if ((retry_object != fs.first_object) ||
947 (retry_pindex != fs.first_pindex)) {
949 unlock_and_deallocate(&fs);
954 * Check whether the protection has changed or the object has
955 * been copied while we left the map unlocked. Changing from
956 * read to write permission is OK - we leave the page
957 * write-protected, and catch the write fault. Changing from
958 * write to read permission means that we can't mark the page
959 * write-enabled after all.
965 * If the page was filled by a pager, update the map entry's
966 * last read offset. Since the pager does not return the
967 * actual set of pages that it read, this update is based on
968 * the requested set. Typically, the requested and actual
971 * XXX The following assignment modifies the map
972 * without holding a write lock on it.
975 fs.entry->next_read = fs.pindex + faultcount - reqpage;
977 vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags, TRUE);
978 vm_page_assert_xbusied(fs.m);
981 * Page must be completely valid or it is not fit to
982 * map into user space. vm_pager_get_pages() ensures this.
984 KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
985 ("vm_fault: page %p partially invalid", fs.m));
986 VM_OBJECT_WUNLOCK(fs.object);
989 * Put this page into the physical map. We had to do the unlock above
990 * because pmap_enter() may sleep. We don't put the page
991 * back on the active queue until later so that the pageout daemon
992 * won't find it (yet).
994 pmap_enter(fs.map->pmap, vaddr, fs.m, prot,
995 fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0);
996 if (faultcount != 1 && (fault_flags & VM_FAULT_WIRE) == 0 &&
998 vm_fault_prefault(&fs, vaddr, faultcount, reqpage);
999 VM_OBJECT_WLOCK(fs.object);
1003 * If the page is not wired down, then put it where the pageout daemon
1006 if ((fault_flags & VM_FAULT_WIRE) != 0) {
1007 KASSERT(wired, ("VM_FAULT_WIRE && !wired"));
1010 vm_page_activate(fs.m);
1011 if (m_hold != NULL) {
1015 vm_page_unlock(fs.m);
1016 vm_page_xunbusy(fs.m);
1019 * Unlock everything, and return
1021 unlock_and_deallocate(&fs);
1023 PCPU_INC(cnt.v_io_faults);
1024 curthread->td_ru.ru_majflt++;
1026 curthread->td_ru.ru_minflt++;
1028 return (KERN_SUCCESS);
1032 * Speed up the reclamation of up to "distance" pages that precede the
1033 * faulting pindex within the first object of the shadow chain.
1036 vm_fault_cache_behind(const struct faultstate *fs, int distance)
1038 vm_object_t first_object, object;
1039 vm_page_t m, m_prev;
1042 object = fs->object;
1043 VM_OBJECT_ASSERT_WLOCKED(object);
1044 first_object = fs->first_object;
1045 if (first_object != object) {
1046 if (!VM_OBJECT_TRYWLOCK(first_object)) {
1047 VM_OBJECT_WUNLOCK(object);
1048 VM_OBJECT_WLOCK(first_object);
1049 VM_OBJECT_WLOCK(object);
1052 /* Neither fictitious nor unmanaged pages can be cached. */
1053 if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
1054 if (fs->first_pindex < distance)
1057 pindex = fs->first_pindex - distance;
1058 if (pindex < OFF_TO_IDX(fs->entry->offset))
1059 pindex = OFF_TO_IDX(fs->entry->offset);
1060 m = first_object != object ? fs->first_m : fs->m;
1061 vm_page_assert_xbusied(m);
1062 m_prev = vm_page_prev(m);
1063 while ((m = m_prev) != NULL && m->pindex >= pindex &&
1064 m->valid == VM_PAGE_BITS_ALL) {
1065 m_prev = vm_page_prev(m);
1066 if (vm_page_busied(m))
1069 if (m->hold_count == 0 && m->wire_count == 0) {
1071 vm_page_aflag_clear(m, PGA_REFERENCED);
1073 vm_page_deactivate(m);
1080 if (first_object != object)
1081 VM_OBJECT_WUNLOCK(first_object);
1085 * vm_fault_prefault provides a quick way of clustering
1086 * pagefaults into a processes address space. It is a "cousin"
1087 * of vm_map_pmap_enter, except it runs at page fault time instead
1091 vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
1092 int faultcount, int reqpage)
1095 vm_map_entry_t entry;
1096 vm_object_t backing_object, lobject;
1097 vm_offset_t addr, starta;
1100 int backward, forward, i;
1102 pmap = fs->map->pmap;
1103 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1106 if (faultcount > 0) {
1108 forward = faultcount - reqpage - 1;
1115 starta = addra - backward * PAGE_SIZE;
1116 if (starta < entry->start) {
1117 starta = entry->start;
1118 } else if (starta > addra) {
1123 * Generate the sequence of virtual addresses that are candidates for
1124 * prefaulting in an outward spiral from the faulting virtual address,
1125 * "addra". Specifically, the sequence is "addra - PAGE_SIZE", "addra
1126 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
1127 * If the candidate address doesn't have a backing physical page, then
1128 * the loop immediately terminates.
1130 for (i = 0; i < 2 * imax(backward, forward); i++) {
1131 addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
1133 if (addr > addra + forward * PAGE_SIZE)
1136 if (addr < starta || addr >= entry->end)
1139 if (!pmap_is_prefaultable(pmap, addr))
1142 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1143 lobject = entry->object.vm_object;
1144 VM_OBJECT_RLOCK(lobject);
1145 while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1146 lobject->type == OBJT_DEFAULT &&
1147 (backing_object = lobject->backing_object) != NULL) {
1148 KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
1149 0, ("vm_fault_prefault: unaligned object offset"));
1150 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1151 VM_OBJECT_RLOCK(backing_object);
1152 VM_OBJECT_RUNLOCK(lobject);
1153 lobject = backing_object;
1156 VM_OBJECT_RUNLOCK(lobject);
1159 if (m->valid == VM_PAGE_BITS_ALL &&
1160 (m->flags & PG_FICTITIOUS) == 0)
1161 pmap_enter_quick(pmap, addr, m, entry->protection);
1162 VM_OBJECT_RUNLOCK(lobject);
1167 * Hold each of the physical pages that are mapped by the specified range of
1168 * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
1169 * and allow the specified types of access, "prot". If all of the implied
1170 * pages are successfully held, then the number of held pages is returned
1171 * together with pointers to those pages in the array "ma". However, if any
1172 * of the pages cannot be held, -1 is returned.
1175 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
1176 vm_prot_t prot, vm_page_t *ma, int max_count)
1178 vm_offset_t end, va;
1181 boolean_t pmap_failed;
1185 end = round_page(addr + len);
1186 addr = trunc_page(addr);
1189 * Check for illegal addresses.
1191 if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
1194 if (atop(end - addr) > max_count)
1195 panic("vm_fault_quick_hold_pages: count > max_count");
1196 count = atop(end - addr);
1199 * Most likely, the physical pages are resident in the pmap, so it is
1200 * faster to try pmap_extract_and_hold() first.
1202 pmap_failed = FALSE;
1203 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
1204 *mp = pmap_extract_and_hold(map->pmap, va, prot);
1207 else if ((prot & VM_PROT_WRITE) != 0 &&
1208 (*mp)->dirty != VM_PAGE_BITS_ALL) {
1210 * Explicitly dirty the physical page. Otherwise, the
1211 * caller's changes may go unnoticed because they are
1212 * performed through an unmanaged mapping or by a DMA
1215 * The object lock is not held here.
1216 * See vm_page_clear_dirty_mask().
1223 * One or more pages could not be held by the pmap. Either no
1224 * page was mapped at the specified virtual address or that
1225 * mapping had insufficient permissions. Attempt to fault in
1226 * and hold these pages.
1228 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1229 if (*mp == NULL && vm_fault_hold(map, va, prot,
1230 VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
1235 for (mp = ma; mp < ma + count; mp++)
1238 vm_page_unhold(*mp);
1239 vm_page_unlock(*mp);
1246 * vm_fault_copy_entry
1248 * Create new shadow object backing dst_entry with private copy of
1249 * all underlying pages. When src_entry is equal to dst_entry,
1250 * function implements COW for wired-down map entry. Otherwise,
1251 * it forks wired entry into dst_map.
1253 * In/out conditions:
1254 * The source and destination maps must be locked for write.
1255 * The source map entry must be wired down (or be a sharing map
1256 * entry corresponding to a main map entry that is wired down).
1259 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1260 vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1261 vm_ooffset_t *fork_charge)
1263 vm_object_t backing_object, dst_object, object, src_object;
1264 vm_pindex_t dst_pindex, pindex, src_pindex;
1265 vm_prot_t access, prot;
1275 upgrade = src_entry == dst_entry;
1276 access = prot = dst_entry->protection;
1278 src_object = src_entry->object.vm_object;
1279 src_pindex = OFF_TO_IDX(src_entry->offset);
1281 if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
1282 dst_object = src_object;
1283 vm_object_reference(dst_object);
1286 * Create the top-level object for the destination entry. (Doesn't
1287 * actually shadow anything - we copy the pages directly.)
1289 dst_object = vm_object_allocate(OBJT_DEFAULT,
1290 OFF_TO_IDX(dst_entry->end - dst_entry->start));
1291 #if VM_NRESERVLEVEL > 0
1292 dst_object->flags |= OBJ_COLORED;
1293 dst_object->pg_color = atop(dst_entry->start);
1297 VM_OBJECT_WLOCK(dst_object);
1298 KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1299 ("vm_fault_copy_entry: vm_object not NULL"));
1300 if (src_object != dst_object) {
1301 dst_entry->object.vm_object = dst_object;
1302 dst_entry->offset = 0;
1303 dst_object->charge = dst_entry->end - dst_entry->start;
1305 if (fork_charge != NULL) {
1306 KASSERT(dst_entry->cred == NULL,
1307 ("vm_fault_copy_entry: leaked swp charge"));
1308 dst_object->cred = curthread->td_ucred;
1309 crhold(dst_object->cred);
1310 *fork_charge += dst_object->charge;
1311 } else if (dst_object->cred == NULL) {
1312 KASSERT(dst_entry->cred != NULL, ("no cred for entry %p",
1314 dst_object->cred = dst_entry->cred;
1315 dst_entry->cred = NULL;
1319 * If not an upgrade, then enter the mappings in the pmap as
1320 * read and/or execute accesses. Otherwise, enter them as
1323 * A writeable large page mapping is only created if all of
1324 * the constituent small page mappings are modified. Marking
1325 * PTEs as modified on inception allows promotion to happen
1326 * without taking potentially large number of soft faults.
1329 access &= ~VM_PROT_WRITE;
1332 * Loop through all of the virtual pages within the entry's
1333 * range, copying each page from the source object to the
1334 * destination object. Since the source is wired, those pages
1335 * must exist. In contrast, the destination is pageable.
1336 * Since the destination object does share any backing storage
1337 * with the source object, all of its pages must be dirtied,
1338 * regardless of whether they can be written.
1340 for (vaddr = dst_entry->start, dst_pindex = 0;
1341 vaddr < dst_entry->end;
1342 vaddr += PAGE_SIZE, dst_pindex++) {
1345 * Find the page in the source object, and copy it in.
1346 * Because the source is wired down, the page will be
1349 if (src_object != dst_object)
1350 VM_OBJECT_RLOCK(src_object);
1351 object = src_object;
1352 pindex = src_pindex + dst_pindex;
1353 while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1354 (backing_object = object->backing_object) != NULL) {
1356 * Unless the source mapping is read-only or
1357 * it is presently being upgraded from
1358 * read-only, the first object in the shadow
1359 * chain should provide all of the pages. In
1360 * other words, this loop body should never be
1361 * executed when the source mapping is already
1364 KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 ||
1366 ("vm_fault_copy_entry: main object missing page"));
1368 VM_OBJECT_RLOCK(backing_object);
1369 pindex += OFF_TO_IDX(object->backing_object_offset);
1370 if (object != dst_object)
1371 VM_OBJECT_RUNLOCK(object);
1372 object = backing_object;
1374 KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing"));
1376 if (object != dst_object) {
1378 * Allocate a page in the destination object.
1380 dst_m = vm_page_alloc(dst_object, (src_object ==
1381 dst_object ? src_pindex : 0) + dst_pindex,
1383 if (dst_m == NULL) {
1384 VM_OBJECT_WUNLOCK(dst_object);
1385 VM_OBJECT_RUNLOCK(object);
1387 VM_OBJECT_WLOCK(dst_object);
1390 pmap_copy_page(src_m, dst_m);
1391 VM_OBJECT_RUNLOCK(object);
1392 dst_m->valid = VM_PAGE_BITS_ALL;
1393 dst_m->dirty = VM_PAGE_BITS_ALL;
1396 if (vm_page_sleep_if_busy(dst_m, "fltupg"))
1398 vm_page_xbusy(dst_m);
1399 KASSERT(dst_m->valid == VM_PAGE_BITS_ALL,
1400 ("invalid dst page %p", dst_m));
1402 VM_OBJECT_WUNLOCK(dst_object);
1405 * Enter it in the pmap. If a wired, copy-on-write
1406 * mapping is being replaced by a write-enabled
1407 * mapping, then wire that new mapping.
1409 pmap_enter(dst_map->pmap, vaddr, dst_m, prot,
1410 access | (upgrade ? PMAP_ENTER_WIRED : 0), 0);
1413 * Mark it no longer busy, and put it on the active list.
1415 VM_OBJECT_WLOCK(dst_object);
1418 if (src_m != dst_m) {
1419 vm_page_lock(src_m);
1420 vm_page_unwire(src_m, 0);
1421 vm_page_unlock(src_m);
1422 vm_page_lock(dst_m);
1423 vm_page_wire(dst_m);
1424 vm_page_unlock(dst_m);
1426 KASSERT(dst_m->wire_count > 0,
1427 ("dst_m %p is not wired", dst_m));
1430 vm_page_lock(dst_m);
1431 vm_page_activate(dst_m);
1432 vm_page_unlock(dst_m);
1434 vm_page_xunbusy(dst_m);
1436 VM_OBJECT_WUNLOCK(dst_object);
1438 dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1439 vm_object_deallocate(src_object);
1445 * This routine checks around the requested page for other pages that
1446 * might be able to be faulted in. This routine brackets the viable
1447 * pages for the pages to be paged in.
1450 * m, rbehind, rahead
1453 * marray (array of vm_page_t), reqpage (index of requested page)
1456 * number of pages in marray
1459 vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
1468 vm_pindex_t pindex, startpindex, endpindex, tpindex;
1470 int cbehind, cahead;
1472 VM_OBJECT_ASSERT_WLOCKED(m->object);
1476 cbehind = cahead = 0;
1479 * if the requested page is not available, then give up now
1481 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1485 if ((cbehind == 0) && (cahead == 0)) {
1491 if (rahead > cahead) {
1495 if (rbehind > cbehind) {
1500 * scan backward for the read behind pages -- in memory
1503 if (rbehind > pindex) {
1507 startpindex = pindex - rbehind;
1510 if ((rtm = TAILQ_PREV(m, pglist, listq)) != NULL &&
1511 rtm->pindex >= startpindex)
1512 startpindex = rtm->pindex + 1;
1514 /* tpindex is unsigned; beware of numeric underflow. */
1515 for (i = 0, tpindex = pindex - 1; tpindex >= startpindex &&
1516 tpindex < pindex; i++, tpindex--) {
1518 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
1519 VM_ALLOC_IFNOTCACHED);
1522 * Shift the allocated pages to the
1523 * beginning of the array.
1525 for (j = 0; j < i; j++) {
1526 marray[j] = marray[j + tpindex + 1 -
1532 marray[tpindex - startpindex] = rtm;
1540 /* page offset of the required page */
1543 tpindex = pindex + 1;
1547 * scan forward for the read ahead pages
1549 endpindex = tpindex + rahead;
1550 if ((rtm = TAILQ_NEXT(m, listq)) != NULL && rtm->pindex < endpindex)
1551 endpindex = rtm->pindex;
1552 if (endpindex > object->size)
1553 endpindex = object->size;
1555 for (; tpindex < endpindex; i++, tpindex++) {
1557 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
1558 VM_ALLOC_IFNOTCACHED);
1566 /* return number of pages */
1571 * Block entry into the machine-independent layer's page fault handler by
1572 * the calling thread. Subsequent calls to vm_fault() by that thread will
1573 * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of
1574 * spurious page faults.
1577 vm_fault_disable_pagefaults(void)
1580 return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
1584 vm_fault_enable_pagefaults(int save)
1587 curthread_pflags_restore(save);