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.
69 * $FreeBSD: src/sys/vm/vm_fault.c,v 1.108.2.8 2002/02/26 05:49:27 silby Exp $
70 * $DragonFly: src/sys/vm/vm_fault.c,v 1.26 2006/09/12 18:41:32 dillon Exp $
74 * Page fault handling module.
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
81 #include <sys/vnode.h>
82 #include <sys/resourcevar.h>
83 #include <sys/vmmeter.h>
86 #include <vm/vm_param.h>
89 #include <vm/vm_map.h>
90 #include <vm/vm_object.h>
91 #include <vm/vm_page.h>
92 #include <vm/vm_pageout.h>
93 #include <vm/vm_kern.h>
94 #include <vm/vm_pager.h>
95 #include <vm/vnode_pager.h>
96 #include <vm/vm_extern.h>
98 #include <sys/thread2.h>
99 #include <vm/vm_page2.h>
101 #define VM_FAULT_READ_AHEAD 8
102 #define VM_FAULT_READ_BEHIND 7
103 #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1)
110 vm_object_t first_object;
111 vm_pindex_t first_pindex;
113 vm_map_entry_t entry;
114 int lookup_still_valid;
124 static int vm_fault_object(struct faultstate *fs, vm_prot_t);
125 static int vm_fault_additional_pages (vm_page_t, int, int, vm_page_t *, int *);
126 static int vm_fault_ratelimit(struct vmspace *vmspace);
129 release_page(struct faultstate *fs)
131 vm_page_wakeup(fs->m);
132 vm_page_deactivate(fs->m);
137 unlock_map(struct faultstate *fs)
139 if (fs->lookup_still_valid) {
140 vm_map_lookup_done(fs->map, fs->entry, 0);
141 fs->lookup_still_valid = FALSE;
146 _unlock_things(struct faultstate *fs, int dealloc)
148 vm_object_pip_wakeup(fs->object);
149 if (fs->object != fs->first_object) {
150 vm_page_free(fs->first_m);
151 vm_object_pip_wakeup(fs->first_object);
155 vm_object_deallocate(fs->first_object);
158 if (fs->vp != NULL) {
164 #define unlock_things(fs) _unlock_things(fs, 0)
165 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
170 * Determine if the pager for the current object *might* contain the page.
172 * We only need to try the pager if this is not a default object (default
173 * objects are zero-fill and have no real pager), and if we are not taking
174 * a wiring fault or if the FS entry is wired.
176 #define TRYPAGER(fs) \
177 (fs->object->type != OBJT_DEFAULT && \
178 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || fs->wired))
183 * Handle a page fault occuring at the given address, requiring the given
184 * permissions, in the map specified. If successful, the page is inserted
185 * into the associated physical map.
187 * NOTE: The given address should be truncated to the proper page address.
189 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
190 * a standard error specifying why the fault is fatal is returned.
192 * The map in question must be referenced, and remains so.
193 * The caller may hold no locks.
196 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
199 struct faultstate fs;
201 mycpu->gd_cnt.v_vm_faults++;
205 fs.fault_flags = fault_flags;
209 * Find the vm_map_entry representing the backing store and resolve
210 * the top level object and page index. This may have the side
211 * effect of executing a copy-on-write on the map entry and/or
212 * creating a shadow object, but will not COW any actual VM pages.
214 * On success fs.map is left read-locked and various other fields
215 * are initialized but not otherwise referenced or locked.
217 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
218 * if the map entry is a virtual page table and also writable,
219 * so we can set the 'A'accessed bit in the virtual page table entry.
222 result = vm_map_lookup(&fs.map, vaddr, fault_type,
223 &fs.entry, &fs.first_object,
224 &fs.first_pindex, &fs.prot, &fs.wired);
227 * If the lookup failed or the map protections are incompatible,
228 * the fault generally fails. However, if the caller is trying
229 * to do a user wiring we have more work to do.
231 if (result != KERN_SUCCESS) {
232 if (result != KERN_PROTECTION_FAILURE)
234 if ((fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
238 * If we are user-wiring a r/w segment, and it is COW, then
239 * we need to do the COW operation. Note that we don't
240 * currently COW RO sections now, because it is NOT desirable
241 * to COW .text. We simply keep .text from ever being COW'ed
242 * and take the heat that one cannot debug wired .text sections.
244 result = vm_map_lookup(&fs.map, vaddr,
245 VM_PROT_READ|VM_PROT_WRITE|
246 VM_PROT_OVERRIDE_WRITE,
247 &fs.entry, &fs.first_object,
248 &fs.first_pindex, &fs.prot, &fs.wired);
249 if (result != KERN_SUCCESS)
253 * If we don't COW now, on a user wire, the user will never
254 * be able to write to the mapping. If we don't make this
255 * restriction, the bookkeeping would be nearly impossible.
257 if ((fs.entry->protection & VM_PROT_WRITE) == 0)
258 fs.entry->max_protection &= ~VM_PROT_WRITE;
262 * fs.map is read-locked
264 * Misc checks. Save the map generation number to detect races.
266 fs.map_generation = fs.map->timestamp;
268 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
269 panic("vm_fault: fault on nofault entry, addr: %lx",
274 * A system map entry may return a NULL object. No object means
275 * no pager means an unrecoverable kernel fault.
277 if (fs.first_object == NULL) {
278 panic("vm_fault: unrecoverable fault at %p in entry %p",
279 (void *)vaddr, fs.entry);
283 * Make a reference to this object to prevent its disposal while we
284 * are messing with it. Once we have the reference, the map is free
285 * to be diddled. Since objects reference their shadows (and copies),
286 * they will stay around as well.
288 * Bump the paging-in-progress count to prevent size changes (e.g.
289 * truncation operations) during I/O. This must be done after
290 * obtaining the vnode lock in order to avoid possible deadlocks.
292 vm_object_reference(fs.first_object);
293 fs.vp = vnode_pager_lock(fs.first_object);
294 vm_object_pip_add(fs.first_object, 1);
296 fs.lookup_still_valid = TRUE;
300 * If the entry is wired we cannot change the page protection.
303 fault_type = fs.prot;
306 * The page we want is at (object, pindex), but if the vm_map_entry
307 * is VM_MAPTYPE_VPAGETABLE we have to traverse the page table to
308 * figure out the actual pindex.
310 fs.object = fs.first_object;
311 fs.pindex = fs.first_pindex;
314 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
315 fs.pindex = fs.entry->avail_ssize; /* page directory */
316 result = vm_fault_object(&fs, fault_type);
317 if (result == KERN_TRY_AGAIN)
319 if (result != KERN_SUCCESS) {
320 /* unlock_and_deallocate(&fs); */
325 * fs.m is busy on return
332 * Now we have the actual (object, pindex), fault in the page. If
333 * vm_fault_object() fails it will unlock and deallocate the FS
336 result = vm_fault_object(&fs, fault_type);
337 if (result == KERN_TRY_AGAIN)
339 if (result != KERN_SUCCESS)
343 * On success vm_fault_object() unlocks but does not deallocate, and
344 * fs.m will contain a busied page.
346 * Enter the page into the pmap and do pmap-related adjustments.
348 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
350 if (((fs.fault_flags & VM_FAULT_WIRE_MASK) == 0) && (fs.wired == 0)) {
351 pmap_prefault(fs.map->pmap, vaddr, fs.entry);
354 vm_page_flag_clear(fs.m, PG_ZERO);
355 vm_page_flag_set(fs.m, PG_MAPPED|PG_REFERENCED);
356 if (fs.fault_flags & VM_FAULT_HOLD)
360 * If the page is not wired down, then put it where the pageout daemon
363 if (fs.fault_flags & VM_FAULT_WIRE_MASK) {
367 vm_page_unwire(fs.m, 1);
369 vm_page_activate(fs.m);
372 if (curproc && (curproc->p_flag & P_SWAPPEDOUT) == 0 &&
375 curproc->p_stats->p_ru.ru_majflt++;
377 curproc->p_stats->p_ru.ru_minflt++;
382 * Unlock everything, and return
384 vm_page_wakeup(fs.m);
385 vm_object_deallocate(fs.first_object);
387 return (KERN_SUCCESS);
391 * Do all operations required to fault in (fs.object, fs.pindex). Run
392 * through the shadow chain as necessary and do required COW or virtual
393 * copy operations. The caller has already fully resolved the vm_map_entry
394 * and, if appropriate, has created a copy-on-write layer. All we need to
395 * do is iterate the object chain.
397 * On failure (fs) is unlocked and deallocated and the caller may return or
398 * retry depending on the failure code. On success (fs) is unlocked but not
399 * deallocated, and fs.m will contain a resolved busied page.
403 vm_fault_object(struct faultstate *fs, vm_prot_t fault_type)
405 vm_object_t next_object;
406 vm_page_t marray[VM_FAULT_READ];
411 * If the object is dead, we stop here
413 if (fs->object->flags & OBJ_DEAD) {
414 unlock_and_deallocate(fs);
415 return (KERN_PROTECTION_FAILURE);
419 * See if page is resident. spl protection is required
420 * to avoid an interrupt unbusy/free race against our
421 * lookup. We must hold the protection through a page
422 * allocation or busy.
425 fs->m = vm_page_lookup(fs->object, fs->pindex);
429 * Wait/Retry if the page is busy. We have to do this
430 * if the page is busy via either PG_BUSY or
431 * vm_page_t->busy because the vm_pager may be using
432 * vm_page_t->busy for pageouts ( and even pageins if
433 * it is the vnode pager ), and we could end up trying
434 * to pagein and pageout the same page simultaneously.
436 * We can theoretically allow the busy case on a read
437 * fault if the page is marked valid, but since such
438 * pages are typically already pmap'd, putting that
439 * special case in might be more effort then it is
440 * worth. We cannot under any circumstances mess
441 * around with a vm_page_t->busy page except, perhaps,
444 if ((fs->m->flags & PG_BUSY) || fs->m->busy) {
446 vm_page_sleep_busy(fs->m, TRUE, "vmpfw");
447 mycpu->gd_cnt.v_intrans++;
448 vm_object_deallocate(fs->first_object);
450 return (KERN_TRY_AGAIN);
454 * If reactivating a page from PQ_CACHE we may have
457 queue = fs->m->queue;
458 vm_page_unqueue_nowakeup(fs->m);
460 if ((queue - fs->m->pc) == PQ_CACHE &&
461 vm_page_count_severe()) {
462 vm_page_activate(fs->m);
463 unlock_and_deallocate(fs);
466 return (KERN_TRY_AGAIN);
470 * Mark page busy for other processes, and the
471 * pagedaemon. If it still isn't completely valid
472 * (readable), jump to readrest, else we found the
473 * page and can return.
475 * We can release the spl once we have marked the
481 if (((fs->m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) &&
482 fs->m->object != kernel_object &&
483 fs->m->object != kmem_object) {
486 break; /* break to PAGE HAS BEEN FOUND */
490 * Page is not resident, If this is the search termination
491 * or the pager might contain the page, allocate a new page.
493 * NOTE: We are still in a critical section.
495 if (TRYPAGER(fs) || fs->object == fs->first_object) {
497 * If the page is beyond the object size we fail
499 if (fs->pindex >= fs->object->size) {
501 unlock_and_deallocate(fs);
502 return (KERN_PROTECTION_FAILURE);
508 if (fs->didlimit == 0 && curproc != NULL) {
511 limticks = vm_fault_ratelimit(curproc->p_vmspace);
514 unlock_and_deallocate(fs);
515 tsleep(curproc, 0, "vmrate", limticks);
517 return (KERN_TRY_AGAIN);
522 * Allocate a new page for this object/offset pair.
525 if (!vm_page_count_severe()) {
526 fs->m = vm_page_alloc(fs->object, fs->pindex,
527 (fs->vp || fs->object->backing_object) ? VM_ALLOC_NORMAL : VM_ALLOC_NORMAL | VM_ALLOC_ZERO);
531 unlock_and_deallocate(fs);
533 return (KERN_TRY_AGAIN);
540 * We have found a valid page or we have allocated a new page.
541 * The page thus may not be valid or may not be entirely
544 * Attempt to fault-in the page if there is a chance that the
545 * pager has it, and potentially fault in additional pages
548 * We are NOT in splvm here and if TRYPAGER is true then
549 * fs.m will be non-NULL and will be PG_BUSY for us.
556 u_char behavior = vm_map_entry_behavior(fs->entry);
558 if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
563 if (behind > VM_FAULT_READ_BEHIND)
564 behind = VM_FAULT_READ_BEHIND;
566 ahead = fs->object->size - fs->pindex;
569 if (ahead > VM_FAULT_READ_AHEAD)
570 ahead = VM_FAULT_READ_AHEAD;
573 if ((fs->first_object->type != OBJT_DEVICE) &&
574 (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
575 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
576 fs->pindex >= fs->entry->lastr &&
577 fs->pindex < fs->entry->lastr + VM_FAULT_READ))
579 vm_pindex_t firstpindex, tmppindex;
581 if (fs->first_pindex < 2 * VM_FAULT_READ)
584 firstpindex = fs->first_pindex - 2 * VM_FAULT_READ;
587 * note: partially valid pages cannot be
588 * included in the lookahead - NFS piecemeal
589 * writes will barf on it badly.
591 * spl protection is required to avoid races
592 * between the lookup and an interrupt
593 * unbusy/free sequence occuring prior to
597 for (tmppindex = fs->first_pindex - 1;
598 tmppindex >= firstpindex;
603 mt = vm_page_lookup(fs->first_object, tmppindex);
604 if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL))
607 (mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) ||
612 vm_page_test_dirty(mt);
614 vm_page_protect(mt, VM_PROT_NONE);
615 vm_page_deactivate(mt);
627 * now we find out if any other pages should be paged
628 * in at this time this routine checks to see if the
629 * pages surrounding this fault reside in the same
630 * object as the page for this fault. If they do,
631 * then they are faulted in also into the object. The
632 * array "marray" returned contains an array of
633 * vm_page_t structs where one of them is the
634 * vm_page_t passed to the routine. The reqpage
635 * return value is the index into the marray for the
636 * vm_page_t passed to the routine.
638 * fs.m plus the additional pages are PG_BUSY'd.
640 faultcount = vm_fault_additional_pages(
641 fs->m, behind, ahead, marray, &reqpage);
644 * update lastr imperfectly (we do not know how much
645 * getpages will actually read), but good enough.
647 fs->entry->lastr = fs->pindex + faultcount - behind;
650 * Call the pager to retrieve the data, if any, after
651 * releasing the lock on the map. We hold a ref on
652 * fs.object and the pages are PG_BUSY'd.
657 rv = vm_pager_get_pages(fs->object, marray,
658 faultcount, reqpage);
663 if (rv == VM_PAGER_OK) {
665 * Found the page. Leave it busy while we play
670 * Relookup in case pager changed page. Pager
671 * is responsible for disposition of old page
674 * XXX other code segments do relookups too.
675 * It's a bad abstraction that needs to be
678 fs->m = vm_page_lookup(fs->object, fs->pindex);
680 unlock_and_deallocate(fs);
681 return (KERN_TRY_AGAIN);
685 break; /* break to PAGE HAS BEEN FOUND */
689 * Remove the bogus page (which does not exist at this
690 * object/offset); before doing so, we must get back
691 * our object lock to preserve our invariant.
693 * Also wake up any other process that may want to bring
696 * If this is the top-level object, we must leave the
697 * busy page to prevent another process from rushing
698 * past us, and inserting the page in that object at
699 * the same time that we are.
701 if (rv == VM_PAGER_ERROR) {
703 printf("vm_fault: pager read error, pid %d (%s)\n", curproc->p_pid, curproc->p_comm);
705 printf("vm_fault: pager read error, thread %p (%s)\n", curthread, curproc->p_comm);
708 * Data outside the range of the pager or an I/O error
711 * XXX - the check for kernel_map is a kludge to work
712 * around having the machine panic on a kernel space
713 * fault w/ I/O error.
715 if (((fs->map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
716 (rv == VM_PAGER_BAD)) {
719 unlock_and_deallocate(fs);
720 if (rv == VM_PAGER_ERROR)
721 return (KERN_FAILURE);
723 return (KERN_PROTECTION_FAILURE);
726 if (fs->object != fs->first_object) {
730 * XXX - we cannot just fall out at this
731 * point, m has been freed and is invalid!
737 * We get here if the object has a default pager (or unwiring)
738 * or the pager doesn't have the page.
740 if (fs->object == fs->first_object)
744 * Move on to the next object. Lock the next object before
745 * unlocking the current one.
747 fs->pindex += OFF_TO_IDX(fs->object->backing_object_offset);
748 next_object = fs->object->backing_object;
749 if (next_object == NULL) {
751 * If there's no object left, fill the page in the top
754 if (fs->object != fs->first_object) {
755 vm_object_pip_wakeup(fs->object);
757 fs->object = fs->first_object;
758 fs->pindex = fs->first_pindex;
764 * Zero the page if necessary and mark it valid.
766 if ((fs->m->flags & PG_ZERO) == 0) {
767 vm_page_zero_fill(fs->m);
769 mycpu->gd_cnt.v_ozfod++;
771 mycpu->gd_cnt.v_zfod++;
772 fs->m->valid = VM_PAGE_BITS_ALL;
773 break; /* break to PAGE HAS BEEN FOUND */
775 if (fs->object != fs->first_object) {
776 vm_object_pip_wakeup(fs->object);
778 KASSERT(fs->object != next_object, ("object loop %p", next_object));
779 fs->object = next_object;
780 vm_object_pip_add(fs->object, 1);
784 KASSERT((fs->m->flags & PG_BUSY) != 0,
785 ("vm_fault: not busy after main loop"));
788 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
793 * If the page is being written, but isn't already owned by the
794 * top-level object, we have to copy it into a new page owned by the
797 if (fs->object != fs->first_object) {
799 * We only really need to copy if we want to write it.
801 if (fault_type & VM_PROT_WRITE) {
803 * This allows pages to be virtually copied from a
804 * backing_object into the first_object, where the
805 * backing object has no other refs to it, and cannot
806 * gain any more refs. Instead of a bcopy, we just
807 * move the page from the backing object to the
808 * first object. Note that we must mark the page
809 * dirty in the first object so that it will go out
810 * to swap when needed.
812 if (fs->map_generation == fs->map->timestamp &&
814 * Only one shadow object
816 (fs->object->shadow_count == 1) &&
818 * No COW refs, except us
820 (fs->object->ref_count == 1) &&
822 * No one else can look this object up
824 (fs->object->handle == NULL) &&
826 * No other ways to look the object up
828 ((fs->object->type == OBJT_DEFAULT) ||
829 (fs->object->type == OBJT_SWAP)) &&
831 * We don't chase down the shadow chain
833 (fs->object == fs->first_object->backing_object) &&
836 * grab the lock if we need to
838 (fs->lookup_still_valid ||
839 lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT) == 0)
842 fs->lookup_still_valid = 1;
844 * get rid of the unnecessary page
846 vm_page_protect(fs->first_m, VM_PROT_NONE);
847 vm_page_free(fs->first_m);
851 * grab the page and put it into the
852 * process'es object. The page is
853 * automatically made dirty.
855 vm_page_rename(fs->m, fs->first_object, fs->first_pindex);
857 vm_page_busy(fs->first_m);
859 mycpu->gd_cnt.v_cow_optim++;
862 * Oh, well, lets copy it.
864 vm_page_copy(fs->m, fs->first_m);
869 * We no longer need the old page or object.
875 * fs->object != fs->first_object due to above
878 vm_object_pip_wakeup(fs->object);
881 * Only use the new page below...
884 mycpu->gd_cnt.v_cow_faults++;
886 fs->object = fs->first_object;
887 fs->pindex = fs->first_pindex;
890 * If it wasn't a write fault avoid having to copy
891 * the page by mapping it read-only.
893 fs->prot &= ~VM_PROT_WRITE;
898 * We may have had to unlock a map to do I/O. If we did then
899 * lookup_still_valid will be FALSE. If the map generation count
900 * also changed then all sorts of things could have happened while
901 * we were doing the I/O and we need to retry.
904 if (!fs->lookup_still_valid &&
905 (fs->map->timestamp != fs->map_generation)) {
907 unlock_and_deallocate(fs);
908 return (KERN_TRY_AGAIN);
914 * THIS IS THE PREVIOUS CODE, WHICH TRIED TO OPTIMIZE THE RETRY PATH.
915 * SINCE WE HAD TO DO I/O ANYWAY, DON'T BOTHER
917 if (!fs->lookup_still_valid &&
918 (fs->map->timestamp != fs->map_generation)) {
919 vm_object_t retry_object;
920 vm_pindex_t retry_pindex;
921 vm_prot_t retry_prot;
924 * Since map entries may be pageable, make sure we can take a
925 * page fault on them.
929 * Unlock vnode before the lookup to avoid deadlock. E.G.
930 * avoid a deadlock between the inode and exec_map that can
931 * occur due to locks being obtained in different orders.
934 if (fs->vp != NULL) {
939 if (fs->map->infork) {
941 unlock_and_deallocate(fs);
942 return (KERN_TRY_AGAIN);
946 * To avoid trying to write_lock the map while another process
947 * has it read_locked (in vm_map_wire), we do not try for
948 * write permission. If the page is still writable, we will
949 * get write permission. If it is not, or has been marked
950 * needs_copy, we enter the mapping without write permission,
951 * and will merely take another fault.
953 result = vm_map_lookup(fs->map, vaddr,
954 fault_type & ~VM_PROT_WRITE, &fs->entry,
955 &retry_object, &retry_pindex,
956 &retry_prot, &fs->wired);
957 fs->map_generation = fs->map->timestamp;
960 * If we don't need the page any longer, put it on the active
961 * list (the easiest thing to do here). If no one needs it,
962 * pageout will grab it eventually.
965 if (result != KERN_SUCCESS) {
967 unlock_and_deallocate(&fs);
970 fs->lookup_still_valid = TRUE;
972 if ((retry_object != fs->first_object) ||
973 (retry_pindex != fs->first_pindex)) {
975 unlock_and_deallocate(fs);
976 return (KERN_TRY_AGAIN);
979 * Check whether the protection has changed or the object has
980 * been copied while we left the map unlocked. Changing from
981 * read to write permission is OK - we leave the page
982 * write-protected, and catch the write fault. Changing from
983 * write to read permission means that we can't mark the page
984 * write-enabled after all.
991 * Put this page into the physical map. We had to do the unlock above
992 * because pmap_enter may cause other faults. We don't put the page
993 * back on the active queue until later so that the page-out daemon
994 * won't find us (yet).
996 if (fs->prot & VM_PROT_WRITE) {
997 vm_page_flag_set(fs->m, PG_WRITEABLE);
998 vm_object_set_writeable_dirty(fs->m->object);
1001 * If the fault is a write, we know that this page is being
1002 * written NOW so dirty it explicitly to save on
1003 * pmap_is_modified() calls later.
1005 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
1006 * if the page is already dirty to prevent data written with
1007 * the expectation of being synced from not being synced.
1008 * Likewise if this entry does not request NOSYNC then make
1009 * sure the page isn't marked NOSYNC. Applications sharing
1010 * data should use the same flags to avoid ping ponging.
1012 * Also tell the backing pager, if any, that it should remove
1013 * any swap backing since the page is now dirty.
1015 if (fs->entry->eflags & MAP_ENTRY_NOSYNC) {
1016 if (fs->m->dirty == 0)
1017 vm_page_flag_set(fs->m, PG_NOSYNC);
1019 vm_page_flag_clear(fs->m, PG_NOSYNC);
1021 if (fs->fault_flags & VM_FAULT_DIRTY) {
1023 vm_page_dirty(fs->m);
1024 vm_pager_page_unswapped(fs->m);
1030 * Page had better still be busy
1033 KASSERT(fs->m->flags & PG_BUSY,
1034 ("vm_fault: page %p not busy!", fs->m));
1039 * Sanity check: page must be completely valid or it is not fit to
1040 * map into user space. vm_pager_get_pages() ensures this.
1042 if (fs->m->valid != VM_PAGE_BITS_ALL) {
1043 vm_page_zero_invalid(fs->m, TRUE);
1044 printf("Warning: page %p partially invalid on fault\n", fs->m);
1047 return (KERN_SUCCESS);
1051 * quick version of vm_fault
1054 vm_fault_quick(caddr_t v, int prot)
1058 if (prot & VM_PROT_WRITE)
1059 r = subyte(v, fubyte(v));
1066 * Wire down a range of virtual addresses in a map. The entry in question
1067 * should be marked in-transition and the map must be locked. We must
1068 * release the map temporarily while faulting-in the page to avoid a
1069 * deadlock. Note that the entry may be clipped while we are blocked but
1070 * will never be freed.
1073 vm_fault_wire(vm_map_t map, vm_map_entry_t entry, boolean_t user_wire)
1075 boolean_t fictitious;
1083 pmap = vm_map_pmap(map);
1084 start = entry->start;
1086 fictitious = entry->object.vm_object &&
1087 (entry->object.vm_object->type == OBJT_DEVICE);
1093 * We simulate a fault to get the page and enter it in the physical
1096 for (va = start; va < end; va += PAGE_SIZE) {
1098 rv = vm_fault(map, va, VM_PROT_READ,
1099 VM_FAULT_USER_WIRE);
1101 rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE,
1102 VM_FAULT_CHANGE_WIRING);
1105 while (va > start) {
1107 if ((pa = pmap_extract(pmap, va)) == 0)
1109 pmap_change_wiring(pmap, va, FALSE);
1111 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1118 return (KERN_SUCCESS);
1122 * Unwire a range of virtual addresses in a map. The map should be
1126 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
1128 boolean_t fictitious;
1135 pmap = vm_map_pmap(map);
1136 start = entry->start;
1138 fictitious = entry->object.vm_object &&
1139 (entry->object.vm_object->type == OBJT_DEVICE);
1142 * Since the pages are wired down, we must be able to get their
1143 * mappings from the physical map system.
1145 for (va = start; va < end; va += PAGE_SIZE) {
1146 pa = pmap_extract(pmap, va);
1148 pmap_change_wiring(pmap, va, FALSE);
1150 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1156 * Reduce the rate at which memory is allocated to a process based
1157 * on the perceived load on the VM system. As the load increases
1158 * the allocation burst rate goes down and the delay increases.
1160 * Rate limiting does not apply when faulting active or inactive
1161 * pages. When faulting 'cache' pages, rate limiting only applies
1162 * if the system currently has a severe page deficit.
1164 * XXX vm_pagesupply should be increased when a page is freed.
1166 * We sleep up to 1/10 of a second.
1169 vm_fault_ratelimit(struct vmspace *vmspace)
1171 if (vm_load_enable == 0)
1173 if (vmspace->vm_pagesupply > 0) {
1174 --vmspace->vm_pagesupply;
1178 if (vm_load_debug) {
1179 printf("load %-4d give %d pgs, wait %d, pid %-5d (%s)\n",
1181 (1000 - vm_load ) / 10, vm_load * hz / 10000,
1182 curproc->p_pid, curproc->p_comm);
1185 vmspace->vm_pagesupply = (1000 - vm_load) / 10;
1186 return(vm_load * hz / 10000);
1191 * vm_fault_copy_entry
1193 * Copy all of the pages from a wired-down map entry to another.
1195 * In/out conditions:
1196 * The source and destination maps must be locked for write.
1197 * The source map entry must be wired down (or be a sharing map
1198 * entry corresponding to a main map entry that is wired down).
1202 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1203 vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
1205 vm_object_t dst_object;
1206 vm_object_t src_object;
1207 vm_ooffset_t dst_offset;
1208 vm_ooffset_t src_offset;
1218 src_object = src_entry->object.vm_object;
1219 src_offset = src_entry->offset;
1222 * Create the top-level object for the destination entry. (Doesn't
1223 * actually shadow anything - we copy the pages directly.)
1225 dst_object = vm_object_allocate(OBJT_DEFAULT,
1226 (vm_size_t) OFF_TO_IDX(dst_entry->end - dst_entry->start));
1228 dst_entry->object.vm_object = dst_object;
1229 dst_entry->offset = 0;
1231 prot = dst_entry->max_protection;
1234 * Loop through all of the pages in the entry's range, copying each
1235 * one from the source object (it should be there) to the destination
1238 for (vaddr = dst_entry->start, dst_offset = 0;
1239 vaddr < dst_entry->end;
1240 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1243 * Allocate a page in the destination object
1246 dst_m = vm_page_alloc(dst_object,
1247 OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1248 if (dst_m == NULL) {
1251 } while (dst_m == NULL);
1254 * Find the page in the source object, and copy it in.
1255 * (Because the source is wired down, the page will be in
1258 src_m = vm_page_lookup(src_object,
1259 OFF_TO_IDX(dst_offset + src_offset));
1261 panic("vm_fault_copy_wired: page missing");
1263 vm_page_copy(src_m, dst_m);
1266 * Enter it in the pmap...
1269 vm_page_flag_clear(dst_m, PG_ZERO);
1270 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
1271 vm_page_flag_set(dst_m, PG_WRITEABLE|PG_MAPPED);
1274 * Mark it no longer busy, and put it on the active list.
1276 vm_page_activate(dst_m);
1277 vm_page_wakeup(dst_m);
1283 * This routine checks around the requested page for other pages that
1284 * might be able to be faulted in. This routine brackets the viable
1285 * pages for the pages to be paged in.
1288 * m, rbehind, rahead
1291 * marray (array of vm_page_t), reqpage (index of requested page)
1294 * number of pages in marray
1297 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
1298 vm_page_t *marray, int *reqpage)
1302 vm_pindex_t pindex, startpindex, endpindex, tpindex;
1304 int cbehind, cahead;
1310 * we don't fault-ahead for device pager
1312 if (object->type == OBJT_DEVICE) {
1319 * if the requested page is not available, then give up now
1322 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1326 if ((cbehind == 0) && (cahead == 0)) {
1332 if (rahead > cahead) {
1336 if (rbehind > cbehind) {
1341 * try to do any readahead that we might have free pages for.
1343 if ((rahead + rbehind) >
1344 ((vmstats.v_free_count + vmstats.v_cache_count) - vmstats.v_free_reserved)) {
1345 pagedaemon_wakeup();
1352 * scan backward for the read behind pages -- in memory
1354 * Assume that if the page is not found an interrupt will not
1355 * create it. Theoretically interrupts can only remove (busy)
1356 * pages, not create new associations.
1359 if (rbehind > pindex) {
1363 startpindex = pindex - rbehind;
1367 for ( tpindex = pindex - 1; tpindex >= startpindex; tpindex -= 1) {
1368 if (vm_page_lookup( object, tpindex)) {
1369 startpindex = tpindex + 1;
1376 for(i = 0, tpindex = startpindex; tpindex < pindex; i++, tpindex++) {
1378 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1381 for (j = 0; j < i; j++) {
1382 vm_page_free(marray[j]);
1398 /* page offset of the required page */
1401 tpindex = pindex + 1;
1405 * scan forward for the read ahead pages
1407 endpindex = tpindex + rahead;
1408 if (endpindex > object->size)
1409 endpindex = object->size;
1412 for( ; tpindex < endpindex; i++, tpindex++) {
1414 if (vm_page_lookup(object, tpindex)) {
1418 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1427 /* return number of bytes of pages */