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