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