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