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/proc.h> 89 #include <sys/racct.h> 90 #include <sys/refcount.h> 91 #include <sys/resourcevar.h> 92 #include <sys/rwlock.h> 93 #include <sys/signalvar.h> 94 #include <sys/sysctl.h> 95 #include <sys/sysent.h> 96 #include <sys/vmmeter.h> 97 #include <sys/vnode.h> 98 #ifdef KTRACE 99 #include <sys/ktrace.h> 100 #endif 101 102 #include <vm/vm.h> 103 #include <vm/vm_param.h> 104 #include <vm/pmap.h> 105 #include <vm/vm_map.h> 106 #include <vm/vm_object.h> 107 #include <vm/vm_page.h> 108 #include <vm/vm_pageout.h> 109 #include <vm/vm_kern.h> 110 #include <vm/vm_pager.h> 111 #include <vm/vm_extern.h> 112 #include <vm/vm_reserv.h> 113 114 #define PFBAK 4 115 #define PFFOR 4 116 117 #define VM_FAULT_READ_DEFAULT (1 + VM_FAULT_READ_AHEAD_INIT) 118 #define VM_FAULT_READ_MAX (1 + VM_FAULT_READ_AHEAD_MAX) 119 120 #define VM_FAULT_DONTNEED_MIN 1048576 121 122 struct faultstate { 123 vm_page_t m; 124 vm_object_t object; 125 vm_pindex_t pindex; 126 vm_page_t first_m; 127 vm_object_t first_object; 128 vm_pindex_t first_pindex; 129 vm_map_t map; 130 vm_map_entry_t entry; 131 int map_generation; 132 bool lookup_still_valid; 133 struct vnode *vp; 134 }; 135 136 static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, 137 int ahead); 138 static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra, 139 int backward, int forward, bool obj_locked); 140 141 static int vm_pfault_oom_attempts = 3; 142 SYSCTL_INT(_vm, OID_AUTO, pfault_oom_attempts, CTLFLAG_RWTUN, 143 &vm_pfault_oom_attempts, 0, 144 "Number of page allocation attempts in page fault handler before it " 145 "triggers OOM handling"); 146 147 static int vm_pfault_oom_wait = 10; 148 SYSCTL_INT(_vm, OID_AUTO, pfault_oom_wait, CTLFLAG_RWTUN, 149 &vm_pfault_oom_wait, 0, 150 "Number of seconds to wait for free pages before retrying " 151 "the page fault handler"); 152 153 static inline void 154 fault_page_release(vm_page_t *mp) 155 { 156 vm_page_t m; 157 158 m = *mp; 159 if (m != NULL) { 160 /* 161 * We are likely to loop around again and attempt to busy 162 * this page. Deactivating it leaves it available for 163 * pageout while optimizing fault restarts. 164 */ 165 vm_page_lock(m); 166 vm_page_deactivate(m); 167 vm_page_unlock(m); 168 vm_page_xunbusy(m); 169 *mp = NULL; 170 } 171 } 172 173 static inline void 174 fault_page_free(vm_page_t *mp) 175 { 176 vm_page_t m; 177 178 m = *mp; 179 if (m != NULL) { 180 VM_OBJECT_ASSERT_WLOCKED(m->object); 181 if (!vm_page_wired(m)) 182 vm_page_free(m); 183 else 184 vm_page_xunbusy(m); 185 *mp = NULL; 186 } 187 } 188 189 static inline void 190 unlock_map(struct faultstate *fs) 191 { 192 193 if (fs->lookup_still_valid) { 194 vm_map_lookup_done(fs->map, fs->entry); 195 fs->lookup_still_valid = false; 196 } 197 } 198 199 static void 200 unlock_vp(struct faultstate *fs) 201 { 202 203 if (fs->vp != NULL) { 204 vput(fs->vp); 205 fs->vp = NULL; 206 } 207 } 208 209 static void 210 fault_deallocate(struct faultstate *fs) 211 { 212 213 fault_page_release(&fs->m); 214 vm_object_pip_wakeup(fs->object); 215 if (fs->object != fs->first_object) { 216 VM_OBJECT_WLOCK(fs->first_object); 217 fault_page_free(&fs->first_m); 218 VM_OBJECT_WUNLOCK(fs->first_object); 219 vm_object_pip_wakeup(fs->first_object); 220 } 221 vm_object_deallocate(fs->first_object); 222 unlock_map(fs); 223 unlock_vp(fs); 224 } 225 226 static void 227 unlock_and_deallocate(struct faultstate *fs) 228 { 229 230 VM_OBJECT_WUNLOCK(fs->object); 231 fault_deallocate(fs); 232 } 233 234 static void 235 vm_fault_dirty(vm_map_entry_t entry, vm_page_t m, vm_prot_t prot, 236 vm_prot_t fault_type, int fault_flags) 237 { 238 bool need_dirty; 239 240 if (((prot & VM_PROT_WRITE) == 0 && 241 (fault_flags & VM_FAULT_DIRTY) == 0) || 242 (m->oflags & VPO_UNMANAGED) != 0) 243 return; 244 245 VM_PAGE_OBJECT_BUSY_ASSERT(m); 246 247 need_dirty = ((fault_type & VM_PROT_WRITE) != 0 && 248 (fault_flags & VM_FAULT_WIRE) == 0) || 249 (fault_flags & VM_FAULT_DIRTY) != 0; 250 251 vm_object_set_writeable_dirty(m->object); 252 253 /* 254 * If the fault is a write, we know that this page is being 255 * written NOW so dirty it explicitly to save on 256 * pmap_is_modified() calls later. 257 * 258 * Also, since the page is now dirty, we can possibly tell 259 * the pager to release any swap backing the page. 260 */ 261 if (need_dirty && vm_page_set_dirty(m) == 0) { 262 /* 263 * If this is a NOSYNC mmap we do not want to set PGA_NOSYNC 264 * if the page is already dirty to prevent data written with 265 * the expectation of being synced from not being synced. 266 * Likewise if this entry does not request NOSYNC then make 267 * sure the page isn't marked NOSYNC. Applications sharing 268 * data should use the same flags to avoid ping ponging. 269 */ 270 if ((entry->eflags & MAP_ENTRY_NOSYNC) != 0) 271 vm_page_aflag_set(m, PGA_NOSYNC); 272 else 273 vm_page_aflag_clear(m, PGA_NOSYNC); 274 } 275 276 } 277 278 /* 279 * Unlocks fs.first_object and fs.map on success. 280 */ 281 static int 282 vm_fault_soft_fast(struct faultstate *fs, vm_offset_t vaddr, vm_prot_t prot, 283 int fault_type, int fault_flags, boolean_t wired, vm_page_t *m_hold) 284 { 285 vm_page_t m, m_map; 286 #if (defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \ 287 __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)) && \ 288 VM_NRESERVLEVEL > 0 289 vm_page_t m_super; 290 int flags; 291 #endif 292 int psind, rv; 293 294 MPASS(fs->vp == NULL); 295 vm_object_busy(fs->first_object); 296 m = vm_page_lookup(fs->first_object, fs->first_pindex); 297 /* A busy page can be mapped for read|execute access. */ 298 if (m == NULL || ((prot & VM_PROT_WRITE) != 0 && 299 vm_page_busied(m)) || !vm_page_all_valid(m)) { 300 rv = KERN_FAILURE; 301 goto out; 302 } 303 m_map = m; 304 psind = 0; 305 #if (defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \ 306 __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)) && \ 307 VM_NRESERVLEVEL > 0 308 if ((m->flags & PG_FICTITIOUS) == 0 && 309 (m_super = vm_reserv_to_superpage(m)) != NULL && 310 rounddown2(vaddr, pagesizes[m_super->psind]) >= fs->entry->start && 311 roundup2(vaddr + 1, pagesizes[m_super->psind]) <= fs->entry->end && 312 (vaddr & (pagesizes[m_super->psind] - 1)) == (VM_PAGE_TO_PHYS(m) & 313 (pagesizes[m_super->psind] - 1)) && !wired && 314 pmap_ps_enabled(fs->map->pmap)) { 315 flags = PS_ALL_VALID; 316 if ((prot & VM_PROT_WRITE) != 0) { 317 /* 318 * Create a superpage mapping allowing write access 319 * only if none of the constituent pages are busy and 320 * all of them are already dirty (except possibly for 321 * the page that was faulted on). 322 */ 323 flags |= PS_NONE_BUSY; 324 if ((fs->first_object->flags & OBJ_UNMANAGED) == 0) 325 flags |= PS_ALL_DIRTY; 326 } 327 if (vm_page_ps_test(m_super, flags, m)) { 328 m_map = m_super; 329 psind = m_super->psind; 330 vaddr = rounddown2(vaddr, pagesizes[psind]); 331 /* Preset the modified bit for dirty superpages. */ 332 if ((flags & PS_ALL_DIRTY) != 0) 333 fault_type |= VM_PROT_WRITE; 334 } 335 } 336 #endif 337 rv = pmap_enter(fs->map->pmap, vaddr, m_map, prot, fault_type | 338 PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED : 0), psind); 339 if (rv != KERN_SUCCESS) 340 goto out; 341 if (m_hold != NULL) { 342 *m_hold = m; 343 vm_page_wire(m); 344 } 345 vm_fault_dirty(fs->entry, m, prot, fault_type, fault_flags); 346 if (psind == 0 && !wired) 347 vm_fault_prefault(fs, vaddr, PFBAK, PFFOR, true); 348 VM_OBJECT_RUNLOCK(fs->first_object); 349 vm_map_lookup_done(fs->map, fs->entry); 350 curthread->td_ru.ru_minflt++; 351 352 out: 353 vm_object_unbusy(fs->first_object); 354 return (rv); 355 } 356 357 static void 358 vm_fault_restore_map_lock(struct faultstate *fs) 359 { 360 361 VM_OBJECT_ASSERT_WLOCKED(fs->first_object); 362 MPASS(REFCOUNT_COUNT(fs->first_object->paging_in_progress) > 0); 363 364 if (!vm_map_trylock_read(fs->map)) { 365 VM_OBJECT_WUNLOCK(fs->first_object); 366 vm_map_lock_read(fs->map); 367 VM_OBJECT_WLOCK(fs->first_object); 368 } 369 fs->lookup_still_valid = true; 370 } 371 372 static void 373 vm_fault_populate_check_page(vm_page_t m) 374 { 375 376 /* 377 * Check each page to ensure that the pager is obeying the 378 * interface: the page must be installed in the object, fully 379 * valid, and exclusively busied. 380 */ 381 MPASS(m != NULL); 382 MPASS(vm_page_all_valid(m)); 383 MPASS(vm_page_xbusied(m)); 384 } 385 386 static void 387 vm_fault_populate_cleanup(vm_object_t object, vm_pindex_t first, 388 vm_pindex_t last) 389 { 390 vm_page_t m; 391 vm_pindex_t pidx; 392 393 VM_OBJECT_ASSERT_WLOCKED(object); 394 MPASS(first <= last); 395 for (pidx = first, m = vm_page_lookup(object, pidx); 396 pidx <= last; pidx++, m = vm_page_next(m)) { 397 vm_fault_populate_check_page(m); 398 vm_page_lock(m); 399 vm_page_deactivate(m); 400 vm_page_unlock(m); 401 vm_page_xunbusy(m); 402 } 403 } 404 405 static int 406 vm_fault_populate(struct faultstate *fs, vm_prot_t prot, int fault_type, 407 int fault_flags, boolean_t wired, vm_page_t *m_hold) 408 { 409 struct mtx *m_mtx; 410 vm_offset_t vaddr; 411 vm_page_t m; 412 vm_pindex_t map_first, map_last, pager_first, pager_last, pidx; 413 int i, npages, psind, rv; 414 415 MPASS(fs->object == fs->first_object); 416 VM_OBJECT_ASSERT_WLOCKED(fs->first_object); 417 MPASS(REFCOUNT_COUNT(fs->first_object->paging_in_progress) > 0); 418 MPASS(fs->first_object->backing_object == NULL); 419 MPASS(fs->lookup_still_valid); 420 421 pager_first = OFF_TO_IDX(fs->entry->offset); 422 pager_last = pager_first + atop(fs->entry->end - fs->entry->start) - 1; 423 unlock_map(fs); 424 unlock_vp(fs); 425 426 /* 427 * Call the pager (driver) populate() method. 428 * 429 * There is no guarantee that the method will be called again 430 * if the current fault is for read, and a future fault is 431 * for write. Report the entry's maximum allowed protection 432 * to the driver. 433 */ 434 rv = vm_pager_populate(fs->first_object, fs->first_pindex, 435 fault_type, fs->entry->max_protection, &pager_first, &pager_last); 436 437 VM_OBJECT_ASSERT_WLOCKED(fs->first_object); 438 if (rv == VM_PAGER_BAD) { 439 /* 440 * VM_PAGER_BAD is the backdoor for a pager to request 441 * normal fault handling. 442 */ 443 vm_fault_restore_map_lock(fs); 444 if (fs->map->timestamp != fs->map_generation) 445 return (KERN_RESOURCE_SHORTAGE); /* RetryFault */ 446 return (KERN_NOT_RECEIVER); 447 } 448 if (rv != VM_PAGER_OK) 449 return (KERN_FAILURE); /* AKA SIGSEGV */ 450 451 /* Ensure that the driver is obeying the interface. */ 452 MPASS(pager_first <= pager_last); 453 MPASS(fs->first_pindex <= pager_last); 454 MPASS(fs->first_pindex >= pager_first); 455 MPASS(pager_last < fs->first_object->size); 456 457 vm_fault_restore_map_lock(fs); 458 if (fs->map->timestamp != fs->map_generation) { 459 vm_fault_populate_cleanup(fs->first_object, pager_first, 460 pager_last); 461 return (KERN_RESOURCE_SHORTAGE); /* RetryFault */ 462 } 463 464 /* 465 * The map is unchanged after our last unlock. Process the fault. 466 * 467 * The range [pager_first, pager_last] that is given to the 468 * pager is only a hint. The pager may populate any range 469 * within the object that includes the requested page index. 470 * In case the pager expanded the range, clip it to fit into 471 * the map entry. 472 */ 473 map_first = OFF_TO_IDX(fs->entry->offset); 474 if (map_first > pager_first) { 475 vm_fault_populate_cleanup(fs->first_object, pager_first, 476 map_first - 1); 477 pager_first = map_first; 478 } 479 map_last = map_first + atop(fs->entry->end - fs->entry->start) - 1; 480 if (map_last < pager_last) { 481 vm_fault_populate_cleanup(fs->first_object, map_last + 1, 482 pager_last); 483 pager_last = map_last; 484 } 485 for (pidx = pager_first, m = vm_page_lookup(fs->first_object, pidx); 486 pidx <= pager_last; 487 pidx += npages, m = vm_page_next(&m[npages - 1])) { 488 vaddr = fs->entry->start + IDX_TO_OFF(pidx) - fs->entry->offset; 489 #if defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \ 490 __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv) 491 psind = m->psind; 492 if (psind > 0 && ((vaddr & (pagesizes[psind] - 1)) != 0 || 493 pidx + OFF_TO_IDX(pagesizes[psind]) - 1 > pager_last || 494 !pmap_ps_enabled(fs->map->pmap) || wired)) 495 psind = 0; 496 #else 497 psind = 0; 498 #endif 499 npages = atop(pagesizes[psind]); 500 for (i = 0; i < npages; i++) { 501 vm_fault_populate_check_page(&m[i]); 502 vm_fault_dirty(fs->entry, &m[i], prot, fault_type, 503 fault_flags); 504 } 505 VM_OBJECT_WUNLOCK(fs->first_object); 506 rv = pmap_enter(fs->map->pmap, vaddr, m, prot, fault_type | 507 (wired ? PMAP_ENTER_WIRED : 0), psind); 508 #if defined(__amd64__) 509 if (psind > 0 && rv == KERN_FAILURE) { 510 for (i = 0; i < npages; i++) { 511 rv = pmap_enter(fs->map->pmap, vaddr + ptoa(i), 512 &m[i], prot, fault_type | 513 (wired ? PMAP_ENTER_WIRED : 0), 0); 514 MPASS(rv == KERN_SUCCESS); 515 } 516 } 517 #else 518 MPASS(rv == KERN_SUCCESS); 519 #endif 520 VM_OBJECT_WLOCK(fs->first_object); 521 m_mtx = NULL; 522 for (i = 0; i < npages; i++) { 523 if ((fault_flags & VM_FAULT_WIRE) != 0) { 524 vm_page_wire(&m[i]); 525 } else { 526 vm_page_change_lock(&m[i], &m_mtx); 527 vm_page_activate(&m[i]); 528 } 529 if (m_hold != NULL && m[i].pindex == fs->first_pindex) { 530 *m_hold = &m[i]; 531 vm_page_wire(&m[i]); 532 } 533 vm_page_xunbusy(&m[i]); 534 } 535 if (m_mtx != NULL) 536 mtx_unlock(m_mtx); 537 } 538 curthread->td_ru.ru_majflt++; 539 return (KERN_SUCCESS); 540 } 541 542 static int prot_fault_translation; 543 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RWTUN, 544 &prot_fault_translation, 0, 545 "Control signal to deliver on protection fault"); 546 547 /* compat definition to keep common code for signal translation */ 548 #define UCODE_PAGEFLT 12 549 #ifdef T_PAGEFLT 550 _Static_assert(UCODE_PAGEFLT == T_PAGEFLT, "T_PAGEFLT"); 551 #endif 552 553 /* 554 * vm_fault_trap: 555 * 556 * Handle a page fault occurring at the given address, 557 * requiring the given permissions, in the map specified. 558 * If successful, the page is inserted into the 559 * associated physical map. 560 * 561 * NOTE: the given address should be truncated to the 562 * proper page address. 563 * 564 * KERN_SUCCESS is returned if the page fault is handled; otherwise, 565 * a standard error specifying why the fault is fatal is returned. 566 * 567 * The map in question must be referenced, and remains so. 568 * Caller may hold no locks. 569 */ 570 int 571 vm_fault_trap(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 572 int fault_flags, int *signo, int *ucode) 573 { 574 int result; 575 576 MPASS(signo == NULL || ucode != NULL); 577 #ifdef KTRACE 578 if (map != kernel_map && KTRPOINT(curthread, KTR_FAULT)) 579 ktrfault(vaddr, fault_type); 580 #endif 581 result = vm_fault(map, trunc_page(vaddr), fault_type, fault_flags, 582 NULL); 583 KASSERT(result == KERN_SUCCESS || result == KERN_FAILURE || 584 result == KERN_INVALID_ADDRESS || 585 result == KERN_RESOURCE_SHORTAGE || 586 result == KERN_PROTECTION_FAILURE || 587 result == KERN_OUT_OF_BOUNDS, 588 ("Unexpected Mach error %d from vm_fault()", result)); 589 #ifdef KTRACE 590 if (map != kernel_map && KTRPOINT(curthread, KTR_FAULTEND)) 591 ktrfaultend(result); 592 #endif 593 if (result != KERN_SUCCESS && signo != NULL) { 594 switch (result) { 595 case KERN_FAILURE: 596 case KERN_INVALID_ADDRESS: 597 *signo = SIGSEGV; 598 *ucode = SEGV_MAPERR; 599 break; 600 case KERN_RESOURCE_SHORTAGE: 601 *signo = SIGBUS; 602 *ucode = BUS_OOMERR; 603 break; 604 case KERN_OUT_OF_BOUNDS: 605 *signo = SIGBUS; 606 *ucode = BUS_OBJERR; 607 break; 608 case KERN_PROTECTION_FAILURE: 609 if (prot_fault_translation == 0) { 610 /* 611 * Autodetect. This check also covers 612 * the images without the ABI-tag ELF 613 * note. 614 */ 615 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD && 616 curproc->p_osrel >= P_OSREL_SIGSEGV) { 617 *signo = SIGSEGV; 618 *ucode = SEGV_ACCERR; 619 } else { 620 *signo = SIGBUS; 621 *ucode = UCODE_PAGEFLT; 622 } 623 } else if (prot_fault_translation == 1) { 624 /* Always compat mode. */ 625 *signo = SIGBUS; 626 *ucode = UCODE_PAGEFLT; 627 } else { 628 /* Always SIGSEGV mode. */ 629 *signo = SIGSEGV; 630 *ucode = SEGV_ACCERR; 631 } 632 break; 633 default: 634 KASSERT(0, ("Unexpected Mach error %d from vm_fault()", 635 result)); 636 break; 637 } 638 } 639 return (result); 640 } 641 642 static int 643 vm_fault_lock_vnode(struct faultstate *fs) 644 { 645 struct vnode *vp; 646 int error, locked; 647 648 if (fs->object->type != OBJT_VNODE) 649 return (KERN_SUCCESS); 650 vp = fs->object->handle; 651 if (vp == fs->vp) { 652 ASSERT_VOP_LOCKED(vp, "saved vnode is not locked"); 653 return (KERN_SUCCESS); 654 } 655 656 /* 657 * Perform an unlock in case the desired vnode changed while 658 * the map was unlocked during a retry. 659 */ 660 unlock_vp(fs); 661 662 locked = VOP_ISLOCKED(vp); 663 if (locked != LK_EXCLUSIVE) 664 locked = LK_SHARED; 665 666 /* 667 * We must not sleep acquiring the vnode lock while we have 668 * the page exclusive busied or the object's 669 * paging-in-progress count incremented. Otherwise, we could 670 * deadlock. 671 */ 672 error = vget(vp, locked | LK_CANRECURSE | LK_NOWAIT, curthread); 673 if (error == 0) { 674 fs->vp = vp; 675 return (KERN_SUCCESS); 676 } 677 678 vhold(vp); 679 unlock_and_deallocate(fs); 680 error = vget(vp, locked | LK_RETRY | LK_CANRECURSE, curthread); 681 vdrop(vp); 682 fs->vp = vp; 683 KASSERT(error == 0, ("vm_fault: vget failed %d", error)); 684 return (KERN_RESOURCE_SHORTAGE); 685 } 686 687 /* 688 * Wait/Retry if the page is busy. We have to do this if the page is 689 * either exclusive or shared busy because the vm_pager may be using 690 * read busy for pageouts (and even pageins if it is the vnode pager), 691 * and we could end up trying to pagein and pageout the same page 692 * simultaneously. 693 * 694 * We can theoretically allow the busy case on a read fault if the page 695 * is marked valid, but since such pages are typically already pmap'd, 696 * putting that special case in might be more effort then it is worth. 697 * We cannot under any circumstances mess around with a shared busied 698 * page except, perhaps, to pmap it. 699 */ 700 static void 701 vm_fault_busy_sleep(struct faultstate *fs) 702 { 703 /* 704 * Reference the page before unlocking and 705 * sleeping so that the page daemon is less 706 * likely to reclaim it. 707 */ 708 vm_page_aflag_set(fs->m, PGA_REFERENCED); 709 if (fs->object != fs->first_object) { 710 fault_page_release(&fs->first_m); 711 vm_object_pip_wakeup(fs->first_object); 712 } 713 vm_object_pip_wakeup(fs->object); 714 unlock_map(fs); 715 if (fs->m == vm_page_lookup(fs->object, fs->pindex)) 716 vm_page_sleep_if_busy(fs->m, "vmpfw"); 717 VM_OBJECT_WUNLOCK(fs->object); 718 VM_CNT_INC(v_intrans); 719 vm_object_deallocate(fs->first_object); 720 } 721 722 int 723 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 724 int fault_flags, vm_page_t *m_hold) 725 { 726 struct faultstate fs; 727 struct domainset *dset; 728 vm_object_t next_object, retry_object; 729 vm_offset_t e_end, e_start; 730 vm_pindex_t retry_pindex; 731 vm_prot_t prot, retry_prot; 732 int ahead, alloc_req, behind, cluster_offset, era, faultcount; 733 int nera, oom, result, rv; 734 u_char behavior; 735 boolean_t wired; /* Passed by reference. */ 736 bool dead, hardfault, is_first_object_locked; 737 738 VM_CNT_INC(v_vm_faults); 739 740 if ((curthread->td_pflags & TDP_NOFAULTING) != 0) 741 return (KERN_PROTECTION_FAILURE); 742 743 fs.vp = NULL; 744 faultcount = 0; 745 nera = -1; 746 hardfault = false; 747 748 RetryFault: 749 oom = 0; 750 RetryFault_oom: 751 752 /* 753 * Find the backing store object and offset into it to begin the 754 * search. 755 */ 756 fs.map = map; 757 result = vm_map_lookup(&fs.map, vaddr, fault_type | 758 VM_PROT_FAULT_LOOKUP, &fs.entry, &fs.first_object, 759 &fs.first_pindex, &prot, &wired); 760 if (result != KERN_SUCCESS) { 761 unlock_vp(&fs); 762 return (result); 763 } 764 765 fs.map_generation = fs.map->timestamp; 766 767 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) { 768 panic("%s: fault on nofault entry, addr: %#lx", 769 __func__, (u_long)vaddr); 770 } 771 772 if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION && 773 fs.entry->wiring_thread != curthread) { 774 vm_map_unlock_read(fs.map); 775 vm_map_lock(fs.map); 776 if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) && 777 (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) { 778 unlock_vp(&fs); 779 fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 780 vm_map_unlock_and_wait(fs.map, 0); 781 } else 782 vm_map_unlock(fs.map); 783 goto RetryFault; 784 } 785 786 MPASS((fs.entry->eflags & MAP_ENTRY_GUARD) == 0); 787 788 if (wired) 789 fault_type = prot | (fault_type & VM_PROT_COPY); 790 else 791 KASSERT((fault_flags & VM_FAULT_WIRE) == 0, 792 ("!wired && VM_FAULT_WIRE")); 793 794 /* 795 * Try to avoid lock contention on the top-level object through 796 * special-case handling of some types of page faults, specifically, 797 * those that are mapping an existing page from the top-level object. 798 * Under this condition, a read lock on the object suffices, allowing 799 * multiple page faults of a similar type to run in parallel. 800 */ 801 if (fs.vp == NULL /* avoid locked vnode leak */ && 802 (fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0) { 803 VM_OBJECT_RLOCK(fs.first_object); 804 rv = vm_fault_soft_fast(&fs, vaddr, prot, fault_type, 805 fault_flags, wired, m_hold); 806 if (rv == KERN_SUCCESS) 807 return (rv); 808 if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) { 809 VM_OBJECT_RUNLOCK(fs.first_object); 810 VM_OBJECT_WLOCK(fs.first_object); 811 } 812 } else { 813 VM_OBJECT_WLOCK(fs.first_object); 814 } 815 816 /* 817 * Make a reference to this object to prevent its disposal while we 818 * are messing with it. Once we have the reference, the map is free 819 * to be diddled. Since objects reference their shadows (and copies), 820 * they will stay around as well. 821 * 822 * Bump the paging-in-progress count to prevent size changes (e.g. 823 * truncation operations) during I/O. 824 */ 825 vm_object_reference_locked(fs.first_object); 826 vm_object_pip_add(fs.first_object, 1); 827 828 fs.lookup_still_valid = true; 829 830 fs.m = fs.first_m = NULL; 831 832 /* 833 * Search for the page at object/offset. 834 */ 835 fs.object = fs.first_object; 836 fs.pindex = fs.first_pindex; 837 while (TRUE) { 838 KASSERT(fs.m == NULL, 839 ("page still set %p at loop start", fs.m)); 840 /* 841 * If the object is marked for imminent termination, 842 * we retry here, since the collapse pass has raced 843 * with us. Otherwise, if we see terminally dead 844 * object, return fail. 845 */ 846 if ((fs.object->flags & OBJ_DEAD) != 0) { 847 dead = fs.object->type == OBJT_DEAD; 848 unlock_and_deallocate(&fs); 849 if (dead) 850 return (KERN_PROTECTION_FAILURE); 851 pause("vmf_de", 1); 852 goto RetryFault; 853 } 854 855 /* 856 * See if page is resident 857 */ 858 fs.m = vm_page_lookup(fs.object, fs.pindex); 859 if (fs.m != NULL) { 860 if (vm_page_tryxbusy(fs.m) == 0) { 861 vm_fault_busy_sleep(&fs); 862 goto RetryFault; 863 } 864 865 /* 866 * The page is marked busy for other processes and the 867 * pagedaemon. If it still isn't completely valid 868 * (readable), jump to readrest, else break-out ( we 869 * found the page ). 870 */ 871 if (!vm_page_all_valid(fs.m)) 872 goto readrest; 873 break; /* break to PAGE HAS BEEN FOUND */ 874 } 875 KASSERT(fs.m == NULL, ("fs.m should be NULL, not %p", fs.m)); 876 877 /* 878 * Page is not resident. If the pager might contain the page 879 * or this is the beginning of the search, allocate a new 880 * page. (Default objects are zero-fill, so there is no real 881 * pager for them.) 882 */ 883 if (fs.object->type != OBJT_DEFAULT || 884 fs.object == fs.first_object) { 885 if ((fs.object->flags & OBJ_SIZEVNLOCK) != 0) { 886 rv = vm_fault_lock_vnode(&fs); 887 MPASS(rv == KERN_SUCCESS || 888 rv == KERN_RESOURCE_SHORTAGE); 889 if (rv == KERN_RESOURCE_SHORTAGE) 890 goto RetryFault; 891 } 892 if (fs.pindex >= fs.object->size) { 893 unlock_and_deallocate(&fs); 894 return (KERN_OUT_OF_BOUNDS); 895 } 896 897 if (fs.object == fs.first_object && 898 (fs.first_object->flags & OBJ_POPULATE) != 0 && 899 fs.first_object->shadow_count == 0) { 900 rv = vm_fault_populate(&fs, prot, fault_type, 901 fault_flags, wired, m_hold); 902 switch (rv) { 903 case KERN_SUCCESS: 904 case KERN_FAILURE: 905 unlock_and_deallocate(&fs); 906 return (rv); 907 case KERN_RESOURCE_SHORTAGE: 908 unlock_and_deallocate(&fs); 909 goto RetryFault; 910 case KERN_NOT_RECEIVER: 911 /* 912 * Pager's populate() method 913 * returned VM_PAGER_BAD. 914 */ 915 break; 916 default: 917 panic("inconsistent return codes"); 918 } 919 } 920 921 /* 922 * Allocate a new page for this object/offset pair. 923 * 924 * Unlocked read of the p_flag is harmless. At 925 * worst, the P_KILLED might be not observed 926 * there, and allocation can fail, causing 927 * restart and new reading of the p_flag. 928 */ 929 dset = fs.object->domain.dr_policy; 930 if (dset == NULL) 931 dset = curthread->td_domain.dr_policy; 932 if (!vm_page_count_severe_set(&dset->ds_mask) || 933 P_KILLED(curproc)) { 934 #if VM_NRESERVLEVEL > 0 935 vm_object_color(fs.object, atop(vaddr) - 936 fs.pindex); 937 #endif 938 alloc_req = P_KILLED(curproc) ? 939 VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL; 940 if (fs.object->type != OBJT_VNODE && 941 fs.object->backing_object == NULL) 942 alloc_req |= VM_ALLOC_ZERO; 943 fs.m = vm_page_alloc(fs.object, fs.pindex, 944 alloc_req); 945 } 946 if (fs.m == NULL) { 947 unlock_and_deallocate(&fs); 948 if (vm_pfault_oom_attempts < 0 || 949 oom < vm_pfault_oom_attempts) { 950 oom++; 951 vm_waitpfault(dset, 952 vm_pfault_oom_wait * hz); 953 goto RetryFault_oom; 954 } 955 if (bootverbose) 956 printf( 957 "proc %d (%s) failed to alloc page on fault, starting OOM\n", 958 curproc->p_pid, curproc->p_comm); 959 vm_pageout_oom(VM_OOM_MEM_PF); 960 goto RetryFault; 961 } 962 } 963 964 readrest: 965 /* 966 * At this point, we have either allocated a new page or found 967 * an existing page that is only partially valid. 968 * 969 * We hold a reference on the current object and the page is 970 * exclusive busied. 971 */ 972 973 /* 974 * If the pager for the current object might have the page, 975 * then determine the number of additional pages to read and 976 * potentially reprioritize previously read pages for earlier 977 * reclamation. These operations should only be performed 978 * once per page fault. Even if the current pager doesn't 979 * have the page, the number of additional pages to read will 980 * apply to subsequent objects in the shadow chain. 981 */ 982 if (fs.object->type != OBJT_DEFAULT && nera == -1 && 983 !P_KILLED(curproc)) { 984 KASSERT(fs.lookup_still_valid, ("map unlocked")); 985 era = fs.entry->read_ahead; 986 behavior = vm_map_entry_behavior(fs.entry); 987 if (behavior == MAP_ENTRY_BEHAV_RANDOM) { 988 nera = 0; 989 } else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) { 990 nera = VM_FAULT_READ_AHEAD_MAX; 991 if (vaddr == fs.entry->next_read) 992 vm_fault_dontneed(&fs, vaddr, nera); 993 } else if (vaddr == fs.entry->next_read) { 994 /* 995 * This is a sequential fault. Arithmetically 996 * increase the requested number of pages in 997 * the read-ahead window. The requested 998 * number of pages is "# of sequential faults 999 * x (read ahead min + 1) + read ahead min" 1000 */ 1001 nera = VM_FAULT_READ_AHEAD_MIN; 1002 if (era > 0) { 1003 nera += era + 1; 1004 if (nera > VM_FAULT_READ_AHEAD_MAX) 1005 nera = VM_FAULT_READ_AHEAD_MAX; 1006 } 1007 if (era == VM_FAULT_READ_AHEAD_MAX) 1008 vm_fault_dontneed(&fs, vaddr, nera); 1009 } else { 1010 /* 1011 * This is a non-sequential fault. 1012 */ 1013 nera = 0; 1014 } 1015 if (era != nera) { 1016 /* 1017 * A read lock on the map suffices to update 1018 * the read ahead count safely. 1019 */ 1020 fs.entry->read_ahead = nera; 1021 } 1022 1023 /* 1024 * Prepare for unlocking the map. Save the map 1025 * entry's start and end addresses, which are used to 1026 * optimize the size of the pager operation below. 1027 * Even if the map entry's addresses change after 1028 * unlocking the map, using the saved addresses is 1029 * safe. 1030 */ 1031 e_start = fs.entry->start; 1032 e_end = fs.entry->end; 1033 } 1034 1035 /* 1036 * Call the pager to retrieve the page if there is a chance 1037 * that the pager has it, and potentially retrieve additional 1038 * pages at the same time. 1039 */ 1040 if (fs.object->type != OBJT_DEFAULT) { 1041 /* 1042 * Release the map lock before locking the vnode or 1043 * sleeping in the pager. (If the current object has 1044 * a shadow, then an earlier iteration of this loop 1045 * may have already unlocked the map.) 1046 */ 1047 unlock_map(&fs); 1048 1049 rv = vm_fault_lock_vnode(&fs); 1050 MPASS(rv == KERN_SUCCESS || 1051 rv == KERN_RESOURCE_SHORTAGE); 1052 if (rv == KERN_RESOURCE_SHORTAGE) 1053 goto RetryFault; 1054 KASSERT(fs.vp == NULL || !fs.map->system_map, 1055 ("vm_fault: vnode-backed object mapped by system map")); 1056 1057 /* 1058 * Page in the requested page and hint the pager, 1059 * that it may bring up surrounding pages. 1060 */ 1061 if (nera == -1 || behavior == MAP_ENTRY_BEHAV_RANDOM || 1062 P_KILLED(curproc)) { 1063 behind = 0; 1064 ahead = 0; 1065 } else { 1066 /* Is this a sequential fault? */ 1067 if (nera > 0) { 1068 behind = 0; 1069 ahead = nera; 1070 } else { 1071 /* 1072 * Request a cluster of pages that is 1073 * aligned to a VM_FAULT_READ_DEFAULT 1074 * page offset boundary within the 1075 * object. Alignment to a page offset 1076 * boundary is more likely to coincide 1077 * with the underlying file system 1078 * block than alignment to a virtual 1079 * address boundary. 1080 */ 1081 cluster_offset = fs.pindex % 1082 VM_FAULT_READ_DEFAULT; 1083 behind = ulmin(cluster_offset, 1084 atop(vaddr - e_start)); 1085 ahead = VM_FAULT_READ_DEFAULT - 1 - 1086 cluster_offset; 1087 } 1088 ahead = ulmin(ahead, atop(e_end - vaddr) - 1); 1089 } 1090 rv = vm_pager_get_pages(fs.object, &fs.m, 1, 1091 &behind, &ahead); 1092 if (rv == VM_PAGER_OK) { 1093 faultcount = behind + 1 + ahead; 1094 hardfault = true; 1095 break; /* break to PAGE HAS BEEN FOUND */ 1096 } 1097 if (rv == VM_PAGER_ERROR) 1098 printf("vm_fault: pager read error, pid %d (%s)\n", 1099 curproc->p_pid, curproc->p_comm); 1100 1101 /* 1102 * If an I/O error occurred or the requested page was 1103 * outside the range of the pager, clean up and return 1104 * an error. 1105 */ 1106 if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) { 1107 fault_page_free(&fs.m); 1108 unlock_and_deallocate(&fs); 1109 return (KERN_OUT_OF_BOUNDS); 1110 } 1111 1112 } 1113 1114 /* 1115 * The requested page does not exist at this object/ 1116 * offset. Remove the invalid page from the object, 1117 * waking up anyone waiting for it, and continue on to 1118 * the next object. However, if this is the top-level 1119 * object, we must leave the busy page in place to 1120 * prevent another process from rushing past us, and 1121 * inserting the page in that object at the same time 1122 * that we are. 1123 */ 1124 if (fs.object == fs.first_object) { 1125 fs.first_m = fs.m; 1126 fs.m = NULL; 1127 } else 1128 fault_page_free(&fs.m); 1129 1130 /* 1131 * Move on to the next object. Lock the next object before 1132 * unlocking the current one. 1133 */ 1134 next_object = fs.object->backing_object; 1135 if (next_object == NULL) { 1136 /* 1137 * If there's no object left, fill the page in the top 1138 * object with zeros. 1139 */ 1140 if (fs.object != fs.first_object) { 1141 vm_object_pip_wakeup(fs.object); 1142 VM_OBJECT_WUNLOCK(fs.object); 1143 1144 fs.object = fs.first_object; 1145 fs.pindex = fs.first_pindex; 1146 VM_OBJECT_WLOCK(fs.object); 1147 } 1148 MPASS(fs.first_m != NULL); 1149 MPASS(fs.m == NULL); 1150 fs.m = fs.first_m; 1151 fs.first_m = NULL; 1152 1153 /* 1154 * Zero the page if necessary and mark it valid. 1155 */ 1156 if ((fs.m->flags & PG_ZERO) == 0) { 1157 pmap_zero_page(fs.m); 1158 } else { 1159 VM_CNT_INC(v_ozfod); 1160 } 1161 VM_CNT_INC(v_zfod); 1162 vm_page_valid(fs.m); 1163 /* Don't try to prefault neighboring pages. */ 1164 faultcount = 1; 1165 break; /* break to PAGE HAS BEEN FOUND */ 1166 } else { 1167 MPASS(fs.first_m != NULL); 1168 KASSERT(fs.object != next_object, 1169 ("object loop %p", next_object)); 1170 VM_OBJECT_WLOCK(next_object); 1171 vm_object_pip_add(next_object, 1); 1172 if (fs.object != fs.first_object) 1173 vm_object_pip_wakeup(fs.object); 1174 fs.pindex += 1175 OFF_TO_IDX(fs.object->backing_object_offset); 1176 VM_OBJECT_WUNLOCK(fs.object); 1177 fs.object = next_object; 1178 } 1179 } 1180 1181 vm_page_assert_xbusied(fs.m); 1182 1183 /* 1184 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock 1185 * is held.] 1186 */ 1187 1188 /* 1189 * If the page is being written, but isn't already owned by the 1190 * top-level object, we have to copy it into a new page owned by the 1191 * top-level object. 1192 */ 1193 if (fs.object != fs.first_object) { 1194 /* 1195 * We only really need to copy if we want to write it. 1196 */ 1197 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) { 1198 /* 1199 * This allows pages to be virtually copied from a 1200 * backing_object into the first_object, where the 1201 * backing object has no other refs to it, and cannot 1202 * gain any more refs. Instead of a bcopy, we just 1203 * move the page from the backing object to the 1204 * first object. Note that we must mark the page 1205 * dirty in the first object so that it will go out 1206 * to swap when needed. 1207 */ 1208 is_first_object_locked = false; 1209 if ( 1210 /* 1211 * Only one shadow object 1212 */ 1213 (fs.object->shadow_count == 1) && 1214 /* 1215 * No COW refs, except us 1216 */ 1217 (fs.object->ref_count == 1) && 1218 /* 1219 * No one else can look this object up 1220 */ 1221 (fs.object->handle == NULL) && 1222 /* 1223 * No other ways to look the object up 1224 */ 1225 ((fs.object->flags & OBJ_ANON) != 0) && 1226 (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) && 1227 /* 1228 * We don't chase down the shadow chain 1229 */ 1230 fs.object == fs.first_object->backing_object) { 1231 1232 (void)vm_page_remove(fs.m); 1233 vm_page_replace_checked(fs.m, fs.first_object, 1234 fs.first_pindex, fs.first_m); 1235 vm_page_free(fs.first_m); 1236 vm_page_dirty(fs.m); 1237 #if VM_NRESERVLEVEL > 0 1238 /* 1239 * Rename the reservation. 1240 */ 1241 vm_reserv_rename(fs.m, fs.first_object, 1242 fs.object, OFF_TO_IDX( 1243 fs.first_object->backing_object_offset)); 1244 #endif 1245 VM_OBJECT_WUNLOCK(fs.object); 1246 fs.first_m = fs.m; 1247 fs.m = NULL; 1248 VM_CNT_INC(v_cow_optim); 1249 } else { 1250 VM_OBJECT_WUNLOCK(fs.object); 1251 /* 1252 * Oh, well, lets copy it. 1253 */ 1254 pmap_copy_page(fs.m, fs.first_m); 1255 vm_page_valid(fs.first_m); 1256 if (wired && (fault_flags & 1257 VM_FAULT_WIRE) == 0) { 1258 vm_page_wire(fs.first_m); 1259 vm_page_unwire(fs.m, PQ_INACTIVE); 1260 } 1261 /* 1262 * We no longer need the old page or object. 1263 */ 1264 fault_page_release(&fs.m); 1265 } 1266 /* 1267 * fs.object != fs.first_object due to above 1268 * conditional 1269 */ 1270 vm_object_pip_wakeup(fs.object); 1271 1272 /* 1273 * We only try to prefault read-only mappings to the 1274 * neighboring pages when this copy-on-write fault is 1275 * a hard fault. In other cases, trying to prefault 1276 * is typically wasted effort. 1277 */ 1278 if (faultcount == 0) 1279 faultcount = 1; 1280 1281 /* 1282 * Only use the new page below... 1283 */ 1284 fs.object = fs.first_object; 1285 fs.pindex = fs.first_pindex; 1286 fs.m = fs.first_m; 1287 if (!is_first_object_locked) 1288 VM_OBJECT_WLOCK(fs.object); 1289 VM_CNT_INC(v_cow_faults); 1290 curthread->td_cow++; 1291 } else { 1292 prot &= ~VM_PROT_WRITE; 1293 } 1294 } 1295 1296 /* 1297 * We must verify that the maps have not changed since our last 1298 * lookup. 1299 */ 1300 if (!fs.lookup_still_valid) { 1301 if (!vm_map_trylock_read(fs.map)) { 1302 unlock_and_deallocate(&fs); 1303 goto RetryFault; 1304 } 1305 fs.lookup_still_valid = true; 1306 if (fs.map->timestamp != fs.map_generation) { 1307 result = vm_map_lookup_locked(&fs.map, vaddr, fault_type, 1308 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired); 1309 1310 /* 1311 * If we don't need the page any longer, put it on the inactive 1312 * list (the easiest thing to do here). If no one needs it, 1313 * pageout will grab it eventually. 1314 */ 1315 if (result != KERN_SUCCESS) { 1316 unlock_and_deallocate(&fs); 1317 1318 /* 1319 * If retry of map lookup would have blocked then 1320 * retry fault from start. 1321 */ 1322 if (result == KERN_FAILURE) 1323 goto RetryFault; 1324 return (result); 1325 } 1326 if ((retry_object != fs.first_object) || 1327 (retry_pindex != fs.first_pindex)) { 1328 unlock_and_deallocate(&fs); 1329 goto RetryFault; 1330 } 1331 1332 /* 1333 * Check whether the protection has changed or the object has 1334 * been copied while we left the map unlocked. Changing from 1335 * read to write permission is OK - we leave the page 1336 * write-protected, and catch the write fault. Changing from 1337 * write to read permission means that we can't mark the page 1338 * write-enabled after all. 1339 */ 1340 prot &= retry_prot; 1341 fault_type &= retry_prot; 1342 if (prot == 0) { 1343 unlock_and_deallocate(&fs); 1344 goto RetryFault; 1345 } 1346 1347 /* Reassert because wired may have changed. */ 1348 KASSERT(wired || (fault_flags & VM_FAULT_WIRE) == 0, 1349 ("!wired && VM_FAULT_WIRE")); 1350 } 1351 } 1352 1353 /* 1354 * If the page was filled by a pager, save the virtual address that 1355 * should be faulted on next under a sequential access pattern to the 1356 * map entry. A read lock on the map suffices to update this address 1357 * safely. 1358 */ 1359 if (hardfault) 1360 fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE; 1361 1362 vm_page_assert_xbusied(fs.m); 1363 vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags); 1364 1365 /* 1366 * Page must be completely valid or it is not fit to 1367 * map into user space. vm_pager_get_pages() ensures this. 1368 */ 1369 KASSERT(vm_page_all_valid(fs.m), 1370 ("vm_fault: page %p partially invalid", fs.m)); 1371 VM_OBJECT_WUNLOCK(fs.object); 1372 1373 /* 1374 * Put this page into the physical map. We had to do the unlock above 1375 * because pmap_enter() may sleep. We don't put the page 1376 * back on the active queue until later so that the pageout daemon 1377 * won't find it (yet). 1378 */ 1379 pmap_enter(fs.map->pmap, vaddr, fs.m, prot, 1380 fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0); 1381 if (faultcount != 1 && (fault_flags & VM_FAULT_WIRE) == 0 && 1382 wired == 0) 1383 vm_fault_prefault(&fs, vaddr, 1384 faultcount > 0 ? behind : PFBAK, 1385 faultcount > 0 ? ahead : PFFOR, false); 1386 1387 /* 1388 * If the page is not wired down, then put it where the pageout daemon 1389 * can find it. 1390 */ 1391 if ((fault_flags & VM_FAULT_WIRE) != 0) { 1392 vm_page_wire(fs.m); 1393 } else { 1394 vm_page_lock(fs.m); 1395 vm_page_activate(fs.m); 1396 vm_page_unlock(fs.m); 1397 } 1398 if (m_hold != NULL) { 1399 *m_hold = fs.m; 1400 vm_page_wire(fs.m); 1401 } 1402 vm_page_xunbusy(fs.m); 1403 fs.m = NULL; 1404 1405 /* 1406 * Unlock everything, and return 1407 */ 1408 fault_deallocate(&fs); 1409 if (hardfault) { 1410 VM_CNT_INC(v_io_faults); 1411 curthread->td_ru.ru_majflt++; 1412 #ifdef RACCT 1413 if (racct_enable && fs.object->type == OBJT_VNODE) { 1414 PROC_LOCK(curproc); 1415 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) { 1416 racct_add_force(curproc, RACCT_WRITEBPS, 1417 PAGE_SIZE + behind * PAGE_SIZE); 1418 racct_add_force(curproc, RACCT_WRITEIOPS, 1); 1419 } else { 1420 racct_add_force(curproc, RACCT_READBPS, 1421 PAGE_SIZE + ahead * PAGE_SIZE); 1422 racct_add_force(curproc, RACCT_READIOPS, 1); 1423 } 1424 PROC_UNLOCK(curproc); 1425 } 1426 #endif 1427 } else 1428 curthread->td_ru.ru_minflt++; 1429 1430 return (KERN_SUCCESS); 1431 } 1432 1433 /* 1434 * Speed up the reclamation of pages that precede the faulting pindex within 1435 * the first object of the shadow chain. Essentially, perform the equivalent 1436 * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes 1437 * the faulting pindex by the cluster size when the pages read by vm_fault() 1438 * cross a cluster-size boundary. The cluster size is the greater of the 1439 * smallest superpage size and VM_FAULT_DONTNEED_MIN. 1440 * 1441 * When "fs->first_object" is a shadow object, the pages in the backing object 1442 * that precede the faulting pindex are deactivated by vm_fault(). So, this 1443 * function must only be concerned with pages in the first object. 1444 */ 1445 static void 1446 vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead) 1447 { 1448 vm_map_entry_t entry; 1449 vm_object_t first_object, object; 1450 vm_offset_t end, start; 1451 vm_page_t m, m_next; 1452 vm_pindex_t pend, pstart; 1453 vm_size_t size; 1454 1455 object = fs->object; 1456 VM_OBJECT_ASSERT_WLOCKED(object); 1457 first_object = fs->first_object; 1458 if (first_object != object) { 1459 if (!VM_OBJECT_TRYWLOCK(first_object)) { 1460 VM_OBJECT_WUNLOCK(object); 1461 VM_OBJECT_WLOCK(first_object); 1462 VM_OBJECT_WLOCK(object); 1463 } 1464 } 1465 /* Neither fictitious nor unmanaged pages can be reclaimed. */ 1466 if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) { 1467 size = VM_FAULT_DONTNEED_MIN; 1468 if (MAXPAGESIZES > 1 && size < pagesizes[1]) 1469 size = pagesizes[1]; 1470 end = rounddown2(vaddr, size); 1471 if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) && 1472 (entry = fs->entry)->start < end) { 1473 if (end - entry->start < size) 1474 start = entry->start; 1475 else 1476 start = end - size; 1477 pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED); 1478 pstart = OFF_TO_IDX(entry->offset) + atop(start - 1479 entry->start); 1480 m_next = vm_page_find_least(first_object, pstart); 1481 pend = OFF_TO_IDX(entry->offset) + atop(end - 1482 entry->start); 1483 while ((m = m_next) != NULL && m->pindex < pend) { 1484 m_next = TAILQ_NEXT(m, listq); 1485 if (!vm_page_all_valid(m) || 1486 vm_page_busied(m)) 1487 continue; 1488 1489 /* 1490 * Don't clear PGA_REFERENCED, since it would 1491 * likely represent a reference by a different 1492 * process. 1493 * 1494 * Typically, at this point, prefetched pages 1495 * are still in the inactive queue. Only 1496 * pages that triggered page faults are in the 1497 * active queue. 1498 */ 1499 vm_page_lock(m); 1500 if (!vm_page_inactive(m)) 1501 vm_page_deactivate(m); 1502 vm_page_unlock(m); 1503 } 1504 } 1505 } 1506 if (first_object != object) 1507 VM_OBJECT_WUNLOCK(first_object); 1508 } 1509 1510 /* 1511 * vm_fault_prefault provides a quick way of clustering 1512 * pagefaults into a processes address space. It is a "cousin" 1513 * of vm_map_pmap_enter, except it runs at page fault time instead 1514 * of mmap time. 1515 */ 1516 static void 1517 vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra, 1518 int backward, int forward, bool obj_locked) 1519 { 1520 pmap_t pmap; 1521 vm_map_entry_t entry; 1522 vm_object_t backing_object, lobject; 1523 vm_offset_t addr, starta; 1524 vm_pindex_t pindex; 1525 vm_page_t m; 1526 int i; 1527 1528 pmap = fs->map->pmap; 1529 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) 1530 return; 1531 1532 entry = fs->entry; 1533 1534 if (addra < backward * PAGE_SIZE) { 1535 starta = entry->start; 1536 } else { 1537 starta = addra - backward * PAGE_SIZE; 1538 if (starta < entry->start) 1539 starta = entry->start; 1540 } 1541 1542 /* 1543 * Generate the sequence of virtual addresses that are candidates for 1544 * prefaulting in an outward spiral from the faulting virtual address, 1545 * "addra". Specifically, the sequence is "addra - PAGE_SIZE", "addra 1546 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ... 1547 * If the candidate address doesn't have a backing physical page, then 1548 * the loop immediately terminates. 1549 */ 1550 for (i = 0; i < 2 * imax(backward, forward); i++) { 1551 addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE : 1552 PAGE_SIZE); 1553 if (addr > addra + forward * PAGE_SIZE) 1554 addr = 0; 1555 1556 if (addr < starta || addr >= entry->end) 1557 continue; 1558 1559 if (!pmap_is_prefaultable(pmap, addr)) 1560 continue; 1561 1562 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT; 1563 lobject = entry->object.vm_object; 1564 if (!obj_locked) 1565 VM_OBJECT_RLOCK(lobject); 1566 while ((m = vm_page_lookup(lobject, pindex)) == NULL && 1567 lobject->type == OBJT_DEFAULT && 1568 (backing_object = lobject->backing_object) != NULL) { 1569 KASSERT((lobject->backing_object_offset & PAGE_MASK) == 1570 0, ("vm_fault_prefault: unaligned object offset")); 1571 pindex += lobject->backing_object_offset >> PAGE_SHIFT; 1572 VM_OBJECT_RLOCK(backing_object); 1573 if (!obj_locked || lobject != entry->object.vm_object) 1574 VM_OBJECT_RUNLOCK(lobject); 1575 lobject = backing_object; 1576 } 1577 if (m == NULL) { 1578 if (!obj_locked || lobject != entry->object.vm_object) 1579 VM_OBJECT_RUNLOCK(lobject); 1580 break; 1581 } 1582 if (vm_page_all_valid(m) && 1583 (m->flags & PG_FICTITIOUS) == 0) 1584 pmap_enter_quick(pmap, addr, m, entry->protection); 1585 if (!obj_locked || lobject != entry->object.vm_object) 1586 VM_OBJECT_RUNLOCK(lobject); 1587 } 1588 } 1589 1590 /* 1591 * Hold each of the physical pages that are mapped by the specified range of 1592 * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid 1593 * and allow the specified types of access, "prot". If all of the implied 1594 * pages are successfully held, then the number of held pages is returned 1595 * together with pointers to those pages in the array "ma". However, if any 1596 * of the pages cannot be held, -1 is returned. 1597 */ 1598 int 1599 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len, 1600 vm_prot_t prot, vm_page_t *ma, int max_count) 1601 { 1602 vm_offset_t end, va; 1603 vm_page_t *mp; 1604 int count; 1605 boolean_t pmap_failed; 1606 1607 if (len == 0) 1608 return (0); 1609 end = round_page(addr + len); 1610 addr = trunc_page(addr); 1611 1612 /* 1613 * Check for illegal addresses. 1614 */ 1615 if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map)) 1616 return (-1); 1617 1618 if (atop(end - addr) > max_count) 1619 panic("vm_fault_quick_hold_pages: count > max_count"); 1620 count = atop(end - addr); 1621 1622 /* 1623 * Most likely, the physical pages are resident in the pmap, so it is 1624 * faster to try pmap_extract_and_hold() first. 1625 */ 1626 pmap_failed = FALSE; 1627 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) { 1628 *mp = pmap_extract_and_hold(map->pmap, va, prot); 1629 if (*mp == NULL) 1630 pmap_failed = TRUE; 1631 else if ((prot & VM_PROT_WRITE) != 0 && 1632 (*mp)->dirty != VM_PAGE_BITS_ALL) { 1633 /* 1634 * Explicitly dirty the physical page. Otherwise, the 1635 * caller's changes may go unnoticed because they are 1636 * performed through an unmanaged mapping or by a DMA 1637 * operation. 1638 * 1639 * The object lock is not held here. 1640 * See vm_page_clear_dirty_mask(). 1641 */ 1642 vm_page_dirty(*mp); 1643 } 1644 } 1645 if (pmap_failed) { 1646 /* 1647 * One or more pages could not be held by the pmap. Either no 1648 * page was mapped at the specified virtual address or that 1649 * mapping had insufficient permissions. Attempt to fault in 1650 * and hold these pages. 1651 * 1652 * If vm_fault_disable_pagefaults() was called, 1653 * i.e., TDP_NOFAULTING is set, we must not sleep nor 1654 * acquire MD VM locks, which means we must not call 1655 * vm_fault(). Some (out of tree) callers mark 1656 * too wide a code area with vm_fault_disable_pagefaults() 1657 * already, use the VM_PROT_QUICK_NOFAULT flag to request 1658 * the proper behaviour explicitly. 1659 */ 1660 if ((prot & VM_PROT_QUICK_NOFAULT) != 0 && 1661 (curthread->td_pflags & TDP_NOFAULTING) != 0) 1662 goto error; 1663 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) 1664 if (*mp == NULL && vm_fault(map, va, prot, 1665 VM_FAULT_NORMAL, mp) != KERN_SUCCESS) 1666 goto error; 1667 } 1668 return (count); 1669 error: 1670 for (mp = ma; mp < ma + count; mp++) 1671 if (*mp != NULL) 1672 vm_page_unwire(*mp, PQ_INACTIVE); 1673 return (-1); 1674 } 1675 1676 /* 1677 * Routine: 1678 * vm_fault_copy_entry 1679 * Function: 1680 * Create new shadow object backing dst_entry with private copy of 1681 * all underlying pages. When src_entry is equal to dst_entry, 1682 * function implements COW for wired-down map entry. Otherwise, 1683 * it forks wired entry into dst_map. 1684 * 1685 * In/out conditions: 1686 * The source and destination maps must be locked for write. 1687 * The source map entry must be wired down (or be a sharing map 1688 * entry corresponding to a main map entry that is wired down). 1689 */ 1690 void 1691 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map, 1692 vm_map_entry_t dst_entry, vm_map_entry_t src_entry, 1693 vm_ooffset_t *fork_charge) 1694 { 1695 vm_object_t backing_object, dst_object, object, src_object; 1696 vm_pindex_t dst_pindex, pindex, src_pindex; 1697 vm_prot_t access, prot; 1698 vm_offset_t vaddr; 1699 vm_page_t dst_m; 1700 vm_page_t src_m; 1701 boolean_t upgrade; 1702 1703 #ifdef lint 1704 src_map++; 1705 #endif /* lint */ 1706 1707 upgrade = src_entry == dst_entry; 1708 access = prot = dst_entry->protection; 1709 1710 src_object = src_entry->object.vm_object; 1711 src_pindex = OFF_TO_IDX(src_entry->offset); 1712 1713 if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) { 1714 dst_object = src_object; 1715 vm_object_reference(dst_object); 1716 } else { 1717 /* 1718 * Create the top-level object for the destination entry. 1719 * Doesn't actually shadow anything - we copy the pages 1720 * directly. 1721 */ 1722 dst_object = vm_object_allocate_anon(atop(dst_entry->end - 1723 dst_entry->start), NULL, NULL, 0); 1724 #if VM_NRESERVLEVEL > 0 1725 dst_object->flags |= OBJ_COLORED; 1726 dst_object->pg_color = atop(dst_entry->start); 1727 #endif 1728 dst_object->domain = src_object->domain; 1729 dst_object->charge = dst_entry->end - dst_entry->start; 1730 } 1731 1732 VM_OBJECT_WLOCK(dst_object); 1733 KASSERT(upgrade || dst_entry->object.vm_object == NULL, 1734 ("vm_fault_copy_entry: vm_object not NULL")); 1735 if (src_object != dst_object) { 1736 dst_entry->object.vm_object = dst_object; 1737 dst_entry->offset = 0; 1738 dst_entry->eflags &= ~MAP_ENTRY_VN_EXEC; 1739 } 1740 if (fork_charge != NULL) { 1741 KASSERT(dst_entry->cred == NULL, 1742 ("vm_fault_copy_entry: leaked swp charge")); 1743 dst_object->cred = curthread->td_ucred; 1744 crhold(dst_object->cred); 1745 *fork_charge += dst_object->charge; 1746 } else if ((dst_object->type == OBJT_DEFAULT || 1747 dst_object->type == OBJT_SWAP) && 1748 dst_object->cred == NULL) { 1749 KASSERT(dst_entry->cred != NULL, ("no cred for entry %p", 1750 dst_entry)); 1751 dst_object->cred = dst_entry->cred; 1752 dst_entry->cred = NULL; 1753 } 1754 1755 /* 1756 * If not an upgrade, then enter the mappings in the pmap as 1757 * read and/or execute accesses. Otherwise, enter them as 1758 * write accesses. 1759 * 1760 * A writeable large page mapping is only created if all of 1761 * the constituent small page mappings are modified. Marking 1762 * PTEs as modified on inception allows promotion to happen 1763 * without taking potentially large number of soft faults. 1764 */ 1765 if (!upgrade) 1766 access &= ~VM_PROT_WRITE; 1767 1768 /* 1769 * Loop through all of the virtual pages within the entry's 1770 * range, copying each page from the source object to the 1771 * destination object. Since the source is wired, those pages 1772 * must exist. In contrast, the destination is pageable. 1773 * Since the destination object doesn't share any backing storage 1774 * with the source object, all of its pages must be dirtied, 1775 * regardless of whether they can be written. 1776 */ 1777 for (vaddr = dst_entry->start, dst_pindex = 0; 1778 vaddr < dst_entry->end; 1779 vaddr += PAGE_SIZE, dst_pindex++) { 1780 again: 1781 /* 1782 * Find the page in the source object, and copy it in. 1783 * Because the source is wired down, the page will be 1784 * in memory. 1785 */ 1786 if (src_object != dst_object) 1787 VM_OBJECT_RLOCK(src_object); 1788 object = src_object; 1789 pindex = src_pindex + dst_pindex; 1790 while ((src_m = vm_page_lookup(object, pindex)) == NULL && 1791 (backing_object = object->backing_object) != NULL) { 1792 /* 1793 * Unless the source mapping is read-only or 1794 * it is presently being upgraded from 1795 * read-only, the first object in the shadow 1796 * chain should provide all of the pages. In 1797 * other words, this loop body should never be 1798 * executed when the source mapping is already 1799 * read/write. 1800 */ 1801 KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 || 1802 upgrade, 1803 ("vm_fault_copy_entry: main object missing page")); 1804 1805 VM_OBJECT_RLOCK(backing_object); 1806 pindex += OFF_TO_IDX(object->backing_object_offset); 1807 if (object != dst_object) 1808 VM_OBJECT_RUNLOCK(object); 1809 object = backing_object; 1810 } 1811 KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing")); 1812 1813 if (object != dst_object) { 1814 /* 1815 * Allocate a page in the destination object. 1816 */ 1817 dst_m = vm_page_alloc(dst_object, (src_object == 1818 dst_object ? src_pindex : 0) + dst_pindex, 1819 VM_ALLOC_NORMAL); 1820 if (dst_m == NULL) { 1821 VM_OBJECT_WUNLOCK(dst_object); 1822 VM_OBJECT_RUNLOCK(object); 1823 vm_wait(dst_object); 1824 VM_OBJECT_WLOCK(dst_object); 1825 goto again; 1826 } 1827 pmap_copy_page(src_m, dst_m); 1828 VM_OBJECT_RUNLOCK(object); 1829 dst_m->dirty = dst_m->valid = src_m->valid; 1830 } else { 1831 dst_m = src_m; 1832 if (vm_page_busy_acquire(dst_m, VM_ALLOC_WAITFAIL) == 0) 1833 goto again; 1834 if (dst_m->pindex >= dst_object->size) { 1835 /* 1836 * We are upgrading. Index can occur 1837 * out of bounds if the object type is 1838 * vnode and the file was truncated. 1839 */ 1840 vm_page_xunbusy(dst_m); 1841 break; 1842 } 1843 } 1844 VM_OBJECT_WUNLOCK(dst_object); 1845 1846 /* 1847 * Enter it in the pmap. If a wired, copy-on-write 1848 * mapping is being replaced by a write-enabled 1849 * mapping, then wire that new mapping. 1850 * 1851 * The page can be invalid if the user called 1852 * msync(MS_INVALIDATE) or truncated the backing vnode 1853 * or shared memory object. In this case, do not 1854 * insert it into pmap, but still do the copy so that 1855 * all copies of the wired map entry have similar 1856 * backing pages. 1857 */ 1858 if (vm_page_all_valid(dst_m)) { 1859 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, 1860 access | (upgrade ? PMAP_ENTER_WIRED : 0), 0); 1861 } 1862 1863 /* 1864 * Mark it no longer busy, and put it on the active list. 1865 */ 1866 VM_OBJECT_WLOCK(dst_object); 1867 1868 if (upgrade) { 1869 if (src_m != dst_m) { 1870 vm_page_unwire(src_m, PQ_INACTIVE); 1871 vm_page_wire(dst_m); 1872 } else { 1873 KASSERT(vm_page_wired(dst_m), 1874 ("dst_m %p is not wired", dst_m)); 1875 } 1876 } else { 1877 vm_page_lock(dst_m); 1878 vm_page_activate(dst_m); 1879 vm_page_unlock(dst_m); 1880 } 1881 vm_page_xunbusy(dst_m); 1882 } 1883 VM_OBJECT_WUNLOCK(dst_object); 1884 if (upgrade) { 1885 dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY); 1886 vm_object_deallocate(src_object); 1887 } 1888 } 1889 1890 /* 1891 * Block entry into the machine-independent layer's page fault handler by 1892 * the calling thread. Subsequent calls to vm_fault() by that thread will 1893 * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of 1894 * spurious page faults. 1895 */ 1896 int 1897 vm_fault_disable_pagefaults(void) 1898 { 1899 1900 return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR)); 1901 } 1902 1903 void 1904 vm_fault_enable_pagefaults(int save) 1905 { 1906 1907 curthread_pflags_restore(save); 1908 } 1909