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