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