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