FreeBSD/Linux Kernel Cross Reference
sys/vm/vm_fault.c
1 /*
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1994 John S. Dyson
5 * All rights reserved.
6 * Copyright (c) 1994 David Greenman
7 * All rights reserved.
8 *
9 *
10 * This code is derived from software contributed to Berkeley by
11 * The Mach Operating System project at Carnegie-Mellon University.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
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.
28 *
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
39 * SUCH DAMAGE.
40 *
41 * from: @(#)vm_fault.c 8.4 (Berkeley) 1/12/94
42 *
43 *
44 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
45 * All rights reserved.
46 *
47 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
48 *
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.
54 *
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.
58 *
59 * Carnegie Mellon requests users of this software to return to
60 *
61 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
62 * School of Computer Science
63 * Carnegie Mellon University
64 * Pittsburgh PA 15213-3890
65 *
66 * any improvements or extensions that they make and grant Carnegie the
67 * rights to redistribute these changes.
68 *
69 * $FreeBSD: releng/5.1/sys/vm/vm_fault.c 113868 2003-04-22 20:01:56Z jhb $
70 */
71
72 /*
73 * Page fault handling module.
74 */
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/lock.h>
79 #include <sys/mutex.h>
80 #include <sys/proc.h>
81 #include <sys/resourcevar.h>
82 #include <sys/sysctl.h>
83 #include <sys/vmmeter.h>
84 #include <sys/vnode.h>
85
86 #include <vm/vm.h>
87 #include <vm/vm_param.h>
88 #include <vm/pmap.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>
97
98 static int vm_fault_additional_pages(vm_page_t, int, int, vm_page_t *, int *);
99
100 #define VM_FAULT_READ_AHEAD 8
101 #define VM_FAULT_READ_BEHIND 7
102 #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1)
103
104 struct faultstate {
105 vm_page_t m;
106 vm_object_t object;
107 vm_pindex_t pindex;
108 vm_page_t first_m;
109 vm_object_t first_object;
110 vm_pindex_t first_pindex;
111 vm_map_t map;
112 vm_map_entry_t entry;
113 int lookup_still_valid;
114 struct vnode *vp;
115 };
116
117 static __inline void
118 release_page(struct faultstate *fs)
119 {
120 vm_page_lock_queues();
121 vm_page_wakeup(fs->m);
122 vm_page_deactivate(fs->m);
123 vm_page_unlock_queues();
124 fs->m = NULL;
125 }
126
127 static __inline void
128 unlock_map(struct faultstate *fs)
129 {
130 if (fs->lookup_still_valid) {
131 vm_map_lookup_done(fs->map, fs->entry);
132 fs->lookup_still_valid = FALSE;
133 }
134 }
135
136 static void
137 _unlock_things(struct faultstate *fs, int dealloc)
138 {
139 GIANT_REQUIRED;
140 VM_OBJECT_LOCK(fs->object);
141 vm_object_pip_wakeup(fs->object);
142 VM_OBJECT_UNLOCK(fs->object);
143 if (fs->object != fs->first_object) {
144 VM_OBJECT_LOCK(fs->first_object);
145 vm_page_lock_queues();
146 vm_page_free(fs->first_m);
147 vm_page_unlock_queues();
148 vm_object_pip_wakeup(fs->first_object);
149 VM_OBJECT_UNLOCK(fs->first_object);
150 fs->first_m = NULL;
151 }
152 if (dealloc) {
153 vm_object_deallocate(fs->first_object);
154 }
155 unlock_map(fs);
156 if (fs->vp != NULL) {
157 vput(fs->vp);
158 fs->vp = NULL;
159 }
160 }
161
162 #define unlock_things(fs) _unlock_things(fs, 0)
163 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
164
165 /*
166 * TRYPAGER - used by vm_fault to calculate whether the pager for the
167 * current object *might* contain the page.
168 *
169 * default objects are zero-fill, there is no real pager.
170 */
171 #define TRYPAGER (fs.object->type != OBJT_DEFAULT && \
172 (((fault_flags & VM_FAULT_WIRE_MASK) == 0) || wired))
173
174 /*
175 * vm_fault:
176 *
177 * Handle a page fault occurring at the given address,
178 * requiring the given permissions, in the map specified.
179 * If successful, the page is inserted into the
180 * associated physical map.
181 *
182 * NOTE: the given address should be truncated to the
183 * proper page address.
184 *
185 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
186 * a standard error specifying why the fault is fatal is returned.
187 *
188 *
189 * The map in question must be referenced, and remains so.
190 * Caller may hold no locks.
191 */
192 int
193 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
194 int fault_flags)
195 {
196 vm_prot_t prot;
197 int result;
198 boolean_t growstack, wired;
199 int map_generation;
200 vm_object_t next_object;
201 vm_page_t marray[VM_FAULT_READ];
202 int hardfault;
203 int faultcount;
204 struct faultstate fs;
205
206 hardfault = 0;
207 growstack = TRUE;
208 atomic_add_int(&cnt.v_vm_faults, 1);
209
210 mtx_lock(&Giant);
211 RetryFault:;
212
213 /*
214 * Find the backing store object and offset into it to begin the
215 * search.
216 */
217 fs.map = map;
218 result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry,
219 &fs.first_object, &fs.first_pindex, &prot, &wired);
220 if (result != KERN_SUCCESS) {
221 if (result != KERN_PROTECTION_FAILURE ||
222 (fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE) {
223 if (growstack && result == KERN_INVALID_ADDRESS &&
224 map != kernel_map && curproc != NULL) {
225 result = vm_map_growstack(curproc, vaddr);
226 if (result != KERN_SUCCESS) {
227 mtx_unlock(&Giant);
228 return (KERN_FAILURE);
229 }
230 growstack = FALSE;
231 goto RetryFault;
232 }
233 mtx_unlock(&Giant);
234 return (result);
235 }
236
237 /*
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 COW
240 * currently 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.
243 */
244 result = vm_map_lookup(&fs.map, vaddr,
245 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_OVERRIDE_WRITE,
246 &fs.entry, &fs.first_object, &fs.first_pindex, &prot, &wired);
247 if (result != KERN_SUCCESS) {
248 mtx_unlock(&Giant);
249 return (result);
250 }
251
252 /*
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.
256 *
257 * XXX The following assignment modifies the map without
258 * holding a write lock on it.
259 */
260 if ((fs.entry->protection & VM_PROT_WRITE) == 0)
261 fs.entry->max_protection &= ~VM_PROT_WRITE;
262 }
263
264 map_generation = fs.map->timestamp;
265
266 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
267 panic("vm_fault: fault on nofault entry, addr: %lx",
268 (u_long)vaddr);
269 }
270
271 /*
272 * Make a reference to this object to prevent its disposal while we
273 * are messing with it. Once we have the reference, the map is free
274 * to be diddled. Since objects reference their shadows (and copies),
275 * they will stay around as well.
276 *
277 * Bump the paging-in-progress count to prevent size changes (e.g.
278 * truncation operations) during I/O. This must be done after
279 * obtaining the vnode lock in order to avoid possible deadlocks.
280 *
281 * XXX vnode_pager_lock() can block without releasing the map lock.
282 */
283 vm_object_reference(fs.first_object);
284 fs.vp = vnode_pager_lock(fs.first_object);
285 VM_OBJECT_LOCK(fs.first_object);
286 vm_object_pip_add(fs.first_object, 1);
287 VM_OBJECT_UNLOCK(fs.first_object);
288
289 fs.lookup_still_valid = TRUE;
290
291 if (wired)
292 fault_type = prot;
293
294 fs.first_m = NULL;
295
296 /*
297 * Search for the page at object/offset.
298 */
299 fs.object = fs.first_object;
300 fs.pindex = fs.first_pindex;
301 while (TRUE) {
302 /*
303 * If the object is dead, we stop here
304 */
305 if (fs.object->flags & OBJ_DEAD) {
306 unlock_and_deallocate(&fs);
307 mtx_unlock(&Giant);
308 return (KERN_PROTECTION_FAILURE);
309 }
310
311 /*
312 * See if page is resident
313 */
314 fs.m = vm_page_lookup(fs.object, fs.pindex);
315 if (fs.m != NULL) {
316 int queue, s;
317
318 /*
319 * check for page-based copy on write.
320 * We check fs.object == fs.first_object so
321 * as to ensure the legacy COW mechanism is
322 * used when the page in question is part of
323 * a shadow object. Otherwise, vm_page_cowfault()
324 * removes the page from the backing object,
325 * which is not what we want.
326 */
327 vm_page_lock_queues();
328 if ((fs.m->cow) &&
329 (fault_type & VM_PROT_WRITE) &&
330 (fs.object == fs.first_object)) {
331 s = splvm();
332 vm_page_cowfault(fs.m);
333 splx(s);
334 vm_page_unlock_queues();
335 unlock_things(&fs);
336 goto RetryFault;
337 }
338
339 /*
340 * Wait/Retry if the page is busy. We have to do this
341 * if the page is busy via either PG_BUSY or
342 * vm_page_t->busy because the vm_pager may be using
343 * vm_page_t->busy for pageouts ( and even pageins if
344 * it is the vnode pager ), and we could end up trying
345 * to pagein and pageout the same page simultaneously.
346 *
347 * We can theoretically allow the busy case on a read
348 * fault if the page is marked valid, but since such
349 * pages are typically already pmap'd, putting that
350 * special case in might be more effort then it is
351 * worth. We cannot under any circumstances mess
352 * around with a vm_page_t->busy page except, perhaps,
353 * to pmap it.
354 */
355 if ((fs.m->flags & PG_BUSY) || fs.m->busy) {
356 vm_page_unlock_queues();
357 unlock_things(&fs);
358 vm_page_lock_queues();
359 if (!vm_page_sleep_if_busy(fs.m, TRUE, "vmpfw"))
360 vm_page_unlock_queues();
361 cnt.v_intrans++;
362 vm_object_deallocate(fs.first_object);
363 goto RetryFault;
364 }
365 queue = fs.m->queue;
366
367 s = splvm();
368 vm_pageq_remove_nowakeup(fs.m);
369 splx(s);
370
371 if ((queue - fs.m->pc) == PQ_CACHE && vm_page_count_severe()) {
372 vm_page_activate(fs.m);
373 vm_page_unlock_queues();
374 unlock_and_deallocate(&fs);
375 VM_WAITPFAULT;
376 goto RetryFault;
377 }
378
379 /*
380 * Mark page busy for other processes, and the
381 * pagedaemon. If it still isn't completely valid
382 * (readable), jump to readrest, else break-out ( we
383 * found the page ).
384 */
385 vm_page_busy(fs.m);
386 vm_page_unlock_queues();
387 if (((fs.m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) &&
388 fs.m->object != kernel_object && fs.m->object != kmem_object) {
389 goto readrest;
390 }
391
392 break;
393 }
394
395 /*
396 * Page is not resident, If this is the search termination
397 * or the pager might contain the page, allocate a new page.
398 */
399 if (TRYPAGER || fs.object == fs.first_object) {
400 if (fs.pindex >= fs.object->size) {
401 unlock_and_deallocate(&fs);
402 mtx_unlock(&Giant);
403 return (KERN_PROTECTION_FAILURE);
404 }
405
406 /*
407 * Allocate a new page for this object/offset pair.
408 */
409 fs.m = NULL;
410 if (!vm_page_count_severe()) {
411 fs.m = vm_page_alloc(fs.object, fs.pindex,
412 (fs.vp || fs.object->backing_object)? VM_ALLOC_NORMAL: VM_ALLOC_ZERO);
413 }
414 if (fs.m == NULL) {
415 unlock_and_deallocate(&fs);
416 VM_WAITPFAULT;
417 goto RetryFault;
418 }
419 }
420
421 readrest:
422 /*
423 * We have found a valid page or we have allocated a new page.
424 * The page thus may not be valid or may not be entirely
425 * valid.
426 *
427 * Attempt to fault-in the page if there is a chance that the
428 * pager has it, and potentially fault in additional pages
429 * at the same time.
430 */
431 if (TRYPAGER) {
432 int rv;
433 int reqpage;
434 int ahead, behind;
435 u_char behavior = vm_map_entry_behavior(fs.entry);
436
437 if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
438 ahead = 0;
439 behind = 0;
440 } else {
441 behind = (vaddr - fs.entry->start) >> PAGE_SHIFT;
442 if (behind > VM_FAULT_READ_BEHIND)
443 behind = VM_FAULT_READ_BEHIND;
444
445 ahead = ((fs.entry->end - vaddr) >> PAGE_SHIFT) - 1;
446 if (ahead > VM_FAULT_READ_AHEAD)
447 ahead = VM_FAULT_READ_AHEAD;
448 }
449
450 if ((fs.first_object->type != OBJT_DEVICE) &&
451 (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
452 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
453 fs.pindex >= fs.entry->lastr &&
454 fs.pindex < fs.entry->lastr + VM_FAULT_READ))
455 ) {
456 vm_pindex_t firstpindex, tmppindex;
457
458 if (fs.first_pindex < 2 * VM_FAULT_READ)
459 firstpindex = 0;
460 else
461 firstpindex = fs.first_pindex - 2 * VM_FAULT_READ;
462
463 vm_page_lock_queues();
464 /*
465 * note: partially valid pages cannot be
466 * included in the lookahead - NFS piecemeal
467 * writes will barf on it badly.
468 */
469 for (tmppindex = fs.first_pindex - 1;
470 tmppindex >= firstpindex;
471 --tmppindex) {
472 vm_page_t mt;
473
474 mt = vm_page_lookup(fs.first_object, tmppindex);
475 if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL))
476 break;
477 if (mt->busy ||
478 (mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) ||
479 mt->hold_count ||
480 mt->wire_count)
481 continue;
482 if (mt->dirty == 0)
483 vm_page_test_dirty(mt);
484 if (mt->dirty) {
485 pmap_remove_all(mt);
486 vm_page_deactivate(mt);
487 } else {
488 vm_page_cache(mt);
489 }
490 }
491 vm_page_unlock_queues();
492 ahead += behind;
493 behind = 0;
494 }
495
496 /*
497 * now we find out if any other pages should be paged
498 * in at this time this routine checks to see if the
499 * pages surrounding this fault reside in the same
500 * object as the page for this fault. If they do,
501 * then they are faulted in also into the object. The
502 * array "marray" returned contains an array of
503 * vm_page_t structs where one of them is the
504 * vm_page_t passed to the routine. The reqpage
505 * return value is the index into the marray for the
506 * vm_page_t passed to the routine.
507 *
508 * fs.m plus the additional pages are PG_BUSY'd.
509 *
510 * XXX vm_fault_additional_pages() can block
511 * without releasing the map lock.
512 */
513 faultcount = vm_fault_additional_pages(
514 fs.m, behind, ahead, marray, &reqpage);
515
516 /*
517 * update lastr imperfectly (we do not know how much
518 * getpages will actually read), but good enough.
519 *
520 * XXX The following assignment modifies the map
521 * without holding a write lock on it.
522 */
523 fs.entry->lastr = fs.pindex + faultcount - behind;
524
525 /*
526 * Call the pager to retrieve the data, if any, after
527 * releasing the lock on the map. We hold a ref on
528 * fs.object and the pages are PG_BUSY'd.
529 */
530 unlock_map(&fs);
531
532 rv = faultcount ?
533 vm_pager_get_pages(fs.object, marray, faultcount,
534 reqpage) : VM_PAGER_FAIL;
535
536 if (rv == VM_PAGER_OK) {
537 /*
538 * Found the page. Leave it busy while we play
539 * with it.
540 */
541
542 /*
543 * Relookup in case pager changed page. Pager
544 * is responsible for disposition of old page
545 * if moved.
546 */
547 fs.m = vm_page_lookup(fs.object, fs.pindex);
548 if (!fs.m) {
549 unlock_and_deallocate(&fs);
550 goto RetryFault;
551 }
552
553 hardfault++;
554 break; /* break to PAGE HAS BEEN FOUND */
555 }
556 /*
557 * Remove the bogus page (which does not exist at this
558 * object/offset); before doing so, we must get back
559 * our object lock to preserve our invariant.
560 *
561 * Also wake up any other process that may want to bring
562 * in this page.
563 *
564 * If this is the top-level object, we must leave the
565 * busy page to prevent another process from rushing
566 * past us, and inserting the page in that object at
567 * the same time that we are.
568 */
569 if (rv == VM_PAGER_ERROR)
570 printf("vm_fault: pager read error, pid %d (%s)\n",
571 curproc->p_pid, curproc->p_comm);
572 /*
573 * Data outside the range of the pager or an I/O error
574 */
575 /*
576 * XXX - the check for kernel_map is a kludge to work
577 * around having the machine panic on a kernel space
578 * fault w/ I/O error.
579 */
580 if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
581 (rv == VM_PAGER_BAD)) {
582 vm_page_lock_queues();
583 vm_page_free(fs.m);
584 vm_page_unlock_queues();
585 fs.m = NULL;
586 unlock_and_deallocate(&fs);
587 mtx_unlock(&Giant);
588 return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
589 }
590 if (fs.object != fs.first_object) {
591 vm_page_lock_queues();
592 vm_page_free(fs.m);
593 vm_page_unlock_queues();
594 fs.m = NULL;
595 /*
596 * XXX - we cannot just fall out at this
597 * point, m has been freed and is invalid!
598 */
599 }
600 }
601
602 /*
603 * We get here if the object has default pager (or unwiring)
604 * or the pager doesn't have the page.
605 */
606 if (fs.object == fs.first_object)
607 fs.first_m = fs.m;
608
609 /*
610 * Move on to the next object. Lock the next object before
611 * unlocking the current one.
612 */
613 fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
614 next_object = fs.object->backing_object;
615 if (next_object == NULL) {
616 /*
617 * If there's no object left, fill the page in the top
618 * object with zeros.
619 */
620 if (fs.object != fs.first_object) {
621 VM_OBJECT_LOCK(fs.object);
622 vm_object_pip_wakeup(fs.object);
623 VM_OBJECT_UNLOCK(fs.object);
624
625 fs.object = fs.first_object;
626 fs.pindex = fs.first_pindex;
627 fs.m = fs.first_m;
628 }
629 fs.first_m = NULL;
630
631 /*
632 * Zero the page if necessary and mark it valid.
633 */
634 if ((fs.m->flags & PG_ZERO) == 0) {
635 pmap_zero_page(fs.m);
636 } else {
637 cnt.v_ozfod++;
638 }
639 cnt.v_zfod++;
640 fs.m->valid = VM_PAGE_BITS_ALL;
641 break; /* break to PAGE HAS BEEN FOUND */
642 } else {
643 if (fs.object != fs.first_object) {
644 VM_OBJECT_LOCK(fs.object);
645 vm_object_pip_wakeup(fs.object);
646 VM_OBJECT_UNLOCK(fs.object);
647 }
648 KASSERT(fs.object != next_object, ("object loop %p", next_object));
649 fs.object = next_object;
650 VM_OBJECT_LOCK(fs.object);
651 vm_object_pip_add(fs.object, 1);
652 VM_OBJECT_UNLOCK(fs.object);
653 }
654 }
655
656 KASSERT((fs.m->flags & PG_BUSY) != 0,
657 ("vm_fault: not busy after main loop"));
658
659 /*
660 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
661 * is held.]
662 */
663
664 /*
665 * If the page is being written, but isn't already owned by the
666 * top-level object, we have to copy it into a new page owned by the
667 * top-level object.
668 */
669 if (fs.object != fs.first_object) {
670 /*
671 * We only really need to copy if we want to write it.
672 */
673 if (fault_type & VM_PROT_WRITE) {
674 /*
675 * This allows pages to be virtually copied from a
676 * backing_object into the first_object, where the
677 * backing object has no other refs to it, and cannot
678 * gain any more refs. Instead of a bcopy, we just
679 * move the page from the backing object to the
680 * first object. Note that we must mark the page
681 * dirty in the first object so that it will go out
682 * to swap when needed.
683 */
684 if (map_generation == fs.map->timestamp &&
685 /*
686 * Only one shadow object
687 */
688 (fs.object->shadow_count == 1) &&
689 /*
690 * No COW refs, except us
691 */
692 (fs.object->ref_count == 1) &&
693 /*
694 * No one else can look this object up
695 */
696 (fs.object->handle == NULL) &&
697 /*
698 * No other ways to look the object up
699 */
700 ((fs.object->type == OBJT_DEFAULT) ||
701 (fs.object->type == OBJT_SWAP)) &&
702 /*
703 * We don't chase down the shadow chain
704 */
705 (fs.object == fs.first_object->backing_object) &&
706
707 /*
708 * grab the lock if we need to
709 */
710 (fs.lookup_still_valid || vm_map_trylock(fs.map))) {
711
712 fs.lookup_still_valid = 1;
713 /*
714 * get rid of the unnecessary page
715 */
716 vm_page_lock_queues();
717 pmap_remove_all(fs.first_m);
718 vm_page_free(fs.first_m);
719 fs.first_m = NULL;
720
721 /*
722 * grab the page and put it into the
723 * process'es object. The page is
724 * automatically made dirty.
725 */
726 vm_page_rename(fs.m, fs.first_object, fs.first_pindex);
727 fs.first_m = fs.m;
728 vm_page_busy(fs.first_m);
729 vm_page_unlock_queues();
730 fs.m = NULL;
731 cnt.v_cow_optim++;
732 } else {
733 /*
734 * Oh, well, lets copy it.
735 */
736 vm_page_copy(fs.m, fs.first_m);
737 }
738
739 if (fs.m) {
740 /*
741 * We no longer need the old page or object.
742 */
743 release_page(&fs);
744 }
745
746 /*
747 * fs.object != fs.first_object due to above
748 * conditional
749 */
750 VM_OBJECT_LOCK(fs.object);
751 vm_object_pip_wakeup(fs.object);
752 VM_OBJECT_UNLOCK(fs.object);
753
754 /*
755 * Only use the new page below...
756 */
757 cnt.v_cow_faults++;
758 fs.m = fs.first_m;
759 fs.object = fs.first_object;
760 fs.pindex = fs.first_pindex;
761
762 } else {
763 prot &= ~VM_PROT_WRITE;
764 }
765 }
766
767 /*
768 * We must verify that the maps have not changed since our last
769 * lookup.
770 */
771 if (!fs.lookup_still_valid &&
772 (fs.map->timestamp != map_generation)) {
773 vm_object_t retry_object;
774 vm_pindex_t retry_pindex;
775 vm_prot_t retry_prot;
776
777 /*
778 * Since map entries may be pageable, make sure we can take a
779 * page fault on them.
780 */
781
782 /*
783 * Unlock vnode before the lookup to avoid deadlock. E.G.
784 * avoid a deadlock between the inode and exec_map that can
785 * occur due to locks being obtained in different orders.
786 */
787 if (fs.vp != NULL) {
788 vput(fs.vp);
789 fs.vp = NULL;
790 }
791
792 if (fs.map->infork) {
793 release_page(&fs);
794 unlock_and_deallocate(&fs);
795 goto RetryFault;
796 }
797
798 /*
799 * To avoid trying to write_lock the map while another process
800 * has it read_locked (in vm_map_pageable), we do not try for
801 * write permission. If the page is still writable, we will
802 * get write permission. If it is not, or has been marked
803 * needs_copy, we enter the mapping without write permission,
804 * and will merely take another fault.
805 */
806 result = vm_map_lookup(&fs.map, vaddr, fault_type & ~VM_PROT_WRITE,
807 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
808 map_generation = fs.map->timestamp;
809
810 /*
811 * If we don't need the page any longer, put it on the active
812 * list (the easiest thing to do here). If no one needs it,
813 * pageout will grab it eventually.
814 */
815 if (result != KERN_SUCCESS) {
816 release_page(&fs);
817 unlock_and_deallocate(&fs);
818 mtx_unlock(&Giant);
819 return (result);
820 }
821 fs.lookup_still_valid = TRUE;
822
823 if ((retry_object != fs.first_object) ||
824 (retry_pindex != fs.first_pindex)) {
825 release_page(&fs);
826 unlock_and_deallocate(&fs);
827 goto RetryFault;
828 }
829 /*
830 * Check whether the protection has changed or the object has
831 * been copied while we left the map unlocked. Changing from
832 * read to write permission is OK - we leave the page
833 * write-protected, and catch the write fault. Changing from
834 * write to read permission means that we can't mark the page
835 * write-enabled after all.
836 */
837 prot &= retry_prot;
838 }
839
840 /*
841 * Put this page into the physical map. We had to do the unlock above
842 * because pmap_enter may cause other faults. We don't put the page
843 * back on the active queue until later so that the page-out daemon
844 * won't find us (yet).
845 */
846
847 if (prot & VM_PROT_WRITE) {
848 vm_page_lock_queues();
849 vm_page_flag_set(fs.m, PG_WRITEABLE);
850 vm_object_set_writeable_dirty(fs.m->object);
851
852 /*
853 * If the fault is a write, we know that this page is being
854 * written NOW so dirty it explicitly to save on
855 * pmap_is_modified() calls later.
856 *
857 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
858 * if the page is already dirty to prevent data written with
859 * the expectation of being synced from not being synced.
860 * Likewise if this entry does not request NOSYNC then make
861 * sure the page isn't marked NOSYNC. Applications sharing
862 * data should use the same flags to avoid ping ponging.
863 *
864 * Also tell the backing pager, if any, that it should remove
865 * any swap backing since the page is now dirty.
866 */
867 if (fs.entry->eflags & MAP_ENTRY_NOSYNC) {
868 if (fs.m->dirty == 0)
869 vm_page_flag_set(fs.m, PG_NOSYNC);
870 } else {
871 vm_page_flag_clear(fs.m, PG_NOSYNC);
872 }
873 vm_page_unlock_queues();
874 if (fault_flags & VM_FAULT_DIRTY) {
875 int s;
876 vm_page_dirty(fs.m);
877 s = splvm();
878 vm_pager_page_unswapped(fs.m);
879 splx(s);
880 }
881 }
882
883 /*
884 * Page had better still be busy
885 */
886 KASSERT(fs.m->flags & PG_BUSY,
887 ("vm_fault: page %p not busy!", fs.m));
888 unlock_things(&fs);
889
890 /*
891 * Sanity check: page must be completely valid or it is not fit to
892 * map into user space. vm_pager_get_pages() ensures this.
893 */
894 if (fs.m->valid != VM_PAGE_BITS_ALL) {
895 vm_page_zero_invalid(fs.m, TRUE);
896 printf("Warning: page %p partially invalid on fault\n", fs.m);
897 }
898 pmap_enter(fs.map->pmap, vaddr, fs.m, prot, wired);
899 if (((fault_flags & VM_FAULT_WIRE_MASK) == 0) && (wired == 0)) {
900 pmap_prefault(fs.map->pmap, vaddr, fs.entry);
901 }
902 vm_page_lock_queues();
903 vm_page_flag_clear(fs.m, PG_ZERO);
904 vm_page_flag_set(fs.m, PG_REFERENCED);
905
906 /*
907 * If the page is not wired down, then put it where the pageout daemon
908 * can find it.
909 */
910 if (fault_flags & VM_FAULT_WIRE_MASK) {
911 if (wired)
912 vm_page_wire(fs.m);
913 else
914 vm_page_unwire(fs.m, 1);
915 } else {
916 vm_page_activate(fs.m);
917 }
918 vm_page_wakeup(fs.m);
919 vm_page_unlock_queues();
920
921 PROC_LOCK(curproc);
922 if ((curproc->p_sflag & PS_INMEM) && curproc->p_stats) {
923 if (hardfault) {
924 curproc->p_stats->p_ru.ru_majflt++;
925 } else {
926 curproc->p_stats->p_ru.ru_minflt++;
927 }
928 }
929 PROC_UNLOCK(curproc);
930
931 /*
932 * Unlock everything, and return
933 */
934 vm_object_deallocate(fs.first_object);
935 mtx_unlock(&Giant);
936 return (KERN_SUCCESS);
937 }
938
939 /*
940 * vm_fault_quick:
941 *
942 * Ensure that the requested virtual address, which may be in userland,
943 * is valid. Fault-in the page if necessary. Return -1 on failure.
944 */
945 int
946 vm_fault_quick(caddr_t v, int prot)
947 {
948 int r;
949
950 if (prot & VM_PROT_WRITE)
951 r = subyte(v, fubyte(v));
952 else
953 r = fubyte(v);
954 return(r);
955 }
956
957 /*
958 * vm_fault_wire:
959 *
960 * Wire down a range of virtual addresses in a map.
961 */
962 int
963 vm_fault_wire(map, start, end, user_wire)
964 vm_map_t map;
965 vm_offset_t start, end;
966 boolean_t user_wire;
967 {
968 vm_offset_t va;
969 int rv;
970
971 /*
972 * We simulate a fault to get the page and enter it in the physical
973 * map. For user wiring, we only ask for read access on currently
974 * read-only sections.
975 */
976 for (va = start; va < end; va += PAGE_SIZE) {
977 rv = vm_fault(map, va,
978 user_wire ? VM_PROT_READ : VM_PROT_READ | VM_PROT_WRITE,
979 user_wire ? VM_FAULT_USER_WIRE : VM_FAULT_CHANGE_WIRING);
980 if (rv) {
981 if (va != start)
982 vm_fault_unwire(map, start, va);
983 return (rv);
984 }
985 }
986 return (KERN_SUCCESS);
987 }
988
989 /*
990 * vm_fault_unwire:
991 *
992 * Unwire a range of virtual addresses in a map.
993 */
994 void
995 vm_fault_unwire(map, start, end)
996 vm_map_t map;
997 vm_offset_t start, end;
998 {
999 vm_paddr_t pa;
1000 vm_offset_t va;
1001 pmap_t pmap;
1002
1003 pmap = vm_map_pmap(map);
1004
1005 mtx_lock(&Giant);
1006 /*
1007 * Since the pages are wired down, we must be able to get their
1008 * mappings from the physical map system.
1009 */
1010 for (va = start; va < end; va += PAGE_SIZE) {
1011 pa = pmap_extract(pmap, va);
1012 if (pa != 0) {
1013 pmap_change_wiring(pmap, va, FALSE);
1014 vm_page_lock_queues();
1015 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1016 vm_page_unlock_queues();
1017 }
1018 }
1019 mtx_unlock(&Giant);
1020 }
1021
1022 /*
1023 * Routine:
1024 * vm_fault_copy_entry
1025 * Function:
1026 * Copy all of the pages from a wired-down map entry to another.
1027 *
1028 * In/out conditions:
1029 * The source and destination maps must be locked for write.
1030 * The source map entry must be wired down (or be a sharing map
1031 * entry corresponding to a main map entry that is wired down).
1032 */
1033 void
1034 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry)
1035 vm_map_t dst_map;
1036 vm_map_t src_map;
1037 vm_map_entry_t dst_entry;
1038 vm_map_entry_t src_entry;
1039 {
1040 vm_object_t dst_object;
1041 vm_object_t src_object;
1042 vm_ooffset_t dst_offset;
1043 vm_ooffset_t src_offset;
1044 vm_prot_t prot;
1045 vm_offset_t vaddr;
1046 vm_page_t dst_m;
1047 vm_page_t src_m;
1048
1049 #ifdef lint
1050 src_map++;
1051 #endif /* lint */
1052
1053 src_object = src_entry->object.vm_object;
1054 src_offset = src_entry->offset;
1055
1056 /*
1057 * Create the top-level object for the destination entry. (Doesn't
1058 * actually shadow anything - we copy the pages directly.)
1059 */
1060 dst_object = vm_object_allocate(OBJT_DEFAULT,
1061 (vm_size_t) OFF_TO_IDX(dst_entry->end - dst_entry->start));
1062
1063 dst_entry->object.vm_object = dst_object;
1064 dst_entry->offset = 0;
1065
1066 prot = dst_entry->max_protection;
1067
1068 /*
1069 * Loop through all of the pages in the entry's range, copying each
1070 * one from the source object (it should be there) to the destination
1071 * object.
1072 */
1073 for (vaddr = dst_entry->start, dst_offset = 0;
1074 vaddr < dst_entry->end;
1075 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1076
1077 /*
1078 * Allocate a page in the destination object
1079 */
1080 do {
1081 dst_m = vm_page_alloc(dst_object,
1082 OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1083 if (dst_m == NULL) {
1084 VM_WAIT;
1085 }
1086 } while (dst_m == NULL);
1087
1088 /*
1089 * Find the page in the source object, and copy it in.
1090 * (Because the source is wired down, the page will be in
1091 * memory.)
1092 */
1093 src_m = vm_page_lookup(src_object,
1094 OFF_TO_IDX(dst_offset + src_offset));
1095 if (src_m == NULL)
1096 panic("vm_fault_copy_wired: page missing");
1097
1098 vm_page_copy(src_m, dst_m);
1099
1100 /*
1101 * Enter it in the pmap...
1102 */
1103 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
1104 vm_page_lock_queues();
1105 vm_page_flag_set(dst_m, PG_WRITEABLE);
1106
1107 /*
1108 * Mark it no longer busy, and put it on the active list.
1109 */
1110 vm_page_activate(dst_m);
1111 vm_page_wakeup(dst_m);
1112 vm_page_unlock_queues();
1113 }
1114 }
1115
1116
1117 /*
1118 * This routine checks around the requested page for other pages that
1119 * might be able to be faulted in. This routine brackets the viable
1120 * pages for the pages to be paged in.
1121 *
1122 * Inputs:
1123 * m, rbehind, rahead
1124 *
1125 * Outputs:
1126 * marray (array of vm_page_t), reqpage (index of requested page)
1127 *
1128 * Return value:
1129 * number of pages in marray
1130 *
1131 * This routine can't block.
1132 */
1133 static int
1134 vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
1135 vm_page_t m;
1136 int rbehind;
1137 int rahead;
1138 vm_page_t *marray;
1139 int *reqpage;
1140 {
1141 int i,j;
1142 vm_object_t object;
1143 vm_pindex_t pindex, startpindex, endpindex, tpindex;
1144 vm_page_t rtm;
1145 int cbehind, cahead;
1146
1147 GIANT_REQUIRED;
1148
1149 object = m->object;
1150 pindex = m->pindex;
1151
1152 /*
1153 * we don't fault-ahead for device pager
1154 */
1155 if (object->type == OBJT_DEVICE) {
1156 *reqpage = 0;
1157 marray[0] = m;
1158 return 1;
1159 }
1160
1161 /*
1162 * if the requested page is not available, then give up now
1163 */
1164 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1165 return 0;
1166 }
1167
1168 if ((cbehind == 0) && (cahead == 0)) {
1169 *reqpage = 0;
1170 marray[0] = m;
1171 return 1;
1172 }
1173
1174 if (rahead > cahead) {
1175 rahead = cahead;
1176 }
1177
1178 if (rbehind > cbehind) {
1179 rbehind = cbehind;
1180 }
1181
1182 /*
1183 * try to do any readahead that we might have free pages for.
1184 */
1185 if ((rahead + rbehind) >
1186 ((cnt.v_free_count + cnt.v_cache_count) - cnt.v_free_reserved)) {
1187 pagedaemon_wakeup();
1188 marray[0] = m;
1189 *reqpage = 0;
1190 return 1;
1191 }
1192
1193 /*
1194 * scan backward for the read behind pages -- in memory
1195 */
1196 if (pindex > 0) {
1197 if (rbehind > pindex) {
1198 rbehind = pindex;
1199 startpindex = 0;
1200 } else {
1201 startpindex = pindex - rbehind;
1202 }
1203
1204 for (tpindex = pindex - 1; tpindex >= startpindex; tpindex -= 1) {
1205 if (vm_page_lookup(object, tpindex)) {
1206 startpindex = tpindex + 1;
1207 break;
1208 }
1209 if (tpindex == 0)
1210 break;
1211 }
1212
1213 for (i = 0, tpindex = startpindex; tpindex < pindex; i++, tpindex++) {
1214
1215 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1216 if (rtm == NULL) {
1217 vm_page_lock_queues();
1218 for (j = 0; j < i; j++) {
1219 vm_page_free(marray[j]);
1220 }
1221 vm_page_unlock_queues();
1222 marray[0] = m;
1223 *reqpage = 0;
1224 return 1;
1225 }
1226
1227 marray[i] = rtm;
1228 }
1229 } else {
1230 startpindex = 0;
1231 i = 0;
1232 }
1233
1234 marray[i] = m;
1235 /* page offset of the required page */
1236 *reqpage = i;
1237
1238 tpindex = pindex + 1;
1239 i++;
1240
1241 /*
1242 * scan forward for the read ahead pages
1243 */
1244 endpindex = tpindex + rahead;
1245 if (endpindex > object->size)
1246 endpindex = object->size;
1247
1248 for (; tpindex < endpindex; i++, tpindex++) {
1249
1250 if (vm_page_lookup(object, tpindex)) {
1251 break;
1252 }
1253
1254 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1255 if (rtm == NULL) {
1256 break;
1257 }
1258
1259 marray[i] = rtm;
1260 }
1261
1262 /* return number of bytes of pages */
1263 return i;
1264 }
Cache object: ce71a8f9716943fe7c44e72bd857202b
|