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