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