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