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