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