1 /* 2 * (MPSAFE) 3 * 4 * Copyright (c) 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * Copyright (c) 1994 John S. Dyson 7 * All rights reserved. 8 * Copyright (c) 1994 David Greenman 9 * All rights reserved. 10 * 11 * 12 * This code is derived from software contributed to Berkeley by 13 * The Mach Operating System project at Carnegie-Mellon University. 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions 17 * are met: 18 * 1. Redistributions of source code must retain the above copyright 19 * notice, this list of conditions and the following disclaimer. 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in the 22 * documentation and/or other materials provided with the distribution. 23 * 3. All advertising materials mentioning features or use of this software 24 * must display the following acknowledgement: 25 * This product includes software developed by the University of 26 * California, Berkeley and its contributors. 27 * 4. Neither the name of the University nor the names of its contributors 28 * may be used to endorse or promote products derived from this software 29 * without specific prior written permission. 30 * 31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 41 * SUCH DAMAGE. 42 * 43 * from: @(#)vm_fault.c 8.4 (Berkeley) 1/12/94 44 * 45 * 46 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 47 * All rights reserved. 48 * 49 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 50 * 51 * Permission to use, copy, modify and distribute this software and 52 * its documentation is hereby granted, provided that both the copyright 53 * notice and this permission notice appear in all copies of the 54 * software, derivative works or modified versions, and any portions 55 * thereof, and that both notices appear in supporting documentation. 56 * 57 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 58 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 59 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 60 * 61 * Carnegie Mellon requests users of this software to return to 62 * 63 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 64 * School of Computer Science 65 * Carnegie Mellon University 66 * Pittsburgh PA 15213-3890 67 * 68 * any improvements or extensions that they make and grant Carnegie the 69 * rights to redistribute these changes. 70 * 71 * $FreeBSD: src/sys/vm/vm_fault.c,v 1.108.2.8 2002/02/26 05:49:27 silby Exp $ 72 * $DragonFly: src/sys/vm/vm_fault.c,v 1.47 2008/07/01 02:02:56 dillon Exp $ 73 */ 74 75 /* 76 * Page fault handling module. 77 */ 78 79 #include <sys/param.h> 80 #include <sys/systm.h> 81 #include <sys/kernel.h> 82 #include <sys/proc.h> 83 #include <sys/vnode.h> 84 #include <sys/resourcevar.h> 85 #include <sys/vmmeter.h> 86 #include <sys/vkernel.h> 87 #include <sys/lock.h> 88 #include <sys/sysctl.h> 89 90 #include <cpu/lwbuf.h> 91 92 #include <vm/vm.h> 93 #include <vm/vm_param.h> 94 #include <vm/pmap.h> 95 #include <vm/vm_map.h> 96 #include <vm/vm_object.h> 97 #include <vm/vm_page.h> 98 #include <vm/vm_pageout.h> 99 #include <vm/vm_kern.h> 100 #include <vm/vm_pager.h> 101 #include <vm/vnode_pager.h> 102 #include <vm/vm_extern.h> 103 104 #include <sys/thread2.h> 105 #include <vm/vm_page2.h> 106 107 struct faultstate { 108 vm_page_t m; 109 vm_object_t object; 110 vm_pindex_t pindex; 111 vm_prot_t prot; 112 vm_page_t first_m; 113 vm_object_t first_object; 114 vm_prot_t first_prot; 115 vm_map_t map; 116 vm_map_entry_t entry; 117 int lookup_still_valid; 118 int didlimit; 119 int hardfault; 120 int fault_flags; 121 int map_generation; 122 boolean_t wired; 123 struct vnode *vp; 124 }; 125 126 static int debug_cluster = 0; 127 SYSCTL_INT(_vm, OID_AUTO, debug_cluster, CTLFLAG_RW, &debug_cluster, 0, ""); 128 129 static int vm_fault_object(struct faultstate *, vm_pindex_t, vm_prot_t); 130 static int vm_fault_vpagetable(struct faultstate *, vm_pindex_t *, vpte_t, int); 131 #if 0 132 static int vm_fault_additional_pages (vm_page_t, int, int, vm_page_t *, int *); 133 #endif 134 static int vm_fault_ratelimit(struct vmspace *); 135 static void vm_set_nosync(vm_page_t m, vm_map_entry_t entry); 136 static void vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, 137 int prot); 138 139 static __inline void 140 release_page(struct faultstate *fs) 141 { 142 vm_page_deactivate(fs->m); 143 vm_page_wakeup(fs->m); 144 fs->m = NULL; 145 } 146 147 /* 148 * NOTE: Once unlocked any cached fs->entry becomes invalid, any reuse 149 * requires relocking and then checking the timestamp. 150 * 151 * NOTE: vm_map_lock_read() does not bump fs->map->timestamp so we do 152 * not have to update fs->map_generation here. 153 * 154 * NOTE: This function can fail due to a deadlock against the caller's 155 * holding of a vm_page BUSY. 156 */ 157 static __inline int 158 relock_map(struct faultstate *fs) 159 { 160 int error; 161 162 if (fs->lookup_still_valid == FALSE && fs->map) { 163 error = vm_map_lock_read_to(fs->map); 164 if (error == 0) 165 fs->lookup_still_valid = TRUE; 166 } else { 167 error = 0; 168 } 169 return error; 170 } 171 172 static __inline void 173 unlock_map(struct faultstate *fs) 174 { 175 if (fs->lookup_still_valid && fs->map) { 176 vm_map_lookup_done(fs->map, fs->entry, 0); 177 fs->lookup_still_valid = FALSE; 178 } 179 } 180 181 /* 182 * Clean up after a successful call to vm_fault_object() so another call 183 * to vm_fault_object() can be made. 184 */ 185 static void 186 _cleanup_successful_fault(struct faultstate *fs, int relock) 187 { 188 if (fs->object != fs->first_object) { 189 vm_page_free(fs->first_m); 190 vm_object_pip_wakeup(fs->object); 191 fs->first_m = NULL; 192 } 193 fs->object = fs->first_object; 194 if (relock && fs->lookup_still_valid == FALSE) { 195 if (fs->map) 196 vm_map_lock_read(fs->map); 197 fs->lookup_still_valid = TRUE; 198 } 199 } 200 201 static void 202 _unlock_things(struct faultstate *fs, int dealloc) 203 { 204 _cleanup_successful_fault(fs, 0); 205 if (dealloc) { 206 /*vm_object_deallocate(fs->first_object);*/ 207 /*fs->first_object = NULL; drop used later on */ 208 } 209 unlock_map(fs); 210 if (fs->vp != NULL) { 211 vput(fs->vp); 212 fs->vp = NULL; 213 } 214 } 215 216 #define unlock_things(fs) _unlock_things(fs, 0) 217 #define unlock_and_deallocate(fs) _unlock_things(fs, 1) 218 #define cleanup_successful_fault(fs) _cleanup_successful_fault(fs, 1) 219 220 /* 221 * TRYPAGER 222 * 223 * Determine if the pager for the current object *might* contain the page. 224 * 225 * We only need to try the pager if this is not a default object (default 226 * objects are zero-fill and have no real pager), and if we are not taking 227 * a wiring fault or if the FS entry is wired. 228 */ 229 #define TRYPAGER(fs) \ 230 (fs->object->type != OBJT_DEFAULT && \ 231 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || fs->wired)) 232 233 /* 234 * vm_fault: 235 * 236 * Handle a page fault occuring at the given address, requiring the given 237 * permissions, in the map specified. If successful, the page is inserted 238 * into the associated physical map. 239 * 240 * NOTE: The given address should be truncated to the proper page address. 241 * 242 * KERN_SUCCESS is returned if the page fault is handled; otherwise, 243 * a standard error specifying why the fault is fatal is returned. 244 * 245 * The map in question must be referenced, and remains so. 246 * The caller may hold no locks. 247 * No other requirements. 248 */ 249 int 250 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags) 251 { 252 int result; 253 vm_pindex_t first_pindex; 254 struct faultstate fs; 255 int growstack; 256 257 mycpu->gd_cnt.v_vm_faults++; 258 259 fs.didlimit = 0; 260 fs.hardfault = 0; 261 fs.fault_flags = fault_flags; 262 growstack = 1; 263 264 lwkt_gettoken(&map->token); 265 266 RetryFault: 267 /* 268 * Find the vm_map_entry representing the backing store and resolve 269 * the top level object and page index. This may have the side 270 * effect of executing a copy-on-write on the map entry and/or 271 * creating a shadow object, but will not COW any actual VM pages. 272 * 273 * On success fs.map is left read-locked and various other fields 274 * are initialized but not otherwise referenced or locked. 275 * 276 * NOTE! vm_map_lookup will try to upgrade the fault_type to 277 * VM_FAULT_WRITE if the map entry is a virtual page table and also 278 * writable, so we can set the 'A'accessed bit in the virtual page 279 * table entry. 280 */ 281 fs.map = map; 282 result = vm_map_lookup(&fs.map, vaddr, fault_type, 283 &fs.entry, &fs.first_object, 284 &first_pindex, &fs.first_prot, &fs.wired); 285 286 /* 287 * If the lookup failed or the map protections are incompatible, 288 * the fault generally fails. However, if the caller is trying 289 * to do a user wiring we have more work to do. 290 */ 291 if (result != KERN_SUCCESS) { 292 if (result != KERN_PROTECTION_FAILURE || 293 (fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE) 294 { 295 if (result == KERN_INVALID_ADDRESS && growstack && 296 map != &kernel_map && curproc != NULL) { 297 result = vm_map_growstack(curproc, vaddr); 298 if (result == KERN_SUCCESS) { 299 growstack = 0; 300 goto RetryFault; 301 } 302 result = KERN_FAILURE; 303 } 304 goto done; 305 } 306 307 /* 308 * If we are user-wiring a r/w segment, and it is COW, then 309 * we need to do the COW operation. Note that we don't 310 * currently COW RO sections now, because it is NOT desirable 311 * to COW .text. We simply keep .text from ever being COW'ed 312 * and take the heat that one cannot debug wired .text sections. 313 */ 314 result = vm_map_lookup(&fs.map, vaddr, 315 VM_PROT_READ|VM_PROT_WRITE| 316 VM_PROT_OVERRIDE_WRITE, 317 &fs.entry, &fs.first_object, 318 &first_pindex, &fs.first_prot, 319 &fs.wired); 320 if (result != KERN_SUCCESS) { 321 result = KERN_FAILURE; 322 goto done; 323 } 324 325 /* 326 * If we don't COW now, on a user wire, the user will never 327 * be able to write to the mapping. If we don't make this 328 * restriction, the bookkeeping would be nearly impossible. 329 */ 330 if ((fs.entry->protection & VM_PROT_WRITE) == 0) 331 fs.entry->max_protection &= ~VM_PROT_WRITE; 332 } 333 334 /* 335 * fs.map is read-locked 336 * 337 * Misc checks. Save the map generation number to detect races. 338 */ 339 fs.map_generation = fs.map->timestamp; 340 341 if (fs.entry->eflags & (MAP_ENTRY_NOFAULT | MAP_ENTRY_KSTACK)) { 342 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) { 343 panic("vm_fault: fault on nofault entry, addr: %p", 344 (void *)vaddr); 345 } 346 if ((fs.entry->eflags & MAP_ENTRY_KSTACK) && 347 vaddr >= fs.entry->start && 348 vaddr < fs.entry->start + PAGE_SIZE) { 349 panic("vm_fault: fault on stack guard, addr: %p", 350 (void *)vaddr); 351 } 352 } 353 354 /* 355 * A system map entry may return a NULL object. No object means 356 * no pager means an unrecoverable kernel fault. 357 */ 358 if (fs.first_object == NULL) { 359 panic("vm_fault: unrecoverable fault at %p in entry %p", 360 (void *)vaddr, fs.entry); 361 } 362 363 /* 364 * Bump the paging-in-progress count to prevent size changes (e.g. 365 * truncation operations) during I/O. This must be done after 366 * obtaining the vnode lock in order to avoid possible deadlocks. 367 */ 368 vm_object_hold(fs.first_object); 369 fs.vp = vnode_pager_lock(fs.first_object); 370 371 fs.lookup_still_valid = TRUE; 372 fs.first_m = NULL; 373 fs.object = fs.first_object; /* so unlock_and_deallocate works */ 374 375 /* 376 * If the entry is wired we cannot change the page protection. 377 */ 378 if (fs.wired) 379 fault_type = fs.first_prot; 380 381 /* 382 * The page we want is at (first_object, first_pindex), but if the 383 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the 384 * page table to figure out the actual pindex. 385 * 386 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION 387 * ONLY 388 */ 389 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) { 390 result = vm_fault_vpagetable(&fs, &first_pindex, 391 fs.entry->aux.master_pde, 392 fault_type); 393 if (result == KERN_TRY_AGAIN) { 394 vm_object_drop(fs.first_object); 395 goto RetryFault; 396 } 397 if (result != KERN_SUCCESS) 398 goto done; 399 } 400 401 /* 402 * Now we have the actual (object, pindex), fault in the page. If 403 * vm_fault_object() fails it will unlock and deallocate the FS 404 * data. If it succeeds everything remains locked and fs->object 405 * will have an additional PIP count if it is not equal to 406 * fs->first_object 407 * 408 * vm_fault_object will set fs->prot for the pmap operation. It is 409 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the 410 * page can be safely written. However, it will force a read-only 411 * mapping for a read fault if the memory is managed by a virtual 412 * page table. 413 */ 414 /* BEFORE */ 415 result = vm_fault_object(&fs, first_pindex, fault_type); 416 417 if (result == KERN_TRY_AGAIN) { 418 vm_object_drop(fs.first_object); 419 goto RetryFault; 420 } 421 if (result != KERN_SUCCESS) 422 goto done; 423 424 /* 425 * On success vm_fault_object() does not unlock or deallocate, and fs.m 426 * will contain a busied page. 427 * 428 * Enter the page into the pmap and do pmap-related adjustments. 429 */ 430 vm_page_flag_set(fs.m, PG_REFERENCED); 431 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired); 432 433 /*KKASSERT(fs.m->queue == PQ_NONE); page-in op may deactivate page */ 434 KKASSERT(fs.m->flags & PG_BUSY); 435 436 /* 437 * If the page is not wired down, then put it where the pageout daemon 438 * can find it. 439 */ 440 if (fs.fault_flags & VM_FAULT_WIRE_MASK) { 441 if (fs.wired) 442 vm_page_wire(fs.m); 443 else 444 vm_page_unwire(fs.m, 1); 445 } else { 446 vm_page_activate(fs.m); 447 } 448 vm_page_wakeup(fs.m); 449 450 /* 451 * Burst in a few more pages if possible. The fs.map should still 452 * be locked. To avoid interlocking against a vnode->getblk 453 * operation we had to be sure to unbusy our primary vm_page above 454 * first. 455 */ 456 if (fault_flags & VM_FAULT_BURST) { 457 if ((fs.fault_flags & VM_FAULT_WIRE_MASK) == 0 && 458 fs.wired == 0) { 459 vm_prefault(fs.map->pmap, vaddr, fs.entry, fs.prot); 460 } 461 } 462 463 /* 464 * Unlock everything, and return 465 */ 466 unlock_things(&fs); 467 468 if (curthread->td_lwp) { 469 if (fs.hardfault) { 470 curthread->td_lwp->lwp_ru.ru_majflt++; 471 } else { 472 curthread->td_lwp->lwp_ru.ru_minflt++; 473 } 474 } 475 476 /*vm_object_deallocate(fs.first_object);*/ 477 /*fs.m = NULL; */ 478 /*fs.first_object = NULL; must still drop later */ 479 480 result = KERN_SUCCESS; 481 done: 482 if (fs.first_object) 483 vm_object_drop(fs.first_object); 484 lwkt_reltoken(&map->token); 485 return (result); 486 } 487 488 /* 489 * Fault in the specified virtual address in the current process map, 490 * returning a held VM page or NULL. See vm_fault_page() for more 491 * information. 492 * 493 * No requirements. 494 */ 495 vm_page_t 496 vm_fault_page_quick(vm_offset_t va, vm_prot_t fault_type, int *errorp) 497 { 498 struct lwp *lp = curthread->td_lwp; 499 vm_page_t m; 500 501 m = vm_fault_page(&lp->lwp_vmspace->vm_map, va, 502 fault_type, VM_FAULT_NORMAL, errorp); 503 return(m); 504 } 505 506 /* 507 * Fault in the specified virtual address in the specified map, doing all 508 * necessary manipulation of the object store and all necessary I/O. Return 509 * a held VM page or NULL, and set *errorp. The related pmap is not 510 * updated. 511 * 512 * The returned page will be properly dirtied if VM_PROT_WRITE was specified, 513 * and marked PG_REFERENCED as well. 514 * 515 * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an 516 * error will be returned. 517 * 518 * No requirements. 519 */ 520 vm_page_t 521 vm_fault_page(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 522 int fault_flags, int *errorp) 523 { 524 vm_pindex_t first_pindex; 525 struct faultstate fs; 526 int result; 527 vm_prot_t orig_fault_type = fault_type; 528 529 mycpu->gd_cnt.v_vm_faults++; 530 531 fs.didlimit = 0; 532 fs.hardfault = 0; 533 fs.fault_flags = fault_flags; 534 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0); 535 536 lwkt_gettoken(&map->token); 537 538 RetryFault: 539 /* 540 * Find the vm_map_entry representing the backing store and resolve 541 * the top level object and page index. This may have the side 542 * effect of executing a copy-on-write on the map entry and/or 543 * creating a shadow object, but will not COW any actual VM pages. 544 * 545 * On success fs.map is left read-locked and various other fields 546 * are initialized but not otherwise referenced or locked. 547 * 548 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE 549 * if the map entry is a virtual page table and also writable, 550 * so we can set the 'A'accessed bit in the virtual page table entry. 551 */ 552 fs.map = map; 553 result = vm_map_lookup(&fs.map, vaddr, fault_type, 554 &fs.entry, &fs.first_object, 555 &first_pindex, &fs.first_prot, &fs.wired); 556 557 if (result != KERN_SUCCESS) { 558 *errorp = result; 559 fs.m = NULL; 560 goto done; 561 } 562 563 /* 564 * fs.map is read-locked 565 * 566 * Misc checks. Save the map generation number to detect races. 567 */ 568 fs.map_generation = fs.map->timestamp; 569 570 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) { 571 panic("vm_fault: fault on nofault entry, addr: %lx", 572 (u_long)vaddr); 573 } 574 575 /* 576 * A system map entry may return a NULL object. No object means 577 * no pager means an unrecoverable kernel fault. 578 */ 579 if (fs.first_object == NULL) { 580 panic("vm_fault: unrecoverable fault at %p in entry %p", 581 (void *)vaddr, fs.entry); 582 } 583 584 /* 585 * Make a reference to this object to prevent its disposal while we 586 * are messing with it. Once we have the reference, the map is free 587 * to be diddled. Since objects reference their shadows (and copies), 588 * they will stay around as well. 589 * 590 * The reference should also prevent an unexpected collapse of the 591 * parent that might move pages from the current object into the 592 * parent unexpectedly, resulting in corruption. 593 * 594 * Bump the paging-in-progress count to prevent size changes (e.g. 595 * truncation operations) during I/O. This must be done after 596 * obtaining the vnode lock in order to avoid possible deadlocks. 597 */ 598 vm_object_hold(fs.first_object); 599 fs.vp = vnode_pager_lock(fs.first_object); 600 601 fs.lookup_still_valid = TRUE; 602 fs.first_m = NULL; 603 fs.object = fs.first_object; /* so unlock_and_deallocate works */ 604 605 /* 606 * If the entry is wired we cannot change the page protection. 607 */ 608 if (fs.wired) 609 fault_type = fs.first_prot; 610 611 /* 612 * The page we want is at (first_object, first_pindex), but if the 613 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the 614 * page table to figure out the actual pindex. 615 * 616 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION 617 * ONLY 618 */ 619 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) { 620 result = vm_fault_vpagetable(&fs, &first_pindex, 621 fs.entry->aux.master_pde, 622 fault_type); 623 if (result == KERN_TRY_AGAIN) { 624 vm_object_drop(fs.first_object); 625 goto RetryFault; 626 } 627 if (result != KERN_SUCCESS) { 628 *errorp = result; 629 fs.m = NULL; 630 goto done; 631 } 632 } 633 634 /* 635 * Now we have the actual (object, pindex), fault in the page. If 636 * vm_fault_object() fails it will unlock and deallocate the FS 637 * data. If it succeeds everything remains locked and fs->object 638 * will have an additinal PIP count if it is not equal to 639 * fs->first_object 640 */ 641 result = vm_fault_object(&fs, first_pindex, fault_type); 642 643 if (result == KERN_TRY_AGAIN) { 644 vm_object_drop(fs.first_object); 645 goto RetryFault; 646 } 647 if (result != KERN_SUCCESS) { 648 *errorp = result; 649 fs.m = NULL; 650 goto done; 651 } 652 653 if ((orig_fault_type & VM_PROT_WRITE) && 654 (fs.prot & VM_PROT_WRITE) == 0) { 655 *errorp = KERN_PROTECTION_FAILURE; 656 unlock_and_deallocate(&fs); 657 fs.m = NULL; 658 goto done; 659 } 660 661 /* 662 * Update the pmap. We really only have to do this if a COW 663 * occured to replace the read-only page with the new page. For 664 * now just do it unconditionally. XXX 665 */ 666 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired); 667 vm_page_flag_set(fs.m, PG_REFERENCED); 668 669 /* 670 * On success vm_fault_object() does not unlock or deallocate, and fs.m 671 * will contain a busied page. So we must unlock here after having 672 * messed with the pmap. 673 */ 674 unlock_things(&fs); 675 676 /* 677 * Return a held page. We are not doing any pmap manipulation so do 678 * not set PG_MAPPED. However, adjust the page flags according to 679 * the fault type because the caller may not use a managed pmapping 680 * (so we don't want to lose the fact that the page will be dirtied 681 * if a write fault was specified). 682 */ 683 vm_page_hold(fs.m); 684 if (fault_type & VM_PROT_WRITE) 685 vm_page_dirty(fs.m); 686 687 /* 688 * Unbusy the page by activating it. It remains held and will not 689 * be reclaimed. 690 */ 691 vm_page_activate(fs.m); 692 693 if (curthread->td_lwp) { 694 if (fs.hardfault) { 695 curthread->td_lwp->lwp_ru.ru_majflt++; 696 } else { 697 curthread->td_lwp->lwp_ru.ru_minflt++; 698 } 699 } 700 701 /* 702 * Unlock everything, and return the held page. 703 */ 704 vm_page_wakeup(fs.m); 705 /*vm_object_deallocate(fs.first_object);*/ 706 /*fs.first_object = NULL; */ 707 *errorp = 0; 708 709 done: 710 if (fs.first_object) 711 vm_object_drop(fs.first_object); 712 lwkt_reltoken(&map->token); 713 return(fs.m); 714 } 715 716 /* 717 * Fault in the specified (object,offset), dirty the returned page as 718 * needed. If the requested fault_type cannot be done NULL and an 719 * error is returned. 720 * 721 * A held (but not busied) page is returned. 722 * 723 * No requirements. 724 */ 725 vm_page_t 726 vm_fault_object_page(vm_object_t object, vm_ooffset_t offset, 727 vm_prot_t fault_type, int fault_flags, int *errorp) 728 { 729 int result; 730 vm_pindex_t first_pindex; 731 struct faultstate fs; 732 struct vm_map_entry entry; 733 734 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 735 bzero(&entry, sizeof(entry)); 736 entry.object.vm_object = object; 737 entry.maptype = VM_MAPTYPE_NORMAL; 738 entry.protection = entry.max_protection = fault_type; 739 740 fs.didlimit = 0; 741 fs.hardfault = 0; 742 fs.fault_flags = fault_flags; 743 fs.map = NULL; 744 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0); 745 746 RetryFault: 747 748 fs.first_object = object; 749 first_pindex = OFF_TO_IDX(offset); 750 fs.entry = &entry; 751 fs.first_prot = fault_type; 752 fs.wired = 0; 753 /*fs.map_generation = 0; unused */ 754 755 /* 756 * Make a reference to this object to prevent its disposal while we 757 * are messing with it. Once we have the reference, the map is free 758 * to be diddled. Since objects reference their shadows (and copies), 759 * they will stay around as well. 760 * 761 * The reference should also prevent an unexpected collapse of the 762 * parent that might move pages from the current object into the 763 * parent unexpectedly, resulting in corruption. 764 * 765 * Bump the paging-in-progress count to prevent size changes (e.g. 766 * truncation operations) during I/O. This must be done after 767 * obtaining the vnode lock in order to avoid possible deadlocks. 768 */ 769 fs.vp = vnode_pager_lock(fs.first_object); 770 771 fs.lookup_still_valid = TRUE; 772 fs.first_m = NULL; 773 fs.object = fs.first_object; /* so unlock_and_deallocate works */ 774 775 #if 0 776 /* XXX future - ability to operate on VM object using vpagetable */ 777 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) { 778 result = vm_fault_vpagetable(&fs, &first_pindex, 779 fs.entry->aux.master_pde, 780 fault_type); 781 if (result == KERN_TRY_AGAIN) 782 goto RetryFault; 783 if (result != KERN_SUCCESS) { 784 *errorp = result; 785 return (NULL); 786 } 787 } 788 #endif 789 790 /* 791 * Now we have the actual (object, pindex), fault in the page. If 792 * vm_fault_object() fails it will unlock and deallocate the FS 793 * data. If it succeeds everything remains locked and fs->object 794 * will have an additinal PIP count if it is not equal to 795 * fs->first_object 796 */ 797 result = vm_fault_object(&fs, first_pindex, fault_type); 798 799 if (result == KERN_TRY_AGAIN) 800 goto RetryFault; 801 if (result != KERN_SUCCESS) { 802 *errorp = result; 803 return(NULL); 804 } 805 806 if ((fault_type & VM_PROT_WRITE) && (fs.prot & VM_PROT_WRITE) == 0) { 807 *errorp = KERN_PROTECTION_FAILURE; 808 unlock_and_deallocate(&fs); 809 return(NULL); 810 } 811 812 /* 813 * On success vm_fault_object() does not unlock or deallocate, so we 814 * do it here. Note that the returned fs.m will be busied. 815 */ 816 unlock_things(&fs); 817 818 /* 819 * Return a held page. We are not doing any pmap manipulation so do 820 * not set PG_MAPPED. However, adjust the page flags according to 821 * the fault type because the caller may not use a managed pmapping 822 * (so we don't want to lose the fact that the page will be dirtied 823 * if a write fault was specified). 824 */ 825 vm_page_hold(fs.m); 826 if (fault_type & VM_PROT_WRITE) 827 vm_page_dirty(fs.m); 828 829 if (fault_flags & VM_FAULT_DIRTY) 830 vm_page_dirty(fs.m); 831 if (fault_flags & VM_FAULT_UNSWAP) 832 swap_pager_unswapped(fs.m); 833 834 /* 835 * Indicate that the page was accessed. 836 */ 837 vm_page_flag_set(fs.m, PG_REFERENCED); 838 839 /* 840 * Unbusy the page by activating it. It remains held and will not 841 * be reclaimed. 842 */ 843 vm_page_activate(fs.m); 844 845 if (curthread->td_lwp) { 846 if (fs.hardfault) { 847 mycpu->gd_cnt.v_vm_faults++; 848 curthread->td_lwp->lwp_ru.ru_majflt++; 849 } else { 850 curthread->td_lwp->lwp_ru.ru_minflt++; 851 } 852 } 853 854 /* 855 * Unlock everything, and return the held page. 856 */ 857 vm_page_wakeup(fs.m); 858 /*vm_object_deallocate(fs.first_object);*/ 859 /*fs.first_object = NULL; */ 860 861 *errorp = 0; 862 return(fs.m); 863 } 864 865 /* 866 * Translate the virtual page number (first_pindex) that is relative 867 * to the address space into a logical page number that is relative to the 868 * backing object. Use the virtual page table pointed to by (vpte). 869 * 870 * This implements an N-level page table. Any level can terminate the 871 * scan by setting VPTE_PS. A linear mapping is accomplished by setting 872 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP). 873 */ 874 static 875 int 876 vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex, 877 vpte_t vpte, int fault_type) 878 { 879 struct lwbuf *lwb; 880 struct lwbuf lwb_cache; 881 int vshift = VPTE_FRAME_END - PAGE_SHIFT; /* index bits remaining */ 882 int result = KERN_SUCCESS; 883 vpte_t *ptep; 884 885 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object)); 886 for (;;) { 887 /* 888 * We cannot proceed if the vpte is not valid, not readable 889 * for a read fault, or not writable for a write fault. 890 */ 891 if ((vpte & VPTE_V) == 0) { 892 unlock_and_deallocate(fs); 893 return (KERN_FAILURE); 894 } 895 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_R) == 0) { 896 unlock_and_deallocate(fs); 897 return (KERN_FAILURE); 898 } 899 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_W) == 0) { 900 unlock_and_deallocate(fs); 901 return (KERN_FAILURE); 902 } 903 if ((vpte & VPTE_PS) || vshift == 0) 904 break; 905 KKASSERT(vshift >= VPTE_PAGE_BITS); 906 907 /* 908 * Get the page table page. Nominally we only read the page 909 * table, but since we are actively setting VPTE_M and VPTE_A, 910 * tell vm_fault_object() that we are writing it. 911 * 912 * There is currently no real need to optimize this. 913 */ 914 result = vm_fault_object(fs, (vpte & VPTE_FRAME) >> PAGE_SHIFT, 915 VM_PROT_READ|VM_PROT_WRITE); 916 if (result != KERN_SUCCESS) 917 return (result); 918 919 /* 920 * Process the returned fs.m and look up the page table 921 * entry in the page table page. 922 */ 923 vshift -= VPTE_PAGE_BITS; 924 lwb = lwbuf_alloc(fs->m, &lwb_cache); 925 ptep = ((vpte_t *)lwbuf_kva(lwb) + 926 ((*pindex >> vshift) & VPTE_PAGE_MASK)); 927 vpte = *ptep; 928 929 /* 930 * Page table write-back. If the vpte is valid for the 931 * requested operation, do a write-back to the page table. 932 * 933 * XXX VPTE_M is not set properly for page directory pages. 934 * It doesn't get set in the page directory if the page table 935 * is modified during a read access. 936 */ 937 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_V) && 938 (vpte & VPTE_W)) { 939 if ((vpte & (VPTE_M|VPTE_A)) != (VPTE_M|VPTE_A)) { 940 atomic_set_long(ptep, VPTE_M | VPTE_A); 941 vm_page_dirty(fs->m); 942 } 943 } 944 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_V) && 945 (vpte & VPTE_R)) { 946 if ((vpte & VPTE_A) == 0) { 947 atomic_set_long(ptep, VPTE_A); 948 vm_page_dirty(fs->m); 949 } 950 } 951 lwbuf_free(lwb); 952 vm_page_flag_set(fs->m, PG_REFERENCED); 953 vm_page_activate(fs->m); 954 vm_page_wakeup(fs->m); 955 fs->m = NULL; 956 cleanup_successful_fault(fs); 957 } 958 /* 959 * Combine remaining address bits with the vpte. 960 */ 961 /* JG how many bits from each? */ 962 *pindex = ((vpte & VPTE_FRAME) >> PAGE_SHIFT) + 963 (*pindex & ((1L << vshift) - 1)); 964 return (KERN_SUCCESS); 965 } 966 967 968 /* 969 * This is the core of the vm_fault code. 970 * 971 * Do all operations required to fault-in (fs.first_object, pindex). Run 972 * through the shadow chain as necessary and do required COW or virtual 973 * copy operations. The caller has already fully resolved the vm_map_entry 974 * and, if appropriate, has created a copy-on-write layer. All we need to 975 * do is iterate the object chain. 976 * 977 * On failure (fs) is unlocked and deallocated and the caller may return or 978 * retry depending on the failure code. On success (fs) is NOT unlocked or 979 * deallocated, fs.m will contained a resolved, busied page, and fs.object 980 * will have an additional PIP count if it is not equal to fs.first_object. 981 * 982 * fs->first_object must be held on call. 983 */ 984 static 985 int 986 vm_fault_object(struct faultstate *fs, 987 vm_pindex_t first_pindex, vm_prot_t fault_type) 988 { 989 vm_object_t next_object; 990 vm_pindex_t pindex; 991 int error; 992 993 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object)); 994 fs->prot = fs->first_prot; 995 fs->object = fs->first_object; 996 pindex = first_pindex; 997 998 vm_object_chain_acquire(fs->first_object); 999 vm_object_pip_add(fs->first_object, 1); 1000 1001 /* 1002 * If a read fault occurs we try to make the page writable if 1003 * possible. There are three cases where we cannot make the 1004 * page mapping writable: 1005 * 1006 * (1) The mapping is read-only or the VM object is read-only, 1007 * fs->prot above will simply not have VM_PROT_WRITE set. 1008 * 1009 * (2) If the mapping is a virtual page table we need to be able 1010 * to detect writes so we can set VPTE_M in the virtual page 1011 * table. 1012 * 1013 * (3) If the VM page is read-only or copy-on-write, upgrading would 1014 * just result in an unnecessary COW fault. 1015 * 1016 * VM_PROT_VPAGED is set if faulting via a virtual page table and 1017 * causes adjustments to the 'M'odify bit to also turn off write 1018 * access to force a re-fault. 1019 */ 1020 if (fs->entry->maptype == VM_MAPTYPE_VPAGETABLE) { 1021 if ((fault_type & VM_PROT_WRITE) == 0) 1022 fs->prot &= ~VM_PROT_WRITE; 1023 } 1024 1025 /* vm_object_hold(fs->object); implied b/c object == first_object */ 1026 1027 for (;;) { 1028 /* 1029 * The entire backing chain from first_object to object 1030 * inclusive is chainlocked. 1031 * 1032 * If the object is dead, we stop here 1033 */ 1034 if (fs->object->flags & OBJ_DEAD) { 1035 vm_object_pip_wakeup(fs->first_object); 1036 vm_object_chain_release_all(fs->first_object, 1037 fs->object); 1038 if (fs->object != fs->first_object) 1039 vm_object_drop(fs->object); 1040 unlock_and_deallocate(fs); 1041 return (KERN_PROTECTION_FAILURE); 1042 } 1043 1044 /* 1045 * See if the page is resident. Wait/Retry if the page is 1046 * busy (lots of stuff may have changed so we can't continue 1047 * in that case). 1048 * 1049 * We can theoretically allow the soft-busy case on a read 1050 * fault if the page is marked valid, but since such 1051 * pages are typically already pmap'd, putting that 1052 * special case in might be more effort then it is 1053 * worth. We cannot under any circumstances mess 1054 * around with a vm_page_t->busy page except, perhaps, 1055 * to pmap it. 1056 */ 1057 fs->m = vm_page_lookup_busy_try(fs->object, pindex, 1058 TRUE, &error); 1059 if (error) { 1060 vm_object_pip_wakeup(fs->first_object); 1061 vm_object_chain_release_all(fs->first_object, 1062 fs->object); 1063 if (fs->object != fs->first_object) 1064 vm_object_drop(fs->object); 1065 unlock_things(fs); 1066 vm_page_sleep_busy(fs->m, TRUE, "vmpfw"); 1067 mycpu->gd_cnt.v_intrans++; 1068 /*vm_object_deallocate(fs->first_object);*/ 1069 /*fs->first_object = NULL;*/ 1070 fs->m = NULL; 1071 return (KERN_TRY_AGAIN); 1072 } 1073 if (fs->m) { 1074 /* 1075 * The page is busied for us. 1076 * 1077 * If reactivating a page from PQ_CACHE we may have 1078 * to rate-limit. 1079 */ 1080 int queue = fs->m->queue; 1081 vm_page_unqueue_nowakeup(fs->m); 1082 1083 if ((queue - fs->m->pc) == PQ_CACHE && 1084 vm_page_count_severe()) { 1085 vm_page_activate(fs->m); 1086 vm_page_wakeup(fs->m); 1087 fs->m = NULL; 1088 vm_object_pip_wakeup(fs->first_object); 1089 vm_object_chain_release_all(fs->first_object, 1090 fs->object); 1091 if (fs->object != fs->first_object) 1092 vm_object_drop(fs->object); 1093 unlock_and_deallocate(fs); 1094 vm_waitpfault(); 1095 return (KERN_TRY_AGAIN); 1096 } 1097 1098 /* 1099 * If it still isn't completely valid (readable), 1100 * or if a read-ahead-mark is set on the VM page, 1101 * jump to readrest, else we found the page and 1102 * can return. 1103 * 1104 * We can release the spl once we have marked the 1105 * page busy. 1106 */ 1107 if (fs->m->object != &kernel_object) { 1108 if ((fs->m->valid & VM_PAGE_BITS_ALL) != 1109 VM_PAGE_BITS_ALL) { 1110 goto readrest; 1111 } 1112 if (fs->m->flags & PG_RAM) { 1113 if (debug_cluster) 1114 kprintf("R"); 1115 vm_page_flag_clear(fs->m, PG_RAM); 1116 goto readrest; 1117 } 1118 } 1119 break; /* break to PAGE HAS BEEN FOUND */ 1120 } 1121 1122 /* 1123 * Page is not resident, If this is the search termination 1124 * or the pager might contain the page, allocate a new page. 1125 */ 1126 if (TRYPAGER(fs) || fs->object == fs->first_object) { 1127 /* 1128 * If the page is beyond the object size we fail 1129 */ 1130 if (pindex >= fs->object->size) { 1131 vm_object_pip_wakeup(fs->first_object); 1132 vm_object_chain_release_all(fs->first_object, 1133 fs->object); 1134 if (fs->object != fs->first_object) 1135 vm_object_drop(fs->object); 1136 unlock_and_deallocate(fs); 1137 return (KERN_PROTECTION_FAILURE); 1138 } 1139 1140 /* 1141 * Ratelimit. 1142 */ 1143 if (fs->didlimit == 0 && curproc != NULL) { 1144 int limticks; 1145 1146 limticks = vm_fault_ratelimit(curproc->p_vmspace); 1147 if (limticks) { 1148 vm_object_pip_wakeup(fs->first_object); 1149 vm_object_chain_release_all( 1150 fs->first_object, fs->object); 1151 if (fs->object != fs->first_object) 1152 vm_object_drop(fs->object); 1153 unlock_and_deallocate(fs); 1154 tsleep(curproc, 0, "vmrate", limticks); 1155 fs->didlimit = 1; 1156 return (KERN_TRY_AGAIN); 1157 } 1158 } 1159 1160 /* 1161 * Allocate a new page for this object/offset pair. 1162 * 1163 * It is possible for the allocation to race, so 1164 * handle the case. 1165 */ 1166 fs->m = NULL; 1167 if (!vm_page_count_severe()) { 1168 fs->m = vm_page_alloc(fs->object, pindex, 1169 ((fs->vp || fs->object->backing_object) ? 1170 VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL : 1171 VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL | 1172 VM_ALLOC_ZERO)); 1173 } 1174 if (fs->m == NULL) { 1175 vm_object_pip_wakeup(fs->first_object); 1176 vm_object_chain_release_all(fs->first_object, 1177 fs->object); 1178 if (fs->object != fs->first_object) 1179 vm_object_drop(fs->object); 1180 unlock_and_deallocate(fs); 1181 vm_waitpfault(); 1182 return (KERN_TRY_AGAIN); 1183 } 1184 1185 /* 1186 * Fall through to readrest. We have a new page which 1187 * will have to be paged (since m->valid will be 0). 1188 */ 1189 } 1190 1191 readrest: 1192 /* 1193 * We have found an invalid or partially valid page, a 1194 * page with a read-ahead mark which might be partially or 1195 * fully valid (and maybe dirty too), or we have allocated 1196 * a new page. 1197 * 1198 * Attempt to fault-in the page if there is a chance that the 1199 * pager has it, and potentially fault in additional pages 1200 * at the same time. 1201 * 1202 * If TRYPAGER is true then fs.m will be non-NULL and busied 1203 * for us. 1204 */ 1205 if (TRYPAGER(fs)) { 1206 int rv; 1207 int seqaccess; 1208 u_char behavior = vm_map_entry_behavior(fs->entry); 1209 1210 if (behavior == MAP_ENTRY_BEHAV_RANDOM) 1211 seqaccess = 0; 1212 else 1213 seqaccess = -1; 1214 1215 /* 1216 * If sequential access is detected then attempt 1217 * to deactivate/cache pages behind the scan to 1218 * prevent resource hogging. 1219 * 1220 * Use of PG_RAM to detect sequential access 1221 * also simulates multi-zone sequential access 1222 * detection for free. 1223 * 1224 * NOTE: Partially valid dirty pages cannot be 1225 * deactivated without causing NFS picemeal 1226 * writes to barf. 1227 */ 1228 if ((fs->first_object->type != OBJT_DEVICE) && 1229 (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL || 1230 (behavior != MAP_ENTRY_BEHAV_RANDOM && 1231 (fs->m->flags & PG_RAM))) 1232 ) { 1233 vm_pindex_t scan_pindex; 1234 int scan_count = 16; 1235 1236 if (first_pindex < 16) { 1237 scan_pindex = 0; 1238 scan_count = 0; 1239 } else { 1240 scan_pindex = first_pindex - 16; 1241 if (scan_pindex < 16) 1242 scan_count = scan_pindex; 1243 else 1244 scan_count = 16; 1245 } 1246 1247 while (scan_count) { 1248 vm_page_t mt; 1249 1250 mt = vm_page_lookup(fs->first_object, 1251 scan_pindex); 1252 if (mt == NULL) 1253 break; 1254 if (vm_page_busy_try(mt, TRUE)) 1255 goto skip; 1256 1257 if (mt->valid != VM_PAGE_BITS_ALL) { 1258 vm_page_wakeup(mt); 1259 break; 1260 } 1261 if ((mt->flags & 1262 (PG_FICTITIOUS | PG_UNMANAGED)) || 1263 mt->hold_count || 1264 mt->wire_count) { 1265 vm_page_wakeup(mt); 1266 goto skip; 1267 } 1268 if (mt->dirty == 0) 1269 vm_page_test_dirty(mt); 1270 if (mt->dirty) { 1271 vm_page_protect(mt, 1272 VM_PROT_NONE); 1273 vm_page_deactivate(mt); 1274 vm_page_wakeup(mt); 1275 } else { 1276 vm_page_cache(mt); 1277 } 1278 skip: 1279 --scan_count; 1280 --scan_pindex; 1281 } 1282 1283 seqaccess = 1; 1284 } 1285 1286 /* 1287 * Avoid deadlocking against the map when doing I/O. 1288 * fs.object and the page is PG_BUSY'd. 1289 * 1290 * NOTE: Once unlocked, fs->entry can become stale 1291 * so this will NULL it out. 1292 * 1293 * NOTE: fs->entry is invalid until we relock the 1294 * map and verify that the timestamp has not 1295 * changed. 1296 */ 1297 unlock_map(fs); 1298 1299 /* 1300 * Acquire the page data. We still hold a ref on 1301 * fs.object and the page has been PG_BUSY's. 1302 * 1303 * The pager may replace the page (for example, in 1304 * order to enter a fictitious page into the 1305 * object). If it does so it is responsible for 1306 * cleaning up the passed page and properly setting 1307 * the new page PG_BUSY. 1308 * 1309 * If we got here through a PG_RAM read-ahead 1310 * mark the page may be partially dirty and thus 1311 * not freeable. Don't bother checking to see 1312 * if the pager has the page because we can't free 1313 * it anyway. We have to depend on the get_page 1314 * operation filling in any gaps whether there is 1315 * backing store or not. 1316 */ 1317 rv = vm_pager_get_page(fs->object, &fs->m, seqaccess); 1318 1319 if (rv == VM_PAGER_OK) { 1320 /* 1321 * Relookup in case pager changed page. Pager 1322 * is responsible for disposition of old page 1323 * if moved. 1324 * 1325 * XXX other code segments do relookups too. 1326 * It's a bad abstraction that needs to be 1327 * fixed/removed. 1328 */ 1329 fs->m = vm_page_lookup(fs->object, pindex); 1330 if (fs->m == NULL) { 1331 vm_object_pip_wakeup(fs->first_object); 1332 vm_object_chain_release_all( 1333 fs->first_object, fs->object); 1334 if (fs->object != fs->first_object) 1335 vm_object_drop(fs->object); 1336 unlock_and_deallocate(fs); 1337 return (KERN_TRY_AGAIN); 1338 } 1339 1340 ++fs->hardfault; 1341 break; /* break to PAGE HAS BEEN FOUND */ 1342 } 1343 1344 /* 1345 * Remove the bogus page (which does not exist at this 1346 * object/offset); before doing so, we must get back 1347 * our object lock to preserve our invariant. 1348 * 1349 * Also wake up any other process that may want to bring 1350 * in this page. 1351 * 1352 * If this is the top-level object, we must leave the 1353 * busy page to prevent another process from rushing 1354 * past us, and inserting the page in that object at 1355 * the same time that we are. 1356 */ 1357 if (rv == VM_PAGER_ERROR) { 1358 if (curproc) { 1359 kprintf("vm_fault: pager read error, " 1360 "pid %d (%s)\n", 1361 curproc->p_pid, 1362 curproc->p_comm); 1363 } else { 1364 kprintf("vm_fault: pager read error, " 1365 "thread %p (%s)\n", 1366 curthread, 1367 curproc->p_comm); 1368 } 1369 } 1370 1371 /* 1372 * Data outside the range of the pager or an I/O error 1373 * 1374 * The page may have been wired during the pagein, 1375 * e.g. by the buffer cache, and cannot simply be 1376 * freed. Call vnode_pager_freepage() to deal with it. 1377 */ 1378 /* 1379 * XXX - the check for kernel_map is a kludge to work 1380 * around having the machine panic on a kernel space 1381 * fault w/ I/O error. 1382 */ 1383 if (((fs->map != &kernel_map) && 1384 (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) { 1385 vnode_pager_freepage(fs->m); 1386 fs->m = NULL; 1387 vm_object_pip_wakeup(fs->first_object); 1388 vm_object_chain_release_all(fs->first_object, 1389 fs->object); 1390 if (fs->object != fs->first_object) 1391 vm_object_drop(fs->object); 1392 unlock_and_deallocate(fs); 1393 if (rv == VM_PAGER_ERROR) 1394 return (KERN_FAILURE); 1395 else 1396 return (KERN_PROTECTION_FAILURE); 1397 /* NOT REACHED */ 1398 } 1399 if (fs->object != fs->first_object) { 1400 vnode_pager_freepage(fs->m); 1401 fs->m = NULL; 1402 /* 1403 * XXX - we cannot just fall out at this 1404 * point, m has been freed and is invalid! 1405 */ 1406 } 1407 } 1408 1409 /* 1410 * We get here if the object has a default pager (or unwiring) 1411 * or the pager doesn't have the page. 1412 */ 1413 if (fs->object == fs->first_object) 1414 fs->first_m = fs->m; 1415 1416 /* 1417 * Move on to the next object. The chain lock should prevent 1418 * the backing_object from getting ripped out from under us. 1419 */ 1420 if ((next_object = fs->object->backing_object) != NULL) { 1421 vm_object_hold(next_object); 1422 vm_object_chain_acquire(next_object); 1423 KKASSERT(next_object == fs->object->backing_object); 1424 pindex += OFF_TO_IDX(fs->object->backing_object_offset); 1425 } 1426 1427 if (next_object == NULL) { 1428 /* 1429 * If there's no object left, fill the page in the top 1430 * object with zeros. 1431 */ 1432 if (fs->object != fs->first_object) { 1433 if (fs->first_object->backing_object != 1434 fs->object) { 1435 vm_object_hold(fs->first_object->backing_object); 1436 } 1437 vm_object_chain_release_all( 1438 fs->first_object->backing_object, 1439 fs->object); 1440 if (fs->first_object->backing_object != 1441 fs->object) { 1442 vm_object_drop(fs->first_object->backing_object); 1443 } 1444 vm_object_pip_wakeup(fs->object); 1445 vm_object_drop(fs->object); 1446 fs->object = fs->first_object; 1447 pindex = first_pindex; 1448 fs->m = fs->first_m; 1449 } 1450 fs->first_m = NULL; 1451 1452 /* 1453 * Zero the page if necessary and mark it valid. 1454 */ 1455 if ((fs->m->flags & PG_ZERO) == 0) { 1456 vm_page_zero_fill(fs->m); 1457 } else { 1458 #ifdef PMAP_DEBUG 1459 pmap_page_assertzero(VM_PAGE_TO_PHYS(fs->m)); 1460 #endif 1461 vm_page_flag_clear(fs->m, PG_ZERO); 1462 mycpu->gd_cnt.v_ozfod++; 1463 } 1464 mycpu->gd_cnt.v_zfod++; 1465 fs->m->valid = VM_PAGE_BITS_ALL; 1466 break; /* break to PAGE HAS BEEN FOUND */ 1467 } 1468 if (fs->object != fs->first_object) { 1469 vm_object_pip_wakeup(fs->object); 1470 vm_object_lock_swap(); 1471 vm_object_drop(fs->object); 1472 } 1473 KASSERT(fs->object != next_object, 1474 ("object loop %p", next_object)); 1475 fs->object = next_object; 1476 vm_object_pip_add(fs->object, 1); 1477 } 1478 1479 /* 1480 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock 1481 * is held.] 1482 * 1483 * object still held. 1484 * 1485 * If the page is being written, but isn't already owned by the 1486 * top-level object, we have to copy it into a new page owned by the 1487 * top-level object. 1488 */ 1489 KASSERT((fs->m->flags & PG_BUSY) != 0, 1490 ("vm_fault: not busy after main loop")); 1491 1492 if (fs->object != fs->first_object) { 1493 /* 1494 * We only really need to copy if we want to write it. 1495 */ 1496 if (fault_type & VM_PROT_WRITE) { 1497 /* 1498 * This allows pages to be virtually copied from a 1499 * backing_object into the first_object, where the 1500 * backing object has no other refs to it, and cannot 1501 * gain any more refs. Instead of a bcopy, we just 1502 * move the page from the backing object to the 1503 * first object. Note that we must mark the page 1504 * dirty in the first object so that it will go out 1505 * to swap when needed. 1506 */ 1507 if ( 1508 /* 1509 * Map, if present, has not changed 1510 */ 1511 (fs->map == NULL || 1512 fs->map_generation == fs->map->timestamp) && 1513 /* 1514 * Only one shadow object 1515 */ 1516 (fs->object->shadow_count == 1) && 1517 /* 1518 * No COW refs, except us 1519 */ 1520 (fs->object->ref_count == 1) && 1521 /* 1522 * No one else can look this object up 1523 */ 1524 (fs->object->handle == NULL) && 1525 /* 1526 * No other ways to look the object up 1527 */ 1528 ((fs->object->type == OBJT_DEFAULT) || 1529 (fs->object->type == OBJT_SWAP)) && 1530 /* 1531 * We don't chase down the shadow chain 1532 */ 1533 (fs->object == fs->first_object->backing_object) && 1534 1535 /* 1536 * grab the lock if we need to 1537 */ 1538 (fs->lookup_still_valid || 1539 fs->map == NULL || 1540 lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT) == 0) 1541 ) { 1542 /* 1543 * (first_m) and (m) are both busied. We have 1544 * move (m) into (first_m)'s object/pindex 1545 * in an atomic fashion, then free (first_m). 1546 * 1547 * first_object is held so second remove 1548 * followed by the rename should wind 1549 * up being atomic. vm_page_free() might 1550 * block so we don't do it until after the 1551 * rename. 1552 */ 1553 fs->lookup_still_valid = 1; 1554 vm_page_protect(fs->first_m, VM_PROT_NONE); 1555 vm_page_remove(fs->first_m); 1556 vm_page_rename(fs->m, fs->first_object, 1557 first_pindex); 1558 vm_page_free(fs->first_m); 1559 fs->first_m = fs->m; 1560 fs->m = NULL; 1561 mycpu->gd_cnt.v_cow_optim++; 1562 } else { 1563 /* 1564 * Oh, well, lets copy it. 1565 */ 1566 vm_page_copy(fs->m, fs->first_m); 1567 vm_page_event(fs->m, VMEVENT_COW); 1568 } 1569 1570 if (fs->m) { 1571 /* 1572 * We no longer need the old page or object. 1573 */ 1574 release_page(fs); 1575 } 1576 1577 /* 1578 * We intend to revert to first_object, undo the 1579 * chain lock through to that. 1580 */ 1581 if (fs->first_object->backing_object != fs->object) 1582 vm_object_hold(fs->first_object->backing_object); 1583 vm_object_chain_release_all( 1584 fs->first_object->backing_object, 1585 fs->object); 1586 if (fs->first_object->backing_object != fs->object) 1587 vm_object_drop(fs->first_object->backing_object); 1588 1589 /* 1590 * fs->object != fs->first_object due to above 1591 * conditional 1592 */ 1593 vm_object_pip_wakeup(fs->object); 1594 vm_object_drop(fs->object); 1595 1596 /* 1597 * Only use the new page below... 1598 */ 1599 1600 mycpu->gd_cnt.v_cow_faults++; 1601 fs->m = fs->first_m; 1602 fs->object = fs->first_object; 1603 pindex = first_pindex; 1604 } else { 1605 /* 1606 * If it wasn't a write fault avoid having to copy 1607 * the page by mapping it read-only. 1608 */ 1609 fs->prot &= ~VM_PROT_WRITE; 1610 } 1611 } 1612 1613 /* 1614 * Relock the map if necessary, then check the generation count. 1615 * relock_map() will update fs->timestamp to account for the 1616 * relocking if necessary. 1617 * 1618 * If the count has changed after relocking then all sorts of 1619 * crap may have happened and we have to retry. 1620 * 1621 * NOTE: The relock_map() can fail due to a deadlock against 1622 * the vm_page we are holding BUSY. 1623 */ 1624 if (fs->lookup_still_valid == FALSE && fs->map) { 1625 if (relock_map(fs) || 1626 fs->map->timestamp != fs->map_generation) { 1627 release_page(fs); 1628 vm_object_pip_wakeup(fs->first_object); 1629 vm_object_chain_release_all(fs->first_object, 1630 fs->object); 1631 if (fs->object != fs->first_object) 1632 vm_object_drop(fs->object); 1633 unlock_and_deallocate(fs); 1634 return (KERN_TRY_AGAIN); 1635 } 1636 } 1637 1638 /* 1639 * If the fault is a write, we know that this page is being 1640 * written NOW so dirty it explicitly to save on pmap_is_modified() 1641 * calls later. 1642 * 1643 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC 1644 * if the page is already dirty to prevent data written with 1645 * the expectation of being synced from not being synced. 1646 * Likewise if this entry does not request NOSYNC then make 1647 * sure the page isn't marked NOSYNC. Applications sharing 1648 * data should use the same flags to avoid ping ponging. 1649 * 1650 * Also tell the backing pager, if any, that it should remove 1651 * any swap backing since the page is now dirty. 1652 */ 1653 if (fs->prot & VM_PROT_WRITE) { 1654 vm_object_set_writeable_dirty(fs->m->object); 1655 vm_set_nosync(fs->m, fs->entry); 1656 if (fs->fault_flags & VM_FAULT_DIRTY) { 1657 vm_page_dirty(fs->m); 1658 swap_pager_unswapped(fs->m); 1659 } 1660 } 1661 1662 vm_object_pip_wakeup(fs->first_object); 1663 vm_object_chain_release_all(fs->first_object, fs->object); 1664 if (fs->object != fs->first_object) 1665 vm_object_drop(fs->object); 1666 1667 /* 1668 * Page had better still be busy. We are still locked up and 1669 * fs->object will have another PIP reference if it is not equal 1670 * to fs->first_object. 1671 */ 1672 KASSERT(fs->m->flags & PG_BUSY, 1673 ("vm_fault: page %p not busy!", fs->m)); 1674 1675 /* 1676 * Sanity check: page must be completely valid or it is not fit to 1677 * map into user space. vm_pager_get_pages() ensures this. 1678 */ 1679 if (fs->m->valid != VM_PAGE_BITS_ALL) { 1680 vm_page_zero_invalid(fs->m, TRUE); 1681 kprintf("Warning: page %p partially invalid on fault\n", fs->m); 1682 } 1683 vm_page_flag_clear(fs->m, PG_ZERO); 1684 1685 return (KERN_SUCCESS); 1686 } 1687 1688 /* 1689 * Wire down a range of virtual addresses in a map. The entry in question 1690 * should be marked in-transition and the map must be locked. We must 1691 * release the map temporarily while faulting-in the page to avoid a 1692 * deadlock. Note that the entry may be clipped while we are blocked but 1693 * will never be freed. 1694 * 1695 * No requirements. 1696 */ 1697 int 1698 vm_fault_wire(vm_map_t map, vm_map_entry_t entry, boolean_t user_wire) 1699 { 1700 boolean_t fictitious; 1701 vm_offset_t start; 1702 vm_offset_t end; 1703 vm_offset_t va; 1704 vm_paddr_t pa; 1705 vm_page_t m; 1706 pmap_t pmap; 1707 int rv; 1708 1709 lwkt_gettoken(&map->token); 1710 1711 pmap = vm_map_pmap(map); 1712 start = entry->start; 1713 end = entry->end; 1714 fictitious = entry->object.vm_object && 1715 (entry->object.vm_object->type == OBJT_DEVICE); 1716 if (entry->eflags & MAP_ENTRY_KSTACK) 1717 start += PAGE_SIZE; 1718 map->timestamp++; 1719 vm_map_unlock(map); 1720 1721 /* 1722 * We simulate a fault to get the page and enter it in the physical 1723 * map. 1724 */ 1725 for (va = start; va < end; va += PAGE_SIZE) { 1726 if (user_wire) { 1727 rv = vm_fault(map, va, VM_PROT_READ, 1728 VM_FAULT_USER_WIRE); 1729 } else { 1730 rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE, 1731 VM_FAULT_CHANGE_WIRING); 1732 } 1733 if (rv) { 1734 while (va > start) { 1735 va -= PAGE_SIZE; 1736 if ((pa = pmap_extract(pmap, va)) == 0) 1737 continue; 1738 pmap_change_wiring(pmap, va, FALSE); 1739 if (!fictitious) { 1740 m = PHYS_TO_VM_PAGE(pa); 1741 vm_page_busy_wait(m, FALSE, "vmwrpg"); 1742 vm_page_unwire(m, 1); 1743 vm_page_wakeup(m); 1744 } 1745 } 1746 goto done; 1747 } 1748 } 1749 rv = KERN_SUCCESS; 1750 done: 1751 vm_map_lock(map); 1752 lwkt_reltoken(&map->token); 1753 return (rv); 1754 } 1755 1756 /* 1757 * Unwire a range of virtual addresses in a map. The map should be 1758 * locked. 1759 */ 1760 void 1761 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry) 1762 { 1763 boolean_t fictitious; 1764 vm_offset_t start; 1765 vm_offset_t end; 1766 vm_offset_t va; 1767 vm_paddr_t pa; 1768 vm_page_t m; 1769 pmap_t pmap; 1770 1771 lwkt_gettoken(&map->token); 1772 1773 pmap = vm_map_pmap(map); 1774 start = entry->start; 1775 end = entry->end; 1776 fictitious = entry->object.vm_object && 1777 (entry->object.vm_object->type == OBJT_DEVICE); 1778 if (entry->eflags & MAP_ENTRY_KSTACK) 1779 start += PAGE_SIZE; 1780 1781 /* 1782 * Since the pages are wired down, we must be able to get their 1783 * mappings from the physical map system. 1784 */ 1785 for (va = start; va < end; va += PAGE_SIZE) { 1786 pa = pmap_extract(pmap, va); 1787 if (pa != 0) { 1788 pmap_change_wiring(pmap, va, FALSE); 1789 if (!fictitious) { 1790 m = PHYS_TO_VM_PAGE(pa); 1791 vm_page_busy_wait(m, FALSE, "vmwupg"); 1792 vm_page_unwire(m, 1); 1793 vm_page_wakeup(m); 1794 } 1795 } 1796 } 1797 lwkt_reltoken(&map->token); 1798 } 1799 1800 /* 1801 * Reduce the rate at which memory is allocated to a process based 1802 * on the perceived load on the VM system. As the load increases 1803 * the allocation burst rate goes down and the delay increases. 1804 * 1805 * Rate limiting does not apply when faulting active or inactive 1806 * pages. When faulting 'cache' pages, rate limiting only applies 1807 * if the system currently has a severe page deficit. 1808 * 1809 * XXX vm_pagesupply should be increased when a page is freed. 1810 * 1811 * We sleep up to 1/10 of a second. 1812 */ 1813 static int 1814 vm_fault_ratelimit(struct vmspace *vmspace) 1815 { 1816 if (vm_load_enable == 0) 1817 return(0); 1818 if (vmspace->vm_pagesupply > 0) { 1819 --vmspace->vm_pagesupply; /* SMP race ok */ 1820 return(0); 1821 } 1822 #ifdef INVARIANTS 1823 if (vm_load_debug) { 1824 kprintf("load %-4d give %d pgs, wait %d, pid %-5d (%s)\n", 1825 vm_load, 1826 (1000 - vm_load ) / 10, vm_load * hz / 10000, 1827 curproc->p_pid, curproc->p_comm); 1828 } 1829 #endif 1830 vmspace->vm_pagesupply = (1000 - vm_load) / 10; 1831 return(vm_load * hz / 10000); 1832 } 1833 1834 /* 1835 * Copy all of the pages from a wired-down map entry to another. 1836 * 1837 * The source and destination maps must be locked for write. 1838 * The source and destination maps token must be held 1839 * The source map entry must be wired down (or be a sharing map 1840 * entry corresponding to a main map entry that is wired down). 1841 * 1842 * No other requirements. 1843 */ 1844 void 1845 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map, 1846 vm_map_entry_t dst_entry, vm_map_entry_t src_entry) 1847 { 1848 vm_object_t dst_object; 1849 vm_object_t src_object; 1850 vm_ooffset_t dst_offset; 1851 vm_ooffset_t src_offset; 1852 vm_prot_t prot; 1853 vm_offset_t vaddr; 1854 vm_page_t dst_m; 1855 vm_page_t src_m; 1856 1857 src_object = src_entry->object.vm_object; 1858 src_offset = src_entry->offset; 1859 1860 /* 1861 * Create the top-level object for the destination entry. (Doesn't 1862 * actually shadow anything - we copy the pages directly.) 1863 */ 1864 vm_map_entry_allocate_object(dst_entry); 1865 dst_object = dst_entry->object.vm_object; 1866 1867 prot = dst_entry->max_protection; 1868 1869 /* 1870 * Loop through all of the pages in the entry's range, copying each 1871 * one from the source object (it should be there) to the destination 1872 * object. 1873 */ 1874 for (vaddr = dst_entry->start, dst_offset = 0; 1875 vaddr < dst_entry->end; 1876 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) { 1877 1878 /* 1879 * Allocate a page in the destination object 1880 */ 1881 do { 1882 dst_m = vm_page_alloc(dst_object, 1883 OFF_TO_IDX(dst_offset), 1884 VM_ALLOC_NORMAL); 1885 if (dst_m == NULL) { 1886 vm_wait(0); 1887 } 1888 } while (dst_m == NULL); 1889 1890 /* 1891 * Find the page in the source object, and copy it in. 1892 * (Because the source is wired down, the page will be in 1893 * memory.) 1894 */ 1895 src_m = vm_page_lookup(src_object, 1896 OFF_TO_IDX(dst_offset + src_offset)); 1897 if (src_m == NULL) 1898 panic("vm_fault_copy_wired: page missing"); 1899 1900 vm_page_copy(src_m, dst_m); 1901 vm_page_event(src_m, VMEVENT_COW); 1902 1903 /* 1904 * Enter it in the pmap... 1905 */ 1906 1907 vm_page_flag_clear(dst_m, PG_ZERO); 1908 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE); 1909 1910 /* 1911 * Mark it no longer busy, and put it on the active list. 1912 */ 1913 vm_page_activate(dst_m); 1914 vm_page_wakeup(dst_m); 1915 } 1916 } 1917 1918 #if 0 1919 1920 /* 1921 * This routine checks around the requested page for other pages that 1922 * might be able to be faulted in. This routine brackets the viable 1923 * pages for the pages to be paged in. 1924 * 1925 * Inputs: 1926 * m, rbehind, rahead 1927 * 1928 * Outputs: 1929 * marray (array of vm_page_t), reqpage (index of requested page) 1930 * 1931 * Return value: 1932 * number of pages in marray 1933 */ 1934 static int 1935 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead, 1936 vm_page_t *marray, int *reqpage) 1937 { 1938 int i,j; 1939 vm_object_t object; 1940 vm_pindex_t pindex, startpindex, endpindex, tpindex; 1941 vm_page_t rtm; 1942 int cbehind, cahead; 1943 1944 object = m->object; 1945 pindex = m->pindex; 1946 1947 /* 1948 * we don't fault-ahead for device pager 1949 */ 1950 if (object->type == OBJT_DEVICE) { 1951 *reqpage = 0; 1952 marray[0] = m; 1953 return 1; 1954 } 1955 1956 /* 1957 * if the requested page is not available, then give up now 1958 */ 1959 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) { 1960 *reqpage = 0; /* not used by caller, fix compiler warn */ 1961 return 0; 1962 } 1963 1964 if ((cbehind == 0) && (cahead == 0)) { 1965 *reqpage = 0; 1966 marray[0] = m; 1967 return 1; 1968 } 1969 1970 if (rahead > cahead) { 1971 rahead = cahead; 1972 } 1973 1974 if (rbehind > cbehind) { 1975 rbehind = cbehind; 1976 } 1977 1978 /* 1979 * Do not do any readahead if we have insufficient free memory. 1980 * 1981 * XXX code was broken disabled before and has instability 1982 * with this conditonal fixed, so shortcut for now. 1983 */ 1984 if (burst_fault == 0 || vm_page_count_severe()) { 1985 marray[0] = m; 1986 *reqpage = 0; 1987 return 1; 1988 } 1989 1990 /* 1991 * scan backward for the read behind pages -- in memory 1992 * 1993 * Assume that if the page is not found an interrupt will not 1994 * create it. Theoretically interrupts can only remove (busy) 1995 * pages, not create new associations. 1996 */ 1997 if (pindex > 0) { 1998 if (rbehind > pindex) { 1999 rbehind = pindex; 2000 startpindex = 0; 2001 } else { 2002 startpindex = pindex - rbehind; 2003 } 2004 2005 vm_object_hold(object); 2006 for (tpindex = pindex; tpindex > startpindex; --tpindex) { 2007 if (vm_page_lookup(object, tpindex - 1)) 2008 break; 2009 } 2010 2011 i = 0; 2012 while (tpindex < pindex) { 2013 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM | 2014 VM_ALLOC_NULL_OK); 2015 if (rtm == NULL) { 2016 for (j = 0; j < i; j++) { 2017 vm_page_free(marray[j]); 2018 } 2019 vm_object_drop(object); 2020 marray[0] = m; 2021 *reqpage = 0; 2022 return 1; 2023 } 2024 marray[i] = rtm; 2025 ++i; 2026 ++tpindex; 2027 } 2028 vm_object_drop(object); 2029 } else { 2030 i = 0; 2031 } 2032 2033 /* 2034 * Assign requested page 2035 */ 2036 marray[i] = m; 2037 *reqpage = i; 2038 ++i; 2039 2040 /* 2041 * Scan forwards for read-ahead pages 2042 */ 2043 tpindex = pindex + 1; 2044 endpindex = tpindex + rahead; 2045 if (endpindex > object->size) 2046 endpindex = object->size; 2047 2048 vm_object_hold(object); 2049 while (tpindex < endpindex) { 2050 if (vm_page_lookup(object, tpindex)) 2051 break; 2052 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM | 2053 VM_ALLOC_NULL_OK); 2054 if (rtm == NULL) 2055 break; 2056 marray[i] = rtm; 2057 ++i; 2058 ++tpindex; 2059 } 2060 vm_object_drop(object); 2061 2062 return (i); 2063 } 2064 2065 #endif 2066 2067 /* 2068 * vm_prefault() provides a quick way of clustering pagefaults into a 2069 * processes address space. It is a "cousin" of pmap_object_init_pt, 2070 * except it runs at page fault time instead of mmap time. 2071 * 2072 * vm.fast_fault Enables pre-faulting zero-fill pages 2073 * 2074 * vm.prefault_pages Number of pages (1/2 negative, 1/2 positive) to 2075 * prefault. Scan stops in either direction when 2076 * a page is found to already exist. 2077 * 2078 * This code used to be per-platform pmap_prefault(). It is now 2079 * machine-independent and enhanced to also pre-fault zero-fill pages 2080 * (see vm.fast_fault) as well as make them writable, which greatly 2081 * reduces the number of page faults programs incur. 2082 * 2083 * Application performance when pre-faulting zero-fill pages is heavily 2084 * dependent on the application. Very tiny applications like /bin/echo 2085 * lose a little performance while applications of any appreciable size 2086 * gain performance. Prefaulting multiple pages also reduces SMP 2087 * congestion and can improve SMP performance significantly. 2088 * 2089 * NOTE! prot may allow writing but this only applies to the top level 2090 * object. If we wind up mapping a page extracted from a backing 2091 * object we have to make sure it is read-only. 2092 * 2093 * NOTE! The caller has already handled any COW operations on the 2094 * vm_map_entry via the normal fault code. Do NOT call this 2095 * shortcut unless the normal fault code has run on this entry. 2096 * 2097 * The related map must be locked. 2098 * No other requirements. 2099 */ 2100 static int vm_prefault_pages = 8; 2101 SYSCTL_INT(_vm, OID_AUTO, prefault_pages, CTLFLAG_RW, &vm_prefault_pages, 0, 2102 "Maximum number of pages to pre-fault"); 2103 static int vm_fast_fault = 1; 2104 SYSCTL_INT(_vm, OID_AUTO, fast_fault, CTLFLAG_RW, &vm_fast_fault, 0, 2105 "Burst fault zero-fill regions"); 2106 2107 /* 2108 * Set PG_NOSYNC if the map entry indicates so, but only if the page 2109 * is not already dirty by other means. This will prevent passive 2110 * filesystem syncing as well as 'sync' from writing out the page. 2111 */ 2112 static void 2113 vm_set_nosync(vm_page_t m, vm_map_entry_t entry) 2114 { 2115 if (entry->eflags & MAP_ENTRY_NOSYNC) { 2116 if (m->dirty == 0) 2117 vm_page_flag_set(m, PG_NOSYNC); 2118 } else { 2119 vm_page_flag_clear(m, PG_NOSYNC); 2120 } 2121 } 2122 2123 static void 2124 vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, int prot) 2125 { 2126 struct lwp *lp; 2127 vm_page_t m; 2128 vm_offset_t addr; 2129 vm_pindex_t index; 2130 vm_pindex_t pindex; 2131 vm_object_t object; 2132 int pprot; 2133 int i; 2134 int noneg; 2135 int nopos; 2136 int maxpages; 2137 2138 /* 2139 * Get stable max count value, disabled if set to 0 2140 */ 2141 maxpages = vm_prefault_pages; 2142 cpu_ccfence(); 2143 if (maxpages <= 0) 2144 return; 2145 2146 /* 2147 * We do not currently prefault mappings that use virtual page 2148 * tables. We do not prefault foreign pmaps. 2149 */ 2150 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) 2151 return; 2152 lp = curthread->td_lwp; 2153 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace))) 2154 return; 2155 2156 /* 2157 * Limit pre-fault count to 1024 pages. 2158 */ 2159 if (maxpages > 1024) 2160 maxpages = 1024; 2161 2162 object = entry->object.vm_object; 2163 KKASSERT(object != NULL); 2164 vm_object_hold(object); 2165 KKASSERT(object == entry->object.vm_object); 2166 vm_object_chain_acquire(object); 2167 2168 noneg = 0; 2169 nopos = 0; 2170 for (i = 0; i < maxpages; ++i) { 2171 vm_object_t lobject; 2172 vm_object_t nobject; 2173 int allocated = 0; 2174 int error; 2175 2176 /* 2177 * This can eat a lot of time on a heavily contended 2178 * machine so yield on the tick if needed. 2179 */ 2180 if ((i & 7) == 7) 2181 lwkt_yield(); 2182 2183 /* 2184 * Calculate the page to pre-fault, stopping the scan in 2185 * each direction separately if the limit is reached. 2186 */ 2187 if (i & 1) { 2188 if (noneg) 2189 continue; 2190 addr = addra - ((i + 1) >> 1) * PAGE_SIZE; 2191 } else { 2192 if (nopos) 2193 continue; 2194 addr = addra + ((i + 2) >> 1) * PAGE_SIZE; 2195 } 2196 if (addr < entry->start) { 2197 noneg = 1; 2198 if (noneg && nopos) 2199 break; 2200 continue; 2201 } 2202 if (addr >= entry->end) { 2203 nopos = 1; 2204 if (noneg && nopos) 2205 break; 2206 continue; 2207 } 2208 2209 /* 2210 * Skip pages already mapped, and stop scanning in that 2211 * direction. When the scan terminates in both directions 2212 * we are done. 2213 */ 2214 if (pmap_prefault_ok(pmap, addr) == 0) { 2215 if (i & 1) 2216 noneg = 1; 2217 else 2218 nopos = 1; 2219 if (noneg && nopos) 2220 break; 2221 continue; 2222 } 2223 2224 /* 2225 * Follow the VM object chain to obtain the page to be mapped 2226 * into the pmap. 2227 * 2228 * If we reach the terminal object without finding a page 2229 * and we determine it would be advantageous, then allocate 2230 * a zero-fill page for the base object. The base object 2231 * is guaranteed to be OBJT_DEFAULT for this case. 2232 * 2233 * In order to not have to check the pager via *haspage*() 2234 * we stop if any non-default object is encountered. e.g. 2235 * a vnode or swap object would stop the loop. 2236 */ 2237 index = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT; 2238 lobject = object; 2239 pindex = index; 2240 pprot = prot; 2241 2242 KKASSERT(lobject == entry->object.vm_object); 2243 /*vm_object_hold(lobject); implied */ 2244 2245 while ((m = vm_page_lookup_busy_try(lobject, pindex, 2246 TRUE, &error)) == NULL) { 2247 if (lobject->type != OBJT_DEFAULT) 2248 break; 2249 if (lobject->backing_object == NULL) { 2250 if (vm_fast_fault == 0) 2251 break; 2252 if ((prot & VM_PROT_WRITE) == 0 || 2253 vm_page_count_min(0)) { 2254 break; 2255 } 2256 2257 /* 2258 * NOTE: Allocated from base object 2259 */ 2260 m = vm_page_alloc(object, index, 2261 VM_ALLOC_NORMAL | 2262 VM_ALLOC_ZERO | 2263 VM_ALLOC_NULL_OK); 2264 if (m == NULL) 2265 break; 2266 2267 if ((m->flags & PG_ZERO) == 0) { 2268 vm_page_zero_fill(m); 2269 } else { 2270 #ifdef PMAP_DEBUG 2271 pmap_page_assertzero( 2272 VM_PAGE_TO_PHYS(m)); 2273 #endif 2274 vm_page_flag_clear(m, PG_ZERO); 2275 mycpu->gd_cnt.v_ozfod++; 2276 } 2277 mycpu->gd_cnt.v_zfod++; 2278 m->valid = VM_PAGE_BITS_ALL; 2279 allocated = 1; 2280 pprot = prot; 2281 /* lobject = object .. not needed */ 2282 break; 2283 } 2284 if (lobject->backing_object_offset & PAGE_MASK) 2285 break; 2286 nobject = lobject->backing_object; 2287 vm_object_hold(nobject); 2288 KKASSERT(nobject == lobject->backing_object); 2289 pindex += lobject->backing_object_offset >> PAGE_SHIFT; 2290 if (lobject != object) { 2291 vm_object_lock_swap(); 2292 vm_object_drop(lobject); 2293 } 2294 lobject = nobject; 2295 pprot &= ~VM_PROT_WRITE; 2296 vm_object_chain_acquire(lobject); 2297 } 2298 2299 /* 2300 * NOTE: A non-NULL (m) will be associated with lobject if 2301 * it was found there, otherwise it is probably a 2302 * zero-fill page associated with the base object. 2303 * 2304 * Give-up if no page is available. 2305 */ 2306 if (m == NULL) { 2307 if (lobject != object) { 2308 if (object->backing_object != lobject) 2309 vm_object_hold(object->backing_object); 2310 vm_object_chain_release_all( 2311 object->backing_object, lobject); 2312 if (object->backing_object != lobject) 2313 vm_object_drop(object->backing_object); 2314 vm_object_drop(lobject); 2315 } 2316 break; 2317 } 2318 2319 /* 2320 * Do not conditionalize on PG_RAM. If pages are present in 2321 * the VM system we assume optimal caching. If caching is 2322 * not optimal the I/O gravy train will be restarted when we 2323 * hit an unavailable page. We do not want to try to restart 2324 * the gravy train now because we really don't know how much 2325 * of the object has been cached. The cost for restarting 2326 * the gravy train should be low (since accesses will likely 2327 * be I/O bound anyway). 2328 * 2329 * The object must be marked dirty if we are mapping a 2330 * writable page. m->object is either lobject or object, 2331 * both of which are still held. 2332 */ 2333 if (pprot & VM_PROT_WRITE) 2334 vm_object_set_writeable_dirty(m->object); 2335 2336 if (lobject != object) { 2337 if (object->backing_object != lobject) 2338 vm_object_hold(object->backing_object); 2339 vm_object_chain_release_all(object->backing_object, 2340 lobject); 2341 if (object->backing_object != lobject) 2342 vm_object_drop(object->backing_object); 2343 vm_object_drop(lobject); 2344 } 2345 2346 /* 2347 * Enter the page into the pmap if appropriate. If we had 2348 * allocated the page we have to place it on a queue. If not 2349 * we just have to make sure it isn't on the cache queue 2350 * (pages on the cache queue are not allowed to be mapped). 2351 */ 2352 if (allocated) { 2353 if (pprot & VM_PROT_WRITE) 2354 vm_set_nosync(m, entry); 2355 pmap_enter(pmap, addr, m, pprot, 0); 2356 vm_page_deactivate(m); 2357 vm_page_wakeup(m); 2358 } else if (error) { 2359 /* couldn't busy page, no wakeup */ 2360 } else if ( 2361 ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) && 2362 (m->flags & PG_FICTITIOUS) == 0) { 2363 /* 2364 * A fully valid page not undergoing soft I/O can 2365 * be immediately entered into the pmap. 2366 */ 2367 if ((m->queue - m->pc) == PQ_CACHE) 2368 vm_page_deactivate(m); 2369 if (pprot & VM_PROT_WRITE) 2370 vm_set_nosync(m, entry); 2371 pmap_enter(pmap, addr, m, pprot, 0); 2372 vm_page_wakeup(m); 2373 } else { 2374 vm_page_wakeup(m); 2375 } 2376 } 2377 vm_object_chain_release(object); 2378 vm_object_drop(object); 2379 } 2380