1 /*- 2 * SPDX-License-Identifier: (BSD-4-Clause AND MIT-CMU) 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 * 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 "opt_ktrace.h" 75 #include "opt_vm.h" 76 77 #include <sys/systm.h> 78 #include <sys/kernel.h> 79 #include <sys/lock.h> 80 #include <sys/mman.h> 81 #include <sys/mutex.h> 82 #include <sys/pctrie.h> 83 #include <sys/proc.h> 84 #include <sys/racct.h> 85 #include <sys/refcount.h> 86 #include <sys/resourcevar.h> 87 #include <sys/rwlock.h> 88 #include <sys/sched.h> 89 #include <sys/sf_buf.h> 90 #include <sys/signalvar.h> 91 #include <sys/sysctl.h> 92 #include <sys/sysent.h> 93 #include <sys/vmmeter.h> 94 #include <sys/vnode.h> 95 #ifdef KTRACE 96 #include <sys/ktrace.h> 97 #endif 98 99 #include <vm/vm.h> 100 #include <vm/vm_param.h> 101 #include <vm/pmap.h> 102 #include <vm/vm_map.h> 103 #include <vm/vm_object.h> 104 #include <vm/vm_page.h> 105 #include <vm/vm_pageout.h> 106 #include <vm/vm_kern.h> 107 #include <vm/vm_pager.h> 108 #include <vm/vm_radix.h> 109 #include <vm/vm_extern.h> 110 #include <vm/vm_reserv.h> 111 112 #define PFBAK 4 113 #define PFFOR 4 114 115 #define VM_FAULT_READ_DEFAULT (1 + VM_FAULT_READ_AHEAD_INIT) 116 117 #define VM_FAULT_DONTNEED_MIN 1048576 118 119 struct faultstate { 120 /* Fault parameters. */ 121 vm_offset_t vaddr; 122 vm_page_t *m_hold; 123 vm_prot_t fault_type; 124 vm_prot_t prot; 125 int fault_flags; 126 boolean_t wired; 127 128 /* Control state. */ 129 struct timeval oom_start_time; 130 bool oom_started; 131 int nera; 132 bool can_read_lock; 133 134 /* Page reference for cow. */ 135 vm_page_t m_cow; 136 137 /* Current object. */ 138 vm_object_t object; 139 vm_pindex_t pindex; 140 vm_page_t m; 141 bool m_needs_zeroing; 142 143 /* Top-level map object. */ 144 vm_object_t first_object; 145 vm_pindex_t first_pindex; 146 vm_page_t first_m; 147 148 /* Map state. */ 149 vm_map_t map; 150 vm_map_entry_t entry; 151 int map_generation; 152 bool lookup_still_valid; 153 154 /* Vnode if locked. */ 155 struct vnode *vp; 156 }; 157 158 /* 159 * Return codes for internal fault routines. 160 */ 161 enum fault_status { 162 FAULT_SUCCESS = 10000, /* Return success to user. */ 163 FAULT_FAILURE, /* Return failure to user. */ 164 FAULT_CONTINUE, /* Continue faulting. */ 165 FAULT_RESTART, /* Restart fault. */ 166 FAULT_OUT_OF_BOUNDS, /* Invalid address for pager. */ 167 FAULT_HARD, /* Performed I/O. */ 168 FAULT_SOFT, /* Found valid page. */ 169 FAULT_PROTECTION_FAILURE, /* Invalid access. */ 170 }; 171 172 enum fault_next_status { 173 FAULT_NEXT_GOTOBJ = 1, 174 FAULT_NEXT_NOOBJ, 175 FAULT_NEXT_RESTART, 176 }; 177 178 static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, 179 int ahead); 180 static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra, 181 int backward, int forward, bool obj_locked); 182 183 static int vm_pfault_oom_attempts = 3; 184 SYSCTL_INT(_vm, OID_AUTO, pfault_oom_attempts, CTLFLAG_RWTUN, 185 &vm_pfault_oom_attempts, 0, 186 "Number of page allocation attempts in page fault handler before it " 187 "triggers OOM handling"); 188 189 static int vm_pfault_oom_wait = 10; 190 SYSCTL_INT(_vm, OID_AUTO, pfault_oom_wait, CTLFLAG_RWTUN, 191 &vm_pfault_oom_wait, 0, 192 "Number of seconds to wait for free pages before retrying " 193 "the page fault handler"); 194 195 static inline void 196 vm_fault_page_release(vm_page_t *mp) 197 { 198 vm_page_t m; 199 200 m = *mp; 201 if (m != NULL) { 202 /* 203 * We are likely to loop around again and attempt to busy 204 * this page. Deactivating it leaves it available for 205 * pageout while optimizing fault restarts. 206 */ 207 vm_page_deactivate(m); 208 if (vm_page_xbusied(m)) 209 vm_page_xunbusy(m); 210 else 211 vm_page_sunbusy(m); 212 *mp = NULL; 213 } 214 } 215 216 static inline void 217 vm_fault_page_free(vm_page_t *mp) 218 { 219 vm_page_t m; 220 221 m = *mp; 222 if (m != NULL) { 223 VM_OBJECT_ASSERT_WLOCKED(m->object); 224 if (!vm_page_wired(m)) 225 vm_page_free(m); 226 else 227 vm_page_xunbusy(m); 228 *mp = NULL; 229 } 230 } 231 232 /* 233 * Return true if a vm_pager_get_pages() call is needed in order to check 234 * whether the pager might have a particular page, false if it can be determined 235 * immediately that the pager can not have a copy. For swap objects, this can 236 * be checked quickly. 237 */ 238 static inline bool 239 vm_fault_object_needs_getpages(vm_object_t object) 240 { 241 VM_OBJECT_ASSERT_LOCKED(object); 242 243 return ((object->flags & OBJ_SWAP) == 0 || 244 !pctrie_is_empty(&object->un_pager.swp.swp_blks)); 245 } 246 247 static inline void 248 vm_fault_unlock_map(struct faultstate *fs) 249 { 250 251 if (fs->lookup_still_valid) { 252 vm_map_lookup_done(fs->map, fs->entry); 253 fs->lookup_still_valid = false; 254 } 255 } 256 257 static void 258 vm_fault_unlock_vp(struct faultstate *fs) 259 { 260 261 if (fs->vp != NULL) { 262 vput(fs->vp); 263 fs->vp = NULL; 264 } 265 } 266 267 static bool 268 vm_fault_might_be_cow(struct faultstate *fs) 269 { 270 return (fs->object != fs->first_object); 271 } 272 273 static void 274 vm_fault_deallocate(struct faultstate *fs) 275 { 276 vm_fault_page_release(&fs->m_cow); 277 vm_fault_page_release(&fs->m); 278 vm_object_pip_wakeup(fs->object); 279 if (vm_fault_might_be_cow(fs)) { 280 VM_OBJECT_WLOCK(fs->first_object); 281 vm_fault_page_free(&fs->first_m); 282 VM_OBJECT_WUNLOCK(fs->first_object); 283 vm_object_pip_wakeup(fs->first_object); 284 } 285 vm_object_deallocate(fs->first_object); 286 vm_fault_unlock_map(fs); 287 vm_fault_unlock_vp(fs); 288 } 289 290 static void 291 vm_fault_unlock_and_deallocate(struct faultstate *fs) 292 { 293 294 VM_OBJECT_UNLOCK(fs->object); 295 vm_fault_deallocate(fs); 296 } 297 298 static void 299 vm_fault_dirty(struct faultstate *fs, vm_page_t m) 300 { 301 bool need_dirty; 302 303 if (((fs->prot & VM_PROT_WRITE) == 0 && 304 (fs->fault_flags & VM_FAULT_DIRTY) == 0) || 305 (m->oflags & VPO_UNMANAGED) != 0) 306 return; 307 308 VM_PAGE_OBJECT_BUSY_ASSERT(m); 309 310 need_dirty = ((fs->fault_type & VM_PROT_WRITE) != 0 && 311 (fs->fault_flags & VM_FAULT_WIRE) == 0) || 312 (fs->fault_flags & VM_FAULT_DIRTY) != 0; 313 314 vm_object_set_writeable_dirty(m->object); 315 316 /* 317 * If the fault is a write, we know that this page is being 318 * written NOW so dirty it explicitly to save on 319 * pmap_is_modified() calls later. 320 * 321 * Also, since the page is now dirty, we can possibly tell 322 * the pager to release any swap backing the page. 323 */ 324 if (need_dirty && vm_page_set_dirty(m) == 0) { 325 /* 326 * If this is a NOSYNC mmap we do not want to set PGA_NOSYNC 327 * if the page is already dirty to prevent data written with 328 * the expectation of being synced from not being synced. 329 * Likewise if this entry does not request NOSYNC then make 330 * sure the page isn't marked NOSYNC. Applications sharing 331 * data should use the same flags to avoid ping ponging. 332 */ 333 if ((fs->entry->eflags & MAP_ENTRY_NOSYNC) != 0) 334 vm_page_aflag_set(m, PGA_NOSYNC); 335 else 336 vm_page_aflag_clear(m, PGA_NOSYNC); 337 } 338 339 } 340 341 static bool 342 vm_fault_is_read(const struct faultstate *fs) 343 { 344 return ((fs->prot & VM_PROT_WRITE) == 0 && 345 (fs->fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) == 0); 346 } 347 348 /* 349 * Unlocks fs.first_object and fs.map on success. 350 */ 351 static enum fault_status 352 vm_fault_soft_fast(struct faultstate *fs) 353 { 354 vm_page_t m, m_map; 355 #if VM_NRESERVLEVEL > 0 356 vm_page_t m_super; 357 int flags; 358 #endif 359 int psind; 360 vm_offset_t vaddr; 361 362 MPASS(fs->vp == NULL); 363 364 /* 365 * If we fail, vast majority of the time it is because the page is not 366 * there to begin with. Opportunistically perform the lookup and 367 * subsequent checks without the object lock, revalidate later. 368 * 369 * Note: a busy page can be mapped for read|execute access. 370 */ 371 m = vm_page_lookup_unlocked(fs->first_object, fs->first_pindex); 372 if (m == NULL || !vm_page_all_valid(m) || 373 ((fs->prot & VM_PROT_WRITE) != 0 && vm_page_busied(m))) { 374 VM_OBJECT_WLOCK(fs->first_object); 375 return (FAULT_FAILURE); 376 } 377 378 vaddr = fs->vaddr; 379 380 VM_OBJECT_RLOCK(fs->first_object); 381 382 /* 383 * Now that we stabilized the state, revalidate the page is in the shape 384 * we encountered above. 385 */ 386 387 if (m->object != fs->first_object || m->pindex != fs->first_pindex) 388 goto fail; 389 390 vm_object_busy(fs->first_object); 391 392 if (!vm_page_all_valid(m) || 393 ((fs->prot & VM_PROT_WRITE) != 0 && vm_page_busied(m))) 394 goto fail_busy; 395 396 m_map = m; 397 psind = 0; 398 #if VM_NRESERVLEVEL > 0 399 if ((m->flags & PG_FICTITIOUS) == 0 && 400 (m_super = vm_reserv_to_superpage(m)) != NULL) { 401 psind = m_super->psind; 402 KASSERT(psind > 0, 403 ("psind %d of m_super %p < 1", psind, m_super)); 404 flags = PS_ALL_VALID; 405 if ((fs->prot & VM_PROT_WRITE) != 0) { 406 /* 407 * Create a superpage mapping allowing write access 408 * only if none of the constituent pages are busy and 409 * all of them are already dirty (except possibly for 410 * the page that was faulted on). 411 */ 412 flags |= PS_NONE_BUSY; 413 if ((fs->first_object->flags & OBJ_UNMANAGED) == 0) 414 flags |= PS_ALL_DIRTY; 415 } 416 while (rounddown2(vaddr, pagesizes[psind]) < fs->entry->start || 417 roundup2(vaddr + 1, pagesizes[psind]) > fs->entry->end || 418 (vaddr & (pagesizes[psind] - 1)) != 419 (VM_PAGE_TO_PHYS(m) & (pagesizes[psind] - 1)) || 420 !vm_page_ps_test(m_super, psind, flags, m) || 421 !pmap_ps_enabled(fs->map->pmap)) { 422 psind--; 423 if (psind == 0) 424 break; 425 m_super += rounddown2(m - m_super, 426 atop(pagesizes[psind])); 427 KASSERT(m_super->psind >= psind, 428 ("psind %d of m_super %p < %d", m_super->psind, 429 m_super, psind)); 430 } 431 if (psind > 0) { 432 m_map = m_super; 433 vaddr = rounddown2(vaddr, pagesizes[psind]); 434 /* Preset the modified bit for dirty superpages. */ 435 if ((flags & PS_ALL_DIRTY) != 0) 436 fs->fault_type |= VM_PROT_WRITE; 437 } 438 } 439 #endif 440 if (pmap_enter(fs->map->pmap, vaddr, m_map, fs->prot, fs->fault_type | 441 PMAP_ENTER_NOSLEEP | (fs->wired ? PMAP_ENTER_WIRED : 0), psind) != 442 KERN_SUCCESS) 443 goto fail_busy; 444 if (fs->m_hold != NULL) { 445 (*fs->m_hold) = m; 446 vm_page_wire(m); 447 } 448 if (psind == 0 && !fs->wired) 449 vm_fault_prefault(fs, vaddr, PFBAK, PFFOR, true); 450 VM_OBJECT_RUNLOCK(fs->first_object); 451 vm_fault_dirty(fs, m); 452 vm_object_unbusy(fs->first_object); 453 vm_map_lookup_done(fs->map, fs->entry); 454 curthread->td_ru.ru_minflt++; 455 return (FAULT_SUCCESS); 456 fail_busy: 457 vm_object_unbusy(fs->first_object); 458 fail: 459 if (!VM_OBJECT_TRYUPGRADE(fs->first_object)) { 460 VM_OBJECT_RUNLOCK(fs->first_object); 461 VM_OBJECT_WLOCK(fs->first_object); 462 } 463 return (FAULT_FAILURE); 464 } 465 466 static void 467 vm_fault_restore_map_lock(struct faultstate *fs) 468 { 469 470 VM_OBJECT_ASSERT_WLOCKED(fs->first_object); 471 MPASS(blockcount_read(&fs->first_object->paging_in_progress) > 0); 472 473 if (!vm_map_trylock_read(fs->map)) { 474 VM_OBJECT_WUNLOCK(fs->first_object); 475 vm_map_lock_read(fs->map); 476 VM_OBJECT_WLOCK(fs->first_object); 477 } 478 fs->lookup_still_valid = true; 479 } 480 481 static void 482 vm_fault_populate_check_page(vm_page_t m) 483 { 484 485 /* 486 * Check each page to ensure that the pager is obeying the 487 * interface: the page must be installed in the object, fully 488 * valid, and exclusively busied. 489 */ 490 MPASS(m != NULL); 491 MPASS(vm_page_all_valid(m)); 492 MPASS(vm_page_xbusied(m)); 493 } 494 495 static void 496 vm_fault_populate_cleanup(vm_object_t object, vm_pindex_t first, 497 vm_pindex_t last) 498 { 499 struct pctrie_iter pages; 500 vm_page_t m; 501 502 VM_OBJECT_ASSERT_WLOCKED(object); 503 MPASS(first <= last); 504 vm_page_iter_limit_init(&pages, object, last + 1); 505 VM_RADIX_FORALL_FROM(m, &pages, first) { 506 vm_fault_populate_check_page(m); 507 vm_page_deactivate(m); 508 vm_page_xunbusy(m); 509 } 510 KASSERT(pages.index == last, 511 ("%s: Object %p first %#jx last %#jx index %#jx", 512 __func__, object, (uintmax_t)first, (uintmax_t)last, 513 (uintmax_t)pages.index)); 514 } 515 516 static enum fault_status 517 vm_fault_populate(struct faultstate *fs) 518 { 519 vm_offset_t vaddr; 520 vm_page_t m; 521 vm_pindex_t map_first, map_last, pager_first, pager_last, pidx; 522 int bdry_idx, i, npages, psind, rv; 523 enum fault_status res; 524 525 MPASS(fs->object == fs->first_object); 526 VM_OBJECT_ASSERT_WLOCKED(fs->first_object); 527 MPASS(blockcount_read(&fs->first_object->paging_in_progress) > 0); 528 MPASS(fs->first_object->backing_object == NULL); 529 MPASS(fs->lookup_still_valid); 530 531 pager_first = OFF_TO_IDX(fs->entry->offset); 532 pager_last = pager_first + atop(fs->entry->end - fs->entry->start) - 1; 533 vm_fault_unlock_map(fs); 534 vm_fault_unlock_vp(fs); 535 536 res = FAULT_SUCCESS; 537 538 /* 539 * Call the pager (driver) populate() method. 540 * 541 * There is no guarantee that the method will be called again 542 * if the current fault is for read, and a future fault is 543 * for write. Report the entry's maximum allowed protection 544 * to the driver. 545 */ 546 rv = vm_pager_populate(fs->first_object, fs->first_pindex, 547 fs->fault_type, fs->entry->max_protection, &pager_first, 548 &pager_last); 549 550 VM_OBJECT_ASSERT_WLOCKED(fs->first_object); 551 if (rv == VM_PAGER_BAD) { 552 /* 553 * VM_PAGER_BAD is the backdoor for a pager to request 554 * normal fault handling. 555 */ 556 vm_fault_restore_map_lock(fs); 557 if (fs->map->timestamp != fs->map_generation) 558 return (FAULT_RESTART); 559 return (FAULT_CONTINUE); 560 } 561 if (rv != VM_PAGER_OK) 562 return (FAULT_FAILURE); /* AKA SIGSEGV */ 563 564 /* Ensure that the driver is obeying the interface. */ 565 MPASS(pager_first <= pager_last); 566 MPASS(fs->first_pindex <= pager_last); 567 MPASS(fs->first_pindex >= pager_first); 568 MPASS(pager_last < fs->first_object->size); 569 570 vm_fault_restore_map_lock(fs); 571 bdry_idx = MAP_ENTRY_SPLIT_BOUNDARY_INDEX(fs->entry); 572 if (fs->map->timestamp != fs->map_generation) { 573 if (bdry_idx == 0) { 574 vm_fault_populate_cleanup(fs->first_object, pager_first, 575 pager_last); 576 } else { 577 m = vm_page_lookup(fs->first_object, pager_first); 578 if (m != fs->m) 579 vm_page_xunbusy(m); 580 } 581 return (FAULT_RESTART); 582 } 583 584 /* 585 * The map is unchanged after our last unlock. Process the fault. 586 * 587 * First, the special case of largepage mappings, where 588 * populate only busies the first page in superpage run. 589 */ 590 if (bdry_idx != 0) { 591 KASSERT(PMAP_HAS_LARGEPAGES, 592 ("missing pmap support for large pages")); 593 m = vm_page_lookup(fs->first_object, pager_first); 594 vm_fault_populate_check_page(m); 595 VM_OBJECT_WUNLOCK(fs->first_object); 596 vaddr = fs->entry->start + IDX_TO_OFF(pager_first) - 597 fs->entry->offset; 598 /* assert alignment for entry */ 599 KASSERT((vaddr & (pagesizes[bdry_idx] - 1)) == 0, 600 ("unaligned superpage start %#jx pager_first %#jx offset %#jx vaddr %#jx", 601 (uintmax_t)fs->entry->start, (uintmax_t)pager_first, 602 (uintmax_t)fs->entry->offset, (uintmax_t)vaddr)); 603 KASSERT((VM_PAGE_TO_PHYS(m) & (pagesizes[bdry_idx] - 1)) == 0, 604 ("unaligned superpage m %p %#jx", m, 605 (uintmax_t)VM_PAGE_TO_PHYS(m))); 606 rv = pmap_enter(fs->map->pmap, vaddr, m, fs->prot, 607 fs->fault_type | (fs->wired ? PMAP_ENTER_WIRED : 0) | 608 PMAP_ENTER_LARGEPAGE, bdry_idx); 609 VM_OBJECT_WLOCK(fs->first_object); 610 vm_page_xunbusy(m); 611 if (rv != KERN_SUCCESS) { 612 res = FAULT_FAILURE; 613 goto out; 614 } 615 if ((fs->fault_flags & VM_FAULT_WIRE) != 0) { 616 for (i = 0; i < atop(pagesizes[bdry_idx]); i++) 617 vm_page_wire(m + i); 618 } 619 if (fs->m_hold != NULL) { 620 *fs->m_hold = m + (fs->first_pindex - pager_first); 621 vm_page_wire(*fs->m_hold); 622 } 623 goto out; 624 } 625 626 /* 627 * The range [pager_first, pager_last] that is given to the 628 * pager is only a hint. The pager may populate any range 629 * within the object that includes the requested page index. 630 * In case the pager expanded the range, clip it to fit into 631 * the map entry. 632 */ 633 map_first = OFF_TO_IDX(fs->entry->offset); 634 if (map_first > pager_first) { 635 vm_fault_populate_cleanup(fs->first_object, pager_first, 636 map_first - 1); 637 pager_first = map_first; 638 } 639 map_last = map_first + atop(fs->entry->end - fs->entry->start) - 1; 640 if (map_last < pager_last) { 641 vm_fault_populate_cleanup(fs->first_object, map_last + 1, 642 pager_last); 643 pager_last = map_last; 644 } 645 for (pidx = pager_first; pidx <= pager_last; pidx += npages) { 646 bool writeable; 647 648 m = vm_page_lookup(fs->first_object, pidx); 649 vaddr = fs->entry->start + IDX_TO_OFF(pidx) - fs->entry->offset; 650 KASSERT(m != NULL && m->pindex == pidx, 651 ("%s: pindex mismatch", __func__)); 652 psind = m->psind; 653 while (psind > 0 && ((vaddr & (pagesizes[psind] - 1)) != 0 || 654 pidx + OFF_TO_IDX(pagesizes[psind]) - 1 > pager_last || 655 !pmap_ps_enabled(fs->map->pmap))) 656 psind--; 657 658 writeable = (fs->prot & VM_PROT_WRITE) != 0; 659 npages = atop(pagesizes[psind]); 660 for (i = 0; i < npages; i++) { 661 vm_fault_populate_check_page(&m[i]); 662 vm_fault_dirty(fs, &m[i]); 663 664 /* 665 * If this is a writeable superpage mapping, all 666 * constituent pages and the new mapping should be 667 * dirty, otherwise the mapping should be read-only. 668 */ 669 if (writeable && psind > 0 && 670 (m[i].oflags & VPO_UNMANAGED) == 0 && 671 m[i].dirty != VM_PAGE_BITS_ALL) 672 writeable = false; 673 } 674 if (psind > 0 && writeable) 675 fs->fault_type |= VM_PROT_WRITE; 676 VM_OBJECT_WUNLOCK(fs->first_object); 677 rv = pmap_enter(fs->map->pmap, vaddr, m, 678 fs->prot & ~(writeable ? 0 : VM_PROT_WRITE), 679 fs->fault_type | (fs->wired ? PMAP_ENTER_WIRED : 0), psind); 680 681 /* 682 * pmap_enter() may fail for a superpage mapping if additional 683 * protection policies prevent the full mapping. 684 * For example, this will happen on amd64 if the entire 685 * address range does not share the same userspace protection 686 * key. Revert to single-page mappings if this happens. 687 */ 688 MPASS(rv == KERN_SUCCESS || 689 (psind > 0 && rv == KERN_PROTECTION_FAILURE)); 690 if (__predict_false(psind > 0 && 691 rv == KERN_PROTECTION_FAILURE)) { 692 MPASS(!fs->wired); 693 for (i = 0; i < npages; i++) { 694 rv = pmap_enter(fs->map->pmap, vaddr + ptoa(i), 695 &m[i], fs->prot, fs->fault_type, 0); 696 MPASS(rv == KERN_SUCCESS); 697 } 698 } 699 700 VM_OBJECT_WLOCK(fs->first_object); 701 for (i = 0; i < npages; i++) { 702 if ((fs->fault_flags & VM_FAULT_WIRE) != 0 && 703 m[i].pindex == fs->first_pindex) 704 vm_page_wire(&m[i]); 705 else 706 vm_page_activate(&m[i]); 707 if (fs->m_hold != NULL && 708 m[i].pindex == fs->first_pindex) { 709 (*fs->m_hold) = &m[i]; 710 vm_page_wire(&m[i]); 711 } 712 vm_page_xunbusy(&m[i]); 713 } 714 } 715 out: 716 curthread->td_ru.ru_majflt++; 717 return (res); 718 } 719 720 static int prot_fault_translation; 721 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RWTUN, 722 &prot_fault_translation, 0, 723 "Control signal to deliver on protection fault"); 724 725 /* compat definition to keep common code for signal translation */ 726 #define UCODE_PAGEFLT 12 727 #ifdef T_PAGEFLT 728 _Static_assert(UCODE_PAGEFLT == T_PAGEFLT, "T_PAGEFLT"); 729 #endif 730 731 /* 732 * vm_fault_trap: 733 * 734 * Helper for the machine-dependent page fault trap handlers, wrapping 735 * vm_fault(). Issues ktrace(2) tracepoints for the faults. 736 * 737 * If the fault cannot be handled successfully by updating the 738 * required mapping, and the faulted instruction cannot be restarted, 739 * the signal number and si_code values are returned for trapsignal() 740 * to deliver. 741 * 742 * Returns Mach error codes, but callers should only check for 743 * KERN_SUCCESS. 744 */ 745 int 746 vm_fault_trap(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 747 int fault_flags, int *signo, int *ucode) 748 { 749 int result; 750 751 MPASS(signo == NULL || ucode != NULL); 752 #ifdef KTRACE 753 if (map != kernel_map && KTRPOINT(curthread, KTR_FAULT)) 754 ktrfault(vaddr, fault_type); 755 #endif 756 result = vm_fault(map, trunc_page(vaddr), fault_type, fault_flags, 757 NULL); 758 KASSERT(result == KERN_SUCCESS || result == KERN_FAILURE || 759 result == KERN_INVALID_ADDRESS || 760 result == KERN_RESOURCE_SHORTAGE || 761 result == KERN_PROTECTION_FAILURE || 762 result == KERN_OUT_OF_BOUNDS, 763 ("Unexpected Mach error %d from vm_fault()", result)); 764 #ifdef KTRACE 765 if (map != kernel_map && KTRPOINT(curthread, KTR_FAULTEND)) 766 ktrfaultend(result); 767 #endif 768 if (result != KERN_SUCCESS && signo != NULL) { 769 switch (result) { 770 case KERN_FAILURE: 771 case KERN_INVALID_ADDRESS: 772 *signo = SIGSEGV; 773 *ucode = SEGV_MAPERR; 774 break; 775 case KERN_RESOURCE_SHORTAGE: 776 *signo = SIGBUS; 777 *ucode = BUS_OOMERR; 778 break; 779 case KERN_OUT_OF_BOUNDS: 780 *signo = SIGBUS; 781 *ucode = BUS_OBJERR; 782 break; 783 case KERN_PROTECTION_FAILURE: 784 if (prot_fault_translation == 0) { 785 /* 786 * Autodetect. This check also covers 787 * the images without the ABI-tag ELF 788 * note. 789 */ 790 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD && 791 curproc->p_osrel >= P_OSREL_SIGSEGV) { 792 *signo = SIGSEGV; 793 *ucode = SEGV_ACCERR; 794 } else { 795 *signo = SIGBUS; 796 *ucode = UCODE_PAGEFLT; 797 } 798 } else if (prot_fault_translation == 1) { 799 /* Always compat mode. */ 800 *signo = SIGBUS; 801 *ucode = UCODE_PAGEFLT; 802 } else { 803 /* Always SIGSEGV mode. */ 804 *signo = SIGSEGV; 805 *ucode = SEGV_ACCERR; 806 } 807 break; 808 default: 809 KASSERT(0, ("Unexpected Mach error %d from vm_fault()", 810 result)); 811 break; 812 } 813 } 814 return (result); 815 } 816 817 static bool 818 vm_fault_object_ensure_wlocked(struct faultstate *fs) 819 { 820 if (fs->object == fs->first_object) 821 VM_OBJECT_ASSERT_WLOCKED(fs->object); 822 823 if (!fs->can_read_lock) { 824 VM_OBJECT_ASSERT_WLOCKED(fs->object); 825 return (true); 826 } 827 828 if (VM_OBJECT_WOWNED(fs->object)) 829 return (true); 830 831 if (VM_OBJECT_TRYUPGRADE(fs->object)) 832 return (true); 833 834 return (false); 835 } 836 837 static enum fault_status 838 vm_fault_lock_vnode(struct faultstate *fs, bool objlocked) 839 { 840 struct vnode *vp; 841 int error, locked; 842 843 if (fs->object->type != OBJT_VNODE) 844 return (FAULT_CONTINUE); 845 vp = fs->object->handle; 846 if (vp == fs->vp) { 847 ASSERT_VOP_LOCKED(vp, "saved vnode is not locked"); 848 return (FAULT_CONTINUE); 849 } 850 851 /* 852 * Perform an unlock in case the desired vnode changed while 853 * the map was unlocked during a retry. 854 */ 855 vm_fault_unlock_vp(fs); 856 857 locked = VOP_ISLOCKED(vp); 858 if (locked != LK_EXCLUSIVE) 859 locked = LK_SHARED; 860 861 /* 862 * We must not sleep acquiring the vnode lock while we have 863 * the page exclusive busied or the object's 864 * paging-in-progress count incremented. Otherwise, we could 865 * deadlock. 866 */ 867 error = vget(vp, locked | LK_CANRECURSE | LK_NOWAIT); 868 if (error == 0) { 869 fs->vp = vp; 870 return (FAULT_CONTINUE); 871 } 872 873 vhold(vp); 874 if (objlocked) 875 vm_fault_unlock_and_deallocate(fs); 876 else 877 vm_fault_deallocate(fs); 878 error = vget(vp, locked | LK_RETRY | LK_CANRECURSE); 879 vdrop(vp); 880 fs->vp = vp; 881 KASSERT(error == 0, ("vm_fault: vget failed %d", error)); 882 return (FAULT_RESTART); 883 } 884 885 /* 886 * Calculate the desired readahead. Handle drop-behind. 887 * 888 * Returns the number of readahead blocks to pass to the pager. 889 */ 890 static int 891 vm_fault_readahead(struct faultstate *fs) 892 { 893 int era, nera; 894 u_char behavior; 895 896 KASSERT(fs->lookup_still_valid, ("map unlocked")); 897 era = fs->entry->read_ahead; 898 behavior = vm_map_entry_behavior(fs->entry); 899 if (behavior == MAP_ENTRY_BEHAV_RANDOM) { 900 nera = 0; 901 } else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) { 902 nera = VM_FAULT_READ_AHEAD_MAX; 903 if (fs->vaddr == fs->entry->next_read) 904 vm_fault_dontneed(fs, fs->vaddr, nera); 905 } else if (fs->vaddr == fs->entry->next_read) { 906 /* 907 * This is a sequential fault. Arithmetically 908 * increase the requested number of pages in 909 * the read-ahead window. The requested 910 * number of pages is "# of sequential faults 911 * x (read ahead min + 1) + read ahead min" 912 */ 913 nera = VM_FAULT_READ_AHEAD_MIN; 914 if (era > 0) { 915 nera += era + 1; 916 if (nera > VM_FAULT_READ_AHEAD_MAX) 917 nera = VM_FAULT_READ_AHEAD_MAX; 918 } 919 if (era == VM_FAULT_READ_AHEAD_MAX) 920 vm_fault_dontneed(fs, fs->vaddr, nera); 921 } else { 922 /* 923 * This is a non-sequential fault. 924 */ 925 nera = 0; 926 } 927 if (era != nera) { 928 /* 929 * A read lock on the map suffices to update 930 * the read ahead count safely. 931 */ 932 fs->entry->read_ahead = nera; 933 } 934 935 return (nera); 936 } 937 938 static int 939 vm_fault_lookup(struct faultstate *fs) 940 { 941 int result; 942 943 KASSERT(!fs->lookup_still_valid, 944 ("vm_fault_lookup: Map already locked.")); 945 result = vm_map_lookup(&fs->map, fs->vaddr, fs->fault_type | 946 VM_PROT_FAULT_LOOKUP, &fs->entry, &fs->first_object, 947 &fs->first_pindex, &fs->prot, &fs->wired); 948 if (result != KERN_SUCCESS) { 949 vm_fault_unlock_vp(fs); 950 return (result); 951 } 952 953 fs->map_generation = fs->map->timestamp; 954 955 if (fs->entry->eflags & MAP_ENTRY_NOFAULT) { 956 panic("%s: fault on nofault entry, addr: %#lx", 957 __func__, (u_long)fs->vaddr); 958 } 959 960 if (fs->entry->eflags & MAP_ENTRY_IN_TRANSITION && 961 fs->entry->wiring_thread != curthread) { 962 vm_map_unlock_read(fs->map); 963 vm_map_lock(fs->map); 964 if (vm_map_lookup_entry(fs->map, fs->vaddr, &fs->entry) && 965 (fs->entry->eflags & MAP_ENTRY_IN_TRANSITION)) { 966 vm_fault_unlock_vp(fs); 967 fs->entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 968 vm_map_unlock_and_wait(fs->map, 0); 969 } else 970 vm_map_unlock(fs->map); 971 return (KERN_RESOURCE_SHORTAGE); 972 } 973 974 MPASS((fs->entry->eflags & MAP_ENTRY_GUARD) == 0); 975 976 if (fs->wired) 977 fs->fault_type = fs->prot | (fs->fault_type & VM_PROT_COPY); 978 else 979 KASSERT((fs->fault_flags & VM_FAULT_WIRE) == 0, 980 ("!fs->wired && VM_FAULT_WIRE")); 981 fs->lookup_still_valid = true; 982 983 return (KERN_SUCCESS); 984 } 985 986 static int 987 vm_fault_relookup(struct faultstate *fs) 988 { 989 vm_object_t retry_object; 990 vm_pindex_t retry_pindex; 991 vm_prot_t retry_prot; 992 int result; 993 994 if (!vm_map_trylock_read(fs->map)) 995 return (KERN_RESTART); 996 997 fs->lookup_still_valid = true; 998 if (fs->map->timestamp == fs->map_generation) 999 return (KERN_SUCCESS); 1000 1001 result = vm_map_lookup_locked(&fs->map, fs->vaddr, fs->fault_type, 1002 &fs->entry, &retry_object, &retry_pindex, &retry_prot, 1003 &fs->wired); 1004 if (result != KERN_SUCCESS) { 1005 /* 1006 * If retry of map lookup would have blocked then 1007 * retry fault from start. 1008 */ 1009 if (result == KERN_FAILURE) 1010 return (KERN_RESTART); 1011 return (result); 1012 } 1013 if (retry_object != fs->first_object || 1014 retry_pindex != fs->first_pindex) 1015 return (KERN_RESTART); 1016 1017 /* 1018 * Check whether the protection has changed or the object has 1019 * been copied while we left the map unlocked. Changing from 1020 * read to write permission is OK - we leave the page 1021 * write-protected, and catch the write fault. Changing from 1022 * write to read permission means that we can't mark the page 1023 * write-enabled after all. 1024 */ 1025 fs->prot &= retry_prot; 1026 fs->fault_type &= retry_prot; 1027 if (fs->prot == 0) 1028 return (KERN_RESTART); 1029 1030 /* Reassert because wired may have changed. */ 1031 KASSERT(fs->wired || (fs->fault_flags & VM_FAULT_WIRE) == 0, 1032 ("!wired && VM_FAULT_WIRE")); 1033 1034 return (KERN_SUCCESS); 1035 } 1036 1037 static bool 1038 vm_fault_can_cow_rename(struct faultstate *fs) 1039 { 1040 return ( 1041 /* Only one shadow object and no other refs. */ 1042 fs->object->shadow_count == 1 && fs->object->ref_count == 1 && 1043 /* No other ways to look the object up. */ 1044 fs->object->handle == NULL && (fs->object->flags & OBJ_ANON) != 0); 1045 } 1046 1047 static void 1048 vm_fault_cow(struct faultstate *fs) 1049 { 1050 bool is_first_object_locked, rename_cow; 1051 1052 KASSERT(vm_fault_might_be_cow(fs), 1053 ("source and target COW objects are identical")); 1054 1055 /* 1056 * This allows pages to be virtually copied from a backing_object 1057 * into the first_object, where the backing object has no other 1058 * refs to it, and cannot gain any more refs. Instead of a bcopy, 1059 * we just move the page from the backing object to the first 1060 * object. Note that we must mark the page dirty in the first 1061 * object so that it will go out to swap when needed. 1062 */ 1063 is_first_object_locked = false; 1064 rename_cow = false; 1065 1066 if (vm_fault_can_cow_rename(fs) && vm_page_xbusied(fs->m)) { 1067 /* 1068 * Check that we don't chase down the shadow chain and 1069 * we can acquire locks. Recheck the conditions for 1070 * rename after the shadow chain is stable after the 1071 * object locking. 1072 */ 1073 is_first_object_locked = VM_OBJECT_TRYWLOCK(fs->first_object); 1074 if (is_first_object_locked && 1075 fs->object == fs->first_object->backing_object) { 1076 if (VM_OBJECT_TRYWLOCK(fs->object)) { 1077 rename_cow = vm_fault_can_cow_rename(fs); 1078 if (!rename_cow) 1079 VM_OBJECT_WUNLOCK(fs->object); 1080 } 1081 } 1082 } 1083 1084 if (rename_cow) { 1085 vm_page_assert_xbusied(fs->m); 1086 1087 /* 1088 * Remove but keep xbusy for replace. fs->m is moved into 1089 * fs->first_object and left busy while fs->first_m is 1090 * conditionally freed. 1091 */ 1092 vm_page_remove_xbusy(fs->m); 1093 vm_page_replace(fs->m, fs->first_object, fs->first_pindex, 1094 fs->first_m); 1095 vm_page_dirty(fs->m); 1096 #if VM_NRESERVLEVEL > 0 1097 /* 1098 * Rename the reservation. 1099 */ 1100 vm_reserv_rename(fs->m, fs->first_object, fs->object, 1101 OFF_TO_IDX(fs->first_object->backing_object_offset)); 1102 #endif 1103 VM_OBJECT_WUNLOCK(fs->object); 1104 VM_OBJECT_WUNLOCK(fs->first_object); 1105 fs->first_m = fs->m; 1106 fs->m = NULL; 1107 VM_CNT_INC(v_cow_optim); 1108 } else { 1109 if (is_first_object_locked) 1110 VM_OBJECT_WUNLOCK(fs->first_object); 1111 /* 1112 * Oh, well, lets copy it. 1113 */ 1114 pmap_copy_page(fs->m, fs->first_m); 1115 if (fs->wired && (fs->fault_flags & VM_FAULT_WIRE) == 0) { 1116 vm_page_wire(fs->first_m); 1117 vm_page_unwire(fs->m, PQ_INACTIVE); 1118 } 1119 /* 1120 * Save the COW page to be released after pmap_enter is 1121 * complete. The new copy will be marked valid when we're ready 1122 * to map it. 1123 */ 1124 fs->m_cow = fs->m; 1125 fs->m = NULL; 1126 1127 /* 1128 * Typically, the shadow object is either private to this 1129 * address space (OBJ_ONEMAPPING) or its pages are read only. 1130 * In the highly unusual case where the pages of a shadow object 1131 * are read/write shared between this and other address spaces, 1132 * we need to ensure that any pmap-level mappings to the 1133 * original, copy-on-write page from the backing object are 1134 * removed from those other address spaces. 1135 * 1136 * The flag check is racy, but this is tolerable: if 1137 * OBJ_ONEMAPPING is cleared after the check, the busy state 1138 * ensures that new mappings of m_cow can't be created. 1139 * pmap_enter() will replace an existing mapping in the current 1140 * address space. If OBJ_ONEMAPPING is set after the check, 1141 * removing mappings will at worse trigger some unnecessary page 1142 * faults. 1143 * 1144 * In the fs->m shared busy case, the xbusy state of 1145 * fs->first_m prevents new mappings of fs->m from 1146 * being created because a parallel fault on this 1147 * shadow chain should wait for xbusy on fs->first_m. 1148 */ 1149 if ((fs->first_object->flags & OBJ_ONEMAPPING) == 0) 1150 pmap_remove_all(fs->m_cow); 1151 } 1152 1153 vm_object_pip_wakeup(fs->object); 1154 1155 /* 1156 * Only use the new page below... 1157 */ 1158 fs->object = fs->first_object; 1159 fs->pindex = fs->first_pindex; 1160 fs->m = fs->first_m; 1161 VM_CNT_INC(v_cow_faults); 1162 curthread->td_cow++; 1163 } 1164 1165 static enum fault_next_status 1166 vm_fault_next(struct faultstate *fs) 1167 { 1168 vm_object_t next_object; 1169 1170 if (fs->object == fs->first_object || !fs->can_read_lock) 1171 VM_OBJECT_ASSERT_WLOCKED(fs->object); 1172 else 1173 VM_OBJECT_ASSERT_LOCKED(fs->object); 1174 1175 /* 1176 * The requested page does not exist at this object/ 1177 * offset. Remove the invalid page from the object, 1178 * waking up anyone waiting for it, and continue on to 1179 * the next object. However, if this is the top-level 1180 * object, we must leave the busy page in place to 1181 * prevent another process from rushing past us, and 1182 * inserting the page in that object at the same time 1183 * that we are. 1184 */ 1185 if (fs->object == fs->first_object) { 1186 fs->first_m = fs->m; 1187 fs->m = NULL; 1188 } else if (fs->m != NULL) { 1189 if (!vm_fault_object_ensure_wlocked(fs)) { 1190 fs->can_read_lock = false; 1191 vm_fault_unlock_and_deallocate(fs); 1192 return (FAULT_NEXT_RESTART); 1193 } 1194 vm_fault_page_free(&fs->m); 1195 } 1196 1197 /* 1198 * Move on to the next object. Lock the next object before 1199 * unlocking the current one. 1200 */ 1201 next_object = fs->object->backing_object; 1202 if (next_object == NULL) 1203 return (FAULT_NEXT_NOOBJ); 1204 MPASS(fs->first_m != NULL); 1205 KASSERT(fs->object != next_object, ("object loop %p", next_object)); 1206 if (fs->can_read_lock) 1207 VM_OBJECT_RLOCK(next_object); 1208 else 1209 VM_OBJECT_WLOCK(next_object); 1210 vm_object_pip_add(next_object, 1); 1211 if (fs->object != fs->first_object) 1212 vm_object_pip_wakeup(fs->object); 1213 fs->pindex += OFF_TO_IDX(fs->object->backing_object_offset); 1214 VM_OBJECT_UNLOCK(fs->object); 1215 fs->object = next_object; 1216 1217 return (FAULT_NEXT_GOTOBJ); 1218 } 1219 1220 static void 1221 vm_fault_zerofill(struct faultstate *fs) 1222 { 1223 1224 /* 1225 * If there's no object left, fill the page in the top 1226 * object with zeros. 1227 */ 1228 if (vm_fault_might_be_cow(fs)) { 1229 vm_object_pip_wakeup(fs->object); 1230 fs->object = fs->first_object; 1231 fs->pindex = fs->first_pindex; 1232 } 1233 MPASS(fs->first_m != NULL); 1234 MPASS(fs->m == NULL); 1235 fs->m = fs->first_m; 1236 fs->first_m = NULL; 1237 1238 /* 1239 * Zero the page if necessary and mark it valid. 1240 */ 1241 if (fs->m_needs_zeroing) { 1242 pmap_zero_page(fs->m); 1243 } else { 1244 #ifdef INVARIANTS 1245 if (vm_check_pg_zero) { 1246 struct sf_buf *sf; 1247 unsigned long *p; 1248 int i; 1249 1250 sched_pin(); 1251 sf = sf_buf_alloc(fs->m, SFB_CPUPRIVATE); 1252 p = (unsigned long *)sf_buf_kva(sf); 1253 for (i = 0; i < PAGE_SIZE / sizeof(*p); i++, p++) { 1254 KASSERT(*p == 0, 1255 ("zerocheck failed page %p PG_ZERO %d %jx", 1256 fs->m, i, (uintmax_t)*p)); 1257 } 1258 sf_buf_free(sf); 1259 sched_unpin(); 1260 } 1261 #endif 1262 VM_CNT_INC(v_ozfod); 1263 } 1264 VM_CNT_INC(v_zfod); 1265 vm_page_valid(fs->m); 1266 } 1267 1268 /* 1269 * Initiate page fault after timeout. Returns true if caller should 1270 * do vm_waitpfault() after the call. 1271 */ 1272 static bool 1273 vm_fault_allocate_oom(struct faultstate *fs) 1274 { 1275 struct timeval now; 1276 1277 vm_fault_unlock_and_deallocate(fs); 1278 if (vm_pfault_oom_attempts < 0) 1279 return (true); 1280 if (!fs->oom_started) { 1281 fs->oom_started = true; 1282 getmicrotime(&fs->oom_start_time); 1283 return (true); 1284 } 1285 1286 getmicrotime(&now); 1287 timevalsub(&now, &fs->oom_start_time); 1288 if (now.tv_sec < vm_pfault_oom_attempts * vm_pfault_oom_wait) 1289 return (true); 1290 1291 if (bootverbose) 1292 printf( 1293 "proc %d (%s) failed to alloc page on fault, starting OOM\n", 1294 curproc->p_pid, curproc->p_comm); 1295 vm_pageout_oom(VM_OOM_MEM_PF); 1296 fs->oom_started = false; 1297 return (false); 1298 } 1299 1300 /* 1301 * Allocate a page directly or via the object populate method. 1302 */ 1303 static enum fault_status 1304 vm_fault_allocate(struct faultstate *fs, struct pctrie_iter *pages) 1305 { 1306 struct domainset *dset; 1307 enum fault_status res; 1308 1309 if ((fs->object->flags & OBJ_SIZEVNLOCK) != 0) { 1310 res = vm_fault_lock_vnode(fs, true); 1311 MPASS(res == FAULT_CONTINUE || res == FAULT_RESTART); 1312 if (res == FAULT_RESTART) 1313 return (res); 1314 } 1315 1316 if (fs->pindex >= fs->object->size) { 1317 vm_fault_unlock_and_deallocate(fs); 1318 return (FAULT_OUT_OF_BOUNDS); 1319 } 1320 1321 if (fs->object == fs->first_object && 1322 (fs->first_object->flags & OBJ_POPULATE) != 0 && 1323 fs->first_object->shadow_count == 0) { 1324 res = vm_fault_populate(fs); 1325 switch (res) { 1326 case FAULT_SUCCESS: 1327 case FAULT_FAILURE: 1328 case FAULT_RESTART: 1329 vm_fault_unlock_and_deallocate(fs); 1330 return (res); 1331 case FAULT_CONTINUE: 1332 pctrie_iter_reset(pages); 1333 /* 1334 * Pager's populate() method 1335 * returned VM_PAGER_BAD. 1336 */ 1337 break; 1338 default: 1339 panic("inconsistent return codes"); 1340 } 1341 } 1342 1343 /* 1344 * Allocate a new page for this object/offset pair. 1345 * 1346 * If the process has a fatal signal pending, prioritize the allocation 1347 * with the expectation that the process will exit shortly and free some 1348 * pages. In particular, the signal may have been posted by the page 1349 * daemon in an attempt to resolve an out-of-memory condition. 1350 * 1351 * The unlocked read of the p_flag is harmless. At worst, the P_KILLED 1352 * might be not observed here, and allocation fails, causing a restart 1353 * and new reading of the p_flag. 1354 */ 1355 dset = fs->object->domain.dr_policy; 1356 if (dset == NULL) 1357 dset = curthread->td_domain.dr_policy; 1358 if (!vm_page_count_severe_set(&dset->ds_mask) || P_KILLED(curproc)) { 1359 #if VM_NRESERVLEVEL > 0 1360 vm_object_color(fs->object, atop(fs->vaddr) - fs->pindex); 1361 #endif 1362 if (!vm_pager_can_alloc_page(fs->object, fs->pindex)) { 1363 vm_fault_unlock_and_deallocate(fs); 1364 return (FAULT_FAILURE); 1365 } 1366 fs->m = vm_page_alloc_iter(fs->object, fs->pindex, 1367 P_KILLED(curproc) ? VM_ALLOC_SYSTEM : 0, pages); 1368 } 1369 if (fs->m == NULL) { 1370 if (vm_fault_allocate_oom(fs)) 1371 vm_waitpfault(dset, vm_pfault_oom_wait * hz); 1372 return (FAULT_RESTART); 1373 } 1374 if (fs->object == fs->first_object) 1375 fs->m_needs_zeroing = (fs->m->flags & PG_ZERO) == 0; 1376 fs->oom_started = false; 1377 1378 return (FAULT_CONTINUE); 1379 } 1380 1381 /* 1382 * Call the pager to retrieve the page if there is a chance 1383 * that the pager has it, and potentially retrieve additional 1384 * pages at the same time. 1385 */ 1386 static enum fault_status 1387 vm_fault_getpages(struct faultstate *fs, int *behindp, int *aheadp) 1388 { 1389 vm_offset_t e_end, e_start; 1390 int ahead, behind, cluster_offset, rv; 1391 enum fault_status status; 1392 u_char behavior; 1393 1394 /* 1395 * Prepare for unlocking the map. Save the map 1396 * entry's start and end addresses, which are used to 1397 * optimize the size of the pager operation below. 1398 * Even if the map entry's addresses change after 1399 * unlocking the map, using the saved addresses is 1400 * safe. 1401 */ 1402 e_start = fs->entry->start; 1403 e_end = fs->entry->end; 1404 behavior = vm_map_entry_behavior(fs->entry); 1405 1406 /* 1407 * If the pager for the current object might have 1408 * the page, then determine the number of additional 1409 * pages to read and potentially reprioritize 1410 * previously read pages for earlier reclamation. 1411 * These operations should only be performed once per 1412 * page fault. Even if the current pager doesn't 1413 * have the page, the number of additional pages to 1414 * read will apply to subsequent objects in the 1415 * shadow chain. 1416 */ 1417 if (fs->nera == -1 && !P_KILLED(curproc)) 1418 fs->nera = vm_fault_readahead(fs); 1419 1420 /* 1421 * Release the map lock before locking the vnode or 1422 * sleeping in the pager. (If the current object has 1423 * a shadow, then an earlier iteration of this loop 1424 * may have already unlocked the map.) 1425 */ 1426 vm_fault_unlock_map(fs); 1427 1428 status = vm_fault_lock_vnode(fs, false); 1429 MPASS(status == FAULT_CONTINUE || status == FAULT_RESTART); 1430 if (status == FAULT_RESTART) 1431 return (status); 1432 KASSERT(fs->vp == NULL || !vm_map_is_system(fs->map), 1433 ("vm_fault: vnode-backed object mapped by system map")); 1434 1435 /* 1436 * Page in the requested page and hint the pager, 1437 * that it may bring up surrounding pages. 1438 */ 1439 if (fs->nera == -1 || behavior == MAP_ENTRY_BEHAV_RANDOM || 1440 P_KILLED(curproc)) { 1441 behind = 0; 1442 ahead = 0; 1443 } else { 1444 /* Is this a sequential fault? */ 1445 if (fs->nera > 0) { 1446 behind = 0; 1447 ahead = fs->nera; 1448 } else { 1449 /* 1450 * Request a cluster of pages that is 1451 * aligned to a VM_FAULT_READ_DEFAULT 1452 * page offset boundary within the 1453 * object. Alignment to a page offset 1454 * boundary is more likely to coincide 1455 * with the underlying file system 1456 * block than alignment to a virtual 1457 * address boundary. 1458 */ 1459 cluster_offset = fs->pindex % VM_FAULT_READ_DEFAULT; 1460 behind = ulmin(cluster_offset, 1461 atop(fs->vaddr - e_start)); 1462 ahead = VM_FAULT_READ_DEFAULT - 1 - cluster_offset; 1463 } 1464 ahead = ulmin(ahead, atop(e_end - fs->vaddr) - 1); 1465 } 1466 *behindp = behind; 1467 *aheadp = ahead; 1468 rv = vm_pager_get_pages(fs->object, &fs->m, 1, behindp, aheadp); 1469 if (rv == VM_PAGER_OK) 1470 return (FAULT_HARD); 1471 if (rv == VM_PAGER_ERROR) 1472 printf("vm_fault: pager read error, pid %d (%s)\n", 1473 curproc->p_pid, curproc->p_comm); 1474 /* 1475 * If an I/O error occurred or the requested page was 1476 * outside the range of the pager, clean up and return 1477 * an error. 1478 */ 1479 if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) { 1480 VM_OBJECT_WLOCK(fs->object); 1481 vm_fault_page_free(&fs->m); 1482 vm_fault_unlock_and_deallocate(fs); 1483 return (FAULT_OUT_OF_BOUNDS); 1484 } 1485 KASSERT(rv == VM_PAGER_FAIL, 1486 ("%s: unexpected pager error %d", __func__, rv)); 1487 return (FAULT_CONTINUE); 1488 } 1489 1490 /* 1491 * Wait/Retry if the page is busy. We have to do this if the page is 1492 * either exclusive or shared busy because the vm_pager may be using 1493 * read busy for pageouts (and even pageins if it is the vnode pager), 1494 * and we could end up trying to pagein and pageout the same page 1495 * simultaneously. 1496 * 1497 * We allow the busy case on a read fault if the page is valid. We 1498 * cannot under any circumstances mess around with a shared busied 1499 * page except, perhaps, to pmap it. This is controlled by the 1500 * VM_ALLOC_SBUSY bit in the allocflags argument. 1501 */ 1502 static void 1503 vm_fault_busy_sleep(struct faultstate *fs, int allocflags) 1504 { 1505 /* 1506 * Reference the page before unlocking and 1507 * sleeping so that the page daemon is less 1508 * likely to reclaim it. 1509 */ 1510 vm_page_aflag_set(fs->m, PGA_REFERENCED); 1511 if (vm_fault_might_be_cow(fs)) { 1512 vm_fault_page_release(&fs->first_m); 1513 vm_object_pip_wakeup(fs->first_object); 1514 } 1515 vm_object_pip_wakeup(fs->object); 1516 vm_fault_unlock_map(fs); 1517 if (!vm_page_busy_sleep(fs->m, "vmpfw", allocflags)) 1518 VM_OBJECT_UNLOCK(fs->object); 1519 VM_CNT_INC(v_intrans); 1520 vm_object_deallocate(fs->first_object); 1521 } 1522 1523 /* 1524 * Handle page lookup, populate, allocate, page-in for the current 1525 * object. 1526 * 1527 * The object is locked on entry and will remain locked with a return 1528 * code of FAULT_CONTINUE so that fault may follow the shadow chain. 1529 * Otherwise, the object will be unlocked upon return. 1530 */ 1531 static enum fault_status 1532 vm_fault_object(struct faultstate *fs, int *behindp, int *aheadp) 1533 { 1534 struct pctrie_iter pages; 1535 enum fault_status res; 1536 bool dead; 1537 1538 if (fs->object == fs->first_object || !fs->can_read_lock) 1539 VM_OBJECT_ASSERT_WLOCKED(fs->object); 1540 else 1541 VM_OBJECT_ASSERT_LOCKED(fs->object); 1542 1543 /* 1544 * If the object is marked for imminent termination, we retry 1545 * here, since the collapse pass has raced with us. Otherwise, 1546 * if we see terminally dead object, return fail. 1547 */ 1548 if ((fs->object->flags & OBJ_DEAD) != 0) { 1549 dead = fs->object->type == OBJT_DEAD; 1550 vm_fault_unlock_and_deallocate(fs); 1551 if (dead) 1552 return (FAULT_PROTECTION_FAILURE); 1553 pause("vmf_de", 1); 1554 return (FAULT_RESTART); 1555 } 1556 1557 /* 1558 * See if the page is resident. 1559 */ 1560 vm_page_iter_init(&pages, fs->object); 1561 fs->m = vm_radix_iter_lookup(&pages, fs->pindex); 1562 if (fs->m != NULL) { 1563 /* 1564 * If the found page is valid, will be either shadowed 1565 * or mapped read-only, and will not be renamed for 1566 * COW, then busy it in shared mode. This allows 1567 * other faults needing this page to proceed in 1568 * parallel. 1569 * 1570 * Unlocked check for validity, rechecked after busy 1571 * is obtained. 1572 */ 1573 if (vm_page_all_valid(fs->m) && 1574 /* 1575 * No write permissions for the new fs->m mapping, 1576 * or the first object has only one mapping, so 1577 * other writeable COW mappings of fs->m cannot 1578 * appear under us. 1579 */ 1580 (vm_fault_is_read(fs) || vm_fault_might_be_cow(fs)) && 1581 /* 1582 * fs->m cannot be renamed from object to 1583 * first_object. These conditions will be 1584 * re-checked with proper synchronization in 1585 * vm_fault_cow(). 1586 */ 1587 (!vm_fault_can_cow_rename(fs) || 1588 fs->object != fs->first_object->backing_object)) { 1589 if (!vm_page_trysbusy(fs->m)) { 1590 vm_fault_busy_sleep(fs, VM_ALLOC_SBUSY); 1591 return (FAULT_RESTART); 1592 } 1593 1594 /* 1595 * Now make sure that racily checked 1596 * conditions are still valid. 1597 */ 1598 if (__predict_true(vm_page_all_valid(fs->m) && 1599 (vm_fault_is_read(fs) || 1600 vm_fault_might_be_cow(fs)))) { 1601 VM_OBJECT_UNLOCK(fs->object); 1602 return (FAULT_SOFT); 1603 } 1604 1605 vm_page_sunbusy(fs->m); 1606 } 1607 1608 if (!vm_page_tryxbusy(fs->m)) { 1609 vm_fault_busy_sleep(fs, 0); 1610 return (FAULT_RESTART); 1611 } 1612 1613 /* 1614 * The page is marked busy for other processes and the 1615 * pagedaemon. If it is still completely valid we are 1616 * done. 1617 */ 1618 if (vm_page_all_valid(fs->m)) { 1619 VM_OBJECT_UNLOCK(fs->object); 1620 return (FAULT_SOFT); 1621 } 1622 } 1623 1624 /* 1625 * Page is not resident. If the pager might contain the page 1626 * or this is the beginning of the search, allocate a new 1627 * page. 1628 */ 1629 if (fs->m == NULL && (vm_fault_object_needs_getpages(fs->object) || 1630 fs->object == fs->first_object)) { 1631 if (!vm_fault_object_ensure_wlocked(fs)) { 1632 fs->can_read_lock = false; 1633 vm_fault_unlock_and_deallocate(fs); 1634 return (FAULT_RESTART); 1635 } 1636 res = vm_fault_allocate(fs, &pages); 1637 if (res != FAULT_CONTINUE) 1638 return (res); 1639 } 1640 1641 /* 1642 * Check to see if the pager can possibly satisfy this fault. 1643 * If not, skip to the next object without dropping the lock to 1644 * preserve atomicity of shadow faults. 1645 */ 1646 if (vm_fault_object_needs_getpages(fs->object)) { 1647 /* 1648 * At this point, we have either allocated a new page 1649 * or found an existing page that is only partially 1650 * valid. 1651 * 1652 * We hold a reference on the current object and the 1653 * page is exclusive busied. The exclusive busy 1654 * prevents simultaneous faults and collapses while 1655 * the object lock is dropped. 1656 */ 1657 VM_OBJECT_UNLOCK(fs->object); 1658 res = vm_fault_getpages(fs, behindp, aheadp); 1659 if (res == FAULT_CONTINUE) 1660 VM_OBJECT_WLOCK(fs->object); 1661 } else { 1662 res = FAULT_CONTINUE; 1663 } 1664 return (res); 1665 } 1666 1667 /* 1668 * vm_fault: 1669 * 1670 * Handle a page fault occurring at the given address, requiring the 1671 * given permissions, in the map specified. If successful, the page 1672 * is inserted into the associated physical map, and optionally 1673 * referenced and returned in *m_hold. 1674 * 1675 * The given address should be truncated to the proper page address. 1676 * 1677 * KERN_SUCCESS is returned if the page fault is handled; otherwise, a 1678 * Mach error code explaining why the fault is fatal is returned. 1679 * 1680 * The map in question must be alive, either being the map for the current 1681 * process, or the owner process hold count has been incremented to prevent 1682 * exit(). 1683 * 1684 * If the thread private TDP_NOFAULTING flag is set, any fault results 1685 * in immediate protection failure. Otherwise the fault is processed, 1686 * and caller may hold no locks. 1687 */ 1688 int 1689 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 1690 int fault_flags, vm_page_t *m_hold) 1691 { 1692 struct pctrie_iter pages; 1693 struct faultstate fs; 1694 int ahead, behind, faultcount, rv; 1695 enum fault_status res; 1696 enum fault_next_status res_next; 1697 bool hardfault; 1698 1699 VM_CNT_INC(v_vm_faults); 1700 1701 if ((curthread->td_pflags & TDP_NOFAULTING) != 0) 1702 return (KERN_PROTECTION_FAILURE); 1703 1704 fs.vp = NULL; 1705 fs.vaddr = vaddr; 1706 fs.m_hold = m_hold; 1707 fs.fault_flags = fault_flags; 1708 fs.map = map; 1709 fs.lookup_still_valid = false; 1710 fs.oom_started = false; 1711 fs.nera = -1; 1712 fs.can_read_lock = true; 1713 faultcount = 0; 1714 hardfault = false; 1715 1716 RetryFault: 1717 fs.fault_type = fault_type; 1718 fs.m_needs_zeroing = true; 1719 1720 /* 1721 * Find the backing store object and offset into it to begin the 1722 * search. 1723 */ 1724 rv = vm_fault_lookup(&fs); 1725 if (rv != KERN_SUCCESS) { 1726 if (rv == KERN_RESOURCE_SHORTAGE) 1727 goto RetryFault; 1728 return (rv); 1729 } 1730 1731 /* 1732 * Try to avoid lock contention on the top-level object through 1733 * special-case handling of some types of page faults, specifically, 1734 * those that are mapping an existing page from the top-level object. 1735 * Under this condition, a read lock on the object suffices, allowing 1736 * multiple page faults of a similar type to run in parallel. 1737 */ 1738 if (fs.vp == NULL /* avoid locked vnode leak */ && 1739 (fs.entry->eflags & MAP_ENTRY_SPLIT_BOUNDARY_MASK) == 0 && 1740 (fs.fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0) { 1741 res = vm_fault_soft_fast(&fs); 1742 if (res == FAULT_SUCCESS) { 1743 VM_OBJECT_ASSERT_UNLOCKED(fs.first_object); 1744 return (KERN_SUCCESS); 1745 } 1746 VM_OBJECT_ASSERT_WLOCKED(fs.first_object); 1747 } else { 1748 vm_page_iter_init(&pages, fs.first_object); 1749 VM_OBJECT_WLOCK(fs.first_object); 1750 } 1751 1752 /* 1753 * Make a reference to this object to prevent its disposal while we 1754 * are messing with it. Once we have the reference, the map is free 1755 * to be diddled. Since objects reference their shadows (and copies), 1756 * they will stay around as well. 1757 * 1758 * Bump the paging-in-progress count to prevent size changes (e.g. 1759 * truncation operations) during I/O. 1760 */ 1761 vm_object_reference_locked(fs.first_object); 1762 vm_object_pip_add(fs.first_object, 1); 1763 1764 fs.m_cow = fs.m = fs.first_m = NULL; 1765 1766 /* 1767 * Search for the page at object/offset. 1768 */ 1769 fs.object = fs.first_object; 1770 fs.pindex = fs.first_pindex; 1771 1772 if ((fs.entry->eflags & MAP_ENTRY_SPLIT_BOUNDARY_MASK) != 0) { 1773 res = vm_fault_allocate(&fs, &pages); 1774 switch (res) { 1775 case FAULT_RESTART: 1776 goto RetryFault; 1777 case FAULT_SUCCESS: 1778 return (KERN_SUCCESS); 1779 case FAULT_FAILURE: 1780 return (KERN_FAILURE); 1781 case FAULT_OUT_OF_BOUNDS: 1782 return (KERN_OUT_OF_BOUNDS); 1783 case FAULT_CONTINUE: 1784 break; 1785 default: 1786 panic("vm_fault: Unhandled status %d", res); 1787 } 1788 } 1789 1790 while (TRUE) { 1791 KASSERT(fs.m == NULL, 1792 ("page still set %p at loop start", fs.m)); 1793 1794 res = vm_fault_object(&fs, &behind, &ahead); 1795 switch (res) { 1796 case FAULT_SOFT: 1797 goto found; 1798 case FAULT_HARD: 1799 faultcount = behind + 1 + ahead; 1800 hardfault = true; 1801 goto found; 1802 case FAULT_RESTART: 1803 goto RetryFault; 1804 case FAULT_SUCCESS: 1805 return (KERN_SUCCESS); 1806 case FAULT_FAILURE: 1807 return (KERN_FAILURE); 1808 case FAULT_OUT_OF_BOUNDS: 1809 return (KERN_OUT_OF_BOUNDS); 1810 case FAULT_PROTECTION_FAILURE: 1811 return (KERN_PROTECTION_FAILURE); 1812 case FAULT_CONTINUE: 1813 break; 1814 default: 1815 panic("vm_fault: Unhandled status %d", res); 1816 } 1817 1818 /* 1819 * The page was not found in the current object. Try to 1820 * traverse into a backing object or zero fill if none is 1821 * found. 1822 */ 1823 res_next = vm_fault_next(&fs); 1824 if (res_next == FAULT_NEXT_RESTART) 1825 goto RetryFault; 1826 else if (res_next == FAULT_NEXT_GOTOBJ) 1827 continue; 1828 MPASS(res_next == FAULT_NEXT_NOOBJ); 1829 if ((fs.fault_flags & VM_FAULT_NOFILL) != 0) { 1830 if (fs.first_object == fs.object) 1831 vm_fault_page_free(&fs.first_m); 1832 vm_fault_unlock_and_deallocate(&fs); 1833 return (KERN_OUT_OF_BOUNDS); 1834 } 1835 VM_OBJECT_UNLOCK(fs.object); 1836 vm_fault_zerofill(&fs); 1837 /* Don't try to prefault neighboring pages. */ 1838 faultcount = 1; 1839 break; 1840 } 1841 1842 found: 1843 /* 1844 * A valid page has been found and busied. The object lock 1845 * must no longer be held if the page was busied. 1846 * 1847 * Regardless of the busy state of fs.m, fs.first_m is always 1848 * exclusively busied after the first iteration of the loop 1849 * calling vm_fault_object(). This is an ordering point for 1850 * the parallel faults occuring in on the same page. 1851 */ 1852 vm_page_assert_busied(fs.m); 1853 VM_OBJECT_ASSERT_UNLOCKED(fs.object); 1854 1855 /* 1856 * If the page is being written, but isn't already owned by the 1857 * top-level object, we have to copy it into a new page owned by the 1858 * top-level object. 1859 */ 1860 if (vm_fault_might_be_cow(&fs)) { 1861 /* 1862 * We only really need to copy if we want to write it. 1863 */ 1864 if ((fs.fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) { 1865 vm_fault_cow(&fs); 1866 /* 1867 * We only try to prefault read-only mappings to the 1868 * neighboring pages when this copy-on-write fault is 1869 * a hard fault. In other cases, trying to prefault 1870 * is typically wasted effort. 1871 */ 1872 if (faultcount == 0) 1873 faultcount = 1; 1874 1875 } else { 1876 fs.prot &= ~VM_PROT_WRITE; 1877 } 1878 } 1879 1880 /* 1881 * We must verify that the maps have not changed since our last 1882 * lookup. 1883 */ 1884 if (!fs.lookup_still_valid) { 1885 rv = vm_fault_relookup(&fs); 1886 if (rv != KERN_SUCCESS) { 1887 vm_fault_deallocate(&fs); 1888 if (rv == KERN_RESTART) 1889 goto RetryFault; 1890 return (rv); 1891 } 1892 } 1893 VM_OBJECT_ASSERT_UNLOCKED(fs.object); 1894 1895 /* 1896 * If the page was filled by a pager, save the virtual address that 1897 * should be faulted on next under a sequential access pattern to the 1898 * map entry. A read lock on the map suffices to update this address 1899 * safely. 1900 */ 1901 if (hardfault) 1902 fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE; 1903 1904 /* 1905 * If the page to be mapped was copied from a backing object, we defer 1906 * marking it valid until here, where the fault handler is guaranteed to 1907 * succeed. Otherwise we can end up with a shadowed, mapped page in the 1908 * backing object, which violates an invariant of vm_object_collapse() 1909 * that shadowed pages are not mapped. 1910 */ 1911 if (fs.m_cow != NULL) { 1912 KASSERT(vm_page_none_valid(fs.m), 1913 ("vm_fault: page %p is already valid", fs.m_cow)); 1914 vm_page_valid(fs.m); 1915 } 1916 1917 /* 1918 * Page must be completely valid or it is not fit to 1919 * map into user space. vm_pager_get_pages() ensures this. 1920 */ 1921 vm_page_assert_busied(fs.m); 1922 KASSERT(vm_page_all_valid(fs.m), 1923 ("vm_fault: page %p partially invalid", fs.m)); 1924 1925 vm_fault_dirty(&fs, fs.m); 1926 1927 /* 1928 * Put this page into the physical map. We had to do the unlock above 1929 * because pmap_enter() may sleep. We don't put the page 1930 * back on the active queue until later so that the pageout daemon 1931 * won't find it (yet). 1932 */ 1933 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, 1934 fs.fault_type | (fs.wired ? PMAP_ENTER_WIRED : 0), 0); 1935 if (faultcount != 1 && (fs.fault_flags & VM_FAULT_WIRE) == 0 && 1936 fs.wired == 0) 1937 vm_fault_prefault(&fs, vaddr, 1938 faultcount > 0 ? behind : PFBAK, 1939 faultcount > 0 ? ahead : PFFOR, false); 1940 1941 /* 1942 * If the page is not wired down, then put it where the pageout daemon 1943 * can find it. 1944 */ 1945 if ((fs.fault_flags & VM_FAULT_WIRE) != 0) 1946 vm_page_wire(fs.m); 1947 else 1948 vm_page_activate(fs.m); 1949 if (fs.m_hold != NULL) { 1950 (*fs.m_hold) = fs.m; 1951 vm_page_wire(fs.m); 1952 } 1953 1954 KASSERT(fs.first_object == fs.object || vm_page_xbusied(fs.first_m), 1955 ("first_m must be xbusy")); 1956 if (vm_page_xbusied(fs.m)) 1957 vm_page_xunbusy(fs.m); 1958 else 1959 vm_page_sunbusy(fs.m); 1960 fs.m = NULL; 1961 1962 /* 1963 * Unlock everything, and return 1964 */ 1965 vm_fault_deallocate(&fs); 1966 if (hardfault) { 1967 VM_CNT_INC(v_io_faults); 1968 curthread->td_ru.ru_majflt++; 1969 #ifdef RACCT 1970 if (racct_enable && fs.object->type == OBJT_VNODE) { 1971 PROC_LOCK(curproc); 1972 if ((fs.fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) { 1973 racct_add_force(curproc, RACCT_WRITEBPS, 1974 PAGE_SIZE + behind * PAGE_SIZE); 1975 racct_add_force(curproc, RACCT_WRITEIOPS, 1); 1976 } else { 1977 racct_add_force(curproc, RACCT_READBPS, 1978 PAGE_SIZE + ahead * PAGE_SIZE); 1979 racct_add_force(curproc, RACCT_READIOPS, 1); 1980 } 1981 PROC_UNLOCK(curproc); 1982 } 1983 #endif 1984 } else 1985 curthread->td_ru.ru_minflt++; 1986 1987 return (KERN_SUCCESS); 1988 } 1989 1990 /* 1991 * Speed up the reclamation of pages that precede the faulting pindex within 1992 * the first object of the shadow chain. Essentially, perform the equivalent 1993 * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes 1994 * the faulting pindex by the cluster size when the pages read by vm_fault() 1995 * cross a cluster-size boundary. The cluster size is the greater of the 1996 * smallest superpage size and VM_FAULT_DONTNEED_MIN. 1997 * 1998 * When "fs->first_object" is a shadow object, the pages in the backing object 1999 * that precede the faulting pindex are deactivated by vm_fault(). So, this 2000 * function must only be concerned with pages in the first object. 2001 */ 2002 static void 2003 vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead) 2004 { 2005 struct pctrie_iter pages; 2006 vm_map_entry_t entry; 2007 vm_object_t first_object; 2008 vm_offset_t end, start; 2009 vm_page_t m; 2010 vm_size_t size; 2011 2012 VM_OBJECT_ASSERT_UNLOCKED(fs->object); 2013 first_object = fs->first_object; 2014 /* Neither fictitious nor unmanaged pages can be reclaimed. */ 2015 if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) { 2016 VM_OBJECT_RLOCK(first_object); 2017 size = VM_FAULT_DONTNEED_MIN; 2018 if (MAXPAGESIZES > 1 && size < pagesizes[1]) 2019 size = pagesizes[1]; 2020 end = rounddown2(vaddr, size); 2021 if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) && 2022 (entry = fs->entry)->start < end) { 2023 if (end - entry->start < size) 2024 start = entry->start; 2025 else 2026 start = end - size; 2027 pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED); 2028 vm_page_iter_limit_init(&pages, first_object, 2029 OFF_TO_IDX(entry->offset) + 2030 atop(end - entry->start)); 2031 VM_RADIX_FOREACH_FROM(m, &pages, 2032 OFF_TO_IDX(entry->offset) + 2033 atop(start - entry->start)) { 2034 if (!vm_page_all_valid(m) || 2035 vm_page_busied(m)) 2036 continue; 2037 2038 /* 2039 * Don't clear PGA_REFERENCED, since it would 2040 * likely represent a reference by a different 2041 * process. 2042 * 2043 * Typically, at this point, prefetched pages 2044 * are still in the inactive queue. Only 2045 * pages that triggered page faults are in the 2046 * active queue. The test for whether the page 2047 * is in the inactive queue is racy; in the 2048 * worst case we will requeue the page 2049 * unnecessarily. 2050 */ 2051 if (!vm_page_inactive(m)) 2052 vm_page_deactivate(m); 2053 } 2054 } 2055 VM_OBJECT_RUNLOCK(first_object); 2056 } 2057 } 2058 2059 /* 2060 * vm_fault_prefault provides a quick way of clustering 2061 * pagefaults into a processes address space. It is a "cousin" 2062 * of vm_map_pmap_enter, except it runs at page fault time instead 2063 * of mmap time. 2064 */ 2065 static void 2066 vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra, 2067 int backward, int forward, bool obj_locked) 2068 { 2069 pmap_t pmap; 2070 vm_map_entry_t entry; 2071 vm_object_t backing_object, lobject; 2072 vm_offset_t addr, starta; 2073 vm_pindex_t pindex; 2074 vm_page_t m; 2075 vm_prot_t prot; 2076 int i; 2077 2078 pmap = fs->map->pmap; 2079 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) 2080 return; 2081 2082 entry = fs->entry; 2083 2084 if (addra < backward * PAGE_SIZE) { 2085 starta = entry->start; 2086 } else { 2087 starta = addra - backward * PAGE_SIZE; 2088 if (starta < entry->start) 2089 starta = entry->start; 2090 } 2091 prot = entry->protection; 2092 2093 /* 2094 * If pmap_enter() has enabled write access on a nearby mapping, then 2095 * don't attempt promotion, because it will fail. 2096 */ 2097 if ((fs->prot & VM_PROT_WRITE) != 0) 2098 prot |= VM_PROT_NO_PROMOTE; 2099 2100 /* 2101 * Generate the sequence of virtual addresses that are candidates for 2102 * prefaulting in an outward spiral from the faulting virtual address, 2103 * "addra". Specifically, the sequence is "addra - PAGE_SIZE", "addra 2104 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ... 2105 * If the candidate address doesn't have a backing physical page, then 2106 * the loop immediately terminates. 2107 */ 2108 for (i = 0; i < 2 * imax(backward, forward); i++) { 2109 addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE : 2110 PAGE_SIZE); 2111 if (addr > addra + forward * PAGE_SIZE) 2112 addr = 0; 2113 2114 if (addr < starta || addr >= entry->end) 2115 continue; 2116 2117 if (!pmap_is_prefaultable(pmap, addr)) 2118 continue; 2119 2120 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT; 2121 lobject = entry->object.vm_object; 2122 if (!obj_locked) 2123 VM_OBJECT_RLOCK(lobject); 2124 while ((m = vm_page_lookup(lobject, pindex)) == NULL && 2125 !vm_fault_object_needs_getpages(lobject) && 2126 (backing_object = lobject->backing_object) != NULL) { 2127 KASSERT((lobject->backing_object_offset & PAGE_MASK) == 2128 0, ("vm_fault_prefault: unaligned object offset")); 2129 pindex += lobject->backing_object_offset >> PAGE_SHIFT; 2130 VM_OBJECT_RLOCK(backing_object); 2131 if (!obj_locked || lobject != entry->object.vm_object) 2132 VM_OBJECT_RUNLOCK(lobject); 2133 lobject = backing_object; 2134 } 2135 if (m == NULL) { 2136 if (!obj_locked || lobject != entry->object.vm_object) 2137 VM_OBJECT_RUNLOCK(lobject); 2138 break; 2139 } 2140 if (vm_page_all_valid(m) && 2141 (m->flags & PG_FICTITIOUS) == 0) 2142 pmap_enter_quick(pmap, addr, m, prot); 2143 if (!obj_locked || lobject != entry->object.vm_object) 2144 VM_OBJECT_RUNLOCK(lobject); 2145 } 2146 } 2147 2148 /* 2149 * Hold each of the physical pages that are mapped by the specified 2150 * range of virtual addresses, ["addr", "addr" + "len"), if those 2151 * mappings are valid and allow the specified types of access, "prot". 2152 * If all of the implied pages are successfully held, then the number 2153 * of held pages is assigned to *ppages_count, together with pointers 2154 * to those pages in the array "ma". The returned value is zero. 2155 * 2156 * However, if any of the pages cannot be held, an error is returned, 2157 * and no pages are held. 2158 * Error values: 2159 * ENOMEM - the range is not valid 2160 * EINVAL - the provided vm_page array is too small to hold all pages 2161 * EAGAIN - a page was not mapped, and the thread is in nofaulting mode 2162 * EFAULT - a page with requested permissions cannot be mapped 2163 * (more detailed result from vm_fault() is lost) 2164 */ 2165 int 2166 vm_fault_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len, 2167 vm_prot_t prot, vm_page_t *ma, int max_count, int *ppages_count) 2168 { 2169 vm_offset_t end, va; 2170 vm_page_t *mp; 2171 int count, error; 2172 boolean_t pmap_failed; 2173 2174 if (len == 0) { 2175 *ppages_count = 0; 2176 return (0); 2177 } 2178 end = round_page(addr + len); 2179 addr = trunc_page(addr); 2180 2181 if (!vm_map_range_valid(map, addr, end)) 2182 return (ENOMEM); 2183 2184 if (atop(end - addr) > max_count) 2185 return (EINVAL); 2186 count = atop(end - addr); 2187 2188 /* 2189 * Most likely, the physical pages are resident in the pmap, so it is 2190 * faster to try pmap_extract_and_hold() first. 2191 */ 2192 pmap_failed = FALSE; 2193 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) { 2194 *mp = pmap_extract_and_hold(map->pmap, va, prot); 2195 if (*mp == NULL) 2196 pmap_failed = TRUE; 2197 else if ((prot & VM_PROT_WRITE) != 0 && 2198 (*mp)->dirty != VM_PAGE_BITS_ALL) { 2199 /* 2200 * Explicitly dirty the physical page. Otherwise, the 2201 * caller's changes may go unnoticed because they are 2202 * performed through an unmanaged mapping or by a DMA 2203 * operation. 2204 * 2205 * The object lock is not held here. 2206 * See vm_page_clear_dirty_mask(). 2207 */ 2208 vm_page_dirty(*mp); 2209 } 2210 } 2211 if (pmap_failed) { 2212 /* 2213 * One or more pages could not be held by the pmap. Either no 2214 * page was mapped at the specified virtual address or that 2215 * mapping had insufficient permissions. Attempt to fault in 2216 * and hold these pages. 2217 * 2218 * If vm_fault_disable_pagefaults() was called, 2219 * i.e., TDP_NOFAULTING is set, we must not sleep nor 2220 * acquire MD VM locks, which means we must not call 2221 * vm_fault(). Some (out of tree) callers mark 2222 * too wide a code area with vm_fault_disable_pagefaults() 2223 * already, use the VM_PROT_QUICK_NOFAULT flag to request 2224 * the proper behaviour explicitly. 2225 */ 2226 if ((prot & VM_PROT_QUICK_NOFAULT) != 0 && 2227 (curthread->td_pflags & TDP_NOFAULTING) != 0) { 2228 error = EAGAIN; 2229 goto fail; 2230 } 2231 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) { 2232 if (*mp == NULL && vm_fault(map, va, prot, 2233 VM_FAULT_NORMAL, mp) != KERN_SUCCESS) { 2234 error = EFAULT; 2235 goto fail; 2236 } 2237 } 2238 } 2239 *ppages_count = count; 2240 return (0); 2241 fail: 2242 for (mp = ma; mp < ma + count; mp++) 2243 if (*mp != NULL) 2244 vm_page_unwire(*mp, PQ_INACTIVE); 2245 return (error); 2246 } 2247 2248 /* 2249 * Hold each of the physical pages that are mapped by the specified range of 2250 * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid 2251 * and allow the specified types of access, "prot". If all of the implied 2252 * pages are successfully held, then the number of held pages is returned 2253 * together with pointers to those pages in the array "ma". However, if any 2254 * of the pages cannot be held, -1 is returned. 2255 */ 2256 int 2257 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len, 2258 vm_prot_t prot, vm_page_t *ma, int max_count) 2259 { 2260 int error, pages_count; 2261 2262 error = vm_fault_hold_pages(map, addr, len, prot, ma, 2263 max_count, &pages_count); 2264 if (error != 0) { 2265 if (error == EINVAL) 2266 panic("vm_fault_quick_hold_pages: count > max_count"); 2267 return (-1); 2268 } 2269 return (pages_count); 2270 } 2271 2272 /* 2273 * Routine: 2274 * vm_fault_copy_entry 2275 * Function: 2276 * Create new object backing dst_entry with private copy of all 2277 * underlying pages. When src_entry is equal to dst_entry, function 2278 * implements COW for wired-down map entry. Otherwise, it forks 2279 * wired entry into dst_map. 2280 * 2281 * In/out conditions: 2282 * The source and destination maps must be locked for write. 2283 * The source map entry must be wired down (or be a sharing map 2284 * entry corresponding to a main map entry that is wired down). 2285 */ 2286 void 2287 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map __unused, 2288 vm_map_entry_t dst_entry, vm_map_entry_t src_entry, 2289 vm_ooffset_t *fork_charge) 2290 { 2291 struct pctrie_iter pages; 2292 vm_object_t backing_object, dst_object, object, src_object; 2293 vm_pindex_t dst_pindex, pindex, src_pindex; 2294 vm_prot_t access, prot; 2295 vm_offset_t vaddr; 2296 vm_page_t dst_m; 2297 vm_page_t src_m; 2298 bool upgrade; 2299 2300 upgrade = src_entry == dst_entry; 2301 KASSERT(upgrade || dst_entry->object.vm_object == NULL, 2302 ("vm_fault_copy_entry: vm_object not NULL")); 2303 2304 /* 2305 * If not an upgrade, then enter the mappings in the pmap as 2306 * read and/or execute accesses. Otherwise, enter them as 2307 * write accesses. 2308 * 2309 * A writeable large page mapping is only created if all of 2310 * the constituent small page mappings are modified. Marking 2311 * PTEs as modified on inception allows promotion to happen 2312 * without taking potentially large number of soft faults. 2313 */ 2314 access = prot = dst_entry->protection; 2315 if (!upgrade) 2316 access &= ~VM_PROT_WRITE; 2317 2318 src_object = src_entry->object.vm_object; 2319 src_pindex = OFF_TO_IDX(src_entry->offset); 2320 2321 if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) { 2322 dst_object = src_object; 2323 vm_object_reference(dst_object); 2324 } else { 2325 /* 2326 * Create the top-level object for the destination entry. 2327 * Doesn't actually shadow anything - we copy the pages 2328 * directly. 2329 */ 2330 dst_object = vm_object_allocate_anon(atop(dst_entry->end - 2331 dst_entry->start), NULL, NULL); 2332 #if VM_NRESERVLEVEL > 0 2333 dst_object->flags |= OBJ_COLORED; 2334 dst_object->pg_color = atop(dst_entry->start); 2335 #endif 2336 dst_object->domain = src_object->domain; 2337 2338 dst_entry->object.vm_object = dst_object; 2339 dst_entry->offset = 0; 2340 dst_entry->eflags &= ~MAP_ENTRY_VN_EXEC; 2341 } 2342 2343 VM_OBJECT_WLOCK(dst_object); 2344 if (fork_charge != NULL) { 2345 KASSERT(dst_entry->cred == NULL, 2346 ("vm_fault_copy_entry: leaked swp charge")); 2347 dst_object->cred = curthread->td_ucred; 2348 crhold(dst_object->cred); 2349 *fork_charge += ptoa(dst_object->size); 2350 } else if ((dst_object->flags & OBJ_SWAP) != 0 && 2351 dst_object->cred == NULL) { 2352 KASSERT(dst_entry->cred != NULL, ("no cred for entry %p", 2353 dst_entry)); 2354 dst_object->cred = dst_entry->cred; 2355 dst_entry->cred = NULL; 2356 } 2357 2358 /* 2359 * Loop through all of the virtual pages within the entry's 2360 * range, copying each page from the source object to the 2361 * destination object. Since the source is wired, those pages 2362 * must exist. In contrast, the destination is pageable. 2363 * Since the destination object doesn't share any backing storage 2364 * with the source object, all of its pages must be dirtied, 2365 * regardless of whether they can be written. 2366 */ 2367 vm_page_iter_init(&pages, dst_object); 2368 for (vaddr = dst_entry->start, dst_pindex = 0; 2369 vaddr < dst_entry->end; 2370 vaddr += PAGE_SIZE, dst_pindex++) { 2371 again: 2372 /* 2373 * Find the page in the source object, and copy it in. 2374 * Because the source is wired down, the page will be 2375 * in memory. 2376 */ 2377 if (src_object != dst_object) 2378 VM_OBJECT_RLOCK(src_object); 2379 object = src_object; 2380 pindex = src_pindex + dst_pindex; 2381 while ((src_m = vm_page_lookup(object, pindex)) == NULL && 2382 (backing_object = object->backing_object) != NULL) { 2383 /* 2384 * Unless the source mapping is read-only or 2385 * it is presently being upgraded from 2386 * read-only, the first object in the shadow 2387 * chain should provide all of the pages. In 2388 * other words, this loop body should never be 2389 * executed when the source mapping is already 2390 * read/write. 2391 */ 2392 KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 || 2393 upgrade, 2394 ("vm_fault_copy_entry: main object missing page")); 2395 2396 VM_OBJECT_RLOCK(backing_object); 2397 pindex += OFF_TO_IDX(object->backing_object_offset); 2398 if (object != dst_object) 2399 VM_OBJECT_RUNLOCK(object); 2400 object = backing_object; 2401 } 2402 KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing")); 2403 2404 if (object != dst_object) { 2405 /* 2406 * Allocate a page in the destination object. 2407 */ 2408 pindex = (src_object == dst_object ? src_pindex : 0) + 2409 dst_pindex; 2410 dst_m = vm_page_alloc_iter(dst_object, pindex, 2411 VM_ALLOC_NORMAL, &pages); 2412 if (dst_m == NULL) { 2413 VM_OBJECT_WUNLOCK(dst_object); 2414 VM_OBJECT_RUNLOCK(object); 2415 vm_wait(dst_object); 2416 VM_OBJECT_WLOCK(dst_object); 2417 pctrie_iter_reset(&pages); 2418 goto again; 2419 } 2420 2421 /* 2422 * See the comment in vm_fault_cow(). 2423 */ 2424 if (src_object == dst_object && 2425 (object->flags & OBJ_ONEMAPPING) == 0) 2426 pmap_remove_all(src_m); 2427 pmap_copy_page(src_m, dst_m); 2428 2429 /* 2430 * The object lock does not guarantee that "src_m" will 2431 * transition from invalid to valid, but it does ensure 2432 * that "src_m" will not transition from valid to 2433 * invalid. 2434 */ 2435 dst_m->dirty = dst_m->valid = src_m->valid; 2436 VM_OBJECT_RUNLOCK(object); 2437 } else { 2438 dst_m = src_m; 2439 if (vm_page_busy_acquire( 2440 dst_m, VM_ALLOC_WAITFAIL) == 0) { 2441 pctrie_iter_reset(&pages); 2442 goto again; 2443 } 2444 if (dst_m->pindex >= dst_object->size) { 2445 /* 2446 * We are upgrading. Index can occur 2447 * out of bounds if the object type is 2448 * vnode and the file was truncated. 2449 */ 2450 vm_page_xunbusy(dst_m); 2451 break; 2452 } 2453 } 2454 2455 /* 2456 * Enter it in the pmap. If a wired, copy-on-write 2457 * mapping is being replaced by a write-enabled 2458 * mapping, then wire that new mapping. 2459 * 2460 * The page can be invalid if the user called 2461 * msync(MS_INVALIDATE) or truncated the backing vnode 2462 * or shared memory object. In this case, do not 2463 * insert it into pmap, but still do the copy so that 2464 * all copies of the wired map entry have similar 2465 * backing pages. 2466 */ 2467 if (vm_page_all_valid(dst_m)) { 2468 VM_OBJECT_WUNLOCK(dst_object); 2469 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, 2470 access | (upgrade ? PMAP_ENTER_WIRED : 0), 0); 2471 VM_OBJECT_WLOCK(dst_object); 2472 } 2473 2474 /* 2475 * Mark it no longer busy, and put it on the active list. 2476 */ 2477 if (upgrade) { 2478 if (src_m != dst_m) { 2479 vm_page_unwire(src_m, PQ_INACTIVE); 2480 vm_page_wire(dst_m); 2481 } else { 2482 KASSERT(vm_page_wired(dst_m), 2483 ("dst_m %p is not wired", dst_m)); 2484 } 2485 } else { 2486 vm_page_activate(dst_m); 2487 } 2488 vm_page_xunbusy(dst_m); 2489 } 2490 VM_OBJECT_WUNLOCK(dst_object); 2491 if (upgrade) { 2492 dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY); 2493 vm_object_deallocate(src_object); 2494 } 2495 } 2496 2497 /* 2498 * Block entry into the machine-independent layer's page fault handler by 2499 * the calling thread. Subsequent calls to vm_fault() by that thread will 2500 * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of 2501 * spurious page faults. 2502 */ 2503 int 2504 vm_fault_disable_pagefaults(void) 2505 { 2506 2507 return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR)); 2508 } 2509 2510 void 2511 vm_fault_enable_pagefaults(int save) 2512 { 2513 2514 curthread_pflags_restore(save); 2515 } 2516