1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (c) 1994 John S. Dyson 5 * All rights reserved. 6 * Copyright (c) 1994 David Greenman 7 * All rights reserved. 8 * 9 * 10 * This code is derived from software contributed to Berkeley by 11 * The Mach Operating System project at Carnegie-Mellon University. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * from: @(#)vm_fault.c 8.4 (Berkeley) 1/12/94 42 * 43 * 44 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 45 * All rights reserved. 46 * 47 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 48 * 49 * Permission to use, copy, modify and distribute this software and 50 * its documentation is hereby granted, provided that both the copyright 51 * notice and this permission notice appear in all copies of the 52 * software, derivative works or modified versions, and any portions 53 * thereof, and that both notices appear in supporting documentation. 54 * 55 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 56 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 57 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 58 * 59 * Carnegie Mellon requests users of this software to return to 60 * 61 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 62 * School of Computer Science 63 * Carnegie Mellon University 64 * Pittsburgh PA 15213-3890 65 * 66 * any improvements or extensions that they make and grant Carnegie the 67 * rights to redistribute these changes. 68 */ 69 70 /* 71 * Page fault handling module. 72 */ 73 74 #include <sys/cdefs.h> 75 __FBSDID("$FreeBSD$"); 76 77 #include "opt_vm.h" 78 79 #include <sys/param.h> 80 #include <sys/systm.h> 81 #include <sys/kernel.h> 82 #include <sys/lock.h> 83 #include <sys/mutex.h> 84 #include <sys/proc.h> 85 #include <sys/resourcevar.h> 86 #include <sys/sysctl.h> 87 #include <sys/vmmeter.h> 88 #include <sys/vnode.h> 89 90 #include <vm/vm.h> 91 #include <vm/vm_param.h> 92 #include <vm/pmap.h> 93 #include <vm/vm_map.h> 94 #include <vm/vm_object.h> 95 #include <vm/vm_page.h> 96 #include <vm/vm_pageout.h> 97 #include <vm/vm_kern.h> 98 #include <vm/vm_pager.h> 99 #include <vm/vm_extern.h> 100 101 #include <sys/mount.h> /* XXX Temporary for VFS_LOCK_GIANT() */ 102 103 #define PFBAK 4 104 #define PFFOR 4 105 #define PAGEORDER_SIZE (PFBAK+PFFOR) 106 107 static int prefault_pageorder[] = { 108 -1 * PAGE_SIZE, 1 * PAGE_SIZE, 109 -2 * PAGE_SIZE, 2 * PAGE_SIZE, 110 -3 * PAGE_SIZE, 3 * PAGE_SIZE, 111 -4 * PAGE_SIZE, 4 * PAGE_SIZE 112 }; 113 114 static int vm_fault_additional_pages(vm_page_t, int, int, vm_page_t *, int *); 115 static void vm_fault_prefault(pmap_t, vm_offset_t, vm_map_entry_t); 116 117 #define VM_FAULT_READ_AHEAD 8 118 #define VM_FAULT_READ_BEHIND 7 119 #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1) 120 121 struct faultstate { 122 vm_page_t m; 123 vm_object_t object; 124 vm_pindex_t pindex; 125 vm_page_t first_m; 126 vm_object_t first_object; 127 vm_pindex_t first_pindex; 128 vm_map_t map; 129 vm_map_entry_t entry; 130 int lookup_still_valid; 131 struct vnode *vp; 132 int vfslocked; 133 }; 134 135 static inline void 136 release_page(struct faultstate *fs) 137 { 138 139 vm_page_wakeup(fs->m); 140 vm_page_lock(fs->m); 141 vm_page_lock_queues(); 142 vm_page_deactivate(fs->m); 143 vm_page_unlock_queues(); 144 vm_page_unlock(fs->m); 145 fs->m = NULL; 146 } 147 148 static inline void 149 unlock_map(struct faultstate *fs) 150 { 151 152 if (fs->lookup_still_valid) { 153 vm_map_lookup_done(fs->map, fs->entry); 154 fs->lookup_still_valid = FALSE; 155 } 156 } 157 158 static void 159 unlock_and_deallocate(struct faultstate *fs) 160 { 161 162 vm_object_pip_wakeup(fs->object); 163 VM_OBJECT_UNLOCK(fs->object); 164 if (fs->object != fs->first_object) { 165 VM_OBJECT_LOCK(fs->first_object); 166 vm_page_lock(fs->first_m); 167 vm_page_lock_queues(); 168 vm_page_free(fs->first_m); 169 vm_page_unlock_queues(); 170 vm_page_unlock(fs->first_m); 171 vm_object_pip_wakeup(fs->first_object); 172 VM_OBJECT_UNLOCK(fs->first_object); 173 fs->first_m = NULL; 174 } 175 vm_object_deallocate(fs->first_object); 176 unlock_map(fs); 177 if (fs->vp != NULL) { 178 vput(fs->vp); 179 fs->vp = NULL; 180 } 181 VFS_UNLOCK_GIANT(fs->vfslocked); 182 fs->vfslocked = 0; 183 } 184 185 /* 186 * TRYPAGER - used by vm_fault to calculate whether the pager for the 187 * current object *might* contain the page. 188 * 189 * default objects are zero-fill, there is no real pager. 190 */ 191 #define TRYPAGER (fs.object->type != OBJT_DEFAULT && \ 192 ((fault_flags & VM_FAULT_CHANGE_WIRING) == 0 || wired)) 193 194 /* 195 * vm_fault: 196 * 197 * Handle a page fault occurring at the given address, 198 * requiring the given permissions, in the map specified. 199 * If successful, the page is inserted into the 200 * associated physical map. 201 * 202 * NOTE: the given address should be truncated to the 203 * proper page address. 204 * 205 * KERN_SUCCESS is returned if the page fault is handled; otherwise, 206 * a standard error specifying why the fault is fatal is returned. 207 * 208 * 209 * The map in question must be referenced, and remains so. 210 * Caller may hold no locks. 211 */ 212 int 213 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 214 int fault_flags) 215 { 216 vm_prot_t prot; 217 int is_first_object_locked, result; 218 boolean_t are_queues_locked, growstack, wired; 219 int map_generation; 220 vm_object_t next_object; 221 vm_page_t marray[VM_FAULT_READ]; 222 int hardfault; 223 int faultcount, ahead, behind, alloc_req; 224 struct faultstate fs; 225 struct vnode *vp; 226 int locked, error; 227 228 hardfault = 0; 229 growstack = TRUE; 230 PCPU_INC(cnt.v_vm_faults); 231 fs.vp = NULL; 232 fs.vfslocked = 0; 233 faultcount = behind = 0; 234 235 RetryFault:; 236 237 /* 238 * Find the backing store object and offset into it to begin the 239 * search. 240 */ 241 fs.map = map; 242 result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry, 243 &fs.first_object, &fs.first_pindex, &prot, &wired); 244 if (result != KERN_SUCCESS) { 245 if (growstack && result == KERN_INVALID_ADDRESS && 246 map != kernel_map) { 247 result = vm_map_growstack(curproc, vaddr); 248 if (result != KERN_SUCCESS) 249 return (KERN_FAILURE); 250 growstack = FALSE; 251 goto RetryFault; 252 } 253 return (result); 254 } 255 256 map_generation = fs.map->timestamp; 257 258 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) { 259 panic("vm_fault: fault on nofault entry, addr: %lx", 260 (u_long)vaddr); 261 } 262 263 /* 264 * Make a reference to this object to prevent its disposal while we 265 * are messing with it. Once we have the reference, the map is free 266 * to be diddled. Since objects reference their shadows (and copies), 267 * they will stay around as well. 268 * 269 * Bump the paging-in-progress count to prevent size changes (e.g. 270 * truncation operations) during I/O. This must be done after 271 * obtaining the vnode lock in order to avoid possible deadlocks. 272 */ 273 VM_OBJECT_LOCK(fs.first_object); 274 vm_object_reference_locked(fs.first_object); 275 vm_object_pip_add(fs.first_object, 1); 276 277 fs.lookup_still_valid = TRUE; 278 279 if (wired) 280 fault_type = prot | (fault_type & VM_PROT_COPY); 281 282 fs.first_m = NULL; 283 284 /* 285 * Search for the page at object/offset. 286 */ 287 fs.object = fs.first_object; 288 fs.pindex = fs.first_pindex; 289 while (TRUE) { 290 /* 291 * If the object is dead, we stop here 292 */ 293 if (fs.object->flags & OBJ_DEAD) { 294 unlock_and_deallocate(&fs); 295 return (KERN_PROTECTION_FAILURE); 296 } 297 298 /* 299 * See if page is resident 300 */ 301 fs.m = vm_page_lookup(fs.object, fs.pindex); 302 if (fs.m != NULL) { 303 /* 304 * check for page-based copy on write. 305 * We check fs.object == fs.first_object so 306 * as to ensure the legacy COW mechanism is 307 * used when the page in question is part of 308 * a shadow object. Otherwise, vm_page_cowfault() 309 * removes the page from the backing object, 310 * which is not what we want. 311 */ 312 vm_page_lock(fs.m); 313 vm_page_lock_queues(); 314 if ((fs.m->cow) && 315 (fault_type & VM_PROT_WRITE) && 316 (fs.object == fs.first_object)) { 317 vm_page_cowfault(fs.m); 318 vm_page_unlock_queues(); 319 vm_page_unlock(fs.m); 320 unlock_and_deallocate(&fs); 321 goto RetryFault; 322 } 323 324 /* 325 * Wait/Retry if the page is busy. We have to do this 326 * if the page is busy via either VPO_BUSY or 327 * vm_page_t->busy because the vm_pager may be using 328 * vm_page_t->busy for pageouts ( and even pageins if 329 * it is the vnode pager ), and we could end up trying 330 * to pagein and pageout the same page simultaneously. 331 * 332 * We can theoretically allow the busy case on a read 333 * fault if the page is marked valid, but since such 334 * pages are typically already pmap'd, putting that 335 * special case in might be more effort then it is 336 * worth. We cannot under any circumstances mess 337 * around with a vm_page_t->busy page except, perhaps, 338 * to pmap it. 339 */ 340 if ((fs.m->oflags & VPO_BUSY) || fs.m->busy) { 341 /* 342 * Reference the page before unlocking and 343 * sleeping so that the page daemon is less 344 * likely to reclaim it. 345 */ 346 vm_page_flag_set(fs.m, PG_REFERENCED); 347 vm_page_unlock_queues(); 348 vm_page_unlock(fs.m); 349 VM_OBJECT_UNLOCK(fs.object); 350 if (fs.object != fs.first_object) { 351 VM_OBJECT_LOCK(fs.first_object); 352 vm_page_lock(fs.first_m); 353 vm_page_lock_queues(); 354 vm_page_free(fs.first_m); 355 vm_page_unlock_queues(); 356 vm_page_unlock(fs.first_m); 357 vm_object_pip_wakeup(fs.first_object); 358 VM_OBJECT_UNLOCK(fs.first_object); 359 fs.first_m = NULL; 360 } 361 unlock_map(&fs); 362 VM_OBJECT_LOCK(fs.object); 363 if (fs.m == vm_page_lookup(fs.object, 364 fs.pindex)) { 365 vm_page_sleep_if_busy(fs.m, TRUE, 366 "vmpfw"); 367 } 368 vm_object_pip_wakeup(fs.object); 369 VM_OBJECT_UNLOCK(fs.object); 370 PCPU_INC(cnt.v_intrans); 371 vm_object_deallocate(fs.first_object); 372 goto RetryFault; 373 } 374 vm_pageq_remove(fs.m); 375 vm_page_unlock_queues(); 376 vm_page_unlock(fs.m); 377 378 /* 379 * Mark page busy for other processes, and the 380 * pagedaemon. If it still isn't completely valid 381 * (readable), jump to readrest, else break-out ( we 382 * found the page ). 383 */ 384 vm_page_busy(fs.m); 385 if (fs.m->valid != VM_PAGE_BITS_ALL && 386 fs.m->object != kernel_object && fs.m->object != kmem_object) { 387 goto readrest; 388 } 389 390 break; 391 } 392 393 /* 394 * Page is not resident, If this is the search termination 395 * or the pager might contain the page, allocate a new page. 396 */ 397 if (TRYPAGER || fs.object == fs.first_object) { 398 if (fs.pindex >= fs.object->size) { 399 unlock_and_deallocate(&fs); 400 return (KERN_PROTECTION_FAILURE); 401 } 402 403 /* 404 * Allocate a new page for this object/offset pair. 405 * 406 * Unlocked read of the p_flag is harmless. At 407 * worst, the P_KILLED might be not observed 408 * there, and allocation can fail, causing 409 * restart and new reading of the p_flag. 410 */ 411 fs.m = NULL; 412 if (!vm_page_count_severe() || P_KILLED(curproc)) { 413 #if VM_NRESERVLEVEL > 0 414 if ((fs.object->flags & OBJ_COLORED) == 0) { 415 fs.object->flags |= OBJ_COLORED; 416 fs.object->pg_color = atop(vaddr) - 417 fs.pindex; 418 } 419 #endif 420 alloc_req = P_KILLED(curproc) ? 421 VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL; 422 if (fs.object->type != OBJT_VNODE && 423 fs.object->backing_object == NULL) 424 alloc_req |= VM_ALLOC_ZERO; 425 fs.m = vm_page_alloc(fs.object, fs.pindex, 426 alloc_req); 427 } 428 if (fs.m == NULL) { 429 unlock_and_deallocate(&fs); 430 VM_WAITPFAULT; 431 goto RetryFault; 432 } else if (fs.m->valid == VM_PAGE_BITS_ALL) 433 break; 434 } 435 436 readrest: 437 /* 438 * We have found a valid page or we have allocated a new page. 439 * The page thus may not be valid or may not be entirely 440 * valid. 441 * 442 * Attempt to fault-in the page if there is a chance that the 443 * pager has it, and potentially fault in additional pages 444 * at the same time. 445 */ 446 if (TRYPAGER) { 447 int rv; 448 int reqpage = 0; 449 u_char behavior = vm_map_entry_behavior(fs.entry); 450 451 if (behavior == MAP_ENTRY_BEHAV_RANDOM || 452 P_KILLED(curproc)) { 453 ahead = 0; 454 behind = 0; 455 } else { 456 behind = (vaddr - fs.entry->start) >> PAGE_SHIFT; 457 if (behind > VM_FAULT_READ_BEHIND) 458 behind = VM_FAULT_READ_BEHIND; 459 460 ahead = ((fs.entry->end - vaddr) >> PAGE_SHIFT) - 1; 461 if (ahead > VM_FAULT_READ_AHEAD) 462 ahead = VM_FAULT_READ_AHEAD; 463 } 464 is_first_object_locked = FALSE; 465 if ((behavior == MAP_ENTRY_BEHAV_SEQUENTIAL || 466 (behavior != MAP_ENTRY_BEHAV_RANDOM && 467 fs.pindex >= fs.entry->lastr && 468 fs.pindex < fs.entry->lastr + VM_FAULT_READ)) && 469 (fs.first_object == fs.object || 470 (is_first_object_locked = VM_OBJECT_TRYLOCK(fs.first_object))) && 471 fs.first_object->type != OBJT_DEVICE && 472 fs.first_object->type != OBJT_PHYS && 473 fs.first_object->type != OBJT_SG) { 474 vm_pindex_t firstpindex, tmppindex; 475 476 if (fs.first_pindex < 2 * VM_FAULT_READ) 477 firstpindex = 0; 478 else 479 firstpindex = fs.first_pindex - 2 * VM_FAULT_READ; 480 481 are_queues_locked = FALSE; 482 /* 483 * note: partially valid pages cannot be 484 * included in the lookahead - NFS piecemeal 485 * writes will barf on it badly. 486 */ 487 for (tmppindex = fs.first_pindex - 1; 488 tmppindex >= firstpindex; 489 --tmppindex) { 490 vm_page_t mt; 491 492 mt = vm_page_lookup(fs.first_object, tmppindex); 493 if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL)) 494 break; 495 if (mt->busy || 496 (mt->oflags & VPO_BUSY)) 497 continue; 498 if (!are_queues_locked) { 499 are_queues_locked = TRUE; 500 vm_page_lock(mt); 501 vm_page_lock_queues(); 502 } else { 503 vm_page_unlock_queues(); 504 vm_page_lock(mt); 505 vm_page_lock_queues(); 506 } 507 if (mt->hold_count || 508 mt->wire_count) { 509 vm_page_unlock(mt); 510 continue; 511 } 512 pmap_remove_all(mt); 513 if (mt->dirty) { 514 vm_page_deactivate(mt); 515 } else { 516 vm_page_cache(mt); 517 } 518 vm_page_unlock(mt); 519 } 520 if (are_queues_locked) 521 vm_page_unlock_queues(); 522 ahead += behind; 523 behind = 0; 524 } 525 if (is_first_object_locked) 526 VM_OBJECT_UNLOCK(fs.first_object); 527 528 /* 529 * Call the pager to retrieve the data, if any, after 530 * releasing the lock on the map. We hold a ref on 531 * fs.object and the pages are VPO_BUSY'd. 532 */ 533 unlock_map(&fs); 534 535 vnode_lock: 536 if (fs.object->type == OBJT_VNODE) { 537 vp = fs.object->handle; 538 if (vp == fs.vp) 539 goto vnode_locked; 540 else if (fs.vp != NULL) { 541 vput(fs.vp); 542 fs.vp = NULL; 543 } 544 locked = VOP_ISLOCKED(vp); 545 546 if (VFS_NEEDSGIANT(vp->v_mount) && !fs.vfslocked) { 547 fs.vfslocked = 1; 548 if (!mtx_trylock(&Giant)) { 549 VM_OBJECT_UNLOCK(fs.object); 550 mtx_lock(&Giant); 551 VM_OBJECT_LOCK(fs.object); 552 goto vnode_lock; 553 } 554 } 555 if (locked != LK_EXCLUSIVE) 556 locked = LK_SHARED; 557 /* Do not sleep for vnode lock while fs.m is busy */ 558 error = vget(vp, locked | LK_CANRECURSE | 559 LK_NOWAIT, curthread); 560 if (error != 0) { 561 int vfslocked; 562 563 vfslocked = fs.vfslocked; 564 fs.vfslocked = 0; /* Keep Giant */ 565 vhold(vp); 566 release_page(&fs); 567 unlock_and_deallocate(&fs); 568 error = vget(vp, locked | LK_RETRY | 569 LK_CANRECURSE, curthread); 570 vdrop(vp); 571 fs.vp = vp; 572 fs.vfslocked = vfslocked; 573 KASSERT(error == 0, 574 ("vm_fault: vget failed")); 575 goto RetryFault; 576 } 577 fs.vp = vp; 578 } 579 vnode_locked: 580 KASSERT(fs.vp == NULL || !fs.map->system_map, 581 ("vm_fault: vnode-backed object mapped by system map")); 582 583 /* 584 * now we find out if any other pages should be paged 585 * in at this time this routine checks to see if the 586 * pages surrounding this fault reside in the same 587 * object as the page for this fault. If they do, 588 * then they are faulted in also into the object. The 589 * array "marray" returned contains an array of 590 * vm_page_t structs where one of them is the 591 * vm_page_t passed to the routine. The reqpage 592 * return value is the index into the marray for the 593 * vm_page_t passed to the routine. 594 * 595 * fs.m plus the additional pages are VPO_BUSY'd. 596 */ 597 faultcount = vm_fault_additional_pages( 598 fs.m, behind, ahead, marray, &reqpage); 599 600 rv = faultcount ? 601 vm_pager_get_pages(fs.object, marray, faultcount, 602 reqpage) : VM_PAGER_FAIL; 603 604 if (rv == VM_PAGER_OK) { 605 /* 606 * Found the page. Leave it busy while we play 607 * with it. 608 */ 609 610 /* 611 * Relookup in case pager changed page. Pager 612 * is responsible for disposition of old page 613 * if moved. 614 */ 615 fs.m = vm_page_lookup(fs.object, fs.pindex); 616 if (!fs.m) { 617 unlock_and_deallocate(&fs); 618 goto RetryFault; 619 } 620 621 hardfault++; 622 break; /* break to PAGE HAS BEEN FOUND */ 623 } 624 /* 625 * Remove the bogus page (which does not exist at this 626 * object/offset); before doing so, we must get back 627 * our object lock to preserve our invariant. 628 * 629 * Also wake up any other process that may want to bring 630 * in this page. 631 * 632 * If this is the top-level object, we must leave the 633 * busy page to prevent another process from rushing 634 * past us, and inserting the page in that object at 635 * the same time that we are. 636 */ 637 if (rv == VM_PAGER_ERROR) 638 printf("vm_fault: pager read error, pid %d (%s)\n", 639 curproc->p_pid, curproc->p_comm); 640 /* 641 * Data outside the range of the pager or an I/O error 642 */ 643 /* 644 * XXX - the check for kernel_map is a kludge to work 645 * around having the machine panic on a kernel space 646 * fault w/ I/O error. 647 */ 648 if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) || 649 (rv == VM_PAGER_BAD)) { 650 vm_page_lock(fs.m); 651 vm_page_lock_queues(); 652 vm_page_free(fs.m); 653 vm_page_unlock_queues(); 654 vm_page_unlock(fs.m); 655 fs.m = NULL; 656 unlock_and_deallocate(&fs); 657 return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE); 658 } 659 if (fs.object != fs.first_object) { 660 vm_page_lock(fs.m); 661 vm_page_lock_queues(); 662 vm_page_free(fs.m); 663 vm_page_unlock_queues(); 664 vm_page_unlock(fs.m); 665 fs.m = NULL; 666 /* 667 * XXX - we cannot just fall out at this 668 * point, m has been freed and is invalid! 669 */ 670 } 671 } 672 673 /* 674 * We get here if the object has default pager (or unwiring) 675 * or the pager doesn't have the page. 676 */ 677 if (fs.object == fs.first_object) 678 fs.first_m = fs.m; 679 680 /* 681 * Move on to the next object. Lock the next object before 682 * unlocking the current one. 683 */ 684 fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset); 685 next_object = fs.object->backing_object; 686 if (next_object == NULL) { 687 /* 688 * If there's no object left, fill the page in the top 689 * object with zeros. 690 */ 691 if (fs.object != fs.first_object) { 692 vm_object_pip_wakeup(fs.object); 693 VM_OBJECT_UNLOCK(fs.object); 694 695 fs.object = fs.first_object; 696 fs.pindex = fs.first_pindex; 697 fs.m = fs.first_m; 698 VM_OBJECT_LOCK(fs.object); 699 } 700 fs.first_m = NULL; 701 702 /* 703 * Zero the page if necessary and mark it valid. 704 */ 705 if ((fs.m->flags & PG_ZERO) == 0) { 706 pmap_zero_page(fs.m); 707 } else { 708 PCPU_INC(cnt.v_ozfod); 709 } 710 PCPU_INC(cnt.v_zfod); 711 fs.m->valid = VM_PAGE_BITS_ALL; 712 break; /* break to PAGE HAS BEEN FOUND */ 713 } else { 714 KASSERT(fs.object != next_object, 715 ("object loop %p", next_object)); 716 VM_OBJECT_LOCK(next_object); 717 vm_object_pip_add(next_object, 1); 718 if (fs.object != fs.first_object) 719 vm_object_pip_wakeup(fs.object); 720 VM_OBJECT_UNLOCK(fs.object); 721 fs.object = next_object; 722 } 723 } 724 725 KASSERT((fs.m->oflags & VPO_BUSY) != 0, 726 ("vm_fault: not busy after main loop")); 727 728 /* 729 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock 730 * is held.] 731 */ 732 733 /* 734 * If the page is being written, but isn't already owned by the 735 * top-level object, we have to copy it into a new page owned by the 736 * top-level object. 737 */ 738 if (fs.object != fs.first_object) { 739 /* 740 * We only really need to copy if we want to write it. 741 */ 742 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) { 743 /* 744 * This allows pages to be virtually copied from a 745 * backing_object into the first_object, where the 746 * backing object has no other refs to it, and cannot 747 * gain any more refs. Instead of a bcopy, we just 748 * move the page from the backing object to the 749 * first object. Note that we must mark the page 750 * dirty in the first object so that it will go out 751 * to swap when needed. 752 */ 753 is_first_object_locked = FALSE; 754 if ( 755 /* 756 * Only one shadow object 757 */ 758 (fs.object->shadow_count == 1) && 759 /* 760 * No COW refs, except us 761 */ 762 (fs.object->ref_count == 1) && 763 /* 764 * No one else can look this object up 765 */ 766 (fs.object->handle == NULL) && 767 /* 768 * No other ways to look the object up 769 */ 770 ((fs.object->type == OBJT_DEFAULT) || 771 (fs.object->type == OBJT_SWAP)) && 772 (is_first_object_locked = VM_OBJECT_TRYLOCK(fs.first_object)) && 773 /* 774 * We don't chase down the shadow chain 775 */ 776 fs.object == fs.first_object->backing_object) { 777 vm_page_lock(fs.first_m); 778 vm_page_lock_queues(); 779 /* 780 * get rid of the unnecessary page 781 */ 782 vm_page_free(fs.first_m); 783 vm_page_unlock_queues(); 784 vm_page_unlock(fs.first_m); 785 /* 786 * grab the page and put it into the 787 * process'es object. The page is 788 * automatically made dirty. 789 */ 790 vm_page_lock(fs.m); 791 vm_page_lock_queues(); 792 vm_page_rename(fs.m, fs.first_object, fs.first_pindex); 793 vm_page_unlock_queues(); 794 vm_page_unlock(fs.m); 795 vm_page_busy(fs.m); 796 fs.first_m = fs.m; 797 fs.m = NULL; 798 PCPU_INC(cnt.v_cow_optim); 799 } else { 800 /* 801 * Oh, well, lets copy it. 802 */ 803 pmap_copy_page(fs.m, fs.first_m); 804 fs.first_m->valid = VM_PAGE_BITS_ALL; 805 if (wired && (fault_flags & 806 VM_FAULT_CHANGE_WIRING) == 0) { 807 vm_page_lock(fs.first_m); 808 vm_page_lock_queues(); 809 vm_page_wire(fs.first_m); 810 vm_page_unlock_queues(); 811 vm_page_unlock(fs.first_m); 812 813 vm_page_lock(fs.m); 814 vm_page_lock_queues(); 815 vm_page_unwire(fs.m, FALSE); 816 vm_page_unlock_queues(); 817 vm_page_unlock(fs.m); 818 } 819 /* 820 * We no longer need the old page or object. 821 */ 822 release_page(&fs); 823 } 824 /* 825 * fs.object != fs.first_object due to above 826 * conditional 827 */ 828 vm_object_pip_wakeup(fs.object); 829 VM_OBJECT_UNLOCK(fs.object); 830 /* 831 * Only use the new page below... 832 */ 833 fs.object = fs.first_object; 834 fs.pindex = fs.first_pindex; 835 fs.m = fs.first_m; 836 if (!is_first_object_locked) 837 VM_OBJECT_LOCK(fs.object); 838 PCPU_INC(cnt.v_cow_faults); 839 } else { 840 prot &= ~VM_PROT_WRITE; 841 } 842 } 843 844 /* 845 * We must verify that the maps have not changed since our last 846 * lookup. 847 */ 848 if (!fs.lookup_still_valid) { 849 vm_object_t retry_object; 850 vm_pindex_t retry_pindex; 851 vm_prot_t retry_prot; 852 853 if (!vm_map_trylock_read(fs.map)) { 854 release_page(&fs); 855 unlock_and_deallocate(&fs); 856 goto RetryFault; 857 } 858 fs.lookup_still_valid = TRUE; 859 if (fs.map->timestamp != map_generation) { 860 result = vm_map_lookup_locked(&fs.map, vaddr, fault_type, 861 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired); 862 863 /* 864 * If we don't need the page any longer, put it on the inactive 865 * list (the easiest thing to do here). If no one needs it, 866 * pageout will grab it eventually. 867 */ 868 if (result != KERN_SUCCESS) { 869 release_page(&fs); 870 unlock_and_deallocate(&fs); 871 872 /* 873 * If retry of map lookup would have blocked then 874 * retry fault from start. 875 */ 876 if (result == KERN_FAILURE) 877 goto RetryFault; 878 return (result); 879 } 880 if ((retry_object != fs.first_object) || 881 (retry_pindex != fs.first_pindex)) { 882 release_page(&fs); 883 unlock_and_deallocate(&fs); 884 goto RetryFault; 885 } 886 887 /* 888 * Check whether the protection has changed or the object has 889 * been copied while we left the map unlocked. Changing from 890 * read to write permission is OK - we leave the page 891 * write-protected, and catch the write fault. Changing from 892 * write to read permission means that we can't mark the page 893 * write-enabled after all. 894 */ 895 prot &= retry_prot; 896 } 897 } 898 /* 899 * If the page was filled by a pager, update the map entry's 900 * last read offset. Since the pager does not return the 901 * actual set of pages that it read, this update is based on 902 * the requested set. Typically, the requested and actual 903 * sets are the same. 904 * 905 * XXX The following assignment modifies the map 906 * without holding a write lock on it. 907 */ 908 if (hardfault) 909 fs.entry->lastr = fs.pindex + faultcount - behind; 910 911 if (prot & VM_PROT_WRITE) { 912 vm_object_set_writeable_dirty(fs.object); 913 914 /* 915 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC 916 * if the page is already dirty to prevent data written with 917 * the expectation of being synced from not being synced. 918 * Likewise if this entry does not request NOSYNC then make 919 * sure the page isn't marked NOSYNC. Applications sharing 920 * data should use the same flags to avoid ping ponging. 921 */ 922 if (fs.entry->eflags & MAP_ENTRY_NOSYNC) { 923 if (fs.m->dirty == 0) 924 fs.m->oflags |= VPO_NOSYNC; 925 } else { 926 fs.m->oflags &= ~VPO_NOSYNC; 927 } 928 929 /* 930 * If the fault is a write, we know that this page is being 931 * written NOW so dirty it explicitly to save on 932 * pmap_is_modified() calls later. 933 * 934 * Also tell the backing pager, if any, that it should remove 935 * any swap backing since the page is now dirty. 936 */ 937 if ((fault_type & VM_PROT_WRITE) != 0 && 938 (fault_flags & VM_FAULT_CHANGE_WIRING) == 0) { 939 vm_page_dirty(fs.m); 940 vm_pager_page_unswapped(fs.m); 941 } 942 } 943 944 /* 945 * Page had better still be busy 946 */ 947 KASSERT(fs.m->oflags & VPO_BUSY, 948 ("vm_fault: page %p not busy!", fs.m)); 949 /* 950 * Page must be completely valid or it is not fit to 951 * map into user space. vm_pager_get_pages() ensures this. 952 */ 953 KASSERT(fs.m->valid == VM_PAGE_BITS_ALL, 954 ("vm_fault: page %p partially invalid", fs.m)); 955 VM_OBJECT_UNLOCK(fs.object); 956 957 /* 958 * Put this page into the physical map. We had to do the unlock above 959 * because pmap_enter() may sleep. We don't put the page 960 * back on the active queue until later so that the pageout daemon 961 * won't find it (yet). 962 */ 963 pmap_enter(fs.map->pmap, vaddr, fault_type, fs.m, prot, wired); 964 if ((fault_flags & VM_FAULT_CHANGE_WIRING) == 0 && wired == 0) 965 vm_fault_prefault(fs.map->pmap, vaddr, fs.entry); 966 VM_OBJECT_LOCK(fs.object); 967 vm_page_lock(fs.m); 968 vm_page_lock_queues(); 969 970 /* 971 * If the page is not wired down, then put it where the pageout daemon 972 * can find it. 973 */ 974 if (fault_flags & VM_FAULT_CHANGE_WIRING) { 975 if (wired) 976 vm_page_wire(fs.m); 977 else 978 vm_page_unwire(fs.m, 1); 979 } else { 980 vm_page_activate(fs.m); 981 } 982 vm_page_unlock_queues(); 983 vm_page_unlock(fs.m); 984 vm_page_wakeup(fs.m); 985 986 /* 987 * Unlock everything, and return 988 */ 989 unlock_and_deallocate(&fs); 990 if (hardfault) 991 curthread->td_ru.ru_majflt++; 992 else 993 curthread->td_ru.ru_minflt++; 994 995 return (KERN_SUCCESS); 996 } 997 998 /* 999 * vm_fault_prefault provides a quick way of clustering 1000 * pagefaults into a processes address space. It is a "cousin" 1001 * of vm_map_pmap_enter, except it runs at page fault time instead 1002 * of mmap time. 1003 */ 1004 static void 1005 vm_fault_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry) 1006 { 1007 int i; 1008 vm_offset_t addr, starta; 1009 vm_pindex_t pindex; 1010 vm_page_t m; 1011 vm_object_t object; 1012 1013 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) 1014 return; 1015 1016 object = entry->object.vm_object; 1017 1018 starta = addra - PFBAK * PAGE_SIZE; 1019 if (starta < entry->start) { 1020 starta = entry->start; 1021 } else if (starta > addra) { 1022 starta = 0; 1023 } 1024 1025 for (i = 0; i < PAGEORDER_SIZE; i++) { 1026 vm_object_t backing_object, lobject; 1027 1028 addr = addra + prefault_pageorder[i]; 1029 if (addr > addra + (PFFOR * PAGE_SIZE)) 1030 addr = 0; 1031 1032 if (addr < starta || addr >= entry->end) 1033 continue; 1034 1035 if (!pmap_is_prefaultable(pmap, addr)) 1036 continue; 1037 1038 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT; 1039 lobject = object; 1040 VM_OBJECT_LOCK(lobject); 1041 while ((m = vm_page_lookup(lobject, pindex)) == NULL && 1042 lobject->type == OBJT_DEFAULT && 1043 (backing_object = lobject->backing_object) != NULL) { 1044 KASSERT((lobject->backing_object_offset & PAGE_MASK) == 1045 0, ("vm_fault_prefault: unaligned object offset")); 1046 pindex += lobject->backing_object_offset >> PAGE_SHIFT; 1047 VM_OBJECT_LOCK(backing_object); 1048 VM_OBJECT_UNLOCK(lobject); 1049 lobject = backing_object; 1050 } 1051 /* 1052 * give-up when a page is not in memory 1053 */ 1054 if (m == NULL) { 1055 VM_OBJECT_UNLOCK(lobject); 1056 break; 1057 } 1058 if (m->valid == VM_PAGE_BITS_ALL && 1059 (m->flags & PG_FICTITIOUS) == 0) { 1060 vm_page_lock(m); 1061 vm_page_lock_queues(); 1062 pmap_enter_quick(pmap, addr, m, entry->protection); 1063 vm_page_unlock_queues(); 1064 vm_page_unlock(m); 1065 } 1066 VM_OBJECT_UNLOCK(lobject); 1067 } 1068 } 1069 1070 /* 1071 * vm_fault_quick: 1072 * 1073 * Ensure that the requested virtual address, which may be in userland, 1074 * is valid. Fault-in the page if necessary. Return -1 on failure. 1075 */ 1076 int 1077 vm_fault_quick(caddr_t v, int prot) 1078 { 1079 int r; 1080 1081 if (prot & VM_PROT_WRITE) 1082 r = subyte(v, fubyte(v)); 1083 else 1084 r = fubyte(v); 1085 return(r); 1086 } 1087 1088 /* 1089 * vm_fault_wire: 1090 * 1091 * Wire down a range of virtual addresses in a map. 1092 */ 1093 int 1094 vm_fault_wire(vm_map_t map, vm_offset_t start, vm_offset_t end, 1095 boolean_t fictitious) 1096 { 1097 vm_offset_t va; 1098 int rv; 1099 1100 /* 1101 * We simulate a fault to get the page and enter it in the physical 1102 * map. For user wiring, we only ask for read access on currently 1103 * read-only sections. 1104 */ 1105 for (va = start; va < end; va += PAGE_SIZE) { 1106 rv = vm_fault(map, va, VM_PROT_NONE, VM_FAULT_CHANGE_WIRING); 1107 if (rv) { 1108 if (va != start) 1109 vm_fault_unwire(map, start, va, fictitious); 1110 return (rv); 1111 } 1112 } 1113 return (KERN_SUCCESS); 1114 } 1115 1116 /* 1117 * vm_fault_unwire: 1118 * 1119 * Unwire a range of virtual addresses in a map. 1120 */ 1121 void 1122 vm_fault_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end, 1123 boolean_t fictitious) 1124 { 1125 vm_paddr_t pa; 1126 vm_offset_t va; 1127 pmap_t pmap; 1128 1129 pmap = vm_map_pmap(map); 1130 1131 /* 1132 * Since the pages are wired down, we must be able to get their 1133 * mappings from the physical map system. 1134 */ 1135 for (va = start; va < end; va += PAGE_SIZE) { 1136 pa = pmap_extract(pmap, va); 1137 if (pa != 0) { 1138 pmap_change_wiring(pmap, va, FALSE); 1139 if (!fictitious) { 1140 vm_page_lock(PHYS_TO_VM_PAGE(pa)); 1141 vm_page_lock_queues(); 1142 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1); 1143 vm_page_unlock_queues(); 1144 vm_page_unlock(PHYS_TO_VM_PAGE(pa)); 1145 } 1146 } 1147 } 1148 } 1149 1150 /* 1151 * Routine: 1152 * vm_fault_copy_entry 1153 * Function: 1154 * Create new shadow object backing dst_entry with private copy of 1155 * all underlying pages. When src_entry is equal to dst_entry, 1156 * function implements COW for wired-down map entry. Otherwise, 1157 * it forks wired entry into dst_map. 1158 * 1159 * In/out conditions: 1160 * The source and destination maps must be locked for write. 1161 * The source map entry must be wired down (or be a sharing map 1162 * entry corresponding to a main map entry that is wired down). 1163 */ 1164 void 1165 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map, 1166 vm_map_entry_t dst_entry, vm_map_entry_t src_entry, 1167 vm_ooffset_t *fork_charge) 1168 { 1169 vm_object_t backing_object, dst_object, object, src_object; 1170 vm_pindex_t dst_pindex, pindex, src_pindex; 1171 vm_prot_t access, prot; 1172 vm_offset_t vaddr; 1173 vm_page_t dst_m; 1174 vm_page_t src_m; 1175 boolean_t src_readonly, upgrade; 1176 1177 #ifdef lint 1178 src_map++; 1179 #endif /* lint */ 1180 1181 upgrade = src_entry == dst_entry; 1182 1183 src_object = src_entry->object.vm_object; 1184 src_pindex = OFF_TO_IDX(src_entry->offset); 1185 src_readonly = (src_entry->protection & VM_PROT_WRITE) == 0; 1186 1187 /* 1188 * Create the top-level object for the destination entry. (Doesn't 1189 * actually shadow anything - we copy the pages directly.) 1190 */ 1191 dst_object = vm_object_allocate(OBJT_DEFAULT, 1192 OFF_TO_IDX(dst_entry->end - dst_entry->start)); 1193 #if VM_NRESERVLEVEL > 0 1194 dst_object->flags |= OBJ_COLORED; 1195 dst_object->pg_color = atop(dst_entry->start); 1196 #endif 1197 1198 VM_OBJECT_LOCK(dst_object); 1199 KASSERT(upgrade || dst_entry->object.vm_object == NULL, 1200 ("vm_fault_copy_entry: vm_object not NULL")); 1201 dst_entry->object.vm_object = dst_object; 1202 dst_entry->offset = 0; 1203 dst_object->charge = dst_entry->end - dst_entry->start; 1204 if (fork_charge != NULL) { 1205 KASSERT(dst_entry->uip == NULL, 1206 ("vm_fault_copy_entry: leaked swp charge")); 1207 dst_object->uip = curthread->td_ucred->cr_ruidinfo; 1208 uihold(dst_object->uip); 1209 *fork_charge += dst_object->charge; 1210 } else { 1211 dst_object->uip = dst_entry->uip; 1212 dst_entry->uip = NULL; 1213 } 1214 access = prot = dst_entry->protection; 1215 /* 1216 * If not an upgrade, then enter the mappings in the pmap as 1217 * read and/or execute accesses. Otherwise, enter them as 1218 * write accesses. 1219 * 1220 * A writeable large page mapping is only created if all of 1221 * the constituent small page mappings are modified. Marking 1222 * PTEs as modified on inception allows promotion to happen 1223 * without taking potentially large number of soft faults. 1224 */ 1225 if (!upgrade) 1226 access &= ~VM_PROT_WRITE; 1227 1228 /* 1229 * Loop through all of the pages in the entry's range, copying each 1230 * one from the source object (it should be there) to the destination 1231 * object. 1232 */ 1233 for (vaddr = dst_entry->start, dst_pindex = 0; 1234 vaddr < dst_entry->end; 1235 vaddr += PAGE_SIZE, dst_pindex++) { 1236 1237 /* 1238 * Allocate a page in the destination object. 1239 */ 1240 do { 1241 dst_m = vm_page_alloc(dst_object, dst_pindex, 1242 VM_ALLOC_NORMAL); 1243 if (dst_m == NULL) { 1244 VM_OBJECT_UNLOCK(dst_object); 1245 VM_WAIT; 1246 VM_OBJECT_LOCK(dst_object); 1247 } 1248 } while (dst_m == NULL); 1249 1250 /* 1251 * Find the page in the source object, and copy it in. 1252 * (Because the source is wired down, the page will be in 1253 * memory.) 1254 */ 1255 VM_OBJECT_LOCK(src_object); 1256 object = src_object; 1257 pindex = src_pindex + dst_pindex; 1258 while ((src_m = vm_page_lookup(object, pindex)) == NULL && 1259 src_readonly && 1260 (backing_object = object->backing_object) != NULL) { 1261 /* 1262 * Allow fallback to backing objects if we are reading. 1263 */ 1264 VM_OBJECT_LOCK(backing_object); 1265 pindex += OFF_TO_IDX(object->backing_object_offset); 1266 VM_OBJECT_UNLOCK(object); 1267 object = backing_object; 1268 } 1269 if (src_m == NULL) 1270 panic("vm_fault_copy_wired: page missing"); 1271 pmap_copy_page(src_m, dst_m); 1272 VM_OBJECT_UNLOCK(object); 1273 dst_m->valid = VM_PAGE_BITS_ALL; 1274 VM_OBJECT_UNLOCK(dst_object); 1275 1276 /* 1277 * Enter it in the pmap. If a wired, copy-on-write 1278 * mapping is being replaced by a write-enabled 1279 * mapping, then wire that new mapping. 1280 */ 1281 pmap_enter(dst_map->pmap, vaddr, access, dst_m, prot, upgrade); 1282 1283 /* 1284 * Mark it no longer busy, and put it on the active list. 1285 */ 1286 VM_OBJECT_LOCK(dst_object); 1287 1288 if (upgrade) { 1289 vm_page_lock(src_m); 1290 vm_page_lock_queues(); 1291 vm_page_unwire(src_m, 0); 1292 vm_page_unlock_queues(); 1293 vm_page_unlock(src_m); 1294 1295 vm_page_lock(dst_m); 1296 vm_page_lock_queues(); 1297 vm_page_wire(dst_m); 1298 vm_page_unlock_queues(); 1299 vm_page_unlock(dst_m); 1300 } else { 1301 vm_page_lock(dst_m); 1302 vm_page_lock_queues(); 1303 vm_page_activate(dst_m); 1304 vm_page_unlock_queues(); 1305 vm_page_unlock(dst_m); 1306 } 1307 vm_page_wakeup(dst_m); 1308 } 1309 VM_OBJECT_UNLOCK(dst_object); 1310 if (upgrade) { 1311 dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY); 1312 vm_object_deallocate(src_object); 1313 } 1314 } 1315 1316 1317 /* 1318 * This routine checks around the requested page for other pages that 1319 * might be able to be faulted in. This routine brackets the viable 1320 * pages for the pages to be paged in. 1321 * 1322 * Inputs: 1323 * m, rbehind, rahead 1324 * 1325 * Outputs: 1326 * marray (array of vm_page_t), reqpage (index of requested page) 1327 * 1328 * Return value: 1329 * number of pages in marray 1330 */ 1331 static int 1332 vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage) 1333 vm_page_t m; 1334 int rbehind; 1335 int rahead; 1336 vm_page_t *marray; 1337 int *reqpage; 1338 { 1339 int i,j; 1340 vm_object_t object; 1341 vm_pindex_t pindex, startpindex, endpindex, tpindex; 1342 vm_page_t rtm; 1343 int cbehind, cahead; 1344 1345 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1346 1347 object = m->object; 1348 pindex = m->pindex; 1349 cbehind = cahead = 0; 1350 1351 /* 1352 * if the requested page is not available, then give up now 1353 */ 1354 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) { 1355 return 0; 1356 } 1357 1358 if ((cbehind == 0) && (cahead == 0)) { 1359 *reqpage = 0; 1360 marray[0] = m; 1361 return 1; 1362 } 1363 1364 if (rahead > cahead) { 1365 rahead = cahead; 1366 } 1367 1368 if (rbehind > cbehind) { 1369 rbehind = cbehind; 1370 } 1371 1372 /* 1373 * scan backward for the read behind pages -- in memory 1374 */ 1375 if (pindex > 0) { 1376 if (rbehind > pindex) { 1377 rbehind = pindex; 1378 startpindex = 0; 1379 } else { 1380 startpindex = pindex - rbehind; 1381 } 1382 1383 if ((rtm = TAILQ_PREV(m, pglist, listq)) != NULL && 1384 rtm->pindex >= startpindex) 1385 startpindex = rtm->pindex + 1; 1386 1387 /* tpindex is unsigned; beware of numeric underflow. */ 1388 for (i = 0, tpindex = pindex - 1; tpindex >= startpindex && 1389 tpindex < pindex; i++, tpindex--) { 1390 1391 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL | 1392 VM_ALLOC_IFNOTCACHED); 1393 if (rtm == NULL) { 1394 /* 1395 * Shift the allocated pages to the 1396 * beginning of the array. 1397 */ 1398 for (j = 0; j < i; j++) { 1399 marray[j] = marray[j + tpindex + 1 - 1400 startpindex]; 1401 } 1402 break; 1403 } 1404 1405 marray[tpindex - startpindex] = rtm; 1406 } 1407 } else { 1408 startpindex = 0; 1409 i = 0; 1410 } 1411 1412 marray[i] = m; 1413 /* page offset of the required page */ 1414 *reqpage = i; 1415 1416 tpindex = pindex + 1; 1417 i++; 1418 1419 /* 1420 * scan forward for the read ahead pages 1421 */ 1422 endpindex = tpindex + rahead; 1423 if ((rtm = TAILQ_NEXT(m, listq)) != NULL && rtm->pindex < endpindex) 1424 endpindex = rtm->pindex; 1425 if (endpindex > object->size) 1426 endpindex = object->size; 1427 1428 for (; tpindex < endpindex; i++, tpindex++) { 1429 1430 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL | 1431 VM_ALLOC_IFNOTCACHED); 1432 if (rtm == NULL) { 1433 break; 1434 } 1435 1436 marray[i] = rtm; 1437 } 1438 1439 /* return number of pages */ 1440 return i; 1441 } 1442