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/resourcevar.h>
87 #include <sys/rwlock.h>
88 #include <sys/sysctl.h>
89 #include <sys/vmmeter.h>
90 #include <sys/vnode.h>
92 #include <sys/ktrace.h>
96 #include <vm/vm_param.h>
98 #include <vm/vm_map.h>
99 #include <vm/vm_object.h>
100 #include <vm/vm_page.h>
101 #include <vm/vm_pageout.h>
102 #include <vm/vm_kern.h>
103 #include <vm/vm_pager.h>
104 #include <vm/vm_extern.h>
105 #include <vm/vm_reserv.h>
110 static int vm_fault_additional_pages(vm_page_t, int, int, vm_page_t *, int *);
112 #define VM_FAULT_READ_BEHIND 8
113 #define VM_FAULT_READ_DEFAULT (1 + VM_FAULT_READ_AHEAD_INIT)
114 #define VM_FAULT_READ_MAX (1 + VM_FAULT_READ_AHEAD_MAX)
115 #define VM_FAULT_NINCR (VM_FAULT_READ_MAX / VM_FAULT_READ_BEHIND)
116 #define VM_FAULT_SUM (VM_FAULT_NINCR * (VM_FAULT_NINCR + 1) / 2)
118 #define VM_FAULT_DONTNEED_MIN 1048576
125 vm_object_t first_object;
126 vm_pindex_t first_pindex;
128 vm_map_entry_t entry;
129 int lookup_still_valid;
133 static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr,
135 static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
136 int faultcount, int reqpage);
139 release_page(struct faultstate *fs)
142 vm_page_xunbusy(fs->m);
144 vm_page_deactivate(fs->m);
145 vm_page_unlock(fs->m);
150 unlock_map(struct faultstate *fs)
153 if (fs->lookup_still_valid) {
154 vm_map_lookup_done(fs->map, fs->entry);
155 fs->lookup_still_valid = FALSE;
160 unlock_and_deallocate(struct faultstate *fs)
163 vm_object_pip_wakeup(fs->object);
164 VM_OBJECT_WUNLOCK(fs->object);
165 if (fs->object != fs->first_object) {
166 VM_OBJECT_WLOCK(fs->first_object);
167 vm_page_lock(fs->first_m);
168 vm_page_free(fs->first_m);
169 vm_page_unlock(fs->first_m);
170 vm_object_pip_wakeup(fs->first_object);
171 VM_OBJECT_WUNLOCK(fs->first_object);
174 vm_object_deallocate(fs->first_object);
176 if (fs->vp != NULL) {
183 vm_fault_dirty(vm_map_entry_t entry, vm_page_t m, vm_prot_t prot,
184 vm_prot_t fault_type, int fault_flags, boolean_t set_wd)
186 boolean_t need_dirty;
188 if (((prot & VM_PROT_WRITE) == 0 &&
189 (fault_flags & VM_FAULT_DIRTY) == 0) ||
190 (m->oflags & VPO_UNMANAGED) != 0)
193 VM_OBJECT_ASSERT_LOCKED(m->object);
195 need_dirty = ((fault_type & VM_PROT_WRITE) != 0 &&
196 (fault_flags & VM_FAULT_CHANGE_WIRING) == 0) ||
197 (fault_flags & VM_FAULT_DIRTY) != 0;
200 vm_object_set_writeable_dirty(m->object);
203 * If two callers of vm_fault_dirty() with set_wd ==
204 * FALSE, one for the map entry with MAP_ENTRY_NOSYNC
205 * flag set, other with flag clear, race, it is
206 * possible for the no-NOSYNC thread to see m->dirty
207 * != 0 and not clear VPO_NOSYNC. Take vm_page lock
208 * around manipulation of VPO_NOSYNC and
209 * vm_page_dirty() call, to avoid the race and keep
210 * m->oflags consistent.
215 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
216 * if the page is already dirty to prevent data written with
217 * the expectation of being synced from not being synced.
218 * Likewise if this entry does not request NOSYNC then make
219 * sure the page isn't marked NOSYNC. Applications sharing
220 * data should use the same flags to avoid ping ponging.
222 if ((entry->eflags & MAP_ENTRY_NOSYNC) != 0) {
224 m->oflags |= VPO_NOSYNC;
227 m->oflags &= ~VPO_NOSYNC;
231 * If the fault is a write, we know that this page is being
232 * written NOW so dirty it explicitly to save on
233 * pmap_is_modified() calls later.
235 * Also tell the backing pager, if any, that it should remove
236 * any swap backing since the page is now dirty.
243 vm_pager_page_unswapped(m);
247 * TRYPAGER - used by vm_fault to calculate whether the pager for the
248 * current object *might* contain the page.
250 * default objects are zero-fill, there is no real pager.
252 #define TRYPAGER (fs.object->type != OBJT_DEFAULT && \
253 ((fault_flags & VM_FAULT_CHANGE_WIRING) == 0 || wired))
258 * Handle a page fault occurring at the given address,
259 * requiring the given permissions, in the map specified.
260 * If successful, the page is inserted into the
261 * associated physical map.
263 * NOTE: the given address should be truncated to the
264 * proper page address.
266 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
267 * a standard error specifying why the fault is fatal is returned.
269 * The map in question must be referenced, and remains so.
270 * Caller may hold no locks.
273 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
280 if ((td->td_pflags & TDP_NOFAULTING) != 0)
281 return (KERN_PROTECTION_FAILURE);
283 if (map != kernel_map && KTRPOINT(td, KTR_FAULT))
284 ktrfault(vaddr, fault_type);
286 result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags,
289 if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND))
296 vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
297 int fault_flags, vm_page_t *m_hold)
300 int alloc_req, era, faultcount, nera, reqpage, result;
301 boolean_t growstack, is_first_object_locked, wired;
303 vm_object_t next_object;
304 vm_page_t marray[VM_FAULT_READ_MAX];
306 struct faultstate fs;
309 int ahead, behind, cluster_offset, error, locked;
313 PCPU_INC(cnt.v_vm_faults);
315 faultcount = reqpage = 0;
320 * Find the backing store object and offset into it to begin the
324 result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry,
325 &fs.first_object, &fs.first_pindex, &prot, &wired);
326 if (result != KERN_SUCCESS) {
327 if (growstack && result == KERN_INVALID_ADDRESS &&
329 result = vm_map_growstack(curproc, vaddr);
330 if (result != KERN_SUCCESS)
331 return (KERN_FAILURE);
338 map_generation = fs.map->timestamp;
340 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
341 panic("vm_fault: fault on nofault entry, addr: %lx",
345 if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
346 fs.entry->wiring_thread != curthread) {
347 vm_map_unlock_read(fs.map);
349 if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
350 (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
351 fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
352 vm_map_unlock_and_wait(fs.map, 0);
354 vm_map_unlock(fs.map);
359 fault_type = prot | (fault_type & VM_PROT_COPY);
361 if (fs.vp == NULL /* avoid locked vnode leak */ &&
362 (fault_flags & (VM_FAULT_CHANGE_WIRING | VM_FAULT_DIRTY)) == 0 &&
363 /* avoid calling vm_object_set_writeable_dirty() */
364 ((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)) {
368 VM_OBJECT_RLOCK(fs.first_object);
369 if ((prot & VM_PROT_WRITE) != 0 &&
370 (fs.first_object->type == OBJT_VNODE ||
371 (fs.first_object->flags & OBJ_TMPFS_NODE) != 0) &&
372 (fs.first_object->flags & OBJ_MIGHTBEDIRTY) == 0)
374 m = vm_page_lookup(fs.first_object, fs.first_pindex);
375 /* A busy page can be mapped for read|execute access. */
376 if (m == NULL || ((prot & VM_PROT_WRITE) != 0 &&
377 vm_page_busied(m)) || m->valid != VM_PAGE_BITS_ALL)
379 result = pmap_enter(fs.map->pmap, vaddr, m, prot,
380 fault_type | PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED :
382 if (result != KERN_SUCCESS)
384 if (m_hold != NULL) {
390 vm_fault_dirty(fs.entry, m, prot, fault_type, fault_flags,
392 VM_OBJECT_RUNLOCK(fs.first_object);
394 vm_fault_prefault(&fs, vaddr, 0, 0);
395 vm_map_lookup_done(fs.map, fs.entry);
396 curthread->td_ru.ru_minflt++;
397 return (KERN_SUCCESS);
399 if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) {
400 VM_OBJECT_RUNLOCK(fs.first_object);
401 VM_OBJECT_WLOCK(fs.first_object);
404 VM_OBJECT_WLOCK(fs.first_object);
408 * Make a reference to this object to prevent its disposal while we
409 * are messing with it. Once we have the reference, the map is free
410 * to be diddled. Since objects reference their shadows (and copies),
411 * they will stay around as well.
413 * Bump the paging-in-progress count to prevent size changes (e.g.
414 * truncation operations) during I/O. This must be done after
415 * obtaining the vnode lock in order to avoid possible deadlocks.
417 vm_object_reference_locked(fs.first_object);
418 vm_object_pip_add(fs.first_object, 1);
420 fs.lookup_still_valid = TRUE;
425 * Search for the page at object/offset.
427 fs.object = fs.first_object;
428 fs.pindex = fs.first_pindex;
431 * If the object is dead, we stop here
433 if (fs.object->flags & OBJ_DEAD) {
434 unlock_and_deallocate(&fs);
435 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)
508 * Page is not resident, If this is the search termination
509 * or the pager might contain the page, allocate a new page.
511 if (TRYPAGER || fs.object == fs.first_object) {
512 if (fs.pindex >= fs.object->size) {
513 unlock_and_deallocate(&fs);
514 return (KERN_PROTECTION_FAILURE);
518 * Allocate a new page for this object/offset pair.
520 * Unlocked read of the p_flag is harmless. At
521 * worst, the P_KILLED might be not observed
522 * there, and allocation can fail, causing
523 * restart and new reading of the p_flag.
526 if (!vm_page_count_severe() || P_KILLED(curproc)) {
527 #if VM_NRESERVLEVEL > 0
528 vm_object_color(fs.object, atop(vaddr) -
531 alloc_req = P_KILLED(curproc) ?
532 VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
533 if (fs.object->type != OBJT_VNODE &&
534 fs.object->backing_object == NULL)
535 alloc_req |= VM_ALLOC_ZERO;
536 fs.m = vm_page_alloc(fs.object, fs.pindex,
540 unlock_and_deallocate(&fs);
543 } else if (fs.m->valid == VM_PAGE_BITS_ALL)
549 * We have found a valid page or we have allocated a new page.
550 * The page thus may not be valid or may not be entirely
553 * Attempt to fault-in the page if there is a chance that the
554 * pager has it, and potentially fault in additional pages
559 u_char behavior = vm_map_entry_behavior(fs.entry);
561 era = fs.entry->read_ahead;
562 if (behavior == MAP_ENTRY_BEHAV_RANDOM ||
567 } else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
569 nera = VM_FAULT_READ_AHEAD_MAX;
571 if (fs.pindex == fs.entry->next_read)
572 vm_fault_dontneed(&fs, vaddr, ahead);
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_dontneed(&fs, vaddr, ahead);
593 * This is a non-sequential fault. Request a
594 * cluster of pages that is aligned to a
595 * VM_FAULT_READ_DEFAULT page offset boundary
596 * within the object. Alignment to a page
597 * offset boundary is more likely to coincide
598 * with the underlying file system block than
599 * alignment to a virtual address boundary.
601 cluster_offset = fs.pindex %
602 VM_FAULT_READ_DEFAULT;
603 behind = ulmin(cluster_offset,
604 atop(vaddr - fs.entry->start));
606 ahead = VM_FAULT_READ_DEFAULT - 1 -
609 ahead = ulmin(ahead, atop(fs.entry->end - vaddr) - 1);
611 fs.entry->read_ahead = nera;
614 * Call the pager to retrieve the data, if any, after
615 * releasing the lock on the map. We hold a ref on
616 * fs.object and the pages are exclusive busied.
620 if (fs.object->type == OBJT_VNODE) {
621 vp = fs.object->handle;
624 else if (fs.vp != NULL) {
628 locked = VOP_ISLOCKED(vp);
630 if (locked != LK_EXCLUSIVE)
632 /* Do not sleep for vnode lock while fs.m is busy */
633 error = vget(vp, locked | LK_CANRECURSE |
634 LK_NOWAIT, curthread);
638 unlock_and_deallocate(&fs);
639 error = vget(vp, locked | LK_RETRY |
640 LK_CANRECURSE, curthread);
644 ("vm_fault: vget failed"));
650 KASSERT(fs.vp == NULL || !fs.map->system_map,
651 ("vm_fault: vnode-backed object mapped by system map"));
654 * now we find out if any other pages should be paged
655 * in at this time this routine checks to see if the
656 * pages surrounding this fault reside in the same
657 * object as the page for this fault. If they do,
658 * then they are faulted in also into the object. The
659 * array "marray" returned contains an array of
660 * vm_page_t structs where one of them is the
661 * vm_page_t passed to the routine. The reqpage
662 * return value is the index into the marray for the
663 * vm_page_t passed to the routine.
665 * fs.m plus the additional pages are exclusive busied.
667 faultcount = vm_fault_additional_pages(
668 fs.m, behind, ahead, marray, &reqpage);
671 vm_pager_get_pages(fs.object, marray, faultcount,
672 reqpage) : VM_PAGER_FAIL;
674 if (rv == VM_PAGER_OK) {
676 * Found the page. Leave it busy while we play
681 * Relookup in case pager changed page. Pager
682 * is responsible for disposition of old page
685 fs.m = vm_page_lookup(fs.object, fs.pindex);
687 unlock_and_deallocate(&fs);
692 break; /* break to PAGE HAS BEEN FOUND */
695 * Remove the bogus page (which does not exist at this
696 * object/offset); before doing so, we must get back
697 * our object lock to preserve our invariant.
699 * Also wake up any other process that may want to bring
702 * If this is the top-level object, we must leave the
703 * busy page to prevent another process from rushing
704 * past us, and inserting the page in that object at
705 * the same time that we are.
707 if (rv == VM_PAGER_ERROR)
708 printf("vm_fault: pager read error, pid %d (%s)\n",
709 curproc->p_pid, curproc->p_comm);
711 * Data outside the range of the pager or an I/O error
714 * XXX - the check for kernel_map is a kludge to work
715 * around having the machine panic on a kernel space
716 * fault w/ I/O error.
718 if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
719 (rv == VM_PAGER_BAD)) {
722 vm_page_unlock(fs.m);
724 unlock_and_deallocate(&fs);
725 return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
727 if (fs.object != fs.first_object) {
730 vm_page_unlock(fs.m);
733 * XXX - we cannot just fall out at this
734 * point, m has been freed and is invalid!
740 * We get here if the object has default pager (or unwiring)
741 * or the pager doesn't have the page.
743 if (fs.object == fs.first_object)
747 * Move on to the next object. Lock the next object before
748 * unlocking the current one.
750 fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
751 next_object = fs.object->backing_object;
752 if (next_object == NULL) {
754 * If there's no object left, fill the page in the top
757 if (fs.object != fs.first_object) {
758 vm_object_pip_wakeup(fs.object);
759 VM_OBJECT_WUNLOCK(fs.object);
761 fs.object = fs.first_object;
762 fs.pindex = fs.first_pindex;
764 VM_OBJECT_WLOCK(fs.object);
769 * Zero the page if necessary and mark it valid.
771 if ((fs.m->flags & PG_ZERO) == 0) {
772 pmap_zero_page(fs.m);
774 PCPU_INC(cnt.v_ozfod);
776 PCPU_INC(cnt.v_zfod);
777 fs.m->valid = VM_PAGE_BITS_ALL;
778 /* Don't try to prefault neighboring pages. */
780 break; /* break to PAGE HAS BEEN FOUND */
782 KASSERT(fs.object != next_object,
783 ("object loop %p", next_object));
784 VM_OBJECT_WLOCK(next_object);
785 vm_object_pip_add(next_object, 1);
786 if (fs.object != fs.first_object)
787 vm_object_pip_wakeup(fs.object);
788 VM_OBJECT_WUNLOCK(fs.object);
789 fs.object = next_object;
793 vm_page_assert_xbusied(fs.m);
796 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
801 * If the page is being written, but isn't already owned by the
802 * top-level object, we have to copy it into a new page owned by the
805 if (fs.object != fs.first_object) {
807 * We only really need to copy if we want to write it.
809 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
811 * This allows pages to be virtually copied from a
812 * backing_object into the first_object, where the
813 * backing object has no other refs to it, and cannot
814 * gain any more refs. Instead of a bcopy, we just
815 * move the page from the backing object to the
816 * first object. Note that we must mark the page
817 * dirty in the first object so that it will go out
818 * to swap when needed.
820 is_first_object_locked = FALSE;
823 * Only one shadow object
825 (fs.object->shadow_count == 1) &&
827 * No COW refs, except us
829 (fs.object->ref_count == 1) &&
831 * No one else can look this object up
833 (fs.object->handle == NULL) &&
835 * No other ways to look the object up
837 ((fs.object->type == OBJT_DEFAULT) ||
838 (fs.object->type == OBJT_SWAP)) &&
839 (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
841 * We don't chase down the shadow chain
843 fs.object == fs.first_object->backing_object) {
845 * get rid of the unnecessary page
847 vm_page_lock(fs.first_m);
848 vm_page_free(fs.first_m);
849 vm_page_unlock(fs.first_m);
851 * grab the page and put it into the
852 * process'es object. The page is
853 * automatically made dirty.
855 if (vm_page_rename(fs.m, fs.first_object,
857 unlock_and_deallocate(&fs);
860 #if VM_NRESERVLEVEL > 0
862 * Rename the reservation.
864 vm_reserv_rename(fs.m, fs.first_object,
865 fs.object, OFF_TO_IDX(
866 fs.first_object->backing_object_offset));
871 PCPU_INC(cnt.v_cow_optim);
874 * Oh, well, lets copy it.
876 pmap_copy_page(fs.m, fs.first_m);
877 fs.first_m->valid = VM_PAGE_BITS_ALL;
878 if (wired && (fault_flags &
879 VM_FAULT_CHANGE_WIRING) == 0) {
880 vm_page_lock(fs.first_m);
881 vm_page_wire(fs.first_m);
882 vm_page_unlock(fs.first_m);
885 vm_page_unwire(fs.m, PQ_INACTIVE);
886 vm_page_unlock(fs.m);
889 * We no longer need the old page or object.
894 * fs.object != fs.first_object due to above
897 vm_object_pip_wakeup(fs.object);
898 VM_OBJECT_WUNLOCK(fs.object);
900 * Only use the new page below...
902 fs.object = fs.first_object;
903 fs.pindex = fs.first_pindex;
905 if (!is_first_object_locked)
906 VM_OBJECT_WLOCK(fs.object);
907 PCPU_INC(cnt.v_cow_faults);
910 prot &= ~VM_PROT_WRITE;
915 * We must verify that the maps have not changed since our last
918 if (!fs.lookup_still_valid) {
919 vm_object_t retry_object;
920 vm_pindex_t retry_pindex;
921 vm_prot_t retry_prot;
923 if (!vm_map_trylock_read(fs.map)) {
925 unlock_and_deallocate(&fs);
928 fs.lookup_still_valid = TRUE;
929 if (fs.map->timestamp != map_generation) {
930 result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
931 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
934 * If we don't need the page any longer, put it on the inactive
935 * list (the easiest thing to do here). If no one needs it,
936 * pageout will grab it eventually.
938 if (result != KERN_SUCCESS) {
940 unlock_and_deallocate(&fs);
943 * If retry of map lookup would have blocked then
944 * retry fault from start.
946 if (result == KERN_FAILURE)
950 if ((retry_object != fs.first_object) ||
951 (retry_pindex != fs.first_pindex)) {
953 unlock_and_deallocate(&fs);
958 * Check whether the protection has changed or the object has
959 * been copied while we left the map unlocked. Changing from
960 * read to write permission is OK - we leave the page
961 * write-protected, and catch the write fault. Changing from
962 * write to read permission means that we can't mark the page
963 * write-enabled after all.
969 * If the page was filled by a pager, update the map entry's
970 * last read offset. Since the pager does not return the
971 * actual set of pages that it read, this update is based on
972 * the requested set. Typically, the requested and actual
975 * XXX The following assignment modifies the map
976 * without holding a write lock on it.
979 fs.entry->next_read = fs.pindex + faultcount - reqpage;
981 vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags, TRUE);
982 vm_page_assert_xbusied(fs.m);
985 * Page must be completely valid or it is not fit to
986 * map into user space. vm_pager_get_pages() ensures this.
988 KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
989 ("vm_fault: page %p partially invalid", fs.m));
990 VM_OBJECT_WUNLOCK(fs.object);
993 * Put this page into the physical map. We had to do the unlock above
994 * because pmap_enter() may sleep. We don't put the page
995 * back on the active queue until later so that the pageout daemon
996 * won't find it (yet).
998 pmap_enter(fs.map->pmap, vaddr, fs.m, prot,
999 fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0);
1000 if (faultcount != 1 && (fault_flags & VM_FAULT_CHANGE_WIRING) == 0 &&
1002 vm_fault_prefault(&fs, vaddr, faultcount, reqpage);
1003 VM_OBJECT_WLOCK(fs.object);
1007 * If the page is not wired down, then put it where the pageout daemon
1010 if (fault_flags & VM_FAULT_CHANGE_WIRING) {
1014 vm_page_unwire(fs.m, PQ_ACTIVE);
1016 vm_page_activate(fs.m);
1017 if (m_hold != NULL) {
1021 vm_page_unlock(fs.m);
1022 vm_page_xunbusy(fs.m);
1025 * Unlock everything, and return
1027 unlock_and_deallocate(&fs);
1029 PCPU_INC(cnt.v_io_faults);
1030 curthread->td_ru.ru_majflt++;
1032 curthread->td_ru.ru_minflt++;
1034 return (KERN_SUCCESS);
1038 * Speed up the reclamation of pages that precede the faulting pindex within
1039 * the first object of the shadow chain. Essentially, perform the equivalent
1040 * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes
1041 * the faulting pindex by the cluster size when the pages read by vm_fault()
1042 * cross a cluster-size boundary. The cluster size is the greater of the
1043 * smallest superpage size and VM_FAULT_DONTNEED_MIN.
1045 * When "fs->first_object" is a shadow object, the pages in the backing object
1046 * that precede the faulting pindex are deactivated by vm_fault(). So, this
1047 * function must only be concerned with pages in the first object.
1050 vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead)
1052 vm_map_entry_t entry;
1053 vm_object_t first_object, object;
1054 vm_offset_t end, start;
1055 vm_page_t m, m_next;
1056 vm_pindex_t pend, pstart;
1059 object = fs->object;
1060 VM_OBJECT_ASSERT_WLOCKED(object);
1061 first_object = fs->first_object;
1062 if (first_object != object) {
1063 if (!VM_OBJECT_TRYWLOCK(first_object)) {
1064 VM_OBJECT_WUNLOCK(object);
1065 VM_OBJECT_WLOCK(first_object);
1066 VM_OBJECT_WLOCK(object);
1069 /* Neither fictitious nor unmanaged pages can be reclaimed. */
1070 if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
1071 size = VM_FAULT_DONTNEED_MIN;
1072 if (MAXPAGESIZES > 1 && size < pagesizes[1])
1073 size = pagesizes[1];
1074 end = rounddown2(vaddr, size);
1075 if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) &&
1076 (entry = fs->entry)->start < end) {
1077 if (end - entry->start < size)
1078 start = entry->start;
1081 pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED);
1082 pstart = OFF_TO_IDX(entry->offset) + atop(start -
1084 m_next = vm_page_find_least(first_object, pstart);
1085 pend = OFF_TO_IDX(entry->offset) + atop(end -
1087 while ((m = m_next) != NULL && m->pindex < pend) {
1088 m_next = TAILQ_NEXT(m, listq);
1089 if (m->valid != VM_PAGE_BITS_ALL ||
1093 if (m->hold_count == 0 && m->wire_count == 0)
1094 vm_page_advise(m, MADV_DONTNEED);
1099 if (first_object != object)
1100 VM_OBJECT_WUNLOCK(first_object);
1104 * vm_fault_prefault provides a quick way of clustering
1105 * pagefaults into a processes address space. It is a "cousin"
1106 * of vm_map_pmap_enter, except it runs at page fault time instead
1110 vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
1111 int faultcount, int reqpage)
1114 vm_map_entry_t entry;
1115 vm_object_t backing_object, lobject;
1116 vm_offset_t addr, starta;
1119 int backward, forward, i;
1121 pmap = fs->map->pmap;
1122 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1125 if (faultcount > 0) {
1127 forward = faultcount - reqpage - 1;
1134 starta = addra - backward * PAGE_SIZE;
1135 if (starta < entry->start) {
1136 starta = entry->start;
1137 } else if (starta > addra) {
1142 * Generate the sequence of virtual addresses that are candidates for
1143 * prefaulting in an outward spiral from the faulting virtual address,
1144 * "addra". Specifically, the sequence is "addra - PAGE_SIZE", "addra
1145 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
1146 * If the candidate address doesn't have a backing physical page, then
1147 * the loop immediately terminates.
1149 for (i = 0; i < 2 * imax(backward, forward); i++) {
1150 addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
1152 if (addr > addra + forward * PAGE_SIZE)
1155 if (addr < starta || addr >= entry->end)
1158 if (!pmap_is_prefaultable(pmap, addr))
1161 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1162 lobject = entry->object.vm_object;
1163 VM_OBJECT_RLOCK(lobject);
1164 while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1165 lobject->type == OBJT_DEFAULT &&
1166 (backing_object = lobject->backing_object) != NULL) {
1167 KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
1168 0, ("vm_fault_prefault: unaligned object offset"));
1169 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1170 VM_OBJECT_RLOCK(backing_object);
1171 VM_OBJECT_RUNLOCK(lobject);
1172 lobject = backing_object;
1175 VM_OBJECT_RUNLOCK(lobject);
1178 if (m->valid == VM_PAGE_BITS_ALL &&
1179 (m->flags & PG_FICTITIOUS) == 0)
1180 pmap_enter_quick(pmap, addr, m, entry->protection);
1181 VM_OBJECT_RUNLOCK(lobject);
1186 * Hold each of the physical pages that are mapped by the specified range of
1187 * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
1188 * and allow the specified types of access, "prot". If all of the implied
1189 * pages are successfully held, then the number of held pages is returned
1190 * together with pointers to those pages in the array "ma". However, if any
1191 * of the pages cannot be held, -1 is returned.
1194 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
1195 vm_prot_t prot, vm_page_t *ma, int max_count)
1197 vm_offset_t end, va;
1200 boolean_t pmap_failed;
1204 end = round_page(addr + len);
1205 addr = trunc_page(addr);
1208 * Check for illegal addresses.
1210 if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
1213 if (atop(end - addr) > max_count)
1214 panic("vm_fault_quick_hold_pages: count > max_count");
1215 count = atop(end - addr);
1218 * Most likely, the physical pages are resident in the pmap, so it is
1219 * faster to try pmap_extract_and_hold() first.
1221 pmap_failed = FALSE;
1222 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
1223 *mp = pmap_extract_and_hold(map->pmap, va, prot);
1226 else if ((prot & VM_PROT_WRITE) != 0 &&
1227 (*mp)->dirty != VM_PAGE_BITS_ALL) {
1229 * Explicitly dirty the physical page. Otherwise, the
1230 * caller's changes may go unnoticed because they are
1231 * performed through an unmanaged mapping or by a DMA
1234 * The object lock is not held here.
1235 * See vm_page_clear_dirty_mask().
1242 * One or more pages could not be held by the pmap. Either no
1243 * page was mapped at the specified virtual address or that
1244 * mapping had insufficient permissions. Attempt to fault in
1245 * and hold these pages.
1247 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1248 if (*mp == NULL && vm_fault_hold(map, va, prot,
1249 VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
1254 for (mp = ma; mp < ma + count; mp++)
1257 vm_page_unhold(*mp);
1258 vm_page_unlock(*mp);
1265 * vm_fault_copy_entry
1267 * Create new shadow object backing dst_entry with private copy of
1268 * all underlying pages. When src_entry is equal to dst_entry,
1269 * function implements COW for wired-down map entry. Otherwise,
1270 * it forks wired entry into dst_map.
1272 * In/out conditions:
1273 * The source and destination maps must be locked for write.
1274 * The source map entry must be wired down (or be a sharing map
1275 * entry corresponding to a main map entry that is wired down).
1278 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1279 vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1280 vm_ooffset_t *fork_charge)
1282 vm_object_t backing_object, dst_object, object, src_object;
1283 vm_pindex_t dst_pindex, pindex, src_pindex;
1284 vm_prot_t access, prot;
1294 upgrade = src_entry == dst_entry;
1295 access = prot = dst_entry->protection;
1297 src_object = src_entry->object.vm_object;
1298 src_pindex = OFF_TO_IDX(src_entry->offset);
1300 if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
1301 dst_object = src_object;
1302 vm_object_reference(dst_object);
1305 * Create the top-level object for the destination entry. (Doesn't
1306 * actually shadow anything - we copy the pages directly.)
1308 dst_object = vm_object_allocate(OBJT_DEFAULT,
1309 OFF_TO_IDX(dst_entry->end - dst_entry->start));
1310 #if VM_NRESERVLEVEL > 0
1311 dst_object->flags |= OBJ_COLORED;
1312 dst_object->pg_color = atop(dst_entry->start);
1316 VM_OBJECT_WLOCK(dst_object);
1317 KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1318 ("vm_fault_copy_entry: vm_object not NULL"));
1319 if (src_object != dst_object) {
1320 dst_entry->object.vm_object = dst_object;
1321 dst_entry->offset = 0;
1322 dst_object->charge = dst_entry->end - dst_entry->start;
1324 if (fork_charge != NULL) {
1325 KASSERT(dst_entry->cred == NULL,
1326 ("vm_fault_copy_entry: leaked swp charge"));
1327 dst_object->cred = curthread->td_ucred;
1328 crhold(dst_object->cred);
1329 *fork_charge += dst_object->charge;
1330 } else if (dst_object->cred == NULL) {
1331 KASSERT(dst_entry->cred != NULL, ("no cred for entry %p",
1333 dst_object->cred = dst_entry->cred;
1334 dst_entry->cred = NULL;
1338 * If not an upgrade, then enter the mappings in the pmap as
1339 * read and/or execute accesses. Otherwise, enter them as
1342 * A writeable large page mapping is only created if all of
1343 * the constituent small page mappings are modified. Marking
1344 * PTEs as modified on inception allows promotion to happen
1345 * without taking potentially large number of soft faults.
1348 access &= ~VM_PROT_WRITE;
1351 * Loop through all of the virtual pages within the entry's
1352 * range, copying each page from the source object to the
1353 * destination object. Since the source is wired, those pages
1354 * must exist. In contrast, the destination is pageable.
1355 * Since the destination object does share any backing storage
1356 * with the source object, all of its pages must be dirtied,
1357 * regardless of whether they can be written.
1359 for (vaddr = dst_entry->start, dst_pindex = 0;
1360 vaddr < dst_entry->end;
1361 vaddr += PAGE_SIZE, dst_pindex++) {
1364 * Find the page in the source object, and copy it in.
1365 * Because the source is wired down, the page will be
1368 if (src_object != dst_object)
1369 VM_OBJECT_RLOCK(src_object);
1370 object = src_object;
1371 pindex = src_pindex + dst_pindex;
1372 while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1373 (backing_object = object->backing_object) != NULL) {
1375 * Unless the source mapping is read-only or
1376 * it is presently being upgraded from
1377 * read-only, the first object in the shadow
1378 * chain should provide all of the pages. In
1379 * other words, this loop body should never be
1380 * executed when the source mapping is already
1383 KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 ||
1385 ("vm_fault_copy_entry: main object missing page"));
1387 VM_OBJECT_RLOCK(backing_object);
1388 pindex += OFF_TO_IDX(object->backing_object_offset);
1389 if (object != dst_object)
1390 VM_OBJECT_RUNLOCK(object);
1391 object = backing_object;
1393 KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing"));
1395 if (object != dst_object) {
1397 * Allocate a page in the destination object.
1399 dst_m = vm_page_alloc(dst_object, (src_object ==
1400 dst_object ? src_pindex : 0) + dst_pindex,
1402 if (dst_m == NULL) {
1403 VM_OBJECT_WUNLOCK(dst_object);
1404 VM_OBJECT_RUNLOCK(object);
1406 VM_OBJECT_WLOCK(dst_object);
1409 pmap_copy_page(src_m, dst_m);
1410 VM_OBJECT_RUNLOCK(object);
1411 dst_m->valid = VM_PAGE_BITS_ALL;
1412 dst_m->dirty = VM_PAGE_BITS_ALL;
1415 if (vm_page_sleep_if_busy(dst_m, "fltupg"))
1417 vm_page_xbusy(dst_m);
1418 KASSERT(dst_m->valid == VM_PAGE_BITS_ALL,
1419 ("invalid dst page %p", dst_m));
1421 VM_OBJECT_WUNLOCK(dst_object);
1424 * Enter it in the pmap. If a wired, copy-on-write
1425 * mapping is being replaced by a write-enabled
1426 * mapping, then wire that new mapping.
1428 pmap_enter(dst_map->pmap, vaddr, dst_m, prot,
1429 access | (upgrade ? PMAP_ENTER_WIRED : 0), 0);
1432 * Mark it no longer busy, and put it on the active list.
1434 VM_OBJECT_WLOCK(dst_object);
1437 if (src_m != dst_m) {
1438 vm_page_lock(src_m);
1439 vm_page_unwire(src_m, PQ_INACTIVE);
1440 vm_page_unlock(src_m);
1441 vm_page_lock(dst_m);
1442 vm_page_wire(dst_m);
1443 vm_page_unlock(dst_m);
1445 KASSERT(dst_m->wire_count > 0,
1446 ("dst_m %p is not wired", dst_m));
1449 vm_page_lock(dst_m);
1450 vm_page_activate(dst_m);
1451 vm_page_unlock(dst_m);
1453 vm_page_xunbusy(dst_m);
1455 VM_OBJECT_WUNLOCK(dst_object);
1457 dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1458 vm_object_deallocate(src_object);
1464 * This routine checks around the requested page for other pages that
1465 * might be able to be faulted in. This routine brackets the viable
1466 * pages for the pages to be paged in.
1469 * m, rbehind, rahead
1472 * marray (array of vm_page_t), reqpage (index of requested page)
1475 * number of pages in marray
1478 vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
1487 vm_pindex_t pindex, startpindex, endpindex, tpindex;
1489 int cbehind, cahead;
1491 VM_OBJECT_ASSERT_WLOCKED(m->object);
1495 cbehind = cahead = 0;
1498 * if the requested page is not available, then give up now
1500 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1504 if ((cbehind == 0) && (cahead == 0)) {
1510 if (rahead > cahead) {
1514 if (rbehind > cbehind) {
1519 * scan backward for the read behind pages -- in memory
1522 if (rbehind > pindex) {
1526 startpindex = pindex - rbehind;
1529 if ((rtm = TAILQ_PREV(m, pglist, listq)) != NULL &&
1530 rtm->pindex >= startpindex)
1531 startpindex = rtm->pindex + 1;
1533 /* tpindex is unsigned; beware of numeric underflow. */
1534 for (i = 0, tpindex = pindex - 1; tpindex >= startpindex &&
1535 tpindex < pindex; i++, tpindex--) {
1537 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
1538 VM_ALLOC_IFNOTCACHED);
1541 * Shift the allocated pages to the
1542 * beginning of the array.
1544 for (j = 0; j < i; j++) {
1545 marray[j] = marray[j + tpindex + 1 -
1551 marray[tpindex - startpindex] = rtm;
1559 /* page offset of the required page */
1562 tpindex = pindex + 1;
1566 * scan forward for the read ahead pages
1568 endpindex = tpindex + rahead;
1569 if ((rtm = TAILQ_NEXT(m, listq)) != NULL && rtm->pindex < endpindex)
1570 endpindex = rtm->pindex;
1571 if (endpindex > object->size)
1572 endpindex = object->size;
1574 for (; tpindex < endpindex; i++, tpindex++) {
1576 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
1577 VM_ALLOC_IFNOTCACHED);
1585 /* return number of pages */
1590 * Block entry into the machine-independent layer's page fault handler by
1591 * the calling thread. Subsequent calls to vm_fault() by that thread will
1592 * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of
1593 * spurious page faults.
1596 vm_fault_disable_pagefaults(void)
1599 return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
1603 vm_fault_enable_pagefaults(int save)
1606 curthread_pflags_restore(save);