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 70 /* 71 * Page fault handling module. 72 */ 73 74 #include <sys/cdefs.h> 75 __FBSDID("$FreeBSD$"); 76 77 #include "opt_ktrace.h" 78 #include "opt_vm.h" 79 80 #include <sys/param.h> 81 #include <sys/systm.h> 82 #include <sys/kernel.h> 83 #include <sys/lock.h> 84 #include <sys/proc.h> 85 #include <sys/resourcevar.h> 86 #include <sys/rwlock.h> 87 #include <sys/sysctl.h> 88 #include <sys/vmmeter.h> 89 #include <sys/vnode.h> 90 #ifdef KTRACE 91 #include <sys/ktrace.h> 92 #endif 93 94 #include <vm/vm.h> 95 #include <vm/vm_param.h> 96 #include <vm/pmap.h> 97 #include <vm/vm_map.h> 98 #include <vm/vm_object.h> 99 #include <vm/vm_page.h> 100 #include <vm/vm_pageout.h> 101 #include <vm/vm_kern.h> 102 #include <vm/vm_pager.h> 103 #include <vm/vm_extern.h> 104 #include <vm/vm_reserv.h> 105 106 #define PFBAK 4 107 #define PFFOR 4 108 109 static int vm_fault_additional_pages(vm_page_t, int, int, vm_page_t *, int *); 110 111 #define VM_FAULT_READ_BEHIND 8 112 #define VM_FAULT_READ_DEFAULT (1 + VM_FAULT_READ_AHEAD_INIT) 113 #define VM_FAULT_READ_MAX (1 + VM_FAULT_READ_AHEAD_MAX) 114 #define VM_FAULT_NINCR (VM_FAULT_READ_MAX / VM_FAULT_READ_BEHIND) 115 #define VM_FAULT_SUM (VM_FAULT_NINCR * (VM_FAULT_NINCR + 1) / 2) 116 #define VM_FAULT_CACHE_BEHIND (VM_FAULT_READ_BEHIND * VM_FAULT_SUM) 117 118 struct faultstate { 119 vm_page_t m; 120 vm_object_t object; 121 vm_pindex_t pindex; 122 vm_page_t first_m; 123 vm_object_t first_object; 124 vm_pindex_t first_pindex; 125 vm_map_t map; 126 vm_map_entry_t entry; 127 int lookup_still_valid; 128 struct vnode *vp; 129 }; 130 131 static void vm_fault_cache_behind(const struct faultstate *fs, int distance); 132 static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra, 133 int faultcount, int reqpage); 134 135 static inline void 136 release_page(struct faultstate *fs) 137 { 138 139 vm_page_xunbusy(fs->m); 140 vm_page_lock(fs->m); 141 vm_page_deactivate(fs->m); 142 vm_page_unlock(fs->m); 143 fs->m = NULL; 144 } 145 146 static inline void 147 unlock_map(struct faultstate *fs) 148 { 149 150 if (fs->lookup_still_valid) { 151 vm_map_lookup_done(fs->map, fs->entry); 152 fs->lookup_still_valid = FALSE; 153 } 154 } 155 156 static void 157 unlock_and_deallocate(struct faultstate *fs) 158 { 159 160 vm_object_pip_wakeup(fs->object); 161 VM_OBJECT_WUNLOCK(fs->object); 162 if (fs->object != fs->first_object) { 163 VM_OBJECT_WLOCK(fs->first_object); 164 vm_page_lock(fs->first_m); 165 vm_page_free(fs->first_m); 166 vm_page_unlock(fs->first_m); 167 vm_object_pip_wakeup(fs->first_object); 168 VM_OBJECT_WUNLOCK(fs->first_object); 169 fs->first_m = NULL; 170 } 171 vm_object_deallocate(fs->first_object); 172 unlock_map(fs); 173 if (fs->vp != NULL) { 174 vput(fs->vp); 175 fs->vp = NULL; 176 } 177 } 178 179 static void 180 vm_fault_dirty(vm_map_entry_t entry, vm_page_t m, vm_prot_t prot, 181 vm_prot_t fault_type, int fault_flags, boolean_t set_wd) 182 { 183 boolean_t need_dirty; 184 185 if (((prot & VM_PROT_WRITE) == 0 && 186 (fault_flags & VM_FAULT_DIRTY) == 0) || 187 (m->oflags & VPO_UNMANAGED) != 0) 188 return; 189 190 VM_OBJECT_ASSERT_LOCKED(m->object); 191 192 need_dirty = ((fault_type & VM_PROT_WRITE) != 0 && 193 (fault_flags & VM_FAULT_CHANGE_WIRING) == 0) || 194 (fault_flags & VM_FAULT_DIRTY) != 0; 195 196 if (set_wd) 197 vm_object_set_writeable_dirty(m->object); 198 else 199 /* 200 * If two callers of vm_fault_dirty() with set_wd == 201 * FALSE, one for the map entry with MAP_ENTRY_NOSYNC 202 * flag set, other with flag clear, race, it is 203 * possible for the no-NOSYNC thread to see m->dirty 204 * != 0 and not clear VPO_NOSYNC. Take vm_page lock 205 * around manipulation of VPO_NOSYNC and 206 * vm_page_dirty() call, to avoid the race and keep 207 * m->oflags consistent. 208 */ 209 vm_page_lock(m); 210 211 /* 212 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC 213 * if the page is already dirty to prevent data written with 214 * the expectation of being synced from not being synced. 215 * Likewise if this entry does not request NOSYNC then make 216 * sure the page isn't marked NOSYNC. Applications sharing 217 * data should use the same flags to avoid ping ponging. 218 */ 219 if ((entry->eflags & MAP_ENTRY_NOSYNC) != 0) { 220 if (m->dirty == 0) { 221 m->oflags |= VPO_NOSYNC; 222 } 223 } else { 224 m->oflags &= ~VPO_NOSYNC; 225 } 226 227 /* 228 * If the fault is a write, we know that this page is being 229 * written NOW so dirty it explicitly to save on 230 * pmap_is_modified() calls later. 231 * 232 * Also tell the backing pager, if any, that it should remove 233 * any swap backing since the page is now dirty. 234 */ 235 if (need_dirty) 236 vm_page_dirty(m); 237 if (!set_wd) 238 vm_page_unlock(m); 239 if (need_dirty) 240 vm_pager_page_unswapped(m); 241 } 242 243 /* 244 * TRYPAGER - used by vm_fault to calculate whether the pager for the 245 * current object *might* contain the page. 246 * 247 * default objects are zero-fill, there is no real pager. 248 */ 249 #define TRYPAGER (fs.object->type != OBJT_DEFAULT && \ 250 ((fault_flags & VM_FAULT_CHANGE_WIRING) == 0 || wired)) 251 252 /* 253 * vm_fault: 254 * 255 * Handle a page fault occurring at the given address, 256 * requiring the given permissions, in the map specified. 257 * If successful, the page is inserted into the 258 * associated physical map. 259 * 260 * NOTE: the given address should be truncated to the 261 * proper page address. 262 * 263 * KERN_SUCCESS is returned if the page fault is handled; otherwise, 264 * a standard error specifying why the fault is fatal is returned. 265 * 266 * The map in question must be referenced, and remains so. 267 * Caller may hold no locks. 268 */ 269 int 270 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 271 int fault_flags) 272 { 273 struct thread *td; 274 int result; 275 276 td = curthread; 277 if ((td->td_pflags & TDP_NOFAULTING) != 0) 278 return (KERN_PROTECTION_FAILURE); 279 #ifdef KTRACE 280 if (map != kernel_map && KTRPOINT(td, KTR_FAULT)) 281 ktrfault(vaddr, fault_type); 282 #endif 283 result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags, 284 NULL); 285 #ifdef KTRACE 286 if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND)) 287 ktrfaultend(result); 288 #endif 289 return (result); 290 } 291 292 int 293 vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 294 int fault_flags, vm_page_t *m_hold) 295 { 296 vm_prot_t prot; 297 int alloc_req, era, faultcount, nera, reqpage, result; 298 boolean_t growstack, is_first_object_locked, wired; 299 int map_generation; 300 vm_object_t next_object; 301 vm_page_t marray[VM_FAULT_READ_MAX]; 302 int hardfault; 303 struct faultstate fs; 304 struct vnode *vp; 305 vm_page_t m; 306 int ahead, behind, cluster_offset, error, locked; 307 308 hardfault = 0; 309 growstack = TRUE; 310 PCPU_INC(cnt.v_vm_faults); 311 fs.vp = NULL; 312 faultcount = reqpage = 0; 313 314 RetryFault:; 315 316 /* 317 * Find the backing store object and offset into it to begin the 318 * search. 319 */ 320 fs.map = map; 321 result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry, 322 &fs.first_object, &fs.first_pindex, &prot, &wired); 323 if (result != KERN_SUCCESS) { 324 if (growstack && result == KERN_INVALID_ADDRESS && 325 map != kernel_map) { 326 result = vm_map_growstack(curproc, vaddr); 327 if (result != KERN_SUCCESS) 328 return (KERN_FAILURE); 329 growstack = FALSE; 330 goto RetryFault; 331 } 332 return (result); 333 } 334 335 map_generation = fs.map->timestamp; 336 337 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) { 338 panic("vm_fault: fault on nofault entry, addr: %lx", 339 (u_long)vaddr); 340 } 341 342 if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION && 343 fs.entry->wiring_thread != curthread) { 344 vm_map_unlock_read(fs.map); 345 vm_map_lock(fs.map); 346 if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) && 347 (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) { 348 fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 349 vm_map_unlock_and_wait(fs.map, 0); 350 } else 351 vm_map_unlock(fs.map); 352 goto RetryFault; 353 } 354 355 if (wired) 356 fault_type = prot | (fault_type & VM_PROT_COPY); 357 358 if (fs.vp == NULL /* avoid locked vnode leak */ && 359 (fault_flags & (VM_FAULT_CHANGE_WIRING | VM_FAULT_DIRTY)) == 0 && 360 /* avoid calling vm_object_set_writeable_dirty() */ 361 ((prot & VM_PROT_WRITE) == 0 || 362 (fs.first_object->type != OBJT_VNODE && 363 (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) || 364 (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0)) { 365 VM_OBJECT_RLOCK(fs.first_object); 366 if ((prot & VM_PROT_WRITE) != 0 && 367 (fs.first_object->type == OBJT_VNODE || 368 (fs.first_object->flags & OBJ_TMPFS_NODE) != 0) && 369 (fs.first_object->flags & OBJ_MIGHTBEDIRTY) == 0) 370 goto fast_failed; 371 m = vm_page_lookup(fs.first_object, fs.first_pindex); 372 /* A busy page can be mapped for read|execute access. */ 373 if (m == NULL || ((prot & VM_PROT_WRITE) != 0 && 374 vm_page_busied(m)) || m->valid != VM_PAGE_BITS_ALL) 375 goto fast_failed; 376 result = pmap_enter(fs.map->pmap, vaddr, m, prot, 377 fault_type | PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED : 378 0), 0); 379 if (result != KERN_SUCCESS) 380 goto fast_failed; 381 if (m_hold != NULL) { 382 *m_hold = m; 383 vm_page_lock(m); 384 vm_page_hold(m); 385 vm_page_unlock(m); 386 } 387 vm_fault_dirty(fs.entry, m, prot, fault_type, fault_flags, 388 FALSE); 389 VM_OBJECT_RUNLOCK(fs.first_object); 390 if (!wired) 391 vm_fault_prefault(&fs, vaddr, 0, 0); 392 vm_map_lookup_done(fs.map, fs.entry); 393 curthread->td_ru.ru_minflt++; 394 return (KERN_SUCCESS); 395 fast_failed: 396 if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) { 397 VM_OBJECT_RUNLOCK(fs.first_object); 398 VM_OBJECT_WLOCK(fs.first_object); 399 } 400 } else { 401 VM_OBJECT_WLOCK(fs.first_object); 402 } 403 404 /* 405 * Make a reference to this object to prevent its disposal while we 406 * are messing with it. Once we have the reference, the map is free 407 * to be diddled. Since objects reference their shadows (and copies), 408 * they will stay around as well. 409 * 410 * Bump the paging-in-progress count to prevent size changes (e.g. 411 * truncation operations) during I/O. This must be done after 412 * obtaining the vnode lock in order to avoid possible deadlocks. 413 */ 414 vm_object_reference_locked(fs.first_object); 415 vm_object_pip_add(fs.first_object, 1); 416 417 fs.lookup_still_valid = TRUE; 418 419 fs.first_m = NULL; 420 421 /* 422 * Search for the page at object/offset. 423 */ 424 fs.object = fs.first_object; 425 fs.pindex = fs.first_pindex; 426 while (TRUE) { 427 /* 428 * If the object is dead, we stop here 429 */ 430 if (fs.object->flags & OBJ_DEAD) { 431 unlock_and_deallocate(&fs); 432 return (KERN_PROTECTION_FAILURE); 433 } 434 435 /* 436 * See if page is resident 437 */ 438 fs.m = vm_page_lookup(fs.object, fs.pindex); 439 if (fs.m != NULL) { 440 /* 441 * Wait/Retry if the page is busy. We have to do this 442 * if the page is either exclusive or shared busy 443 * because the vm_pager may be using read busy for 444 * pageouts (and even pageins if it is the vnode 445 * pager), and we could end up trying to pagein and 446 * pageout the same page simultaneously. 447 * 448 * We can theoretically allow the busy case on a read 449 * fault if the page is marked valid, but since such 450 * pages are typically already pmap'd, putting that 451 * special case in might be more effort then it is 452 * worth. We cannot under any circumstances mess 453 * around with a shared busied page except, perhaps, 454 * to pmap it. 455 */ 456 if (vm_page_busied(fs.m)) { 457 /* 458 * Reference the page before unlocking and 459 * sleeping so that the page daemon is less 460 * likely to reclaim it. 461 */ 462 vm_page_aflag_set(fs.m, PGA_REFERENCED); 463 if (fs.object != fs.first_object) { 464 if (!VM_OBJECT_TRYWLOCK( 465 fs.first_object)) { 466 VM_OBJECT_WUNLOCK(fs.object); 467 VM_OBJECT_WLOCK(fs.first_object); 468 VM_OBJECT_WLOCK(fs.object); 469 } 470 vm_page_lock(fs.first_m); 471 vm_page_free(fs.first_m); 472 vm_page_unlock(fs.first_m); 473 vm_object_pip_wakeup(fs.first_object); 474 VM_OBJECT_WUNLOCK(fs.first_object); 475 fs.first_m = NULL; 476 } 477 unlock_map(&fs); 478 if (fs.m == vm_page_lookup(fs.object, 479 fs.pindex)) { 480 vm_page_sleep_if_busy(fs.m, "vmpfw"); 481 } 482 vm_object_pip_wakeup(fs.object); 483 VM_OBJECT_WUNLOCK(fs.object); 484 PCPU_INC(cnt.v_intrans); 485 vm_object_deallocate(fs.first_object); 486 goto RetryFault; 487 } 488 vm_page_lock(fs.m); 489 vm_page_remque(fs.m); 490 vm_page_unlock(fs.m); 491 492 /* 493 * Mark page busy for other processes, and the 494 * pagedaemon. If it still isn't completely valid 495 * (readable), jump to readrest, else break-out ( we 496 * found the page ). 497 */ 498 vm_page_xbusy(fs.m); 499 if (fs.m->valid != VM_PAGE_BITS_ALL) 500 goto readrest; 501 break; 502 } 503 504 /* 505 * Page is not resident, If this is the search termination 506 * or the pager might contain the page, allocate a new page. 507 */ 508 if (TRYPAGER || fs.object == fs.first_object) { 509 if (fs.pindex >= fs.object->size) { 510 unlock_and_deallocate(&fs); 511 return (KERN_PROTECTION_FAILURE); 512 } 513 514 /* 515 * Allocate a new page for this object/offset pair. 516 * 517 * Unlocked read of the p_flag is harmless. At 518 * worst, the P_KILLED might be not observed 519 * there, and allocation can fail, causing 520 * restart and new reading of the p_flag. 521 */ 522 fs.m = NULL; 523 if (!vm_page_count_severe() || P_KILLED(curproc)) { 524 #if VM_NRESERVLEVEL > 0 525 vm_object_color(fs.object, atop(vaddr) - 526 fs.pindex); 527 #endif 528 alloc_req = P_KILLED(curproc) ? 529 VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL; 530 if (fs.object->type != OBJT_VNODE && 531 fs.object->backing_object == NULL) 532 alloc_req |= VM_ALLOC_ZERO; 533 fs.m = vm_page_alloc(fs.object, fs.pindex, 534 alloc_req); 535 } 536 if (fs.m == NULL) { 537 unlock_and_deallocate(&fs); 538 VM_WAITPFAULT; 539 goto RetryFault; 540 } else if (fs.m->valid == VM_PAGE_BITS_ALL) 541 break; 542 } 543 544 readrest: 545 /* 546 * We have found a valid page or we have allocated a new page. 547 * The page thus may not be valid or may not be entirely 548 * valid. 549 * 550 * Attempt to fault-in the page if there is a chance that the 551 * pager has it, and potentially fault in additional pages 552 * at the same time. 553 */ 554 if (TRYPAGER) { 555 int rv; 556 u_char behavior = vm_map_entry_behavior(fs.entry); 557 558 era = fs.entry->read_ahead; 559 if (behavior == MAP_ENTRY_BEHAV_RANDOM || 560 P_KILLED(curproc)) { 561 behind = 0; 562 nera = 0; 563 ahead = 0; 564 } else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) { 565 behind = 0; 566 nera = VM_FAULT_READ_AHEAD_MAX; 567 ahead = nera; 568 if (fs.pindex == fs.entry->next_read) 569 vm_fault_cache_behind(&fs, 570 VM_FAULT_READ_MAX); 571 } else if (fs.pindex == fs.entry->next_read) { 572 /* 573 * This is a sequential fault. Arithmetically 574 * increase the requested number of pages in 575 * the read-ahead window. The requested 576 * number of pages is "# of sequential faults 577 * x (read ahead min + 1) + read ahead min" 578 */ 579 behind = 0; 580 nera = VM_FAULT_READ_AHEAD_MIN; 581 if (era > 0) { 582 nera += era + 1; 583 if (nera > VM_FAULT_READ_AHEAD_MAX) 584 nera = VM_FAULT_READ_AHEAD_MAX; 585 } 586 ahead = nera; 587 if (era == VM_FAULT_READ_AHEAD_MAX) 588 vm_fault_cache_behind(&fs, 589 VM_FAULT_CACHE_BEHIND); 590 } else { 591 /* 592 * This is a non-sequential fault. Request a 593 * cluster of pages that is aligned to a 594 * VM_FAULT_READ_DEFAULT page offset boundary 595 * within the object. Alignment to a page 596 * offset boundary is more likely to coincide 597 * with the underlying file system block than 598 * alignment to a virtual address boundary. 599 */ 600 cluster_offset = fs.pindex % 601 VM_FAULT_READ_DEFAULT; 602 behind = ulmin(cluster_offset, 603 atop(vaddr - fs.entry->start)); 604 nera = 0; 605 ahead = VM_FAULT_READ_DEFAULT - 1 - 606 cluster_offset; 607 } 608 ahead = ulmin(ahead, atop(fs.entry->end - vaddr) - 1); 609 if (era != nera) 610 fs.entry->read_ahead = nera; 611 612 /* 613 * Call the pager to retrieve the data, if any, after 614 * releasing the lock on the map. We hold a ref on 615 * fs.object and the pages are exclusive busied. 616 */ 617 unlock_map(&fs); 618 619 if (fs.object->type == OBJT_VNODE) { 620 vp = fs.object->handle; 621 if (vp == fs.vp) 622 goto vnode_locked; 623 else if (fs.vp != NULL) { 624 vput(fs.vp); 625 fs.vp = NULL; 626 } 627 locked = VOP_ISLOCKED(vp); 628 629 if (locked != LK_EXCLUSIVE) 630 locked = LK_SHARED; 631 /* Do not sleep for vnode lock while fs.m is busy */ 632 error = vget(vp, locked | LK_CANRECURSE | 633 LK_NOWAIT, curthread); 634 if (error != 0) { 635 vhold(vp); 636 release_page(&fs); 637 unlock_and_deallocate(&fs); 638 error = vget(vp, locked | LK_RETRY | 639 LK_CANRECURSE, curthread); 640 vdrop(vp); 641 fs.vp = vp; 642 KASSERT(error == 0, 643 ("vm_fault: vget failed")); 644 goto RetryFault; 645 } 646 fs.vp = vp; 647 } 648 vnode_locked: 649 KASSERT(fs.vp == NULL || !fs.map->system_map, 650 ("vm_fault: vnode-backed object mapped by system map")); 651 652 /* 653 * now we find out if any other pages should be paged 654 * in at this time this routine checks to see if the 655 * pages surrounding this fault reside in the same 656 * object as the page for this fault. If they do, 657 * then they are faulted in also into the object. The 658 * array "marray" returned contains an array of 659 * vm_page_t structs where one of them is the 660 * vm_page_t passed to the routine. The reqpage 661 * return value is the index into the marray for the 662 * vm_page_t passed to the routine. 663 * 664 * fs.m plus the additional pages are exclusive busied. 665 */ 666 faultcount = vm_fault_additional_pages( 667 fs.m, behind, ahead, marray, &reqpage); 668 669 rv = faultcount ? 670 vm_pager_get_pages(fs.object, marray, faultcount, 671 reqpage) : VM_PAGER_FAIL; 672 673 if (rv == VM_PAGER_OK) { 674 /* 675 * Found the page. Leave it busy while we play 676 * with it. 677 */ 678 679 /* 680 * Relookup in case pager changed page. Pager 681 * is responsible for disposition of old page 682 * if moved. 683 */ 684 fs.m = vm_page_lookup(fs.object, fs.pindex); 685 if (!fs.m) { 686 unlock_and_deallocate(&fs); 687 goto RetryFault; 688 } 689 690 hardfault++; 691 break; /* break to PAGE HAS BEEN FOUND */ 692 } 693 /* 694 * Remove the bogus page (which does not exist at this 695 * object/offset); before doing so, we must get back 696 * our object lock to preserve our invariant. 697 * 698 * Also wake up any other process that may want to bring 699 * in this page. 700 * 701 * If this is the top-level object, we must leave the 702 * busy page to prevent another process from rushing 703 * past us, and inserting the page in that object at 704 * the same time that we are. 705 */ 706 if (rv == VM_PAGER_ERROR) 707 printf("vm_fault: pager read error, pid %d (%s)\n", 708 curproc->p_pid, curproc->p_comm); 709 /* 710 * Data outside the range of the pager or an I/O error 711 */ 712 /* 713 * XXX - the check for kernel_map is a kludge to work 714 * around having the machine panic on a kernel space 715 * fault w/ I/O error. 716 */ 717 if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) || 718 (rv == VM_PAGER_BAD)) { 719 vm_page_lock(fs.m); 720 vm_page_free(fs.m); 721 vm_page_unlock(fs.m); 722 fs.m = NULL; 723 unlock_and_deallocate(&fs); 724 return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE); 725 } 726 if (fs.object != fs.first_object) { 727 vm_page_lock(fs.m); 728 vm_page_free(fs.m); 729 vm_page_unlock(fs.m); 730 fs.m = NULL; 731 /* 732 * XXX - we cannot just fall out at this 733 * point, m has been freed and is invalid! 734 */ 735 } 736 } 737 738 /* 739 * We get here if the object has default pager (or unwiring) 740 * or the pager doesn't have the page. 741 */ 742 if (fs.object == fs.first_object) 743 fs.first_m = fs.m; 744 745 /* 746 * Move on to the next object. Lock the next object before 747 * unlocking the current one. 748 */ 749 fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset); 750 next_object = fs.object->backing_object; 751 if (next_object == NULL) { 752 /* 753 * If there's no object left, fill the page in the top 754 * object with zeros. 755 */ 756 if (fs.object != fs.first_object) { 757 vm_object_pip_wakeup(fs.object); 758 VM_OBJECT_WUNLOCK(fs.object); 759 760 fs.object = fs.first_object; 761 fs.pindex = fs.first_pindex; 762 fs.m = fs.first_m; 763 VM_OBJECT_WLOCK(fs.object); 764 } 765 fs.first_m = NULL; 766 767 /* 768 * Zero the page if necessary and mark it valid. 769 */ 770 if ((fs.m->flags & PG_ZERO) == 0) { 771 pmap_zero_page(fs.m); 772 } else { 773 PCPU_INC(cnt.v_ozfod); 774 } 775 PCPU_INC(cnt.v_zfod); 776 fs.m->valid = VM_PAGE_BITS_ALL; 777 /* Don't try to prefault neighboring pages. */ 778 faultcount = 1; 779 break; /* break to PAGE HAS BEEN FOUND */ 780 } else { 781 KASSERT(fs.object != next_object, 782 ("object loop %p", next_object)); 783 VM_OBJECT_WLOCK(next_object); 784 vm_object_pip_add(next_object, 1); 785 if (fs.object != fs.first_object) 786 vm_object_pip_wakeup(fs.object); 787 VM_OBJECT_WUNLOCK(fs.object); 788 fs.object = next_object; 789 } 790 } 791 792 vm_page_assert_xbusied(fs.m); 793 794 /* 795 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock 796 * is held.] 797 */ 798 799 /* 800 * If the page is being written, but isn't already owned by the 801 * top-level object, we have to copy it into a new page owned by the 802 * top-level object. 803 */ 804 if (fs.object != fs.first_object) { 805 /* 806 * We only really need to copy if we want to write it. 807 */ 808 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) { 809 /* 810 * This allows pages to be virtually copied from a 811 * backing_object into the first_object, where the 812 * backing object has no other refs to it, and cannot 813 * gain any more refs. Instead of a bcopy, we just 814 * move the page from the backing object to the 815 * first object. Note that we must mark the page 816 * dirty in the first object so that it will go out 817 * to swap when needed. 818 */ 819 is_first_object_locked = FALSE; 820 if ( 821 /* 822 * Only one shadow object 823 */ 824 (fs.object->shadow_count == 1) && 825 /* 826 * No COW refs, except us 827 */ 828 (fs.object->ref_count == 1) && 829 /* 830 * No one else can look this object up 831 */ 832 (fs.object->handle == NULL) && 833 /* 834 * No other ways to look the object up 835 */ 836 ((fs.object->type == OBJT_DEFAULT) || 837 (fs.object->type == OBJT_SWAP)) && 838 (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) && 839 /* 840 * We don't chase down the shadow chain 841 */ 842 fs.object == fs.first_object->backing_object) { 843 /* 844 * get rid of the unnecessary page 845 */ 846 vm_page_lock(fs.first_m); 847 vm_page_free(fs.first_m); 848 vm_page_unlock(fs.first_m); 849 /* 850 * grab the page and put it into the 851 * process'es object. The page is 852 * automatically made dirty. 853 */ 854 if (vm_page_rename(fs.m, fs.first_object, 855 fs.first_pindex)) { 856 unlock_and_deallocate(&fs); 857 goto RetryFault; 858 } 859 #if VM_NRESERVLEVEL > 0 860 /* 861 * Rename the reservation. 862 */ 863 vm_reserv_rename(fs.m, fs.first_object, 864 fs.object, OFF_TO_IDX( 865 fs.first_object->backing_object_offset)); 866 #endif 867 vm_page_xbusy(fs.m); 868 fs.first_m = fs.m; 869 fs.m = NULL; 870 PCPU_INC(cnt.v_cow_optim); 871 } else { 872 /* 873 * Oh, well, lets copy it. 874 */ 875 pmap_copy_page(fs.m, fs.first_m); 876 fs.first_m->valid = VM_PAGE_BITS_ALL; 877 if (wired && (fault_flags & 878 VM_FAULT_CHANGE_WIRING) == 0) { 879 vm_page_lock(fs.first_m); 880 vm_page_wire(fs.first_m); 881 vm_page_unlock(fs.first_m); 882 883 vm_page_lock(fs.m); 884 vm_page_unwire(fs.m, PQ_INACTIVE); 885 vm_page_unlock(fs.m); 886 } 887 /* 888 * We no longer need the old page or object. 889 */ 890 release_page(&fs); 891 } 892 /* 893 * fs.object != fs.first_object due to above 894 * conditional 895 */ 896 vm_object_pip_wakeup(fs.object); 897 VM_OBJECT_WUNLOCK(fs.object); 898 /* 899 * Only use the new page below... 900 */ 901 fs.object = fs.first_object; 902 fs.pindex = fs.first_pindex; 903 fs.m = fs.first_m; 904 if (!is_first_object_locked) 905 VM_OBJECT_WLOCK(fs.object); 906 PCPU_INC(cnt.v_cow_faults); 907 curthread->td_cow++; 908 } else { 909 prot &= ~VM_PROT_WRITE; 910 } 911 } 912 913 /* 914 * We must verify that the maps have not changed since our last 915 * lookup. 916 */ 917 if (!fs.lookup_still_valid) { 918 vm_object_t retry_object; 919 vm_pindex_t retry_pindex; 920 vm_prot_t retry_prot; 921 922 if (!vm_map_trylock_read(fs.map)) { 923 release_page(&fs); 924 unlock_and_deallocate(&fs); 925 goto RetryFault; 926 } 927 fs.lookup_still_valid = TRUE; 928 if (fs.map->timestamp != map_generation) { 929 result = vm_map_lookup_locked(&fs.map, vaddr, fault_type, 930 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired); 931 932 /* 933 * If we don't need the page any longer, put it on the inactive 934 * list (the easiest thing to do here). If no one needs it, 935 * pageout will grab it eventually. 936 */ 937 if (result != KERN_SUCCESS) { 938 release_page(&fs); 939 unlock_and_deallocate(&fs); 940 941 /* 942 * If retry of map lookup would have blocked then 943 * retry fault from start. 944 */ 945 if (result == KERN_FAILURE) 946 goto RetryFault; 947 return (result); 948 } 949 if ((retry_object != fs.first_object) || 950 (retry_pindex != fs.first_pindex)) { 951 release_page(&fs); 952 unlock_and_deallocate(&fs); 953 goto RetryFault; 954 } 955 956 /* 957 * Check whether the protection has changed or the object has 958 * been copied while we left the map unlocked. Changing from 959 * read to write permission is OK - we leave the page 960 * write-protected, and catch the write fault. Changing from 961 * write to read permission means that we can't mark the page 962 * write-enabled after all. 963 */ 964 prot &= retry_prot; 965 } 966 } 967 /* 968 * If the page was filled by a pager, update the map entry's 969 * last read offset. Since the pager does not return the 970 * actual set of pages that it read, this update is based on 971 * the requested set. Typically, the requested and actual 972 * sets are the same. 973 * 974 * XXX The following assignment modifies the map 975 * without holding a write lock on it. 976 */ 977 if (hardfault) 978 fs.entry->next_read = fs.pindex + faultcount - reqpage; 979 980 vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags, TRUE); 981 vm_page_assert_xbusied(fs.m); 982 983 /* 984 * Page must be completely valid or it is not fit to 985 * map into user space. vm_pager_get_pages() ensures this. 986 */ 987 KASSERT(fs.m->valid == VM_PAGE_BITS_ALL, 988 ("vm_fault: page %p partially invalid", fs.m)); 989 VM_OBJECT_WUNLOCK(fs.object); 990 991 /* 992 * Put this page into the physical map. We had to do the unlock above 993 * because pmap_enter() may sleep. We don't put the page 994 * back on the active queue until later so that the pageout daemon 995 * won't find it (yet). 996 */ 997 pmap_enter(fs.map->pmap, vaddr, fs.m, prot, 998 fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0); 999 if (faultcount != 1 && (fault_flags & VM_FAULT_CHANGE_WIRING) == 0 && 1000 wired == 0) 1001 vm_fault_prefault(&fs, vaddr, faultcount, reqpage); 1002 VM_OBJECT_WLOCK(fs.object); 1003 vm_page_lock(fs.m); 1004 1005 /* 1006 * If the page is not wired down, then put it where the pageout daemon 1007 * can find it. 1008 */ 1009 if (fault_flags & VM_FAULT_CHANGE_WIRING) { 1010 if (wired) 1011 vm_page_wire(fs.m); 1012 else 1013 vm_page_unwire(fs.m, PQ_ACTIVE); 1014 } else 1015 vm_page_activate(fs.m); 1016 if (m_hold != NULL) { 1017 *m_hold = fs.m; 1018 vm_page_hold(fs.m); 1019 } 1020 vm_page_unlock(fs.m); 1021 vm_page_xunbusy(fs.m); 1022 1023 /* 1024 * Unlock everything, and return 1025 */ 1026 unlock_and_deallocate(&fs); 1027 if (hardfault) { 1028 PCPU_INC(cnt.v_io_faults); 1029 curthread->td_ru.ru_majflt++; 1030 } else 1031 curthread->td_ru.ru_minflt++; 1032 1033 return (KERN_SUCCESS); 1034 } 1035 1036 /* 1037 * Speed up the reclamation of up to "distance" pages that precede the 1038 * faulting pindex within the first object of the shadow chain. 1039 */ 1040 static void 1041 vm_fault_cache_behind(const struct faultstate *fs, int distance) 1042 { 1043 vm_object_t first_object, object; 1044 vm_page_t m, m_prev; 1045 vm_pindex_t pindex; 1046 1047 object = fs->object; 1048 VM_OBJECT_ASSERT_WLOCKED(object); 1049 first_object = fs->first_object; 1050 if (first_object != object) { 1051 if (!VM_OBJECT_TRYWLOCK(first_object)) { 1052 VM_OBJECT_WUNLOCK(object); 1053 VM_OBJECT_WLOCK(first_object); 1054 VM_OBJECT_WLOCK(object); 1055 } 1056 } 1057 /* Neither fictitious nor unmanaged pages can be cached. */ 1058 if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) { 1059 if (fs->first_pindex < distance) 1060 pindex = 0; 1061 else 1062 pindex = fs->first_pindex - distance; 1063 if (pindex < OFF_TO_IDX(fs->entry->offset)) 1064 pindex = OFF_TO_IDX(fs->entry->offset); 1065 m = first_object != object ? fs->first_m : fs->m; 1066 vm_page_assert_xbusied(m); 1067 m_prev = vm_page_prev(m); 1068 while ((m = m_prev) != NULL && m->pindex >= pindex && 1069 m->valid == VM_PAGE_BITS_ALL) { 1070 m_prev = vm_page_prev(m); 1071 if (vm_page_busied(m)) 1072 continue; 1073 vm_page_lock(m); 1074 if (m->hold_count == 0 && m->wire_count == 0) { 1075 pmap_remove_all(m); 1076 vm_page_aflag_clear(m, PGA_REFERENCED); 1077 if (m->dirty != 0) 1078 vm_page_deactivate(m); 1079 else 1080 vm_page_cache(m); 1081 } 1082 vm_page_unlock(m); 1083 } 1084 } 1085 if (first_object != object) 1086 VM_OBJECT_WUNLOCK(first_object); 1087 } 1088 1089 /* 1090 * vm_fault_prefault provides a quick way of clustering 1091 * pagefaults into a processes address space. It is a "cousin" 1092 * of vm_map_pmap_enter, except it runs at page fault time instead 1093 * of mmap time. 1094 */ 1095 static void 1096 vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra, 1097 int faultcount, int reqpage) 1098 { 1099 pmap_t pmap; 1100 vm_map_entry_t entry; 1101 vm_object_t backing_object, lobject; 1102 vm_offset_t addr, starta; 1103 vm_pindex_t pindex; 1104 vm_page_t m; 1105 int backward, forward, i; 1106 1107 pmap = fs->map->pmap; 1108 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) 1109 return; 1110 1111 if (faultcount > 0) { 1112 backward = reqpage; 1113 forward = faultcount - reqpage - 1; 1114 } else { 1115 backward = PFBAK; 1116 forward = PFFOR; 1117 } 1118 entry = fs->entry; 1119 1120 starta = addra - backward * PAGE_SIZE; 1121 if (starta < entry->start) { 1122 starta = entry->start; 1123 } else if (starta > addra) { 1124 starta = 0; 1125 } 1126 1127 /* 1128 * Generate the sequence of virtual addresses that are candidates for 1129 * prefaulting in an outward spiral from the faulting virtual address, 1130 * "addra". Specifically, the sequence is "addra - PAGE_SIZE", "addra 1131 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ... 1132 * If the candidate address doesn't have a backing physical page, then 1133 * the loop immediately terminates. 1134 */ 1135 for (i = 0; i < 2 * imax(backward, forward); i++) { 1136 addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE : 1137 PAGE_SIZE); 1138 if (addr > addra + forward * PAGE_SIZE) 1139 addr = 0; 1140 1141 if (addr < starta || addr >= entry->end) 1142 continue; 1143 1144 if (!pmap_is_prefaultable(pmap, addr)) 1145 continue; 1146 1147 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT; 1148 lobject = entry->object.vm_object; 1149 VM_OBJECT_RLOCK(lobject); 1150 while ((m = vm_page_lookup(lobject, pindex)) == NULL && 1151 lobject->type == OBJT_DEFAULT && 1152 (backing_object = lobject->backing_object) != NULL) { 1153 KASSERT((lobject->backing_object_offset & PAGE_MASK) == 1154 0, ("vm_fault_prefault: unaligned object offset")); 1155 pindex += lobject->backing_object_offset >> PAGE_SHIFT; 1156 VM_OBJECT_RLOCK(backing_object); 1157 VM_OBJECT_RUNLOCK(lobject); 1158 lobject = backing_object; 1159 } 1160 if (m == NULL) { 1161 VM_OBJECT_RUNLOCK(lobject); 1162 break; 1163 } 1164 if (m->valid == VM_PAGE_BITS_ALL && 1165 (m->flags & PG_FICTITIOUS) == 0) 1166 pmap_enter_quick(pmap, addr, m, entry->protection); 1167 VM_OBJECT_RUNLOCK(lobject); 1168 } 1169 } 1170 1171 /* 1172 * Hold each of the physical pages that are mapped by the specified range of 1173 * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid 1174 * and allow the specified types of access, "prot". If all of the implied 1175 * pages are successfully held, then the number of held pages is returned 1176 * together with pointers to those pages in the array "ma". However, if any 1177 * of the pages cannot be held, -1 is returned. 1178 */ 1179 int 1180 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len, 1181 vm_prot_t prot, vm_page_t *ma, int max_count) 1182 { 1183 vm_offset_t end, va; 1184 vm_page_t *mp; 1185 int count; 1186 boolean_t pmap_failed; 1187 1188 if (len == 0) 1189 return (0); 1190 end = round_page(addr + len); 1191 addr = trunc_page(addr); 1192 1193 /* 1194 * Check for illegal addresses. 1195 */ 1196 if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map)) 1197 return (-1); 1198 1199 if (atop(end - addr) > max_count) 1200 panic("vm_fault_quick_hold_pages: count > max_count"); 1201 count = atop(end - addr); 1202 1203 /* 1204 * Most likely, the physical pages are resident in the pmap, so it is 1205 * faster to try pmap_extract_and_hold() first. 1206 */ 1207 pmap_failed = FALSE; 1208 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) { 1209 *mp = pmap_extract_and_hold(map->pmap, va, prot); 1210 if (*mp == NULL) 1211 pmap_failed = TRUE; 1212 else if ((prot & VM_PROT_WRITE) != 0 && 1213 (*mp)->dirty != VM_PAGE_BITS_ALL) { 1214 /* 1215 * Explicitly dirty the physical page. Otherwise, the 1216 * caller's changes may go unnoticed because they are 1217 * performed through an unmanaged mapping or by a DMA 1218 * operation. 1219 * 1220 * The object lock is not held here. 1221 * See vm_page_clear_dirty_mask(). 1222 */ 1223 vm_page_dirty(*mp); 1224 } 1225 } 1226 if (pmap_failed) { 1227 /* 1228 * One or more pages could not be held by the pmap. Either no 1229 * page was mapped at the specified virtual address or that 1230 * mapping had insufficient permissions. Attempt to fault in 1231 * and hold these pages. 1232 */ 1233 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) 1234 if (*mp == NULL && vm_fault_hold(map, va, prot, 1235 VM_FAULT_NORMAL, mp) != KERN_SUCCESS) 1236 goto error; 1237 } 1238 return (count); 1239 error: 1240 for (mp = ma; mp < ma + count; mp++) 1241 if (*mp != NULL) { 1242 vm_page_lock(*mp); 1243 vm_page_unhold(*mp); 1244 vm_page_unlock(*mp); 1245 } 1246 return (-1); 1247 } 1248 1249 /* 1250 * Routine: 1251 * vm_fault_copy_entry 1252 * Function: 1253 * Create new shadow object backing dst_entry with private copy of 1254 * all underlying pages. When src_entry is equal to dst_entry, 1255 * function implements COW for wired-down map entry. Otherwise, 1256 * it forks wired entry into dst_map. 1257 * 1258 * In/out conditions: 1259 * The source and destination maps must be locked for write. 1260 * The source map entry must be wired down (or be a sharing map 1261 * entry corresponding to a main map entry that is wired down). 1262 */ 1263 void 1264 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map, 1265 vm_map_entry_t dst_entry, vm_map_entry_t src_entry, 1266 vm_ooffset_t *fork_charge) 1267 { 1268 vm_object_t backing_object, dst_object, object, src_object; 1269 vm_pindex_t dst_pindex, pindex, src_pindex; 1270 vm_prot_t access, prot; 1271 vm_offset_t vaddr; 1272 vm_page_t dst_m; 1273 vm_page_t src_m; 1274 boolean_t upgrade; 1275 1276 #ifdef lint 1277 src_map++; 1278 #endif /* lint */ 1279 1280 upgrade = src_entry == dst_entry; 1281 access = prot = dst_entry->protection; 1282 1283 src_object = src_entry->object.vm_object; 1284 src_pindex = OFF_TO_IDX(src_entry->offset); 1285 1286 if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) { 1287 dst_object = src_object; 1288 vm_object_reference(dst_object); 1289 } else { 1290 /* 1291 * Create the top-level object for the destination entry. (Doesn't 1292 * actually shadow anything - we copy the pages directly.) 1293 */ 1294 dst_object = vm_object_allocate(OBJT_DEFAULT, 1295 OFF_TO_IDX(dst_entry->end - dst_entry->start)); 1296 #if VM_NRESERVLEVEL > 0 1297 dst_object->flags |= OBJ_COLORED; 1298 dst_object->pg_color = atop(dst_entry->start); 1299 #endif 1300 } 1301 1302 VM_OBJECT_WLOCK(dst_object); 1303 KASSERT(upgrade || dst_entry->object.vm_object == NULL, 1304 ("vm_fault_copy_entry: vm_object not NULL")); 1305 if (src_object != dst_object) { 1306 dst_entry->object.vm_object = dst_object; 1307 dst_entry->offset = 0; 1308 dst_object->charge = dst_entry->end - dst_entry->start; 1309 } 1310 if (fork_charge != NULL) { 1311 KASSERT(dst_entry->cred == NULL, 1312 ("vm_fault_copy_entry: leaked swp charge")); 1313 dst_object->cred = curthread->td_ucred; 1314 crhold(dst_object->cred); 1315 *fork_charge += dst_object->charge; 1316 } else if (dst_object->cred == NULL) { 1317 KASSERT(dst_entry->cred != NULL, ("no cred for entry %p", 1318 dst_entry)); 1319 dst_object->cred = dst_entry->cred; 1320 dst_entry->cred = NULL; 1321 } 1322 1323 /* 1324 * If not an upgrade, then enter the mappings in the pmap as 1325 * read and/or execute accesses. Otherwise, enter them as 1326 * write accesses. 1327 * 1328 * A writeable large page mapping is only created if all of 1329 * the constituent small page mappings are modified. Marking 1330 * PTEs as modified on inception allows promotion to happen 1331 * without taking potentially large number of soft faults. 1332 */ 1333 if (!upgrade) 1334 access &= ~VM_PROT_WRITE; 1335 1336 /* 1337 * Loop through all of the virtual pages within the entry's 1338 * range, copying each page from the source object to the 1339 * destination object. Since the source is wired, those pages 1340 * must exist. In contrast, the destination is pageable. 1341 * Since the destination object does share any backing storage 1342 * with the source object, all of its pages must be dirtied, 1343 * regardless of whether they can be written. 1344 */ 1345 for (vaddr = dst_entry->start, dst_pindex = 0; 1346 vaddr < dst_entry->end; 1347 vaddr += PAGE_SIZE, dst_pindex++) { 1348 again: 1349 /* 1350 * Find the page in the source object, and copy it in. 1351 * Because the source is wired down, the page will be 1352 * in memory. 1353 */ 1354 if (src_object != dst_object) 1355 VM_OBJECT_RLOCK(src_object); 1356 object = src_object; 1357 pindex = src_pindex + dst_pindex; 1358 while ((src_m = vm_page_lookup(object, pindex)) == NULL && 1359 (backing_object = object->backing_object) != NULL) { 1360 /* 1361 * Unless the source mapping is read-only or 1362 * it is presently being upgraded from 1363 * read-only, the first object in the shadow 1364 * chain should provide all of the pages. In 1365 * other words, this loop body should never be 1366 * executed when the source mapping is already 1367 * read/write. 1368 */ 1369 KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 || 1370 upgrade, 1371 ("vm_fault_copy_entry: main object missing page")); 1372 1373 VM_OBJECT_RLOCK(backing_object); 1374 pindex += OFF_TO_IDX(object->backing_object_offset); 1375 if (object != dst_object) 1376 VM_OBJECT_RUNLOCK(object); 1377 object = backing_object; 1378 } 1379 KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing")); 1380 1381 if (object != dst_object) { 1382 /* 1383 * Allocate a page in the destination object. 1384 */ 1385 dst_m = vm_page_alloc(dst_object, (src_object == 1386 dst_object ? src_pindex : 0) + dst_pindex, 1387 VM_ALLOC_NORMAL); 1388 if (dst_m == NULL) { 1389 VM_OBJECT_WUNLOCK(dst_object); 1390 VM_OBJECT_RUNLOCK(object); 1391 VM_WAIT; 1392 VM_OBJECT_WLOCK(dst_object); 1393 goto again; 1394 } 1395 pmap_copy_page(src_m, dst_m); 1396 VM_OBJECT_RUNLOCK(object); 1397 dst_m->valid = VM_PAGE_BITS_ALL; 1398 dst_m->dirty = VM_PAGE_BITS_ALL; 1399 } else { 1400 dst_m = src_m; 1401 if (vm_page_sleep_if_busy(dst_m, "fltupg")) 1402 goto again; 1403 vm_page_xbusy(dst_m); 1404 KASSERT(dst_m->valid == VM_PAGE_BITS_ALL, 1405 ("invalid dst page %p", dst_m)); 1406 } 1407 VM_OBJECT_WUNLOCK(dst_object); 1408 1409 /* 1410 * Enter it in the pmap. If a wired, copy-on-write 1411 * mapping is being replaced by a write-enabled 1412 * mapping, then wire that new mapping. 1413 */ 1414 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, 1415 access | (upgrade ? PMAP_ENTER_WIRED : 0), 0); 1416 1417 /* 1418 * Mark it no longer busy, and put it on the active list. 1419 */ 1420 VM_OBJECT_WLOCK(dst_object); 1421 1422 if (upgrade) { 1423 if (src_m != dst_m) { 1424 vm_page_lock(src_m); 1425 vm_page_unwire(src_m, PQ_INACTIVE); 1426 vm_page_unlock(src_m); 1427 vm_page_lock(dst_m); 1428 vm_page_wire(dst_m); 1429 vm_page_unlock(dst_m); 1430 } else { 1431 KASSERT(dst_m->wire_count > 0, 1432 ("dst_m %p is not wired", dst_m)); 1433 } 1434 } else { 1435 vm_page_lock(dst_m); 1436 vm_page_activate(dst_m); 1437 vm_page_unlock(dst_m); 1438 } 1439 vm_page_xunbusy(dst_m); 1440 } 1441 VM_OBJECT_WUNLOCK(dst_object); 1442 if (upgrade) { 1443 dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY); 1444 vm_object_deallocate(src_object); 1445 } 1446 } 1447 1448 1449 /* 1450 * This routine checks around the requested page for other pages that 1451 * might be able to be faulted in. This routine brackets the viable 1452 * pages for the pages to be paged in. 1453 * 1454 * Inputs: 1455 * m, rbehind, rahead 1456 * 1457 * Outputs: 1458 * marray (array of vm_page_t), reqpage (index of requested page) 1459 * 1460 * Return value: 1461 * number of pages in marray 1462 */ 1463 static int 1464 vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage) 1465 vm_page_t m; 1466 int rbehind; 1467 int rahead; 1468 vm_page_t *marray; 1469 int *reqpage; 1470 { 1471 int i,j; 1472 vm_object_t object; 1473 vm_pindex_t pindex, startpindex, endpindex, tpindex; 1474 vm_page_t rtm; 1475 int cbehind, cahead; 1476 1477 VM_OBJECT_ASSERT_WLOCKED(m->object); 1478 1479 object = m->object; 1480 pindex = m->pindex; 1481 cbehind = cahead = 0; 1482 1483 /* 1484 * if the requested page is not available, then give up now 1485 */ 1486 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) { 1487 return 0; 1488 } 1489 1490 if ((cbehind == 0) && (cahead == 0)) { 1491 *reqpage = 0; 1492 marray[0] = m; 1493 return 1; 1494 } 1495 1496 if (rahead > cahead) { 1497 rahead = cahead; 1498 } 1499 1500 if (rbehind > cbehind) { 1501 rbehind = cbehind; 1502 } 1503 1504 /* 1505 * scan backward for the read behind pages -- in memory 1506 */ 1507 if (pindex > 0) { 1508 if (rbehind > pindex) { 1509 rbehind = pindex; 1510 startpindex = 0; 1511 } else { 1512 startpindex = pindex - rbehind; 1513 } 1514 1515 if ((rtm = TAILQ_PREV(m, pglist, listq)) != NULL && 1516 rtm->pindex >= startpindex) 1517 startpindex = rtm->pindex + 1; 1518 1519 /* tpindex is unsigned; beware of numeric underflow. */ 1520 for (i = 0, tpindex = pindex - 1; tpindex >= startpindex && 1521 tpindex < pindex; i++, tpindex--) { 1522 1523 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL | 1524 VM_ALLOC_IFNOTCACHED); 1525 if (rtm == NULL) { 1526 /* 1527 * Shift the allocated pages to the 1528 * beginning of the array. 1529 */ 1530 for (j = 0; j < i; j++) { 1531 marray[j] = marray[j + tpindex + 1 - 1532 startpindex]; 1533 } 1534 break; 1535 } 1536 1537 marray[tpindex - startpindex] = rtm; 1538 } 1539 } else { 1540 startpindex = 0; 1541 i = 0; 1542 } 1543 1544 marray[i] = m; 1545 /* page offset of the required page */ 1546 *reqpage = i; 1547 1548 tpindex = pindex + 1; 1549 i++; 1550 1551 /* 1552 * scan forward for the read ahead pages 1553 */ 1554 endpindex = tpindex + rahead; 1555 if ((rtm = TAILQ_NEXT(m, listq)) != NULL && rtm->pindex < endpindex) 1556 endpindex = rtm->pindex; 1557 if (endpindex > object->size) 1558 endpindex = object->size; 1559 1560 for (; tpindex < endpindex; i++, tpindex++) { 1561 1562 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL | 1563 VM_ALLOC_IFNOTCACHED); 1564 if (rtm == NULL) { 1565 break; 1566 } 1567 1568 marray[i] = rtm; 1569 } 1570 1571 /* return number of pages */ 1572 return i; 1573 } 1574 1575 /* 1576 * Block entry into the machine-independent layer's page fault handler by 1577 * the calling thread. Subsequent calls to vm_fault() by that thread will 1578 * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of 1579 * spurious page faults. 1580 */ 1581 int 1582 vm_fault_disable_pagefaults(void) 1583 { 1584 1585 return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR)); 1586 } 1587 1588 void 1589 vm_fault_enable_pagefaults(int save) 1590 { 1591 1592 curthread_pflags_restore(save); 1593 } 1594