xref: /freebsd/sys/vm/vm_page.c (revision ae1a0648b05acf798816e7b83b3c10856de5c8e5)
1 /*-
2  * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
3  *
4  * Copyright (c) 1991 Regents of the University of California.
5  * All rights reserved.
6  * Copyright (c) 1998 Matthew Dillon.  All Rights Reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * The Mach Operating System project at Carnegie-Mellon University.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 /*-
37  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38  * All rights reserved.
39  *
40  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
41  *
42  * Permission to use, copy, modify and distribute this software and
43  * its documentation is hereby granted, provided that both the copyright
44  * notice and this permission notice appear in all copies of the
45  * software, derivative works or modified versions, and any portions
46  * thereof, and that both notices appear in supporting documentation.
47  *
48  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51  *
52  * Carnegie Mellon requests users of this software to return to
53  *
54  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55  *  School of Computer Science
56  *  Carnegie Mellon University
57  *  Pittsburgh PA 15213-3890
58  *
59  * any improvements or extensions that they make and grant Carnegie the
60  * rights to redistribute these changes.
61  */
62 
63 /*
64  *	Resident memory management module.
65  */
66 
67 #include <sys/cdefs.h>
68 #include "opt_vm.h"
69 
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/counter.h>
73 #include <sys/domainset.h>
74 #include <sys/kernel.h>
75 #include <sys/limits.h>
76 #include <sys/linker.h>
77 #include <sys/lock.h>
78 #include <sys/malloc.h>
79 #include <sys/mman.h>
80 #include <sys/msgbuf.h>
81 #include <sys/mutex.h>
82 #include <sys/proc.h>
83 #include <sys/rwlock.h>
84 #include <sys/sleepqueue.h>
85 #include <sys/sbuf.h>
86 #include <sys/sched.h>
87 #include <sys/smp.h>
88 #include <sys/sysctl.h>
89 #include <sys/vmmeter.h>
90 #include <sys/vnode.h>
91 
92 #include <vm/vm.h>
93 #include <vm/pmap.h>
94 #include <vm/vm_param.h>
95 #include <vm/vm_domainset.h>
96 #include <vm/vm_kern.h>
97 #include <vm/vm_map.h>
98 #include <vm/vm_object.h>
99 #include <vm/vm_page.h>
100 #include <vm/vm_pageout.h>
101 #include <vm/vm_phys.h>
102 #include <vm/vm_pagequeue.h>
103 #include <vm/vm_pager.h>
104 #include <vm/vm_radix.h>
105 #include <vm/vm_reserv.h>
106 #include <vm/vm_extern.h>
107 #include <vm/vm_dumpset.h>
108 #include <vm/uma.h>
109 #include <vm/uma_int.h>
110 
111 #include <machine/md_var.h>
112 
113 struct vm_domain vm_dom[MAXMEMDOM];
114 
115 DPCPU_DEFINE_STATIC(struct vm_batchqueue, pqbatch[MAXMEMDOM][PQ_COUNT]);
116 
117 struct mtx_padalign __exclusive_cache_line pa_lock[PA_LOCK_COUNT];
118 
119 struct mtx_padalign __exclusive_cache_line vm_domainset_lock;
120 /* The following fields are protected by the domainset lock. */
121 domainset_t __exclusive_cache_line vm_min_domains;
122 domainset_t __exclusive_cache_line vm_severe_domains;
123 static int vm_min_waiters;
124 static int vm_severe_waiters;
125 static int vm_pageproc_waiters;
126 
127 static SYSCTL_NODE(_vm_stats, OID_AUTO, page, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
128     "VM page statistics");
129 
130 static COUNTER_U64_DEFINE_EARLY(pqstate_commit_retries);
131 SYSCTL_COUNTER_U64(_vm_stats_page, OID_AUTO, pqstate_commit_retries,
132     CTLFLAG_RD, &pqstate_commit_retries,
133     "Number of failed per-page atomic queue state updates");
134 
135 static COUNTER_U64_DEFINE_EARLY(queue_ops);
136 SYSCTL_COUNTER_U64(_vm_stats_page, OID_AUTO, queue_ops,
137     CTLFLAG_RD, &queue_ops,
138     "Number of batched queue operations");
139 
140 static COUNTER_U64_DEFINE_EARLY(queue_nops);
141 SYSCTL_COUNTER_U64(_vm_stats_page, OID_AUTO, queue_nops,
142     CTLFLAG_RD, &queue_nops,
143     "Number of batched queue operations with no effects");
144 
145 /*
146  * bogus page -- for I/O to/from partially complete buffers,
147  * or for paging into sparsely invalid regions.
148  */
149 vm_page_t bogus_page;
150 
151 vm_page_t vm_page_array;
152 long vm_page_array_size;
153 long first_page;
154 
155 struct bitset *vm_page_dump;
156 long vm_page_dump_pages;
157 
158 static TAILQ_HEAD(, vm_page) blacklist_head;
159 static int sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS);
160 SYSCTL_PROC(_vm, OID_AUTO, page_blacklist, CTLTYPE_STRING | CTLFLAG_RD |
161     CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_page_blacklist, "A", "Blacklist pages");
162 
163 static uma_zone_t fakepg_zone;
164 
165 static void vm_page_alloc_check(vm_page_t m);
166 static vm_page_t vm_page_alloc_nofree_domain(int domain, int req);
167 static bool _vm_page_busy_sleep(vm_object_t obj, vm_page_t m,
168     vm_pindex_t pindex, const char *wmesg, int allocflags, bool locked);
169 static void vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits);
170 static void vm_page_enqueue(vm_page_t m, uint8_t queue);
171 static bool vm_page_free_prep(vm_page_t m);
172 static void vm_page_free_toq(vm_page_t m);
173 static void vm_page_init(void *dummy);
174 static int vm_page_insert_after(vm_page_t m, vm_object_t object,
175     vm_pindex_t pindex, vm_page_t mpred);
176 static void vm_page_insert_radixdone(vm_page_t m, vm_object_t object,
177     vm_page_t mpred);
178 static void vm_page_mvqueue(vm_page_t m, const uint8_t queue,
179     const uint16_t nflag);
180 static int vm_page_reclaim_run(int req_class, int domain, u_long npages,
181     vm_page_t m_run, vm_paddr_t high);
182 static void vm_page_release_toq(vm_page_t m, uint8_t nqueue, bool noreuse);
183 static int vm_domain_alloc_fail(struct vm_domain *vmd, vm_object_t object,
184     int req);
185 static int vm_page_zone_import(void *arg, void **store, int cnt, int domain,
186     int flags);
187 static void vm_page_zone_release(void *arg, void **store, int cnt);
188 
189 SYSINIT(vm_page, SI_SUB_VM, SI_ORDER_SECOND, vm_page_init, NULL);
190 
191 static void
192 vm_page_init(void *dummy)
193 {
194 
195 	fakepg_zone = uma_zcreate("fakepg", sizeof(struct vm_page), NULL, NULL,
196 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
197 	bogus_page = vm_page_alloc_noobj(VM_ALLOC_WIRED);
198 }
199 
200 static int pgcache_zone_max_pcpu;
201 SYSCTL_INT(_vm, OID_AUTO, pgcache_zone_max_pcpu,
202     CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &pgcache_zone_max_pcpu, 0,
203     "Per-CPU page cache size");
204 
205 /*
206  * The cache page zone is initialized later since we need to be able to allocate
207  * pages before UMA is fully initialized.
208  */
209 static void
210 vm_page_init_cache_zones(void *dummy __unused)
211 {
212 	struct vm_domain *vmd;
213 	struct vm_pgcache *pgcache;
214 	int cache, domain, maxcache, pool;
215 
216 	TUNABLE_INT_FETCH("vm.pgcache_zone_max_pcpu", &pgcache_zone_max_pcpu);
217 	maxcache = pgcache_zone_max_pcpu * mp_ncpus;
218 	for (domain = 0; domain < vm_ndomains; domain++) {
219 		vmd = VM_DOMAIN(domain);
220 		for (pool = 0; pool < VM_NFREEPOOL; pool++) {
221 			pgcache = &vmd->vmd_pgcache[pool];
222 			pgcache->domain = domain;
223 			pgcache->pool = pool;
224 			pgcache->zone = uma_zcache_create("vm pgcache",
225 			    PAGE_SIZE, NULL, NULL, NULL, NULL,
226 			    vm_page_zone_import, vm_page_zone_release, pgcache,
227 			    UMA_ZONE_VM);
228 
229 			/*
230 			 * Limit each pool's zone to 0.1% of the pages in the
231 			 * domain.
232 			 */
233 			cache = maxcache != 0 ? maxcache :
234 			    vmd->vmd_page_count / 1000;
235 			uma_zone_set_maxcache(pgcache->zone, cache);
236 		}
237 	}
238 }
239 SYSINIT(vm_page2, SI_SUB_VM_CONF, SI_ORDER_ANY, vm_page_init_cache_zones, NULL);
240 
241 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */
242 #if PAGE_SIZE == 32768
243 #ifdef CTASSERT
244 CTASSERT(sizeof(u_long) >= 8);
245 #endif
246 #endif
247 
248 /*
249  *	vm_set_page_size:
250  *
251  *	Sets the page size, perhaps based upon the memory
252  *	size.  Must be called before any use of page-size
253  *	dependent functions.
254  */
255 void
256 vm_set_page_size(void)
257 {
258 	if (vm_cnt.v_page_size == 0)
259 		vm_cnt.v_page_size = PAGE_SIZE;
260 	if (((vm_cnt.v_page_size - 1) & vm_cnt.v_page_size) != 0)
261 		panic("vm_set_page_size: page size not a power of two");
262 }
263 
264 /*
265  *	vm_page_blacklist_next:
266  *
267  *	Find the next entry in the provided string of blacklist
268  *	addresses.  Entries are separated by space, comma, or newline.
269  *	If an invalid integer is encountered then the rest of the
270  *	string is skipped.  Updates the list pointer to the next
271  *	character, or NULL if the string is exhausted or invalid.
272  */
273 static vm_paddr_t
274 vm_page_blacklist_next(char **list, char *end)
275 {
276 	vm_paddr_t bad;
277 	char *cp, *pos;
278 
279 	if (list == NULL || *list == NULL)
280 		return (0);
281 	if (**list =='\0') {
282 		*list = NULL;
283 		return (0);
284 	}
285 
286 	/*
287 	 * If there's no end pointer then the buffer is coming from
288 	 * the kenv and we know it's null-terminated.
289 	 */
290 	if (end == NULL)
291 		end = *list + strlen(*list);
292 
293 	/* Ensure that strtoq() won't walk off the end */
294 	if (*end != '\0') {
295 		if (*end == '\n' || *end == ' ' || *end  == ',')
296 			*end = '\0';
297 		else {
298 			printf("Blacklist not terminated, skipping\n");
299 			*list = NULL;
300 			return (0);
301 		}
302 	}
303 
304 	for (pos = *list; *pos != '\0'; pos = cp) {
305 		bad = strtoq(pos, &cp, 0);
306 		if (*cp == '\0' || *cp == ' ' || *cp == ',' || *cp == '\n') {
307 			if (bad == 0) {
308 				if (++cp < end)
309 					continue;
310 				else
311 					break;
312 			}
313 		} else
314 			break;
315 		if (*cp == '\0' || ++cp >= end)
316 			*list = NULL;
317 		else
318 			*list = cp;
319 		return (trunc_page(bad));
320 	}
321 	printf("Garbage in RAM blacklist, skipping\n");
322 	*list = NULL;
323 	return (0);
324 }
325 
326 bool
327 vm_page_blacklist_add(vm_paddr_t pa, bool verbose)
328 {
329 	struct vm_domain *vmd;
330 	vm_page_t m;
331 	bool found;
332 
333 	m = vm_phys_paddr_to_vm_page(pa);
334 	if (m == NULL)
335 		return (true); /* page does not exist, no failure */
336 
337 	vmd = VM_DOMAIN(vm_phys_domain(pa));
338 	vm_domain_free_lock(vmd);
339 	found = vm_phys_unfree_page(pa);
340 	vm_domain_free_unlock(vmd);
341 	if (found) {
342 		vm_domain_freecnt_inc(vmd, -1);
343 		TAILQ_INSERT_TAIL(&blacklist_head, m, listq);
344 		if (verbose)
345 			printf("Skipping page with pa 0x%jx\n", (uintmax_t)pa);
346 	}
347 	return (found);
348 }
349 
350 /*
351  *	vm_page_blacklist_check:
352  *
353  *	Iterate through the provided string of blacklist addresses, pulling
354  *	each entry out of the physical allocator free list and putting it
355  *	onto a list for reporting via the vm.page_blacklist sysctl.
356  */
357 static void
358 vm_page_blacklist_check(char *list, char *end)
359 {
360 	vm_paddr_t pa;
361 	char *next;
362 
363 	next = list;
364 	while (next != NULL) {
365 		if ((pa = vm_page_blacklist_next(&next, end)) == 0)
366 			continue;
367 		vm_page_blacklist_add(pa, bootverbose);
368 	}
369 }
370 
371 /*
372  *	vm_page_blacklist_load:
373  *
374  *	Search for a special module named "ram_blacklist".  It'll be a
375  *	plain text file provided by the user via the loader directive
376  *	of the same name.
377  */
378 static void
379 vm_page_blacklist_load(char **list, char **end)
380 {
381 	void *mod;
382 	u_char *ptr;
383 	u_int len;
384 
385 	mod = NULL;
386 	ptr = NULL;
387 
388 	mod = preload_search_by_type("ram_blacklist");
389 	if (mod != NULL) {
390 		ptr = preload_fetch_addr(mod);
391 		len = preload_fetch_size(mod);
392         }
393 	*list = ptr;
394 	if (ptr != NULL)
395 		*end = ptr + len;
396 	else
397 		*end = NULL;
398 	return;
399 }
400 
401 static int
402 sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS)
403 {
404 	vm_page_t m;
405 	struct sbuf sbuf;
406 	int error, first;
407 
408 	first = 1;
409 	error = sysctl_wire_old_buffer(req, 0);
410 	if (error != 0)
411 		return (error);
412 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
413 	TAILQ_FOREACH(m, &blacklist_head, listq) {
414 		sbuf_printf(&sbuf, "%s%#jx", first ? "" : ",",
415 		    (uintmax_t)m->phys_addr);
416 		first = 0;
417 	}
418 	error = sbuf_finish(&sbuf);
419 	sbuf_delete(&sbuf);
420 	return (error);
421 }
422 
423 /*
424  * Initialize a dummy page for use in scans of the specified paging queue.
425  * In principle, this function only needs to set the flag PG_MARKER.
426  * Nonetheless, it write busies the page as a safety precaution.
427  */
428 void
429 vm_page_init_marker(vm_page_t marker, int queue, uint16_t aflags)
430 {
431 
432 	bzero(marker, sizeof(*marker));
433 	marker->flags = PG_MARKER;
434 	marker->a.flags = aflags;
435 	marker->busy_lock = VPB_CURTHREAD_EXCLUSIVE;
436 	marker->a.queue = queue;
437 }
438 
439 static void
440 vm_page_domain_init(int domain)
441 {
442 	struct vm_domain *vmd;
443 	struct vm_pagequeue *pq;
444 	int i;
445 
446 	vmd = VM_DOMAIN(domain);
447 	bzero(vmd, sizeof(*vmd));
448 	*__DECONST(const char **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_name) =
449 	    "vm inactive pagequeue";
450 	*__DECONST(const char **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_name) =
451 	    "vm active pagequeue";
452 	*__DECONST(const char **, &vmd->vmd_pagequeues[PQ_LAUNDRY].pq_name) =
453 	    "vm laundry pagequeue";
454 	*__DECONST(const char **,
455 	    &vmd->vmd_pagequeues[PQ_UNSWAPPABLE].pq_name) =
456 	    "vm unswappable pagequeue";
457 	vmd->vmd_domain = domain;
458 	vmd->vmd_page_count = 0;
459 	vmd->vmd_free_count = 0;
460 	vmd->vmd_segs = 0;
461 	vmd->vmd_oom = FALSE;
462 	for (i = 0; i < PQ_COUNT; i++) {
463 		pq = &vmd->vmd_pagequeues[i];
464 		TAILQ_INIT(&pq->pq_pl);
465 		mtx_init(&pq->pq_mutex, pq->pq_name, "vm pagequeue",
466 		    MTX_DEF | MTX_DUPOK);
467 		pq->pq_pdpages = 0;
468 		vm_page_init_marker(&vmd->vmd_markers[i], i, 0);
469 	}
470 	mtx_init(&vmd->vmd_free_mtx, "vm page free queue", NULL, MTX_DEF);
471 	mtx_init(&vmd->vmd_pageout_mtx, "vm pageout lock", NULL, MTX_DEF);
472 	snprintf(vmd->vmd_name, sizeof(vmd->vmd_name), "%d", domain);
473 
474 	/*
475 	 * inacthead is used to provide FIFO ordering for LRU-bypassing
476 	 * insertions.
477 	 */
478 	vm_page_init_marker(&vmd->vmd_inacthead, PQ_INACTIVE, PGA_ENQUEUED);
479 	TAILQ_INSERT_HEAD(&vmd->vmd_pagequeues[PQ_INACTIVE].pq_pl,
480 	    &vmd->vmd_inacthead, plinks.q);
481 
482 	/*
483 	 * The clock pages are used to implement active queue scanning without
484 	 * requeues.  Scans start at clock[0], which is advanced after the scan
485 	 * ends.  When the two clock hands meet, they are reset and scanning
486 	 * resumes from the head of the queue.
487 	 */
488 	vm_page_init_marker(&vmd->vmd_clock[0], PQ_ACTIVE, PGA_ENQUEUED);
489 	vm_page_init_marker(&vmd->vmd_clock[1], PQ_ACTIVE, PGA_ENQUEUED);
490 	TAILQ_INSERT_HEAD(&vmd->vmd_pagequeues[PQ_ACTIVE].pq_pl,
491 	    &vmd->vmd_clock[0], plinks.q);
492 	TAILQ_INSERT_TAIL(&vmd->vmd_pagequeues[PQ_ACTIVE].pq_pl,
493 	    &vmd->vmd_clock[1], plinks.q);
494 }
495 
496 /*
497  * Initialize a physical page in preparation for adding it to the free
498  * lists.
499  */
500 void
501 vm_page_init_page(vm_page_t m, vm_paddr_t pa, int segind, int pool)
502 {
503 	m->object = NULL;
504 	m->ref_count = 0;
505 	m->busy_lock = VPB_FREED;
506 	m->flags = m->a.flags = 0;
507 	m->phys_addr = pa;
508 	m->a.queue = PQ_NONE;
509 	m->psind = 0;
510 	m->segind = segind;
511 	m->order = VM_NFREEORDER;
512 	m->pool = pool;
513 	m->valid = m->dirty = 0;
514 	pmap_page_init(m);
515 }
516 
517 #ifndef PMAP_HAS_PAGE_ARRAY
518 static vm_paddr_t
519 vm_page_array_alloc(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t page_range)
520 {
521 	vm_paddr_t new_end;
522 
523 	/*
524 	 * Reserve an unmapped guard page to trap access to vm_page_array[-1].
525 	 * However, because this page is allocated from KVM, out-of-bounds
526 	 * accesses using the direct map will not be trapped.
527 	 */
528 	*vaddr += PAGE_SIZE;
529 
530 	/*
531 	 * Allocate physical memory for the page structures, and map it.
532 	 */
533 	new_end = trunc_page(end - page_range * sizeof(struct vm_page));
534 	vm_page_array = (vm_page_t)pmap_map(vaddr, new_end, end,
535 	    VM_PROT_READ | VM_PROT_WRITE);
536 	vm_page_array_size = page_range;
537 
538 	return (new_end);
539 }
540 #endif
541 
542 /*
543  *	vm_page_startup:
544  *
545  *	Initializes the resident memory module.  Allocates physical memory for
546  *	bootstrapping UMA and some data structures that are used to manage
547  *	physical pages.  Initializes these structures, and populates the free
548  *	page queues.
549  */
550 vm_offset_t
551 vm_page_startup(vm_offset_t vaddr)
552 {
553 	struct vm_phys_seg *seg;
554 	struct vm_domain *vmd;
555 	vm_page_t m;
556 	char *list, *listend;
557 	vm_paddr_t end, high_avail, low_avail, new_end, size;
558 	vm_paddr_t page_range __unused;
559 	vm_paddr_t last_pa, pa, startp, endp;
560 	u_long pagecount;
561 #if MINIDUMP_PAGE_TRACKING
562 	u_long vm_page_dump_size;
563 #endif
564 	int biggestone, i, segind;
565 #ifdef WITNESS
566 	vm_offset_t mapped;
567 	int witness_size;
568 #endif
569 #if defined(__i386__) && defined(VM_PHYSSEG_DENSE)
570 	long ii;
571 #endif
572 #ifdef VM_FREEPOOL_LAZYINIT
573 	int lazyinit;
574 #endif
575 
576 	vaddr = round_page(vaddr);
577 
578 	vm_phys_early_startup();
579 	biggestone = vm_phys_avail_largest();
580 	end = phys_avail[biggestone+1];
581 
582 	/*
583 	 * Initialize the page and queue locks.
584 	 */
585 	mtx_init(&vm_domainset_lock, "vm domainset lock", NULL, MTX_DEF);
586 	for (i = 0; i < PA_LOCK_COUNT; i++)
587 		mtx_init(&pa_lock[i], "vm page", NULL, MTX_DEF);
588 	for (i = 0; i < vm_ndomains; i++)
589 		vm_page_domain_init(i);
590 
591 	new_end = end;
592 #ifdef WITNESS
593 	witness_size = round_page(witness_startup_count());
594 	new_end -= witness_size;
595 	mapped = pmap_map(&vaddr, new_end, new_end + witness_size,
596 	    VM_PROT_READ | VM_PROT_WRITE);
597 	bzero((void *)mapped, witness_size);
598 	witness_startup((void *)mapped);
599 #endif
600 
601 #if MINIDUMP_PAGE_TRACKING
602 	/*
603 	 * Allocate a bitmap to indicate that a random physical page
604 	 * needs to be included in a minidump.
605 	 *
606 	 * The amd64 port needs this to indicate which direct map pages
607 	 * need to be dumped, via calls to dump_add_page()/dump_drop_page().
608 	 *
609 	 * However, i386 still needs this workspace internally within the
610 	 * minidump code.  In theory, they are not needed on i386, but are
611 	 * included should the sf_buf code decide to use them.
612 	 */
613 	last_pa = 0;
614 	vm_page_dump_pages = 0;
615 	for (i = 0; dump_avail[i + 1] != 0; i += 2) {
616 		vm_page_dump_pages += howmany(dump_avail[i + 1], PAGE_SIZE) -
617 		    dump_avail[i] / PAGE_SIZE;
618 		if (dump_avail[i + 1] > last_pa)
619 			last_pa = dump_avail[i + 1];
620 	}
621 	vm_page_dump_size = round_page(BITSET_SIZE(vm_page_dump_pages));
622 	new_end -= vm_page_dump_size;
623 	vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
624 	    new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
625 	bzero((void *)vm_page_dump, vm_page_dump_size);
626 #if MINIDUMP_STARTUP_PAGE_TRACKING
627 	/*
628 	 * Include the UMA bootstrap pages, witness pages and vm_page_dump
629 	 * in a crash dump.  When pmap_map() uses the direct map, they are
630 	 * not automatically included.
631 	 */
632 	for (pa = new_end; pa < end; pa += PAGE_SIZE)
633 		dump_add_page(pa);
634 #endif
635 #else
636 	(void)last_pa;
637 #endif
638 	phys_avail[biggestone + 1] = new_end;
639 #ifdef __amd64__
640 	/*
641 	 * Request that the physical pages underlying the message buffer be
642 	 * included in a crash dump.  Since the message buffer is accessed
643 	 * through the direct map, they are not automatically included.
644 	 */
645 	pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr);
646 	last_pa = pa + round_page(msgbufsize);
647 	while (pa < last_pa) {
648 		dump_add_page(pa);
649 		pa += PAGE_SIZE;
650 	}
651 #endif
652 	/*
653 	 * Compute the number of pages of memory that will be available for
654 	 * use, taking into account the overhead of a page structure per page.
655 	 * In other words, solve
656 	 *	"available physical memory" - round_page(page_range *
657 	 *	    sizeof(struct vm_page)) = page_range * PAGE_SIZE
658 	 * for page_range.
659 	 */
660 	low_avail = phys_avail[0];
661 	high_avail = phys_avail[1];
662 	for (i = 0; i < vm_phys_nsegs; i++) {
663 		if (vm_phys_segs[i].start < low_avail)
664 			low_avail = vm_phys_segs[i].start;
665 		if (vm_phys_segs[i].end > high_avail)
666 			high_avail = vm_phys_segs[i].end;
667 	}
668 	/* Skip the first chunk.  It is already accounted for. */
669 	for (i = 2; phys_avail[i + 1] != 0; i += 2) {
670 		if (phys_avail[i] < low_avail)
671 			low_avail = phys_avail[i];
672 		if (phys_avail[i + 1] > high_avail)
673 			high_avail = phys_avail[i + 1];
674 	}
675 	first_page = low_avail / PAGE_SIZE;
676 #ifdef VM_PHYSSEG_SPARSE
677 	size = 0;
678 	for (i = 0; i < vm_phys_nsegs; i++)
679 		size += vm_phys_segs[i].end - vm_phys_segs[i].start;
680 	for (i = 0; phys_avail[i + 1] != 0; i += 2)
681 		size += phys_avail[i + 1] - phys_avail[i];
682 #elif defined(VM_PHYSSEG_DENSE)
683 	size = high_avail - low_avail;
684 #else
685 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
686 #endif
687 
688 #ifdef PMAP_HAS_PAGE_ARRAY
689 	pmap_page_array_startup(size / PAGE_SIZE);
690 	biggestone = vm_phys_avail_largest();
691 	end = new_end = phys_avail[biggestone + 1];
692 #else
693 #ifdef VM_PHYSSEG_DENSE
694 	/*
695 	 * In the VM_PHYSSEG_DENSE case, the number of pages can account for
696 	 * the overhead of a page structure per page only if vm_page_array is
697 	 * allocated from the last physical memory chunk.  Otherwise, we must
698 	 * allocate page structures representing the physical memory
699 	 * underlying vm_page_array, even though they will not be used.
700 	 */
701 	if (new_end != high_avail)
702 		page_range = size / PAGE_SIZE;
703 	else
704 #endif
705 	{
706 		page_range = size / (PAGE_SIZE + sizeof(struct vm_page));
707 
708 		/*
709 		 * If the partial bytes remaining are large enough for
710 		 * a page (PAGE_SIZE) without a corresponding
711 		 * 'struct vm_page', then new_end will contain an
712 		 * extra page after subtracting the length of the VM
713 		 * page array.  Compensate by subtracting an extra
714 		 * page from new_end.
715 		 */
716 		if (size % (PAGE_SIZE + sizeof(struct vm_page)) >= PAGE_SIZE) {
717 			if (new_end == high_avail)
718 				high_avail -= PAGE_SIZE;
719 			new_end -= PAGE_SIZE;
720 		}
721 	}
722 	end = new_end;
723 	new_end = vm_page_array_alloc(&vaddr, end, page_range);
724 #endif
725 
726 #if VM_NRESERVLEVEL > 0
727 	/*
728 	 * Allocate physical memory for the reservation management system's
729 	 * data structures, and map it.
730 	 */
731 	new_end = vm_reserv_startup(&vaddr, new_end);
732 #endif
733 #if MINIDUMP_PAGE_TRACKING && MINIDUMP_STARTUP_PAGE_TRACKING
734 	/*
735 	 * Include vm_page_array and vm_reserv_array in a crash dump.
736 	 */
737 	for (pa = new_end; pa < end; pa += PAGE_SIZE)
738 		dump_add_page(pa);
739 #endif
740 	phys_avail[biggestone + 1] = new_end;
741 
742 	/*
743 	 * Add physical memory segments corresponding to the available
744 	 * physical pages.
745 	 */
746 	for (i = 0; phys_avail[i + 1] != 0; i += 2)
747 		if (vm_phys_avail_size(i) != 0)
748 			vm_phys_add_seg(phys_avail[i], phys_avail[i + 1]);
749 
750 	/*
751 	 * Initialize the physical memory allocator.
752 	 */
753 	vm_phys_init();
754 
755 #ifdef VM_FREEPOOL_LAZYINIT
756 	lazyinit = 1;
757 	TUNABLE_INT_FETCH("debug.vm.lazy_page_init", &lazyinit);
758 #endif
759 
760 	/*
761 	 * Initialize the page structures and add every available page to the
762 	 * physical memory allocator's free lists.
763 	 */
764 #if defined(__i386__) && defined(VM_PHYSSEG_DENSE)
765 	for (ii = 0; ii < vm_page_array_size; ii++) {
766 		m = &vm_page_array[ii];
767 		vm_page_init_page(m, (first_page + ii) << PAGE_SHIFT, 0,
768 		    VM_FREEPOOL_DEFAULT);
769 		m->flags = PG_FICTITIOUS;
770 	}
771 #endif
772 	vm_cnt.v_page_count = 0;
773 	for (segind = 0; segind < vm_phys_nsegs; segind++) {
774 		seg = &vm_phys_segs[segind];
775 
776 		/*
777 		 * If lazy vm_page initialization is not enabled, simply
778 		 * initialize all of the pages in the segment.  Otherwise, we
779 		 * only initialize:
780 		 * 1. Pages not covered by phys_avail[], since they might be
781 		 *    freed to the allocator at some future point, e.g., by
782 		 *    kmem_bootstrap_free().
783 		 * 2. The first page of each run of free pages handed to the
784 		 *    vm_phys allocator, which in turn defers initialization
785 		 *    of pages until they are needed.
786 		 * This avoids blocking the boot process for long periods, which
787 		 * may be relevant for VMs (which ought to boot as quickly as
788 		 * possible) and/or systems with large amounts of physical
789 		 * memory.
790 		 */
791 #ifdef VM_FREEPOOL_LAZYINIT
792 		if (lazyinit) {
793 			startp = seg->start;
794 			for (i = 0; phys_avail[i + 1] != 0; i += 2) {
795 				if (startp >= seg->end)
796 					break;
797 
798 				if (phys_avail[i + 1] < startp)
799 					continue;
800 				if (phys_avail[i] <= startp) {
801 					startp = phys_avail[i + 1];
802 					continue;
803 				}
804 
805 				m = vm_phys_seg_paddr_to_vm_page(seg, startp);
806 				for (endp = MIN(phys_avail[i], seg->end);
807 				    startp < endp; startp += PAGE_SIZE, m++) {
808 					vm_page_init_page(m, startp, segind,
809 					    VM_FREEPOOL_DEFAULT);
810 				}
811 			}
812 		} else
813 #endif
814 			for (m = seg->first_page, pa = seg->start;
815 			    pa < seg->end; m++, pa += PAGE_SIZE) {
816 				vm_page_init_page(m, pa, segind,
817 				    VM_FREEPOOL_DEFAULT);
818 			}
819 
820 		/*
821 		 * Add the segment's pages that are covered by one of
822 		 * phys_avail's ranges to the free lists.
823 		 */
824 		for (i = 0; phys_avail[i + 1] != 0; i += 2) {
825 			if (seg->end <= phys_avail[i] ||
826 			    seg->start >= phys_avail[i + 1])
827 				continue;
828 
829 			startp = MAX(seg->start, phys_avail[i]);
830 			endp = MIN(seg->end, phys_avail[i + 1]);
831 			pagecount = (u_long)atop(endp - startp);
832 			if (pagecount == 0)
833 				continue;
834 
835 			m = vm_phys_seg_paddr_to_vm_page(seg, startp);
836 #ifdef VM_FREEPOOL_LAZYINIT
837 			if (lazyinit) {
838 				vm_page_init_page(m, startp, segind,
839 				    VM_FREEPOOL_LAZYINIT);
840 			}
841 #endif
842 			vmd = VM_DOMAIN(seg->domain);
843 			vm_domain_free_lock(vmd);
844 			vm_phys_enqueue_contig(m, pagecount);
845 			vm_domain_free_unlock(vmd);
846 			vm_domain_freecnt_inc(vmd, pagecount);
847 			vm_cnt.v_page_count += (u_int)pagecount;
848 			vmd->vmd_page_count += (u_int)pagecount;
849 			vmd->vmd_segs |= 1UL << segind;
850 		}
851 	}
852 
853 	/*
854 	 * Remove blacklisted pages from the physical memory allocator.
855 	 */
856 	TAILQ_INIT(&blacklist_head);
857 	vm_page_blacklist_load(&list, &listend);
858 	vm_page_blacklist_check(list, listend);
859 
860 	list = kern_getenv("vm.blacklist");
861 	vm_page_blacklist_check(list, NULL);
862 
863 	freeenv(list);
864 #if VM_NRESERVLEVEL > 0
865 	/*
866 	 * Initialize the reservation management system.
867 	 */
868 	vm_reserv_init();
869 #endif
870 
871 	return (vaddr);
872 }
873 
874 void
875 vm_page_reference(vm_page_t m)
876 {
877 
878 	vm_page_aflag_set(m, PGA_REFERENCED);
879 }
880 
881 /*
882  *	vm_page_trybusy
883  *
884  *	Helper routine for grab functions to trylock busy.
885  *
886  *	Returns true on success and false on failure.
887  */
888 static bool
889 vm_page_trybusy(vm_page_t m, int allocflags)
890 {
891 
892 	if ((allocflags & (VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY)) != 0)
893 		return (vm_page_trysbusy(m));
894 	else
895 		return (vm_page_tryxbusy(m));
896 }
897 
898 /*
899  *	vm_page_tryacquire
900  *
901  *	Helper routine for grab functions to trylock busy and wire.
902  *
903  *	Returns true on success and false on failure.
904  */
905 static inline bool
906 vm_page_tryacquire(vm_page_t m, int allocflags)
907 {
908 	bool locked;
909 
910 	locked = vm_page_trybusy(m, allocflags);
911 	if (locked && (allocflags & VM_ALLOC_WIRED) != 0)
912 		vm_page_wire(m);
913 	return (locked);
914 }
915 
916 /*
917  *	vm_page_busy_acquire:
918  *
919  *	Acquire the busy lock as described by VM_ALLOC_* flags.  Will loop
920  *	and drop the object lock if necessary.
921  */
922 bool
923 vm_page_busy_acquire(vm_page_t m, int allocflags)
924 {
925 	vm_object_t obj;
926 	bool locked;
927 
928 	/*
929 	 * The page-specific object must be cached because page
930 	 * identity can change during the sleep, causing the
931 	 * re-lock of a different object.
932 	 * It is assumed that a reference to the object is already
933 	 * held by the callers.
934 	 */
935 	obj = atomic_load_ptr(&m->object);
936 	for (;;) {
937 		if (vm_page_tryacquire(m, allocflags))
938 			return (true);
939 		if ((allocflags & VM_ALLOC_NOWAIT) != 0)
940 			return (false);
941 		if (obj != NULL)
942 			locked = VM_OBJECT_WOWNED(obj);
943 		else
944 			locked = false;
945 		MPASS(locked || vm_page_wired(m));
946 		if (_vm_page_busy_sleep(obj, m, m->pindex, "vmpba", allocflags,
947 		    locked) && locked)
948 			VM_OBJECT_WLOCK(obj);
949 		if ((allocflags & VM_ALLOC_WAITFAIL) != 0)
950 			return (false);
951 		KASSERT(m->object == obj || m->object == NULL,
952 		    ("vm_page_busy_acquire: page %p does not belong to %p",
953 		    m, obj));
954 	}
955 }
956 
957 /*
958  *	vm_page_busy_downgrade:
959  *
960  *	Downgrade an exclusive busy page into a single shared busy page.
961  */
962 void
963 vm_page_busy_downgrade(vm_page_t m)
964 {
965 	u_int x;
966 
967 	vm_page_assert_xbusied(m);
968 
969 	x = vm_page_busy_fetch(m);
970 	for (;;) {
971 		if (atomic_fcmpset_rel_int(&m->busy_lock,
972 		    &x, VPB_SHARERS_WORD(1)))
973 			break;
974 	}
975 	if ((x & VPB_BIT_WAITERS) != 0)
976 		wakeup(m);
977 }
978 
979 /*
980  *
981  *	vm_page_busy_tryupgrade:
982  *
983  *	Attempt to upgrade a single shared busy into an exclusive busy.
984  */
985 int
986 vm_page_busy_tryupgrade(vm_page_t m)
987 {
988 	u_int ce, x;
989 
990 	vm_page_assert_sbusied(m);
991 
992 	x = vm_page_busy_fetch(m);
993 	ce = VPB_CURTHREAD_EXCLUSIVE;
994 	for (;;) {
995 		if (VPB_SHARERS(x) > 1)
996 			return (0);
997 		KASSERT((x & ~VPB_BIT_WAITERS) == VPB_SHARERS_WORD(1),
998 		    ("vm_page_busy_tryupgrade: invalid lock state"));
999 		if (!atomic_fcmpset_acq_int(&m->busy_lock, &x,
1000 		    ce | (x & VPB_BIT_WAITERS)))
1001 			continue;
1002 		return (1);
1003 	}
1004 }
1005 
1006 /*
1007  *	vm_page_sbusied:
1008  *
1009  *	Return a positive value if the page is shared busied, 0 otherwise.
1010  */
1011 int
1012 vm_page_sbusied(vm_page_t m)
1013 {
1014 	u_int x;
1015 
1016 	x = vm_page_busy_fetch(m);
1017 	return ((x & VPB_BIT_SHARED) != 0 && x != VPB_UNBUSIED);
1018 }
1019 
1020 /*
1021  *	vm_page_sunbusy:
1022  *
1023  *	Shared unbusy a page.
1024  */
1025 void
1026 vm_page_sunbusy(vm_page_t m)
1027 {
1028 	u_int x;
1029 
1030 	vm_page_assert_sbusied(m);
1031 
1032 	x = vm_page_busy_fetch(m);
1033 	for (;;) {
1034 		KASSERT(x != VPB_FREED,
1035 		    ("vm_page_sunbusy: Unlocking freed page."));
1036 		if (VPB_SHARERS(x) > 1) {
1037 			if (atomic_fcmpset_int(&m->busy_lock, &x,
1038 			    x - VPB_ONE_SHARER))
1039 				break;
1040 			continue;
1041 		}
1042 		KASSERT((x & ~VPB_BIT_WAITERS) == VPB_SHARERS_WORD(1),
1043 		    ("vm_page_sunbusy: invalid lock state"));
1044 		if (!atomic_fcmpset_rel_int(&m->busy_lock, &x, VPB_UNBUSIED))
1045 			continue;
1046 		if ((x & VPB_BIT_WAITERS) == 0)
1047 			break;
1048 		wakeup(m);
1049 		break;
1050 	}
1051 }
1052 
1053 /*
1054  *	vm_page_busy_sleep:
1055  *
1056  *	Sleep if the page is busy, using the page pointer as wchan.
1057  *	This is used to implement the hard-path of the busying mechanism.
1058  *
1059  *	If VM_ALLOC_IGN_SBUSY is specified in allocflags, the function
1060  *	will not sleep if the page is shared-busy.
1061  *
1062  *	The object lock must be held on entry.
1063  *
1064  *	Returns true if it slept and dropped the object lock, or false
1065  *	if there was no sleep and the lock is still held.
1066  */
1067 bool
1068 vm_page_busy_sleep(vm_page_t m, const char *wmesg, int allocflags)
1069 {
1070 	vm_object_t obj;
1071 
1072 	obj = m->object;
1073 	VM_OBJECT_ASSERT_LOCKED(obj);
1074 
1075 	return (_vm_page_busy_sleep(obj, m, m->pindex, wmesg, allocflags,
1076 	    true));
1077 }
1078 
1079 /*
1080  *	vm_page_busy_sleep_unlocked:
1081  *
1082  *	Sleep if the page is busy, using the page pointer as wchan.
1083  *	This is used to implement the hard-path of busying mechanism.
1084  *
1085  *	If VM_ALLOC_IGN_SBUSY is specified in allocflags, the function
1086  *	will not sleep if the page is shared-busy.
1087  *
1088  *	The object lock must not be held on entry.  The operation will
1089  *	return if the page changes identity.
1090  */
1091 void
1092 vm_page_busy_sleep_unlocked(vm_object_t obj, vm_page_t m, vm_pindex_t pindex,
1093     const char *wmesg, int allocflags)
1094 {
1095 	VM_OBJECT_ASSERT_UNLOCKED(obj);
1096 
1097 	(void)_vm_page_busy_sleep(obj, m, pindex, wmesg, allocflags, false);
1098 }
1099 
1100 /*
1101  *	_vm_page_busy_sleep:
1102  *
1103  *	Internal busy sleep function.  Verifies the page identity and
1104  *	lockstate against parameters.  Returns true if it sleeps and
1105  *	false otherwise.
1106  *
1107  *	allocflags uses VM_ALLOC_* flags to specify the lock required.
1108  *
1109  *	If locked is true the lock will be dropped for any true returns
1110  *	and held for any false returns.
1111  */
1112 static bool
1113 _vm_page_busy_sleep(vm_object_t obj, vm_page_t m, vm_pindex_t pindex,
1114     const char *wmesg, int allocflags, bool locked)
1115 {
1116 	bool xsleep;
1117 	u_int x;
1118 
1119 	/*
1120 	 * If the object is busy we must wait for that to drain to zero
1121 	 * before trying the page again.
1122 	 */
1123 	if (obj != NULL && vm_object_busied(obj)) {
1124 		if (locked)
1125 			VM_OBJECT_DROP(obj);
1126 		vm_object_busy_wait(obj, wmesg);
1127 		return (true);
1128 	}
1129 
1130 	if (!vm_page_busied(m))
1131 		return (false);
1132 
1133 	xsleep = (allocflags & (VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY)) != 0;
1134 	sleepq_lock(m);
1135 	x = vm_page_busy_fetch(m);
1136 	do {
1137 		/*
1138 		 * If the page changes objects or becomes unlocked we can
1139 		 * simply return.
1140 		 */
1141 		if (x == VPB_UNBUSIED ||
1142 		    (xsleep && (x & VPB_BIT_SHARED) != 0) ||
1143 		    m->object != obj || m->pindex != pindex) {
1144 			sleepq_release(m);
1145 			return (false);
1146 		}
1147 		if ((x & VPB_BIT_WAITERS) != 0)
1148 			break;
1149 	} while (!atomic_fcmpset_int(&m->busy_lock, &x, x | VPB_BIT_WAITERS));
1150 	if (locked)
1151 		VM_OBJECT_DROP(obj);
1152 	DROP_GIANT();
1153 	sleepq_add(m, NULL, wmesg, 0, 0);
1154 	sleepq_wait(m, PVM);
1155 	PICKUP_GIANT();
1156 	return (true);
1157 }
1158 
1159 /*
1160  *	vm_page_trysbusy:
1161  *
1162  *	Try to shared busy a page.
1163  *	If the operation succeeds 1 is returned otherwise 0.
1164  *	The operation never sleeps.
1165  */
1166 int
1167 vm_page_trysbusy(vm_page_t m)
1168 {
1169 	vm_object_t obj;
1170 	u_int x;
1171 
1172 	obj = m->object;
1173 	x = vm_page_busy_fetch(m);
1174 	for (;;) {
1175 		if ((x & VPB_BIT_SHARED) == 0)
1176 			return (0);
1177 		/*
1178 		 * Reduce the window for transient busies that will trigger
1179 		 * false negatives in vm_page_ps_test().
1180 		 */
1181 		if (obj != NULL && vm_object_busied(obj))
1182 			return (0);
1183 		if (atomic_fcmpset_acq_int(&m->busy_lock, &x,
1184 		    x + VPB_ONE_SHARER))
1185 			break;
1186 	}
1187 
1188 	/* Refetch the object now that we're guaranteed that it is stable. */
1189 	obj = m->object;
1190 	if (obj != NULL && vm_object_busied(obj)) {
1191 		vm_page_sunbusy(m);
1192 		return (0);
1193 	}
1194 	return (1);
1195 }
1196 
1197 /*
1198  *	vm_page_tryxbusy:
1199  *
1200  *	Try to exclusive busy a page.
1201  *	If the operation succeeds 1 is returned otherwise 0.
1202  *	The operation never sleeps.
1203  */
1204 int
1205 vm_page_tryxbusy(vm_page_t m)
1206 {
1207 	vm_object_t obj;
1208 
1209         if (atomic_cmpset_acq_int(&m->busy_lock, VPB_UNBUSIED,
1210             VPB_CURTHREAD_EXCLUSIVE) == 0)
1211 		return (0);
1212 
1213 	obj = m->object;
1214 	if (obj != NULL && vm_object_busied(obj)) {
1215 		vm_page_xunbusy(m);
1216 		return (0);
1217 	}
1218 	return (1);
1219 }
1220 
1221 static void
1222 vm_page_xunbusy_hard_tail(vm_page_t m)
1223 {
1224 	atomic_store_rel_int(&m->busy_lock, VPB_UNBUSIED);
1225 	/* Wake the waiter. */
1226 	wakeup(m);
1227 }
1228 
1229 /*
1230  *	vm_page_xunbusy_hard:
1231  *
1232  *	Called when unbusy has failed because there is a waiter.
1233  */
1234 void
1235 vm_page_xunbusy_hard(vm_page_t m)
1236 {
1237 	vm_page_assert_xbusied(m);
1238 	vm_page_xunbusy_hard_tail(m);
1239 }
1240 
1241 void
1242 vm_page_xunbusy_hard_unchecked(vm_page_t m)
1243 {
1244 	vm_page_assert_xbusied_unchecked(m);
1245 	vm_page_xunbusy_hard_tail(m);
1246 }
1247 
1248 static void
1249 vm_page_busy_free(vm_page_t m)
1250 {
1251 	u_int x;
1252 
1253 	atomic_thread_fence_rel();
1254 	x = atomic_swap_int(&m->busy_lock, VPB_FREED);
1255 	if ((x & VPB_BIT_WAITERS) != 0)
1256 		wakeup(m);
1257 }
1258 
1259 /*
1260  *	vm_page_unhold_pages:
1261  *
1262  *	Unhold each of the pages that is referenced by the given array.
1263  */
1264 void
1265 vm_page_unhold_pages(vm_page_t *ma, int count)
1266 {
1267 
1268 	for (; count != 0; count--) {
1269 		vm_page_unwire(*ma, PQ_ACTIVE);
1270 		ma++;
1271 	}
1272 }
1273 
1274 vm_page_t
1275 PHYS_TO_VM_PAGE(vm_paddr_t pa)
1276 {
1277 	vm_page_t m;
1278 
1279 #ifdef VM_PHYSSEG_SPARSE
1280 	m = vm_phys_paddr_to_vm_page(pa);
1281 	if (m == NULL)
1282 		m = vm_phys_fictitious_to_vm_page(pa);
1283 	return (m);
1284 #elif defined(VM_PHYSSEG_DENSE)
1285 	long pi;
1286 
1287 	pi = atop(pa);
1288 	if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
1289 		m = &vm_page_array[pi - first_page];
1290 		return (m);
1291 	}
1292 	return (vm_phys_fictitious_to_vm_page(pa));
1293 #else
1294 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
1295 #endif
1296 }
1297 
1298 /*
1299  *	vm_page_getfake:
1300  *
1301  *	Create a fictitious page with the specified physical address and
1302  *	memory attribute.  The memory attribute is the only the machine-
1303  *	dependent aspect of a fictitious page that must be initialized.
1304  */
1305 vm_page_t
1306 vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
1307 {
1308 	vm_page_t m;
1309 
1310 	m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
1311 	vm_page_initfake(m, paddr, memattr);
1312 	return (m);
1313 }
1314 
1315 void
1316 vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
1317 {
1318 
1319 	if ((m->flags & PG_FICTITIOUS) != 0) {
1320 		/*
1321 		 * The page's memattr might have changed since the
1322 		 * previous initialization.  Update the pmap to the
1323 		 * new memattr.
1324 		 */
1325 		goto memattr;
1326 	}
1327 	m->phys_addr = paddr;
1328 	m->a.queue = PQ_NONE;
1329 	/* Fictitious pages don't use "segind". */
1330 	m->flags = PG_FICTITIOUS;
1331 	/* Fictitious pages don't use "order" or "pool". */
1332 	m->oflags = VPO_UNMANAGED;
1333 	m->busy_lock = VPB_CURTHREAD_EXCLUSIVE;
1334 	/* Fictitious pages are unevictable. */
1335 	m->ref_count = 1;
1336 	pmap_page_init(m);
1337 memattr:
1338 	pmap_page_set_memattr(m, memattr);
1339 }
1340 
1341 /*
1342  *	vm_page_putfake:
1343  *
1344  *	Release a fictitious page.
1345  */
1346 void
1347 vm_page_putfake(vm_page_t m)
1348 {
1349 
1350 	KASSERT((m->oflags & VPO_UNMANAGED) != 0, ("managed %p", m));
1351 	KASSERT((m->flags & PG_FICTITIOUS) != 0,
1352 	    ("vm_page_putfake: bad page %p", m));
1353 	vm_page_assert_xbusied(m);
1354 	vm_page_busy_free(m);
1355 	uma_zfree(fakepg_zone, m);
1356 }
1357 
1358 /*
1359  *	vm_page_updatefake:
1360  *
1361  *	Update the given fictitious page to the specified physical address and
1362  *	memory attribute.
1363  */
1364 void
1365 vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
1366 {
1367 
1368 	KASSERT((m->flags & PG_FICTITIOUS) != 0,
1369 	    ("vm_page_updatefake: bad page %p", m));
1370 	m->phys_addr = paddr;
1371 	pmap_page_set_memattr(m, memattr);
1372 }
1373 
1374 /*
1375  *	vm_page_free:
1376  *
1377  *	Free a page.
1378  */
1379 void
1380 vm_page_free(vm_page_t m)
1381 {
1382 
1383 	m->flags &= ~PG_ZERO;
1384 	vm_page_free_toq(m);
1385 }
1386 
1387 /*
1388  *	vm_page_free_zero:
1389  *
1390  *	Free a page to the zerod-pages queue
1391  */
1392 void
1393 vm_page_free_zero(vm_page_t m)
1394 {
1395 
1396 	m->flags |= PG_ZERO;
1397 	vm_page_free_toq(m);
1398 }
1399 
1400 /*
1401  * Unbusy and handle the page queueing for a page from a getpages request that
1402  * was optionally read ahead or behind.
1403  */
1404 void
1405 vm_page_readahead_finish(vm_page_t m)
1406 {
1407 
1408 	/* We shouldn't put invalid pages on queues. */
1409 	KASSERT(!vm_page_none_valid(m), ("%s: %p is invalid", __func__, m));
1410 
1411 	/*
1412 	 * Since the page is not the actually needed one, whether it should
1413 	 * be activated or deactivated is not obvious.  Empirical results
1414 	 * have shown that deactivating the page is usually the best choice,
1415 	 * unless the page is wanted by another thread.
1416 	 */
1417 	if ((vm_page_busy_fetch(m) & VPB_BIT_WAITERS) != 0)
1418 		vm_page_activate(m);
1419 	else
1420 		vm_page_deactivate(m);
1421 	vm_page_xunbusy_unchecked(m);
1422 }
1423 
1424 /*
1425  * Destroy the identity of an invalid page and free it if possible.
1426  * This is intended to be used when reading a page from backing store fails.
1427  */
1428 void
1429 vm_page_free_invalid(vm_page_t m)
1430 {
1431 
1432 	KASSERT(vm_page_none_valid(m), ("page %p is valid", m));
1433 	KASSERT(!pmap_page_is_mapped(m), ("page %p is mapped", m));
1434 	KASSERT(m->object != NULL, ("page %p has no object", m));
1435 	VM_OBJECT_ASSERT_WLOCKED(m->object);
1436 
1437 	/*
1438 	 * We may be attempting to free the page as part of the handling for an
1439 	 * I/O error, in which case the page was xbusied by a different thread.
1440 	 */
1441 	vm_page_xbusy_claim(m);
1442 
1443 	/*
1444 	 * If someone has wired this page while the object lock
1445 	 * was not held, then the thread that unwires is responsible
1446 	 * for freeing the page.  Otherwise just free the page now.
1447 	 * The wire count of this unmapped page cannot change while
1448 	 * we have the page xbusy and the page's object wlocked.
1449 	 */
1450 	if (vm_page_remove(m))
1451 		vm_page_free(m);
1452 }
1453 
1454 /*
1455  *	vm_page_dirty_KBI:		[ internal use only ]
1456  *
1457  *	Set all bits in the page's dirty field.
1458  *
1459  *	The object containing the specified page must be locked if the
1460  *	call is made from the machine-independent layer.
1461  *
1462  *	See vm_page_clear_dirty_mask().
1463  *
1464  *	This function should only be called by vm_page_dirty().
1465  */
1466 void
1467 vm_page_dirty_KBI(vm_page_t m)
1468 {
1469 
1470 	/* Refer to this operation by its public name. */
1471 	KASSERT(vm_page_all_valid(m), ("vm_page_dirty: page is invalid!"));
1472 	m->dirty = VM_PAGE_BITS_ALL;
1473 }
1474 
1475 /*
1476  * Insert the given page into the given object at the given pindex.  mpred is
1477  * used for memq linkage.  From vm_page_insert, lookup is true, mpred is
1478  * initially NULL, and this procedure looks it up.  From vm_page_insert_after,
1479  * lookup is false and mpred is known to the caller to be valid, and may be
1480  * NULL if this will be the page with the lowest pindex.
1481  *
1482  * The procedure is marked __always_inline to suggest to the compiler to
1483  * eliminate the lookup parameter and the associated alternate branch.
1484  */
1485 static __always_inline int
1486 vm_page_insert_lookup(vm_page_t m, vm_object_t object, vm_pindex_t pindex,
1487     vm_page_t mpred, bool lookup)
1488 {
1489 	int error;
1490 
1491 	VM_OBJECT_ASSERT_WLOCKED(object);
1492 	KASSERT(m->object == NULL,
1493 	    ("vm_page_insert: page %p already inserted", m));
1494 
1495 	/*
1496 	 * Record the object/offset pair in this page.
1497 	 */
1498 	m->object = object;
1499 	m->pindex = pindex;
1500 	m->ref_count |= VPRC_OBJREF;
1501 
1502 	/*
1503 	 * Add this page to the object's radix tree, and look up mpred if
1504 	 * needed.
1505 	 */
1506 	if (lookup)
1507 		error = vm_radix_insert_lookup_lt(&object->rtree, m, &mpred);
1508 	else
1509 		error = vm_radix_insert(&object->rtree, m);
1510 	if (__predict_false(error != 0)) {
1511 		m->object = NULL;
1512 		m->pindex = 0;
1513 		m->ref_count &= ~VPRC_OBJREF;
1514 		return (1);
1515 	}
1516 
1517 	/*
1518 	 * Now link into the object's ordered list of backed pages.
1519 	 */
1520 	vm_page_insert_radixdone(m, object, mpred);
1521 	vm_pager_page_inserted(object, m);
1522 	return (0);
1523 }
1524 
1525 /*
1526  *	vm_page_insert:		[ internal use only ]
1527  *
1528  *	Inserts the given mem entry into the object and object list.
1529  *
1530  *	The object must be locked.
1531  */
1532 int
1533 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
1534 {
1535 	return (vm_page_insert_lookup(m, object, pindex, NULL, true));
1536 }
1537 
1538 /*
1539  *	vm_page_insert_after:
1540  *
1541  *	Inserts the page "m" into the specified object at offset "pindex".
1542  *
1543  *	The page "mpred" must immediately precede the offset "pindex" within
1544  *	the specified object.
1545  *
1546  *	The object must be locked.
1547  */
1548 static int
1549 vm_page_insert_after(vm_page_t m, vm_object_t object, vm_pindex_t pindex,
1550     vm_page_t mpred)
1551 {
1552 	return (vm_page_insert_lookup(m, object, pindex, mpred, false));
1553 }
1554 
1555 /*
1556  *	vm_page_insert_radixdone:
1557  *
1558  *	Complete page "m" insertion into the specified object after the
1559  *	radix trie hooking.
1560  *
1561  *	The page "mpred" must precede the offset "m->pindex" within the
1562  *	specified object.
1563  *
1564  *	The object must be locked.
1565  */
1566 static void
1567 vm_page_insert_radixdone(vm_page_t m, vm_object_t object, vm_page_t mpred)
1568 {
1569 
1570 	VM_OBJECT_ASSERT_WLOCKED(object);
1571 	KASSERT(object != NULL && m->object == object,
1572 	    ("vm_page_insert_radixdone: page %p has inconsistent object", m));
1573 	KASSERT((m->ref_count & VPRC_OBJREF) != 0,
1574 	    ("vm_page_insert_radixdone: page %p is missing object ref", m));
1575 	if (mpred != NULL) {
1576 		KASSERT(mpred->object == object,
1577 		    ("vm_page_insert_radixdone: object doesn't contain mpred"));
1578 		KASSERT(mpred->pindex < m->pindex,
1579 		    ("vm_page_insert_radixdone: mpred doesn't precede pindex"));
1580 		KASSERT(TAILQ_NEXT(mpred, listq) == NULL ||
1581 		    m->pindex < TAILQ_NEXT(mpred, listq)->pindex,
1582 		    ("vm_page_insert_radixdone: pindex doesn't precede msucc"));
1583 	} else {
1584 		KASSERT(TAILQ_EMPTY(&object->memq) ||
1585 		    m->pindex < TAILQ_FIRST(&object->memq)->pindex,
1586 		    ("vm_page_insert_radixdone: no mpred but not first page"));
1587 	}
1588 
1589 	if (mpred != NULL)
1590 		TAILQ_INSERT_AFTER(&object->memq, mpred, m, listq);
1591 	else
1592 		TAILQ_INSERT_HEAD(&object->memq, m, listq);
1593 
1594 	/*
1595 	 * Show that the object has one more resident page.
1596 	 */
1597 	object->resident_page_count++;
1598 
1599 	/*
1600 	 * Hold the vnode until the last page is released.
1601 	 */
1602 	if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
1603 		vhold(object->handle);
1604 
1605 	/*
1606 	 * Since we are inserting a new and possibly dirty page,
1607 	 * update the object's generation count.
1608 	 */
1609 	if (pmap_page_is_write_mapped(m))
1610 		vm_object_set_writeable_dirty(object);
1611 }
1612 
1613 /*
1614  * Do the work to remove a page from its object.  The caller is responsible for
1615  * updating the page's fields to reflect this removal.
1616  */
1617 static void
1618 vm_page_object_remove(vm_page_t m)
1619 {
1620 	vm_object_t object;
1621 	vm_page_t mrem __diagused;
1622 
1623 	vm_page_assert_xbusied(m);
1624 	object = m->object;
1625 	VM_OBJECT_ASSERT_WLOCKED(object);
1626 	KASSERT((m->ref_count & VPRC_OBJREF) != 0,
1627 	    ("page %p is missing its object ref", m));
1628 
1629 	/* Deferred free of swap space. */
1630 	if ((m->a.flags & PGA_SWAP_FREE) != 0)
1631 		vm_pager_page_unswapped(m);
1632 
1633 	vm_pager_page_removed(object, m);
1634 
1635 	m->object = NULL;
1636 	mrem = vm_radix_remove(&object->rtree, m->pindex);
1637 	KASSERT(mrem == m, ("removed page %p, expected page %p", mrem, m));
1638 
1639 	/*
1640 	 * Now remove from the object's list of backed pages.
1641 	 */
1642 	TAILQ_REMOVE(&object->memq, m, listq);
1643 
1644 	/*
1645 	 * And show that the object has one fewer resident page.
1646 	 */
1647 	object->resident_page_count--;
1648 
1649 	/*
1650 	 * The vnode may now be recycled.
1651 	 */
1652 	if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
1653 		vdrop(object->handle);
1654 }
1655 
1656 /*
1657  *	vm_page_remove:
1658  *
1659  *	Removes the specified page from its containing object, but does not
1660  *	invalidate any backing storage.  Returns true if the object's reference
1661  *	was the last reference to the page, and false otherwise.
1662  *
1663  *	The object must be locked and the page must be exclusively busied.
1664  *	The exclusive busy will be released on return.  If this is not the
1665  *	final ref and the caller does not hold a wire reference it may not
1666  *	continue to access the page.
1667  */
1668 bool
1669 vm_page_remove(vm_page_t m)
1670 {
1671 	bool dropped;
1672 
1673 	dropped = vm_page_remove_xbusy(m);
1674 	vm_page_xunbusy(m);
1675 
1676 	return (dropped);
1677 }
1678 
1679 /*
1680  *	vm_page_remove_xbusy
1681  *
1682  *	Removes the page but leaves the xbusy held.  Returns true if this
1683  *	removed the final ref and false otherwise.
1684  */
1685 bool
1686 vm_page_remove_xbusy(vm_page_t m)
1687 {
1688 
1689 	vm_page_object_remove(m);
1690 	return (vm_page_drop(m, VPRC_OBJREF) == VPRC_OBJREF);
1691 }
1692 
1693 /*
1694  *	vm_page_lookup:
1695  *
1696  *	Returns the page associated with the object/offset
1697  *	pair specified; if none is found, NULL is returned.
1698  *
1699  *	The object must be locked.
1700  */
1701 vm_page_t
1702 vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
1703 {
1704 
1705 	VM_OBJECT_ASSERT_LOCKED(object);
1706 	return (vm_radix_lookup(&object->rtree, pindex));
1707 }
1708 
1709 /*
1710  *	vm_page_iter_init:
1711  *
1712  *	Initialize iterator for vm pages.
1713  */
1714 void
1715 vm_page_iter_init(struct pctrie_iter *pages, vm_object_t object)
1716 {
1717 
1718 	VM_OBJECT_ASSERT_LOCKED(object);
1719 	vm_radix_iter_init(pages, &object->rtree);
1720 }
1721 
1722 /*
1723  *	vm_page_iter_init:
1724  *
1725  *	Initialize iterator for vm pages.
1726  */
1727 void
1728 vm_page_iter_limit_init(struct pctrie_iter *pages, vm_object_t object,
1729     vm_pindex_t limit)
1730 {
1731 
1732 	VM_OBJECT_ASSERT_LOCKED(object);
1733 	vm_radix_iter_limit_init(pages, &object->rtree, limit);
1734 }
1735 
1736 /*
1737  *	vm_page_iter_lookup:
1738  *
1739  *	Returns the page associated with the object/offset pair specified, and
1740  *	stores the path to its position; if none is found, NULL is returned.
1741  *
1742  *	The iter pctrie must be locked.
1743  */
1744 vm_page_t
1745 vm_page_iter_lookup(struct pctrie_iter *pages, vm_pindex_t pindex)
1746 {
1747 
1748 	return (vm_radix_iter_lookup(pages, pindex));
1749 }
1750 
1751 /*
1752  *	vm_page_lookup_unlocked:
1753  *
1754  *	Returns the page associated with the object/offset pair specified;
1755  *	if none is found, NULL is returned.  The page may be no longer be
1756  *	present in the object at the time that this function returns.  Only
1757  *	useful for opportunistic checks such as inmem().
1758  */
1759 vm_page_t
1760 vm_page_lookup_unlocked(vm_object_t object, vm_pindex_t pindex)
1761 {
1762 
1763 	return (vm_radix_lookup_unlocked(&object->rtree, pindex));
1764 }
1765 
1766 /*
1767  *	vm_page_relookup:
1768  *
1769  *	Returns a page that must already have been busied by
1770  *	the caller.  Used for bogus page replacement.
1771  */
1772 vm_page_t
1773 vm_page_relookup(vm_object_t object, vm_pindex_t pindex)
1774 {
1775 	vm_page_t m;
1776 
1777 	m = vm_radix_lookup_unlocked(&object->rtree, pindex);
1778 	KASSERT(m != NULL && (vm_page_busied(m) || vm_page_wired(m)) &&
1779 	    m->object == object && m->pindex == pindex,
1780 	    ("vm_page_relookup: Invalid page %p", m));
1781 	return (m);
1782 }
1783 
1784 /*
1785  * This should only be used by lockless functions for releasing transient
1786  * incorrect acquires.  The page may have been freed after we acquired a
1787  * busy lock.  In this case busy_lock == VPB_FREED and we have nothing
1788  * further to do.
1789  */
1790 static void
1791 vm_page_busy_release(vm_page_t m)
1792 {
1793 	u_int x;
1794 
1795 	x = vm_page_busy_fetch(m);
1796 	for (;;) {
1797 		if (x == VPB_FREED)
1798 			break;
1799 		if ((x & VPB_BIT_SHARED) != 0 && VPB_SHARERS(x) > 1) {
1800 			if (atomic_fcmpset_int(&m->busy_lock, &x,
1801 			    x - VPB_ONE_SHARER))
1802 				break;
1803 			continue;
1804 		}
1805 		KASSERT((x & VPB_BIT_SHARED) != 0 ||
1806 		    (x & ~VPB_BIT_WAITERS) == VPB_CURTHREAD_EXCLUSIVE,
1807 		    ("vm_page_busy_release: %p xbusy not owned.", m));
1808 		if (!atomic_fcmpset_rel_int(&m->busy_lock, &x, VPB_UNBUSIED))
1809 			continue;
1810 		if ((x & VPB_BIT_WAITERS) != 0)
1811 			wakeup(m);
1812 		break;
1813 	}
1814 }
1815 
1816 /*
1817  *	vm_page_find_least:
1818  *
1819  *	Returns the page associated with the object with least pindex
1820  *	greater than or equal to the parameter pindex, or NULL.
1821  *
1822  *	The object must be locked.
1823  */
1824 vm_page_t
1825 vm_page_find_least(vm_object_t object, vm_pindex_t pindex)
1826 {
1827 	vm_page_t m;
1828 
1829 	VM_OBJECT_ASSERT_LOCKED(object);
1830 	if ((m = TAILQ_FIRST(&object->memq)) != NULL && m->pindex < pindex)
1831 		m = vm_radix_lookup_ge(&object->rtree, pindex);
1832 	return (m);
1833 }
1834 
1835 /*
1836  *	vm_page_iter_lookup_ge:
1837  *
1838  *	Returns the page associated with the object with least pindex
1839  *	greater than or equal to the parameter pindex, or NULL.  Initializes the
1840  *	iterator to point to that page.
1841  *
1842  *	The iter pctrie must be locked.
1843  */
1844 vm_page_t
1845 vm_page_iter_lookup_ge(struct pctrie_iter *pages, vm_pindex_t pindex)
1846 {
1847 
1848 	return (vm_radix_iter_lookup_ge(pages, pindex));
1849 }
1850 
1851 /*
1852  * Returns the given page's successor (by pindex) within the object if it is
1853  * resident; if none is found, NULL is returned.
1854  *
1855  * The object must be locked.
1856  */
1857 vm_page_t
1858 vm_page_next(vm_page_t m)
1859 {
1860 	vm_page_t next;
1861 
1862 	VM_OBJECT_ASSERT_LOCKED(m->object);
1863 	if ((next = TAILQ_NEXT(m, listq)) != NULL) {
1864 		MPASS(next->object == m->object);
1865 		if (next->pindex != m->pindex + 1)
1866 			next = NULL;
1867 	}
1868 	return (next);
1869 }
1870 
1871 /*
1872  * Returns the given page's predecessor (by pindex) within the object if it is
1873  * resident; if none is found, NULL is returned.
1874  *
1875  * The object must be locked.
1876  */
1877 vm_page_t
1878 vm_page_prev(vm_page_t m)
1879 {
1880 	vm_page_t prev;
1881 
1882 	VM_OBJECT_ASSERT_LOCKED(m->object);
1883 	if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL) {
1884 		MPASS(prev->object == m->object);
1885 		if (prev->pindex != m->pindex - 1)
1886 			prev = NULL;
1887 	}
1888 	return (prev);
1889 }
1890 
1891 /*
1892  * Uses the page mnew as a replacement for an existing page at index
1893  * pindex which must be already present in the object.
1894  *
1895  * Both pages must be exclusively busied on enter.  The old page is
1896  * unbusied on exit.
1897  *
1898  * A return value of true means mold is now free.  If this is not the
1899  * final ref and the caller does not hold a wire reference it may not
1900  * continue to access the page.
1901  */
1902 static bool
1903 vm_page_replace_hold(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex,
1904     vm_page_t mold)
1905 {
1906 	vm_page_t mret __diagused;
1907 	bool dropped;
1908 
1909 	VM_OBJECT_ASSERT_WLOCKED(object);
1910 	vm_page_assert_xbusied(mold);
1911 	KASSERT(mnew->object == NULL && (mnew->ref_count & VPRC_OBJREF) == 0,
1912 	    ("vm_page_replace: page %p already in object", mnew));
1913 
1914 	/*
1915 	 * This function mostly follows vm_page_insert() and
1916 	 * vm_page_remove() without the radix, object count and vnode
1917 	 * dance.  Double check such functions for more comments.
1918 	 */
1919 
1920 	mnew->object = object;
1921 	mnew->pindex = pindex;
1922 	atomic_set_int(&mnew->ref_count, VPRC_OBJREF);
1923 	mret = vm_radix_replace(&object->rtree, mnew);
1924 	KASSERT(mret == mold,
1925 	    ("invalid page replacement, mold=%p, mret=%p", mold, mret));
1926 	KASSERT((mold->oflags & VPO_UNMANAGED) ==
1927 	    (mnew->oflags & VPO_UNMANAGED),
1928 	    ("vm_page_replace: mismatched VPO_UNMANAGED"));
1929 
1930 	/* Keep the resident page list in sorted order. */
1931 	TAILQ_INSERT_AFTER(&object->memq, mold, mnew, listq);
1932 	TAILQ_REMOVE(&object->memq, mold, listq);
1933 	mold->object = NULL;
1934 
1935 	/*
1936 	 * The object's resident_page_count does not change because we have
1937 	 * swapped one page for another, but the generation count should
1938 	 * change if the page is dirty.
1939 	 */
1940 	if (pmap_page_is_write_mapped(mnew))
1941 		vm_object_set_writeable_dirty(object);
1942 	dropped = vm_page_drop(mold, VPRC_OBJREF) == VPRC_OBJREF;
1943 	vm_page_xunbusy(mold);
1944 
1945 	return (dropped);
1946 }
1947 
1948 void
1949 vm_page_replace(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex,
1950     vm_page_t mold)
1951 {
1952 
1953 	vm_page_assert_xbusied(mnew);
1954 
1955 	if (vm_page_replace_hold(mnew, object, pindex, mold))
1956 		vm_page_free(mold);
1957 }
1958 
1959 /*
1960  *	vm_page_rename:
1961  *
1962  *	Move the given memory entry from its
1963  *	current object to the specified target object/offset.
1964  *
1965  *	Note: swap associated with the page must be invalidated by the move.  We
1966  *	      have to do this for several reasons:  (1) we aren't freeing the
1967  *	      page, (2) we are dirtying the page, (3) the VM system is probably
1968  *	      moving the page from object A to B, and will then later move
1969  *	      the backing store from A to B and we can't have a conflict.
1970  *
1971  *	Note: we *always* dirty the page.  It is necessary both for the
1972  *	      fact that we moved it, and because we may be invalidating
1973  *	      swap.
1974  *
1975  *	The objects must be locked.
1976  */
1977 int
1978 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
1979 {
1980 	vm_page_t mpred;
1981 	vm_pindex_t opidx;
1982 
1983 	VM_OBJECT_ASSERT_WLOCKED(new_object);
1984 
1985 	KASSERT(m->ref_count != 0, ("vm_page_rename: page %p has no refs", m));
1986 
1987 	/*
1988 	 * Create a custom version of vm_page_insert() which does not depend
1989 	 * by m_prev and can cheat on the implementation aspects of the
1990 	 * function.
1991 	 */
1992 	opidx = m->pindex;
1993 	m->pindex = new_pindex;
1994 	if (vm_radix_insert_lookup_lt(&new_object->rtree, m, &mpred) != 0) {
1995 		m->pindex = opidx;
1996 		return (1);
1997 	}
1998 
1999 	/*
2000 	 * The operation cannot fail anymore.  The removal must happen before
2001 	 * the listq iterator is tainted.
2002 	 */
2003 	m->pindex = opidx;
2004 	vm_page_object_remove(m);
2005 
2006 	/* Return back to the new pindex to complete vm_page_insert(). */
2007 	m->pindex = new_pindex;
2008 	m->object = new_object;
2009 
2010 	vm_page_insert_radixdone(m, new_object, mpred);
2011 	vm_page_dirty(m);
2012 	vm_pager_page_inserted(new_object, m);
2013 	return (0);
2014 }
2015 
2016 /*
2017  *	vm_page_alloc:
2018  *
2019  *	Allocate and return a page that is associated with the specified
2020  *	object and offset pair.  By default, this page is exclusive busied.
2021  *
2022  *	The caller must always specify an allocation class.
2023  *
2024  *	allocation classes:
2025  *	VM_ALLOC_NORMAL		normal process request
2026  *	VM_ALLOC_SYSTEM		system *really* needs a page
2027  *	VM_ALLOC_INTERRUPT	interrupt time request
2028  *
2029  *	optional allocation flags:
2030  *	VM_ALLOC_COUNT(number)	the number of additional pages that the caller
2031  *				intends to allocate
2032  *	VM_ALLOC_NOBUSY		do not exclusive busy the page
2033  *	VM_ALLOC_NODUMP		do not include the page in a kernel core dump
2034  *	VM_ALLOC_SBUSY		shared busy the allocated page
2035  *	VM_ALLOC_WIRED		wire the allocated page
2036  *	VM_ALLOC_ZERO		prefer a zeroed page
2037  */
2038 vm_page_t
2039 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
2040 {
2041 
2042 	return (vm_page_alloc_after(object, pindex, req,
2043 	    vm_radix_lookup_le(&object->rtree, pindex)));
2044 }
2045 
2046 vm_page_t
2047 vm_page_alloc_domain(vm_object_t object, vm_pindex_t pindex, int domain,
2048     int req)
2049 {
2050 
2051 	return (vm_page_alloc_domain_after(object, pindex, domain, req,
2052 	    vm_radix_lookup_le(&object->rtree, pindex)));
2053 }
2054 
2055 /*
2056  * Allocate a page in the specified object with the given page index.  To
2057  * optimize insertion of the page into the object, the caller must also specify
2058  * the resident page in the object with largest index smaller than the given
2059  * page index, or NULL if no such page exists.
2060  */
2061 vm_page_t
2062 vm_page_alloc_after(vm_object_t object, vm_pindex_t pindex,
2063     int req, vm_page_t mpred)
2064 {
2065 	struct vm_domainset_iter di;
2066 	vm_page_t m;
2067 	int domain;
2068 
2069 	vm_domainset_iter_page_init(&di, object, pindex, &domain, &req);
2070 	do {
2071 		m = vm_page_alloc_domain_after(object, pindex, domain, req,
2072 		    mpred);
2073 		if (m != NULL)
2074 			break;
2075 	} while (vm_domainset_iter_page(&di, object, &domain) == 0);
2076 
2077 	return (m);
2078 }
2079 
2080 /*
2081  * Returns true if the number of free pages exceeds the minimum
2082  * for the request class and false otherwise.
2083  */
2084 static int
2085 _vm_domain_allocate(struct vm_domain *vmd, int req_class, int npages)
2086 {
2087 	u_int limit, old, new;
2088 
2089 	if (req_class == VM_ALLOC_INTERRUPT)
2090 		limit = 0;
2091 	else if (req_class == VM_ALLOC_SYSTEM)
2092 		limit = vmd->vmd_interrupt_free_min;
2093 	else
2094 		limit = vmd->vmd_free_reserved;
2095 
2096 	/*
2097 	 * Attempt to reserve the pages.  Fail if we're below the limit.
2098 	 */
2099 	limit += npages;
2100 	old = vmd->vmd_free_count;
2101 	do {
2102 		if (old < limit)
2103 			return (0);
2104 		new = old - npages;
2105 	} while (atomic_fcmpset_int(&vmd->vmd_free_count, &old, new) == 0);
2106 
2107 	/* Wake the page daemon if we've crossed the threshold. */
2108 	if (vm_paging_needed(vmd, new) && !vm_paging_needed(vmd, old))
2109 		pagedaemon_wakeup(vmd->vmd_domain);
2110 
2111 	/* Only update bitsets on transitions. */
2112 	if ((old >= vmd->vmd_free_min && new < vmd->vmd_free_min) ||
2113 	    (old >= vmd->vmd_free_severe && new < vmd->vmd_free_severe))
2114 		vm_domain_set(vmd);
2115 
2116 	return (1);
2117 }
2118 
2119 int
2120 vm_domain_allocate(struct vm_domain *vmd, int req, int npages)
2121 {
2122 	int req_class;
2123 
2124 	/*
2125 	 * The page daemon is allowed to dig deeper into the free page list.
2126 	 */
2127 	req_class = req & VM_ALLOC_CLASS_MASK;
2128 	if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
2129 		req_class = VM_ALLOC_SYSTEM;
2130 	return (_vm_domain_allocate(vmd, req_class, npages));
2131 }
2132 
2133 vm_page_t
2134 vm_page_alloc_domain_after(vm_object_t object, vm_pindex_t pindex, int domain,
2135     int req, vm_page_t mpred)
2136 {
2137 	struct vm_domain *vmd;
2138 	vm_page_t m;
2139 	int flags;
2140 
2141 #define	VPA_FLAGS	(VM_ALLOC_CLASS_MASK | VM_ALLOC_WAITFAIL |	\
2142 			 VM_ALLOC_NOWAIT | VM_ALLOC_NOBUSY |		\
2143 			 VM_ALLOC_SBUSY | VM_ALLOC_WIRED |		\
2144 			 VM_ALLOC_NODUMP | VM_ALLOC_ZERO |		\
2145 			 VM_ALLOC_NOFREE | VM_ALLOC_COUNT_MASK)
2146 	KASSERT((req & ~VPA_FLAGS) == 0,
2147 	    ("invalid request %#x", req));
2148 	KASSERT(((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
2149 	    (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
2150 	    ("invalid request %#x", req));
2151 	KASSERT(mpred == NULL || mpred->pindex < pindex,
2152 	    ("mpred %p doesn't precede pindex 0x%jx", mpred,
2153 	    (uintmax_t)pindex));
2154 	VM_OBJECT_ASSERT_WLOCKED(object);
2155 
2156 	flags = 0;
2157 	m = NULL;
2158 	if (!vm_pager_can_alloc_page(object, pindex))
2159 		return (NULL);
2160 again:
2161 	if (__predict_false((req & VM_ALLOC_NOFREE) != 0)) {
2162 		m = vm_page_alloc_nofree_domain(domain, req);
2163 		if (m != NULL)
2164 			goto found;
2165 	}
2166 #if VM_NRESERVLEVEL > 0
2167 	/*
2168 	 * Can we allocate the page from a reservation?
2169 	 */
2170 	if (vm_object_reserv(object) &&
2171 	    (m = vm_reserv_alloc_page(object, pindex, domain, req, mpred)) !=
2172 	    NULL) {
2173 		goto found;
2174 	}
2175 #endif
2176 	vmd = VM_DOMAIN(domain);
2177 	if (vmd->vmd_pgcache[VM_FREEPOOL_DEFAULT].zone != NULL) {
2178 		m = uma_zalloc(vmd->vmd_pgcache[VM_FREEPOOL_DEFAULT].zone,
2179 		    M_NOWAIT | M_NOVM);
2180 		if (m != NULL) {
2181 			flags |= PG_PCPU_CACHE;
2182 			goto found;
2183 		}
2184 	}
2185 	if (vm_domain_allocate(vmd, req, 1)) {
2186 		/*
2187 		 * If not, allocate it from the free page queues.
2188 		 */
2189 		vm_domain_free_lock(vmd);
2190 		m = vm_phys_alloc_pages(domain, VM_FREEPOOL_DEFAULT, 0);
2191 		vm_domain_free_unlock(vmd);
2192 		if (m == NULL) {
2193 			vm_domain_freecnt_inc(vmd, 1);
2194 #if VM_NRESERVLEVEL > 0
2195 			if (vm_reserv_reclaim_inactive(domain))
2196 				goto again;
2197 #endif
2198 		}
2199 	}
2200 	if (m == NULL) {
2201 		/*
2202 		 * Not allocatable, give up.
2203 		 */
2204 		if (vm_domain_alloc_fail(vmd, object, req))
2205 			goto again;
2206 		return (NULL);
2207 	}
2208 
2209 	/*
2210 	 * At this point we had better have found a good page.
2211 	 */
2212 found:
2213 	vm_page_dequeue(m);
2214 	vm_page_alloc_check(m);
2215 
2216 	/*
2217 	 * Initialize the page.  Only the PG_ZERO flag is inherited.
2218 	 */
2219 	flags |= m->flags & PG_ZERO;
2220 	if ((req & VM_ALLOC_NODUMP) != 0)
2221 		flags |= PG_NODUMP;
2222 	if ((req & VM_ALLOC_NOFREE) != 0)
2223 		flags |= PG_NOFREE;
2224 	m->flags = flags;
2225 	m->a.flags = 0;
2226 	m->oflags = (object->flags & OBJ_UNMANAGED) != 0 ? VPO_UNMANAGED : 0;
2227 	if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0)
2228 		m->busy_lock = VPB_CURTHREAD_EXCLUSIVE;
2229 	else if ((req & VM_ALLOC_SBUSY) != 0)
2230 		m->busy_lock = VPB_SHARERS_WORD(1);
2231 	else
2232 		m->busy_lock = VPB_UNBUSIED;
2233 	if (req & VM_ALLOC_WIRED) {
2234 		vm_wire_add(1);
2235 		m->ref_count = 1;
2236 	}
2237 	m->a.act_count = 0;
2238 
2239 	if (vm_page_insert_after(m, object, pindex, mpred)) {
2240 		if (req & VM_ALLOC_WIRED) {
2241 			vm_wire_sub(1);
2242 			m->ref_count = 0;
2243 		}
2244 		KASSERT(m->object == NULL, ("page %p has object", m));
2245 		m->oflags = VPO_UNMANAGED;
2246 		m->busy_lock = VPB_UNBUSIED;
2247 		/* Don't change PG_ZERO. */
2248 		vm_page_free_toq(m);
2249 		if (req & VM_ALLOC_WAITFAIL) {
2250 			VM_OBJECT_WUNLOCK(object);
2251 			vm_radix_wait();
2252 			VM_OBJECT_WLOCK(object);
2253 		}
2254 		return (NULL);
2255 	}
2256 
2257 	/* Ignore device objects; the pager sets "memattr" for them. */
2258 	if (object->memattr != VM_MEMATTR_DEFAULT &&
2259 	    (object->flags & OBJ_FICTITIOUS) == 0)
2260 		pmap_page_set_memattr(m, object->memattr);
2261 
2262 	return (m);
2263 }
2264 
2265 /*
2266  *	vm_page_alloc_contig:
2267  *
2268  *	Allocate a contiguous set of physical pages of the given size "npages"
2269  *	from the free lists.  All of the physical pages must be at or above
2270  *	the given physical address "low" and below the given physical address
2271  *	"high".  The given value "alignment" determines the alignment of the
2272  *	first physical page in the set.  If the given value "boundary" is
2273  *	non-zero, then the set of physical pages cannot cross any physical
2274  *	address boundary that is a multiple of that value.  Both "alignment"
2275  *	and "boundary" must be a power of two.
2276  *
2277  *	If the specified memory attribute, "memattr", is VM_MEMATTR_DEFAULT,
2278  *	then the memory attribute setting for the physical pages is configured
2279  *	to the object's memory attribute setting.  Otherwise, the memory
2280  *	attribute setting for the physical pages is configured to "memattr",
2281  *	overriding the object's memory attribute setting.  However, if the
2282  *	object's memory attribute setting is not VM_MEMATTR_DEFAULT, then the
2283  *	memory attribute setting for the physical pages cannot be configured
2284  *	to VM_MEMATTR_DEFAULT.
2285  *
2286  *	The specified object may not contain fictitious pages.
2287  *
2288  *	The caller must always specify an allocation class.
2289  *
2290  *	allocation classes:
2291  *	VM_ALLOC_NORMAL		normal process request
2292  *	VM_ALLOC_SYSTEM		system *really* needs a page
2293  *	VM_ALLOC_INTERRUPT	interrupt time request
2294  *
2295  *	optional allocation flags:
2296  *	VM_ALLOC_NOBUSY		do not exclusive busy the page
2297  *	VM_ALLOC_NODUMP		do not include the page in a kernel core dump
2298  *	VM_ALLOC_SBUSY		shared busy the allocated page
2299  *	VM_ALLOC_WIRED		wire the allocated page
2300  *	VM_ALLOC_ZERO		prefer a zeroed page
2301  */
2302 vm_page_t
2303 vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req,
2304     u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
2305     vm_paddr_t boundary, vm_memattr_t memattr)
2306 {
2307 	struct vm_domainset_iter di;
2308 	vm_page_t bounds[2];
2309 	vm_page_t m;
2310 	int domain;
2311 	int start_segind;
2312 
2313 	start_segind = -1;
2314 
2315 	vm_domainset_iter_page_init(&di, object, pindex, &domain, &req);
2316 	do {
2317 		m = vm_page_alloc_contig_domain(object, pindex, domain, req,
2318 		    npages, low, high, alignment, boundary, memattr);
2319 		if (m != NULL)
2320 			break;
2321 		if (start_segind == -1)
2322 			start_segind = vm_phys_lookup_segind(low);
2323 		if (vm_phys_find_range(bounds, start_segind, domain,
2324 		    npages, low, high) == -1) {
2325 			vm_domainset_iter_ignore(&di, domain);
2326 		}
2327 	} while (vm_domainset_iter_page(&di, object, &domain) == 0);
2328 
2329 	return (m);
2330 }
2331 
2332 static vm_page_t
2333 vm_page_find_contig_domain(int domain, int req, u_long npages, vm_paddr_t low,
2334     vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
2335 {
2336 	struct vm_domain *vmd;
2337 	vm_page_t m_ret;
2338 
2339 	/*
2340 	 * Can we allocate the pages without the number of free pages falling
2341 	 * below the lower bound for the allocation class?
2342 	 */
2343 	vmd = VM_DOMAIN(domain);
2344 	if (!vm_domain_allocate(vmd, req, npages))
2345 		return (NULL);
2346 	/*
2347 	 * Try to allocate the pages from the free page queues.
2348 	 */
2349 	vm_domain_free_lock(vmd);
2350 	m_ret = vm_phys_alloc_contig(domain, npages, low, high,
2351 	    alignment, boundary);
2352 	vm_domain_free_unlock(vmd);
2353 	if (m_ret != NULL)
2354 		return (m_ret);
2355 #if VM_NRESERVLEVEL > 0
2356 	/*
2357 	 * Try to break a reservation to allocate the pages.
2358 	 */
2359 	if ((req & VM_ALLOC_NORECLAIM) == 0) {
2360 		m_ret = vm_reserv_reclaim_contig(domain, npages, low,
2361 	            high, alignment, boundary);
2362 		if (m_ret != NULL)
2363 			return (m_ret);
2364 	}
2365 #endif
2366 	vm_domain_freecnt_inc(vmd, npages);
2367 	return (NULL);
2368 }
2369 
2370 vm_page_t
2371 vm_page_alloc_contig_domain(vm_object_t object, vm_pindex_t pindex, int domain,
2372     int req, u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
2373     vm_paddr_t boundary, vm_memattr_t memattr)
2374 {
2375 	vm_page_t m, m_ret, mpred;
2376 	u_int busy_lock, flags, oflags;
2377 
2378 #define	VPAC_FLAGS	(VPA_FLAGS | VM_ALLOC_NORECLAIM)
2379 	KASSERT((req & ~VPAC_FLAGS) == 0,
2380 	    ("invalid request %#x", req));
2381 	KASSERT(((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
2382 	    (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
2383 	    ("invalid request %#x", req));
2384 	KASSERT((req & (VM_ALLOC_WAITOK | VM_ALLOC_NORECLAIM)) !=
2385 	    (VM_ALLOC_WAITOK | VM_ALLOC_NORECLAIM),
2386 	    ("invalid request %#x", req));
2387 	VM_OBJECT_ASSERT_WLOCKED(object);
2388 	KASSERT((object->flags & OBJ_FICTITIOUS) == 0,
2389 	    ("vm_page_alloc_contig: object %p has fictitious pages",
2390 	    object));
2391 	KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero"));
2392 
2393 	mpred = vm_radix_lookup_le(&object->rtree, pindex);
2394 	KASSERT(mpred == NULL || mpred->pindex != pindex,
2395 	    ("vm_page_alloc_contig: pindex already allocated"));
2396 	for (;;) {
2397 #if VM_NRESERVLEVEL > 0
2398 		/*
2399 		 * Can we allocate the pages from a reservation?
2400 		 */
2401 		if (vm_object_reserv(object) &&
2402 		    (m_ret = vm_reserv_alloc_contig(object, pindex, domain, req,
2403 		    mpred, npages, low, high, alignment, boundary)) != NULL) {
2404 			break;
2405 		}
2406 #endif
2407 		if ((m_ret = vm_page_find_contig_domain(domain, req, npages,
2408 		    low, high, alignment, boundary)) != NULL)
2409 			break;
2410 		if (!vm_domain_alloc_fail(VM_DOMAIN(domain), object, req))
2411 			return (NULL);
2412 	}
2413 
2414 	/*
2415 	 * Initialize the pages.  Only the PG_ZERO flag is inherited.
2416 	 */
2417 	flags = PG_ZERO;
2418 	if ((req & VM_ALLOC_NODUMP) != 0)
2419 		flags |= PG_NODUMP;
2420 	oflags = (object->flags & OBJ_UNMANAGED) != 0 ? VPO_UNMANAGED : 0;
2421 	if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0)
2422 		busy_lock = VPB_CURTHREAD_EXCLUSIVE;
2423 	else if ((req & VM_ALLOC_SBUSY) != 0)
2424 		busy_lock = VPB_SHARERS_WORD(1);
2425 	else
2426 		busy_lock = VPB_UNBUSIED;
2427 	if ((req & VM_ALLOC_WIRED) != 0)
2428 		vm_wire_add(npages);
2429 	if (object->memattr != VM_MEMATTR_DEFAULT &&
2430 	    memattr == VM_MEMATTR_DEFAULT)
2431 		memattr = object->memattr;
2432 	for (m = m_ret; m < &m_ret[npages]; m++) {
2433 		vm_page_dequeue(m);
2434 		vm_page_alloc_check(m);
2435 		m->a.flags = 0;
2436 		m->flags = (m->flags | PG_NODUMP) & flags;
2437 		m->busy_lock = busy_lock;
2438 		if ((req & VM_ALLOC_WIRED) != 0)
2439 			m->ref_count = 1;
2440 		m->a.act_count = 0;
2441 		m->oflags = oflags;
2442 		if (vm_page_insert_after(m, object, pindex, mpred)) {
2443 			if ((req & VM_ALLOC_WIRED) != 0)
2444 				vm_wire_sub(npages);
2445 			KASSERT(m->object == NULL,
2446 			    ("page %p has object", m));
2447 			mpred = m;
2448 			for (m = m_ret; m < &m_ret[npages]; m++) {
2449 				if (m <= mpred &&
2450 				    (req & VM_ALLOC_WIRED) != 0)
2451 					m->ref_count = 0;
2452 				m->oflags = VPO_UNMANAGED;
2453 				m->busy_lock = VPB_UNBUSIED;
2454 				/* Don't change PG_ZERO. */
2455 				vm_page_free_toq(m);
2456 			}
2457 			if (req & VM_ALLOC_WAITFAIL) {
2458 				VM_OBJECT_WUNLOCK(object);
2459 				vm_radix_wait();
2460 				VM_OBJECT_WLOCK(object);
2461 			}
2462 			return (NULL);
2463 		}
2464 		mpred = m;
2465 		if (memattr != VM_MEMATTR_DEFAULT)
2466 			pmap_page_set_memattr(m, memattr);
2467 		pindex++;
2468 	}
2469 	return (m_ret);
2470 }
2471 
2472 /*
2473  * Allocate a physical page that is not intended to be inserted into a VM
2474  * object.
2475  */
2476 vm_page_t
2477 vm_page_alloc_noobj_domain(int domain, int req)
2478 {
2479 	struct vm_domain *vmd;
2480 	vm_page_t m;
2481 	int flags;
2482 
2483 #define	VPAN_FLAGS	(VM_ALLOC_CLASS_MASK | VM_ALLOC_WAITFAIL |      \
2484 			 VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK |		\
2485 			 VM_ALLOC_NOBUSY | VM_ALLOC_WIRED |		\
2486 			 VM_ALLOC_NODUMP | VM_ALLOC_ZERO |		\
2487 			 VM_ALLOC_NOFREE | VM_ALLOC_COUNT_MASK)
2488 	KASSERT((req & ~VPAN_FLAGS) == 0,
2489 	    ("invalid request %#x", req));
2490 
2491 	flags = ((req & VM_ALLOC_NODUMP) != 0 ? PG_NODUMP : 0) |
2492 	    ((req & VM_ALLOC_NOFREE) != 0 ? PG_NOFREE : 0);
2493 	vmd = VM_DOMAIN(domain);
2494 again:
2495 	if (__predict_false((req & VM_ALLOC_NOFREE) != 0)) {
2496 		m = vm_page_alloc_nofree_domain(domain, req);
2497 		if (m != NULL)
2498 			goto found;
2499 	}
2500 
2501 	if (vmd->vmd_pgcache[VM_FREEPOOL_DIRECT].zone != NULL) {
2502 		m = uma_zalloc(vmd->vmd_pgcache[VM_FREEPOOL_DIRECT].zone,
2503 		    M_NOWAIT | M_NOVM);
2504 		if (m != NULL) {
2505 			flags |= PG_PCPU_CACHE;
2506 			goto found;
2507 		}
2508 	}
2509 
2510 	if (vm_domain_allocate(vmd, req, 1)) {
2511 		vm_domain_free_lock(vmd);
2512 		m = vm_phys_alloc_pages(domain, VM_FREEPOOL_DIRECT, 0);
2513 		vm_domain_free_unlock(vmd);
2514 		if (m == NULL) {
2515 			vm_domain_freecnt_inc(vmd, 1);
2516 #if VM_NRESERVLEVEL > 0
2517 			if (vm_reserv_reclaim_inactive(domain))
2518 				goto again;
2519 #endif
2520 		}
2521 	}
2522 	if (m == NULL) {
2523 		if (vm_domain_alloc_fail(vmd, NULL, req))
2524 			goto again;
2525 		return (NULL);
2526 	}
2527 
2528 found:
2529 	vm_page_dequeue(m);
2530 	vm_page_alloc_check(m);
2531 
2532 	/*
2533 	 * Consumers should not rely on a useful default pindex value.
2534 	 */
2535 	m->pindex = 0xdeadc0dedeadc0de;
2536 	m->flags = (m->flags & PG_ZERO) | flags;
2537 	m->a.flags = 0;
2538 	m->oflags = VPO_UNMANAGED;
2539 	m->busy_lock = VPB_UNBUSIED;
2540 	if ((req & VM_ALLOC_WIRED) != 0) {
2541 		vm_wire_add(1);
2542 		m->ref_count = 1;
2543 	}
2544 
2545 	if ((req & VM_ALLOC_ZERO) != 0 && (m->flags & PG_ZERO) == 0)
2546 		pmap_zero_page(m);
2547 
2548 	return (m);
2549 }
2550 
2551 #if VM_NRESERVLEVEL > 1
2552 #define	VM_NOFREE_IMPORT_ORDER	(VM_LEVEL_1_ORDER + VM_LEVEL_0_ORDER)
2553 #elif VM_NRESERVLEVEL > 0
2554 #define	VM_NOFREE_IMPORT_ORDER	VM_LEVEL_0_ORDER
2555 #else
2556 #define	VM_NOFREE_IMPORT_ORDER	8
2557 #endif
2558 
2559 /*
2560  * Allocate a single NOFREE page.
2561  *
2562  * This routine hands out NOFREE pages from higher-order
2563  * physical memory blocks in order to reduce memory fragmentation.
2564  * When a NOFREE for a given domain chunk is used up,
2565  * the routine will try to fetch a new one from the freelists
2566  * and discard the old one.
2567  */
2568 static vm_page_t
2569 vm_page_alloc_nofree_domain(int domain, int req)
2570 {
2571 	vm_page_t m;
2572 	struct vm_domain *vmd;
2573 	struct vm_nofreeq *nqp;
2574 
2575 	KASSERT((req & VM_ALLOC_NOFREE) != 0, ("invalid request %#x", req));
2576 
2577 	vmd = VM_DOMAIN(domain);
2578 	nqp = &vmd->vmd_nofreeq;
2579 	vm_domain_free_lock(vmd);
2580 	if (nqp->offs >= (1 << VM_NOFREE_IMPORT_ORDER) || nqp->ma == NULL) {
2581 		if (!vm_domain_allocate(vmd, req,
2582 		    1 << VM_NOFREE_IMPORT_ORDER)) {
2583 			vm_domain_free_unlock(vmd);
2584 			return (NULL);
2585 		}
2586 		nqp->ma = vm_phys_alloc_pages(domain, VM_FREEPOOL_DEFAULT,
2587 		    VM_NOFREE_IMPORT_ORDER);
2588 		if (nqp->ma == NULL) {
2589 			vm_domain_freecnt_inc(vmd, 1 << VM_NOFREE_IMPORT_ORDER);
2590 			vm_domain_free_unlock(vmd);
2591 			return (NULL);
2592 		}
2593 		nqp->offs = 0;
2594 	}
2595 	m = &nqp->ma[nqp->offs++];
2596 	vm_domain_free_unlock(vmd);
2597 
2598 	return (m);
2599 }
2600 
2601 vm_page_t
2602 vm_page_alloc_noobj(int req)
2603 {
2604 	struct vm_domainset_iter di;
2605 	vm_page_t m;
2606 	int domain;
2607 
2608 	vm_domainset_iter_page_init(&di, NULL, 0, &domain, &req);
2609 	do {
2610 		m = vm_page_alloc_noobj_domain(domain, req);
2611 		if (m != NULL)
2612 			break;
2613 	} while (vm_domainset_iter_page(&di, NULL, &domain) == 0);
2614 
2615 	return (m);
2616 }
2617 
2618 vm_page_t
2619 vm_page_alloc_noobj_contig(int req, u_long npages, vm_paddr_t low,
2620     vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
2621     vm_memattr_t memattr)
2622 {
2623 	struct vm_domainset_iter di;
2624 	vm_page_t m;
2625 	int domain;
2626 
2627 	vm_domainset_iter_page_init(&di, NULL, 0, &domain, &req);
2628 	do {
2629 		m = vm_page_alloc_noobj_contig_domain(domain, req, npages, low,
2630 		    high, alignment, boundary, memattr);
2631 		if (m != NULL)
2632 			break;
2633 	} while (vm_domainset_iter_page(&di, NULL, &domain) == 0);
2634 
2635 	return (m);
2636 }
2637 
2638 vm_page_t
2639 vm_page_alloc_noobj_contig_domain(int domain, int req, u_long npages,
2640     vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
2641     vm_memattr_t memattr)
2642 {
2643 	vm_page_t m, m_ret;
2644 	u_int flags;
2645 
2646 #define	VPANC_FLAGS	(VPAN_FLAGS | VM_ALLOC_NORECLAIM)
2647 	KASSERT((req & ~VPANC_FLAGS) == 0,
2648 	    ("invalid request %#x", req));
2649 	KASSERT((req & (VM_ALLOC_WAITOK | VM_ALLOC_NORECLAIM)) !=
2650 	    (VM_ALLOC_WAITOK | VM_ALLOC_NORECLAIM),
2651 	    ("invalid request %#x", req));
2652 	KASSERT(((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
2653 	    (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
2654 	    ("invalid request %#x", req));
2655 	KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero"));
2656 
2657 	while ((m_ret = vm_page_find_contig_domain(domain, req, npages,
2658 	    low, high, alignment, boundary)) == NULL) {
2659 		if (!vm_domain_alloc_fail(VM_DOMAIN(domain), NULL, req))
2660 			return (NULL);
2661 	}
2662 
2663 	/*
2664 	 * Initialize the pages.  Only the PG_ZERO flag is inherited.
2665 	 */
2666 	flags = PG_ZERO;
2667 	if ((req & VM_ALLOC_NODUMP) != 0)
2668 		flags |= PG_NODUMP;
2669 	if ((req & VM_ALLOC_WIRED) != 0)
2670 		vm_wire_add(npages);
2671 	for (m = m_ret; m < &m_ret[npages]; m++) {
2672 		vm_page_dequeue(m);
2673 		vm_page_alloc_check(m);
2674 
2675 		/*
2676 		 * Consumers should not rely on a useful default pindex value.
2677 		 */
2678 		m->pindex = 0xdeadc0dedeadc0de;
2679 		m->a.flags = 0;
2680 		m->flags = (m->flags | PG_NODUMP) & flags;
2681 		m->busy_lock = VPB_UNBUSIED;
2682 		if ((req & VM_ALLOC_WIRED) != 0)
2683 			m->ref_count = 1;
2684 		m->a.act_count = 0;
2685 		m->oflags = VPO_UNMANAGED;
2686 
2687 		/*
2688 		 * Zero the page before updating any mappings since the page is
2689 		 * not yet shared with any devices which might require the
2690 		 * non-default memory attribute.  pmap_page_set_memattr()
2691 		 * flushes data caches before returning.
2692 		 */
2693 		if ((req & VM_ALLOC_ZERO) != 0 && (m->flags & PG_ZERO) == 0)
2694 			pmap_zero_page(m);
2695 		if (memattr != VM_MEMATTR_DEFAULT)
2696 			pmap_page_set_memattr(m, memattr);
2697 	}
2698 	return (m_ret);
2699 }
2700 
2701 /*
2702  * Check a page that has been freshly dequeued from a freelist.
2703  */
2704 static void
2705 vm_page_alloc_check(vm_page_t m)
2706 {
2707 
2708 	KASSERT(m->object == NULL, ("page %p has object", m));
2709 	KASSERT(m->a.queue == PQ_NONE &&
2710 	    (m->a.flags & PGA_QUEUE_STATE_MASK) == 0,
2711 	    ("page %p has unexpected queue %d, flags %#x",
2712 	    m, m->a.queue, (m->a.flags & PGA_QUEUE_STATE_MASK)));
2713 	KASSERT(m->ref_count == 0, ("page %p has references", m));
2714 	KASSERT(vm_page_busy_freed(m), ("page %p is not freed", m));
2715 	KASSERT(m->dirty == 0, ("page %p is dirty", m));
2716 	KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
2717 	    ("page %p has unexpected memattr %d",
2718 	    m, pmap_page_get_memattr(m)));
2719 	KASSERT(vm_page_none_valid(m), ("free page %p is valid", m));
2720 	pmap_vm_page_alloc_check(m);
2721 }
2722 
2723 static int
2724 vm_page_zone_import(void *arg, void **store, int cnt, int domain, int flags)
2725 {
2726 	struct vm_domain *vmd;
2727 	struct vm_pgcache *pgcache;
2728 	int i;
2729 
2730 	pgcache = arg;
2731 	vmd = VM_DOMAIN(pgcache->domain);
2732 
2733 	/*
2734 	 * The page daemon should avoid creating extra memory pressure since its
2735 	 * main purpose is to replenish the store of free pages.
2736 	 */
2737 	if (vmd->vmd_severeset || curproc == pageproc ||
2738 	    !_vm_domain_allocate(vmd, VM_ALLOC_NORMAL, cnt))
2739 		return (0);
2740 	domain = vmd->vmd_domain;
2741 	vm_domain_free_lock(vmd);
2742 	i = vm_phys_alloc_npages(domain, pgcache->pool, cnt,
2743 	    (vm_page_t *)store);
2744 	vm_domain_free_unlock(vmd);
2745 	if (cnt != i)
2746 		vm_domain_freecnt_inc(vmd, cnt - i);
2747 
2748 	return (i);
2749 }
2750 
2751 static void
2752 vm_page_zone_release(void *arg, void **store, int cnt)
2753 {
2754 	struct vm_domain *vmd;
2755 	struct vm_pgcache *pgcache;
2756 	vm_page_t m;
2757 	int i;
2758 
2759 	pgcache = arg;
2760 	vmd = VM_DOMAIN(pgcache->domain);
2761 	vm_domain_free_lock(vmd);
2762 	for (i = 0; i < cnt; i++) {
2763 		m = (vm_page_t)store[i];
2764 		vm_phys_free_pages(m, 0);
2765 	}
2766 	vm_domain_free_unlock(vmd);
2767 	vm_domain_freecnt_inc(vmd, cnt);
2768 }
2769 
2770 #define	VPSC_ANY	0	/* No restrictions. */
2771 #define	VPSC_NORESERV	1	/* Skip reservations; implies VPSC_NOSUPER. */
2772 #define	VPSC_NOSUPER	2	/* Skip superpages. */
2773 
2774 /*
2775  *	vm_page_scan_contig:
2776  *
2777  *	Scan vm_page_array[] between the specified entries "m_start" and
2778  *	"m_end" for a run of contiguous physical pages that satisfy the
2779  *	specified conditions, and return the lowest page in the run.  The
2780  *	specified "alignment" determines the alignment of the lowest physical
2781  *	page in the run.  If the specified "boundary" is non-zero, then the
2782  *	run of physical pages cannot span a physical address that is a
2783  *	multiple of "boundary".
2784  *
2785  *	"m_end" is never dereferenced, so it need not point to a vm_page
2786  *	structure within vm_page_array[].
2787  *
2788  *	"npages" must be greater than zero.  "m_start" and "m_end" must not
2789  *	span a hole (or discontiguity) in the physical address space.  Both
2790  *	"alignment" and "boundary" must be a power of two.
2791  */
2792 static vm_page_t
2793 vm_page_scan_contig(u_long npages, vm_page_t m_start, vm_page_t m_end,
2794     u_long alignment, vm_paddr_t boundary, int options)
2795 {
2796 	vm_object_t object;
2797 	vm_paddr_t pa;
2798 	vm_page_t m, m_run;
2799 #if VM_NRESERVLEVEL > 0
2800 	int level;
2801 #endif
2802 	int m_inc, order, run_ext, run_len;
2803 
2804 	KASSERT(npages > 0, ("npages is 0"));
2805 	KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
2806 	KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
2807 	m_run = NULL;
2808 	run_len = 0;
2809 	for (m = m_start; m < m_end && run_len < npages; m += m_inc) {
2810 		KASSERT((m->flags & PG_MARKER) == 0,
2811 		    ("page %p is PG_MARKER", m));
2812 		KASSERT((m->flags & PG_FICTITIOUS) == 0 || m->ref_count >= 1,
2813 		    ("fictitious page %p has invalid ref count", m));
2814 
2815 		/*
2816 		 * If the current page would be the start of a run, check its
2817 		 * physical address against the end, alignment, and boundary
2818 		 * conditions.  If it doesn't satisfy these conditions, either
2819 		 * terminate the scan or advance to the next page that
2820 		 * satisfies the failed condition.
2821 		 */
2822 		if (run_len == 0) {
2823 			KASSERT(m_run == NULL, ("m_run != NULL"));
2824 			if (m + npages > m_end)
2825 				break;
2826 			pa = VM_PAGE_TO_PHYS(m);
2827 			if (!vm_addr_align_ok(pa, alignment)) {
2828 				m_inc = atop(roundup2(pa, alignment) - pa);
2829 				continue;
2830 			}
2831 			if (!vm_addr_bound_ok(pa, ptoa(npages), boundary)) {
2832 				m_inc = atop(roundup2(pa, boundary) - pa);
2833 				continue;
2834 			}
2835 		} else
2836 			KASSERT(m_run != NULL, ("m_run == NULL"));
2837 
2838 retry:
2839 		m_inc = 1;
2840 		if (vm_page_wired(m))
2841 			run_ext = 0;
2842 #if VM_NRESERVLEVEL > 0
2843 		else if ((level = vm_reserv_level(m)) >= 0 &&
2844 		    (options & VPSC_NORESERV) != 0) {
2845 			run_ext = 0;
2846 			/* Advance to the end of the reservation. */
2847 			pa = VM_PAGE_TO_PHYS(m);
2848 			m_inc = atop(roundup2(pa + 1, vm_reserv_size(level)) -
2849 			    pa);
2850 		}
2851 #endif
2852 		else if ((object = atomic_load_ptr(&m->object)) != NULL) {
2853 			/*
2854 			 * The page is considered eligible for relocation if
2855 			 * and only if it could be laundered or reclaimed by
2856 			 * the page daemon.
2857 			 */
2858 			VM_OBJECT_RLOCK(object);
2859 			if (object != m->object) {
2860 				VM_OBJECT_RUNLOCK(object);
2861 				goto retry;
2862 			}
2863 			/* Don't care: PG_NODUMP, PG_ZERO. */
2864 			if ((object->flags & OBJ_SWAP) == 0 &&
2865 			    object->type != OBJT_VNODE) {
2866 				run_ext = 0;
2867 #if VM_NRESERVLEVEL > 0
2868 			} else if ((options & VPSC_NOSUPER) != 0 &&
2869 			    (level = vm_reserv_level_iffullpop(m)) >= 0) {
2870 				run_ext = 0;
2871 				/* Advance to the end of the superpage. */
2872 				pa = VM_PAGE_TO_PHYS(m);
2873 				m_inc = atop(roundup2(pa + 1,
2874 				    vm_reserv_size(level)) - pa);
2875 #endif
2876 			} else if (object->memattr == VM_MEMATTR_DEFAULT &&
2877 			    vm_page_queue(m) != PQ_NONE && !vm_page_busied(m)) {
2878 				/*
2879 				 * The page is allocated but eligible for
2880 				 * relocation.  Extend the current run by one
2881 				 * page.
2882 				 */
2883 				KASSERT(pmap_page_get_memattr(m) ==
2884 				    VM_MEMATTR_DEFAULT,
2885 				    ("page %p has an unexpected memattr", m));
2886 				KASSERT((m->oflags & (VPO_SWAPINPROG |
2887 				    VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0,
2888 				    ("page %p has unexpected oflags", m));
2889 				/* Don't care: PGA_NOSYNC. */
2890 				run_ext = 1;
2891 			} else
2892 				run_ext = 0;
2893 			VM_OBJECT_RUNLOCK(object);
2894 #if VM_NRESERVLEVEL > 0
2895 		} else if (level >= 0) {
2896 			/*
2897 			 * The page is reserved but not yet allocated.  In
2898 			 * other words, it is still free.  Extend the current
2899 			 * run by one page.
2900 			 */
2901 			run_ext = 1;
2902 #endif
2903 		} else if ((order = m->order) < VM_NFREEORDER) {
2904 			/*
2905 			 * The page is enqueued in the physical memory
2906 			 * allocator's free page queues.  Moreover, it is the
2907 			 * first page in a power-of-two-sized run of
2908 			 * contiguous free pages.  Add these pages to the end
2909 			 * of the current run, and jump ahead.
2910 			 */
2911 			run_ext = 1 << order;
2912 			m_inc = 1 << order;
2913 		} else {
2914 			/*
2915 			 * Skip the page for one of the following reasons: (1)
2916 			 * It is enqueued in the physical memory allocator's
2917 			 * free page queues.  However, it is not the first
2918 			 * page in a run of contiguous free pages.  (This case
2919 			 * rarely occurs because the scan is performed in
2920 			 * ascending order.) (2) It is not reserved, and it is
2921 			 * transitioning from free to allocated.  (Conversely,
2922 			 * the transition from allocated to free for managed
2923 			 * pages is blocked by the page busy lock.) (3) It is
2924 			 * allocated but not contained by an object and not
2925 			 * wired, e.g., allocated by Xen's balloon driver.
2926 			 */
2927 			run_ext = 0;
2928 		}
2929 
2930 		/*
2931 		 * Extend or reset the current run of pages.
2932 		 */
2933 		if (run_ext > 0) {
2934 			if (run_len == 0)
2935 				m_run = m;
2936 			run_len += run_ext;
2937 		} else {
2938 			if (run_len > 0) {
2939 				m_run = NULL;
2940 				run_len = 0;
2941 			}
2942 		}
2943 	}
2944 	if (run_len >= npages)
2945 		return (m_run);
2946 	return (NULL);
2947 }
2948 
2949 /*
2950  *	vm_page_reclaim_run:
2951  *
2952  *	Try to relocate each of the allocated virtual pages within the
2953  *	specified run of physical pages to a new physical address.  Free the
2954  *	physical pages underlying the relocated virtual pages.  A virtual page
2955  *	is relocatable if and only if it could be laundered or reclaimed by
2956  *	the page daemon.  Whenever possible, a virtual page is relocated to a
2957  *	physical address above "high".
2958  *
2959  *	Returns 0 if every physical page within the run was already free or
2960  *	just freed by a successful relocation.  Otherwise, returns a non-zero
2961  *	value indicating why the last attempt to relocate a virtual page was
2962  *	unsuccessful.
2963  *
2964  *	"req_class" must be an allocation class.
2965  */
2966 static int
2967 vm_page_reclaim_run(int req_class, int domain, u_long npages, vm_page_t m_run,
2968     vm_paddr_t high)
2969 {
2970 	struct vm_domain *vmd;
2971 	struct spglist free;
2972 	vm_object_t object;
2973 	vm_paddr_t pa;
2974 	vm_page_t m, m_end, m_new;
2975 	int error, order, req;
2976 
2977 	KASSERT((req_class & VM_ALLOC_CLASS_MASK) == req_class,
2978 	    ("req_class is not an allocation class"));
2979 	SLIST_INIT(&free);
2980 	error = 0;
2981 	m = m_run;
2982 	m_end = m_run + npages;
2983 	for (; error == 0 && m < m_end; m++) {
2984 		KASSERT((m->flags & (PG_FICTITIOUS | PG_MARKER)) == 0,
2985 		    ("page %p is PG_FICTITIOUS or PG_MARKER", m));
2986 
2987 		/*
2988 		 * Racily check for wirings.  Races are handled once the object
2989 		 * lock is held and the page is unmapped.
2990 		 */
2991 		if (vm_page_wired(m))
2992 			error = EBUSY;
2993 		else if ((object = atomic_load_ptr(&m->object)) != NULL) {
2994 			/*
2995 			 * The page is relocated if and only if it could be
2996 			 * laundered or reclaimed by the page daemon.
2997 			 */
2998 			VM_OBJECT_WLOCK(object);
2999 			/* Don't care: PG_NODUMP, PG_ZERO. */
3000 			if (m->object != object ||
3001 			    ((object->flags & OBJ_SWAP) == 0 &&
3002 			    object->type != OBJT_VNODE))
3003 				error = EINVAL;
3004 			else if (object->memattr != VM_MEMATTR_DEFAULT)
3005 				error = EINVAL;
3006 			else if (vm_page_queue(m) != PQ_NONE &&
3007 			    vm_page_tryxbusy(m) != 0) {
3008 				if (vm_page_wired(m)) {
3009 					vm_page_xunbusy(m);
3010 					error = EBUSY;
3011 					goto unlock;
3012 				}
3013 				KASSERT(pmap_page_get_memattr(m) ==
3014 				    VM_MEMATTR_DEFAULT,
3015 				    ("page %p has an unexpected memattr", m));
3016 				KASSERT(m->oflags == 0,
3017 				    ("page %p has unexpected oflags", m));
3018 				/* Don't care: PGA_NOSYNC. */
3019 				if (!vm_page_none_valid(m)) {
3020 					/*
3021 					 * First, try to allocate a new page
3022 					 * that is above "high".  Failing
3023 					 * that, try to allocate a new page
3024 					 * that is below "m_run".  Allocate
3025 					 * the new page between the end of
3026 					 * "m_run" and "high" only as a last
3027 					 * resort.
3028 					 */
3029 					req = req_class;
3030 					if ((m->flags & PG_NODUMP) != 0)
3031 						req |= VM_ALLOC_NODUMP;
3032 					if (trunc_page(high) !=
3033 					    ~(vm_paddr_t)PAGE_MASK) {
3034 						m_new =
3035 						    vm_page_alloc_noobj_contig(
3036 						    req, 1, round_page(high),
3037 						    ~(vm_paddr_t)0, PAGE_SIZE,
3038 						    0, VM_MEMATTR_DEFAULT);
3039 					} else
3040 						m_new = NULL;
3041 					if (m_new == NULL) {
3042 						pa = VM_PAGE_TO_PHYS(m_run);
3043 						m_new =
3044 						    vm_page_alloc_noobj_contig(
3045 						    req, 1, 0, pa - 1,
3046 						    PAGE_SIZE, 0,
3047 						    VM_MEMATTR_DEFAULT);
3048 					}
3049 					if (m_new == NULL) {
3050 						pa += ptoa(npages);
3051 						m_new =
3052 						    vm_page_alloc_noobj_contig(
3053 						    req, 1, pa, high, PAGE_SIZE,
3054 						    0, VM_MEMATTR_DEFAULT);
3055 					}
3056 					if (m_new == NULL) {
3057 						vm_page_xunbusy(m);
3058 						error = ENOMEM;
3059 						goto unlock;
3060 					}
3061 
3062 					/*
3063 					 * Unmap the page and check for new
3064 					 * wirings that may have been acquired
3065 					 * through a pmap lookup.
3066 					 */
3067 					if (object->ref_count != 0 &&
3068 					    !vm_page_try_remove_all(m)) {
3069 						vm_page_xunbusy(m);
3070 						vm_page_free(m_new);
3071 						error = EBUSY;
3072 						goto unlock;
3073 					}
3074 
3075 					/*
3076 					 * Replace "m" with the new page.  For
3077 					 * vm_page_replace(), "m" must be busy
3078 					 * and dequeued.  Finally, change "m"
3079 					 * as if vm_page_free() was called.
3080 					 */
3081 					m_new->a.flags = m->a.flags &
3082 					    ~PGA_QUEUE_STATE_MASK;
3083 					KASSERT(m_new->oflags == VPO_UNMANAGED,
3084 					    ("page %p is managed", m_new));
3085 					m_new->oflags = 0;
3086 					pmap_copy_page(m, m_new);
3087 					m_new->valid = m->valid;
3088 					m_new->dirty = m->dirty;
3089 					m->flags &= ~PG_ZERO;
3090 					vm_page_dequeue(m);
3091 					if (vm_page_replace_hold(m_new, object,
3092 					    m->pindex, m) &&
3093 					    vm_page_free_prep(m))
3094 						SLIST_INSERT_HEAD(&free, m,
3095 						    plinks.s.ss);
3096 
3097 					/*
3098 					 * The new page must be deactivated
3099 					 * before the object is unlocked.
3100 					 */
3101 					vm_page_deactivate(m_new);
3102 				} else {
3103 					m->flags &= ~PG_ZERO;
3104 					vm_page_dequeue(m);
3105 					if (vm_page_free_prep(m))
3106 						SLIST_INSERT_HEAD(&free, m,
3107 						    plinks.s.ss);
3108 					KASSERT(m->dirty == 0,
3109 					    ("page %p is dirty", m));
3110 				}
3111 			} else
3112 				error = EBUSY;
3113 unlock:
3114 			VM_OBJECT_WUNLOCK(object);
3115 		} else {
3116 			MPASS(vm_page_domain(m) == domain);
3117 			vmd = VM_DOMAIN(domain);
3118 			vm_domain_free_lock(vmd);
3119 			order = m->order;
3120 			if (order < VM_NFREEORDER) {
3121 				/*
3122 				 * The page is enqueued in the physical memory
3123 				 * allocator's free page queues.  Moreover, it
3124 				 * is the first page in a power-of-two-sized
3125 				 * run of contiguous free pages.  Jump ahead
3126 				 * to the last page within that run, and
3127 				 * continue from there.
3128 				 */
3129 				m += (1 << order) - 1;
3130 			}
3131 #if VM_NRESERVLEVEL > 0
3132 			else if (vm_reserv_is_page_free(m))
3133 				order = 0;
3134 #endif
3135 			vm_domain_free_unlock(vmd);
3136 			if (order == VM_NFREEORDER)
3137 				error = EINVAL;
3138 		}
3139 	}
3140 	if ((m = SLIST_FIRST(&free)) != NULL) {
3141 		int cnt;
3142 
3143 		vmd = VM_DOMAIN(domain);
3144 		cnt = 0;
3145 		vm_domain_free_lock(vmd);
3146 		do {
3147 			MPASS(vm_page_domain(m) == domain);
3148 			SLIST_REMOVE_HEAD(&free, plinks.s.ss);
3149 			vm_phys_free_pages(m, 0);
3150 			cnt++;
3151 		} while ((m = SLIST_FIRST(&free)) != NULL);
3152 		vm_domain_free_unlock(vmd);
3153 		vm_domain_freecnt_inc(vmd, cnt);
3154 	}
3155 	return (error);
3156 }
3157 
3158 #define	NRUNS	16
3159 
3160 #define	RUN_INDEX(count, nruns)	((count) % (nruns))
3161 
3162 #define	MIN_RECLAIM	8
3163 
3164 /*
3165  *	vm_page_reclaim_contig:
3166  *
3167  *	Reclaim allocated, contiguous physical memory satisfying the specified
3168  *	conditions by relocating the virtual pages using that physical memory.
3169  *	Returns 0 if reclamation is successful, ERANGE if the specified domain
3170  *	can't possibly satisfy the reclamation request, or ENOMEM if not
3171  *	currently able to reclaim the requested number of pages.  Since
3172  *	relocation requires the allocation of physical pages, reclamation may
3173  *	fail with ENOMEM due to a shortage of free pages.  When reclamation
3174  *	fails in this manner, callers are expected to perform vm_wait() before
3175  *	retrying a failed allocation operation, e.g., vm_page_alloc_contig().
3176  *
3177  *	The caller must always specify an allocation class through "req".
3178  *
3179  *	allocation classes:
3180  *	VM_ALLOC_NORMAL		normal process request
3181  *	VM_ALLOC_SYSTEM		system *really* needs a page
3182  *	VM_ALLOC_INTERRUPT	interrupt time request
3183  *
3184  *	The optional allocation flags are ignored.
3185  *
3186  *	"npages" must be greater than zero.  Both "alignment" and "boundary"
3187  *	must be a power of two.
3188  */
3189 int
3190 vm_page_reclaim_contig_domain_ext(int domain, int req, u_long npages,
3191     vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
3192     int desired_runs)
3193 {
3194 	struct vm_domain *vmd;
3195 	vm_page_t bounds[2], m_run, _m_runs[NRUNS], *m_runs;
3196 	u_long count, minalign, reclaimed;
3197 	int error, i, min_reclaim, nruns, options, req_class;
3198 	int segind, start_segind;
3199 	int ret;
3200 
3201 	KASSERT(npages > 0, ("npages is 0"));
3202 	KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
3203 	KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
3204 
3205 	ret = ENOMEM;
3206 
3207 	/*
3208 	 * If the caller wants to reclaim multiple runs, try to allocate
3209 	 * space to store the runs.  If that fails, fall back to the old
3210 	 * behavior of just reclaiming MIN_RECLAIM pages.
3211 	 */
3212 	if (desired_runs > 1)
3213 		m_runs = malloc((NRUNS + desired_runs) * sizeof(*m_runs),
3214 		    M_TEMP, M_NOWAIT);
3215 	else
3216 		m_runs = NULL;
3217 
3218 	if (m_runs == NULL) {
3219 		m_runs = _m_runs;
3220 		nruns = NRUNS;
3221 	} else {
3222 		nruns = NRUNS + desired_runs - 1;
3223 	}
3224 	min_reclaim = MAX(desired_runs * npages, MIN_RECLAIM);
3225 
3226 	/*
3227 	 * The caller will attempt an allocation after some runs have been
3228 	 * reclaimed and added to the vm_phys buddy lists.  Due to limitations
3229 	 * of vm_phys_alloc_contig(), round up the requested length to the next
3230 	 * power of two or maximum chunk size, and ensure that each run is
3231 	 * suitably aligned.
3232 	 */
3233 	minalign = 1ul << imin(flsl(npages - 1), VM_NFREEORDER - 1);
3234 	npages = roundup2(npages, minalign);
3235 	if (alignment < ptoa(minalign))
3236 		alignment = ptoa(minalign);
3237 
3238 	/*
3239 	 * The page daemon is allowed to dig deeper into the free page list.
3240 	 */
3241 	req_class = req & VM_ALLOC_CLASS_MASK;
3242 	if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
3243 		req_class = VM_ALLOC_SYSTEM;
3244 
3245 	start_segind = vm_phys_lookup_segind(low);
3246 
3247 	/*
3248 	 * Return if the number of free pages cannot satisfy the requested
3249 	 * allocation.
3250 	 */
3251 	vmd = VM_DOMAIN(domain);
3252 	count = vmd->vmd_free_count;
3253 	if (count < npages + vmd->vmd_free_reserved || (count < npages +
3254 	    vmd->vmd_interrupt_free_min && req_class == VM_ALLOC_SYSTEM) ||
3255 	    (count < npages && req_class == VM_ALLOC_INTERRUPT))
3256 		goto done;
3257 
3258 	/*
3259 	 * Scan up to three times, relaxing the restrictions ("options") on
3260 	 * the reclamation of reservations and superpages each time.
3261 	 */
3262 	for (options = VPSC_NORESERV;;) {
3263 		bool phys_range_exists = false;
3264 
3265 		/*
3266 		 * Find the highest runs that satisfy the given constraints
3267 		 * and restrictions, and record them in "m_runs".
3268 		 */
3269 		count = 0;
3270 		segind = start_segind;
3271 		while ((segind = vm_phys_find_range(bounds, segind, domain,
3272 		    npages, low, high)) != -1) {
3273 			phys_range_exists = true;
3274 			while ((m_run = vm_page_scan_contig(npages, bounds[0],
3275 			    bounds[1], alignment, boundary, options))) {
3276 				bounds[0] = m_run + npages;
3277 				m_runs[RUN_INDEX(count, nruns)] = m_run;
3278 				count++;
3279 			}
3280 			segind++;
3281 		}
3282 
3283 		if (!phys_range_exists) {
3284 			ret = ERANGE;
3285 			goto done;
3286 		}
3287 
3288 		/*
3289 		 * Reclaim the highest runs in LIFO (descending) order until
3290 		 * the number of reclaimed pages, "reclaimed", is at least
3291 		 * "min_reclaim".  Reset "reclaimed" each time because each
3292 		 * reclamation is idempotent, and runs will (likely) recur
3293 		 * from one scan to the next as restrictions are relaxed.
3294 		 */
3295 		reclaimed = 0;
3296 		for (i = 0; count > 0 && i < nruns; i++) {
3297 			count--;
3298 			m_run = m_runs[RUN_INDEX(count, nruns)];
3299 			error = vm_page_reclaim_run(req_class, domain, npages,
3300 			    m_run, high);
3301 			if (error == 0) {
3302 				reclaimed += npages;
3303 				if (reclaimed >= min_reclaim) {
3304 					ret = 0;
3305 					goto done;
3306 				}
3307 			}
3308 		}
3309 
3310 		/*
3311 		 * Either relax the restrictions on the next scan or return if
3312 		 * the last scan had no restrictions.
3313 		 */
3314 		if (options == VPSC_NORESERV)
3315 			options = VPSC_NOSUPER;
3316 		else if (options == VPSC_NOSUPER)
3317 			options = VPSC_ANY;
3318 		else if (options == VPSC_ANY) {
3319 			if (reclaimed != 0)
3320 				ret = 0;
3321 			goto done;
3322 		}
3323 	}
3324 done:
3325 	if (m_runs != _m_runs)
3326 		free(m_runs, M_TEMP);
3327 	return (ret);
3328 }
3329 
3330 int
3331 vm_page_reclaim_contig_domain(int domain, int req, u_long npages,
3332     vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
3333 {
3334 	return (vm_page_reclaim_contig_domain_ext(domain, req, npages, low, high,
3335 	    alignment, boundary, 1));
3336 }
3337 
3338 int
3339 vm_page_reclaim_contig(int req, u_long npages, vm_paddr_t low, vm_paddr_t high,
3340     u_long alignment, vm_paddr_t boundary)
3341 {
3342 	struct vm_domainset_iter di;
3343 	int domain, ret, status;
3344 
3345 	ret = ERANGE;
3346 
3347 	vm_domainset_iter_page_init(&di, NULL, 0, &domain, &req);
3348 	do {
3349 		status = vm_page_reclaim_contig_domain(domain, req, npages, low,
3350 		    high, alignment, boundary);
3351 		if (status == 0)
3352 			return (0);
3353 		else if (status == ERANGE)
3354 			vm_domainset_iter_ignore(&di, domain);
3355 		else {
3356 			KASSERT(status == ENOMEM, ("Unrecognized error %d "
3357 			    "from vm_page_reclaim_contig_domain()", status));
3358 			ret = ENOMEM;
3359 		}
3360 	} while (vm_domainset_iter_page(&di, NULL, &domain) == 0);
3361 
3362 	return (ret);
3363 }
3364 
3365 /*
3366  * Set the domain in the appropriate page level domainset.
3367  */
3368 void
3369 vm_domain_set(struct vm_domain *vmd)
3370 {
3371 
3372 	mtx_lock(&vm_domainset_lock);
3373 	if (!vmd->vmd_minset && vm_paging_min(vmd)) {
3374 		vmd->vmd_minset = 1;
3375 		DOMAINSET_SET(vmd->vmd_domain, &vm_min_domains);
3376 	}
3377 	if (!vmd->vmd_severeset && vm_paging_severe(vmd)) {
3378 		vmd->vmd_severeset = 1;
3379 		DOMAINSET_SET(vmd->vmd_domain, &vm_severe_domains);
3380 	}
3381 	mtx_unlock(&vm_domainset_lock);
3382 }
3383 
3384 /*
3385  * Clear the domain from the appropriate page level domainset.
3386  */
3387 void
3388 vm_domain_clear(struct vm_domain *vmd)
3389 {
3390 
3391 	mtx_lock(&vm_domainset_lock);
3392 	if (vmd->vmd_minset && !vm_paging_min(vmd)) {
3393 		vmd->vmd_minset = 0;
3394 		DOMAINSET_CLR(vmd->vmd_domain, &vm_min_domains);
3395 		if (vm_min_waiters != 0) {
3396 			vm_min_waiters = 0;
3397 			wakeup(&vm_min_domains);
3398 		}
3399 	}
3400 	if (vmd->vmd_severeset && !vm_paging_severe(vmd)) {
3401 		vmd->vmd_severeset = 0;
3402 		DOMAINSET_CLR(vmd->vmd_domain, &vm_severe_domains);
3403 		if (vm_severe_waiters != 0) {
3404 			vm_severe_waiters = 0;
3405 			wakeup(&vm_severe_domains);
3406 		}
3407 	}
3408 
3409 	/*
3410 	 * If pageout daemon needs pages, then tell it that there are
3411 	 * some free.
3412 	 */
3413 	if (vmd->vmd_pageout_pages_needed &&
3414 	    vmd->vmd_free_count >= vmd->vmd_pageout_free_min) {
3415 		wakeup(&vmd->vmd_pageout_pages_needed);
3416 		vmd->vmd_pageout_pages_needed = 0;
3417 	}
3418 
3419 	/* See comments in vm_wait_doms(). */
3420 	if (vm_pageproc_waiters) {
3421 		vm_pageproc_waiters = 0;
3422 		wakeup(&vm_pageproc_waiters);
3423 	}
3424 	mtx_unlock(&vm_domainset_lock);
3425 }
3426 
3427 /*
3428  * Wait for free pages to exceed the min threshold globally.
3429  */
3430 void
3431 vm_wait_min(void)
3432 {
3433 
3434 	mtx_lock(&vm_domainset_lock);
3435 	while (vm_page_count_min()) {
3436 		vm_min_waiters++;
3437 		msleep(&vm_min_domains, &vm_domainset_lock, PVM, "vmwait", 0);
3438 	}
3439 	mtx_unlock(&vm_domainset_lock);
3440 }
3441 
3442 /*
3443  * Wait for free pages to exceed the severe threshold globally.
3444  */
3445 void
3446 vm_wait_severe(void)
3447 {
3448 
3449 	mtx_lock(&vm_domainset_lock);
3450 	while (vm_page_count_severe()) {
3451 		vm_severe_waiters++;
3452 		msleep(&vm_severe_domains, &vm_domainset_lock, PVM,
3453 		    "vmwait", 0);
3454 	}
3455 	mtx_unlock(&vm_domainset_lock);
3456 }
3457 
3458 u_int
3459 vm_wait_count(void)
3460 {
3461 
3462 	return (vm_severe_waiters + vm_min_waiters + vm_pageproc_waiters);
3463 }
3464 
3465 int
3466 vm_wait_doms(const domainset_t *wdoms, int mflags)
3467 {
3468 	int error;
3469 
3470 	error = 0;
3471 
3472 	/*
3473 	 * We use racey wakeup synchronization to avoid expensive global
3474 	 * locking for the pageproc when sleeping with a non-specific vm_wait.
3475 	 * To handle this, we only sleep for one tick in this instance.  It
3476 	 * is expected that most allocations for the pageproc will come from
3477 	 * kmem or vm_page_grab* which will use the more specific and
3478 	 * race-free vm_wait_domain().
3479 	 */
3480 	if (curproc == pageproc) {
3481 		mtx_lock(&vm_domainset_lock);
3482 		vm_pageproc_waiters++;
3483 		error = msleep(&vm_pageproc_waiters, &vm_domainset_lock,
3484 		    PVM | PDROP | mflags, "pageprocwait", 1);
3485 	} else {
3486 		/*
3487 		 * XXX Ideally we would wait only until the allocation could
3488 		 * be satisfied.  This condition can cause new allocators to
3489 		 * consume all freed pages while old allocators wait.
3490 		 */
3491 		mtx_lock(&vm_domainset_lock);
3492 		if (vm_page_count_min_set(wdoms)) {
3493 			if (pageproc == NULL)
3494 				panic("vm_wait in early boot");
3495 			vm_min_waiters++;
3496 			error = msleep(&vm_min_domains, &vm_domainset_lock,
3497 			    PVM | PDROP | mflags, "vmwait", 0);
3498 		} else
3499 			mtx_unlock(&vm_domainset_lock);
3500 	}
3501 	return (error);
3502 }
3503 
3504 /*
3505  *	vm_wait_domain:
3506  *
3507  *	Sleep until free pages are available for allocation.
3508  *	- Called in various places after failed memory allocations.
3509  */
3510 void
3511 vm_wait_domain(int domain)
3512 {
3513 	struct vm_domain *vmd;
3514 	domainset_t wdom;
3515 
3516 	vmd = VM_DOMAIN(domain);
3517 	vm_domain_free_assert_unlocked(vmd);
3518 
3519 	if (curproc == pageproc) {
3520 		mtx_lock(&vm_domainset_lock);
3521 		if (vmd->vmd_free_count < vmd->vmd_pageout_free_min) {
3522 			vmd->vmd_pageout_pages_needed = 1;
3523 			msleep(&vmd->vmd_pageout_pages_needed,
3524 			    &vm_domainset_lock, PDROP | PSWP, "VMWait", 0);
3525 		} else
3526 			mtx_unlock(&vm_domainset_lock);
3527 	} else {
3528 		DOMAINSET_ZERO(&wdom);
3529 		DOMAINSET_SET(vmd->vmd_domain, &wdom);
3530 		vm_wait_doms(&wdom, 0);
3531 	}
3532 }
3533 
3534 static int
3535 vm_wait_flags(vm_object_t obj, int mflags)
3536 {
3537 	struct domainset *d;
3538 
3539 	d = NULL;
3540 
3541 	/*
3542 	 * Carefully fetch pointers only once: the struct domainset
3543 	 * itself is ummutable but the pointer might change.
3544 	 */
3545 	if (obj != NULL)
3546 		d = obj->domain.dr_policy;
3547 	if (d == NULL)
3548 		d = curthread->td_domain.dr_policy;
3549 
3550 	return (vm_wait_doms(&d->ds_mask, mflags));
3551 }
3552 
3553 /*
3554  *	vm_wait:
3555  *
3556  *	Sleep until free pages are available for allocation in the
3557  *	affinity domains of the obj.  If obj is NULL, the domain set
3558  *	for the calling thread is used.
3559  *	Called in various places after failed memory allocations.
3560  */
3561 void
3562 vm_wait(vm_object_t obj)
3563 {
3564 	(void)vm_wait_flags(obj, 0);
3565 }
3566 
3567 int
3568 vm_wait_intr(vm_object_t obj)
3569 {
3570 	return (vm_wait_flags(obj, PCATCH));
3571 }
3572 
3573 /*
3574  *	vm_domain_alloc_fail:
3575  *
3576  *	Called when a page allocation function fails.  Informs the
3577  *	pagedaemon and performs the requested wait.  Requires the
3578  *	domain_free and object lock on entry.  Returns with the
3579  *	object lock held and free lock released.  Returns an error when
3580  *	retry is necessary.
3581  *
3582  */
3583 static int
3584 vm_domain_alloc_fail(struct vm_domain *vmd, vm_object_t object, int req)
3585 {
3586 
3587 	vm_domain_free_assert_unlocked(vmd);
3588 
3589 	atomic_add_int(&vmd->vmd_pageout_deficit,
3590 	    max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
3591 	if (req & (VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL)) {
3592 		if (object != NULL)
3593 			VM_OBJECT_WUNLOCK(object);
3594 		vm_wait_domain(vmd->vmd_domain);
3595 		if (object != NULL)
3596 			VM_OBJECT_WLOCK(object);
3597 		if (req & VM_ALLOC_WAITOK)
3598 			return (EAGAIN);
3599 	}
3600 
3601 	return (0);
3602 }
3603 
3604 /*
3605  *	vm_waitpfault:
3606  *
3607  *	Sleep until free pages are available for allocation.
3608  *	- Called only in vm_fault so that processes page faulting
3609  *	  can be easily tracked.
3610  *	- Sleeps at a lower priority than vm_wait() so that vm_wait()ing
3611  *	  processes will be able to grab memory first.  Do not change
3612  *	  this balance without careful testing first.
3613  */
3614 void
3615 vm_waitpfault(struct domainset *dset, int timo)
3616 {
3617 
3618 	/*
3619 	 * XXX Ideally we would wait only until the allocation could
3620 	 * be satisfied.  This condition can cause new allocators to
3621 	 * consume all freed pages while old allocators wait.
3622 	 */
3623 	mtx_lock(&vm_domainset_lock);
3624 	if (vm_page_count_min_set(&dset->ds_mask)) {
3625 		vm_min_waiters++;
3626 		msleep(&vm_min_domains, &vm_domainset_lock, PUSER | PDROP,
3627 		    "pfault", timo);
3628 	} else
3629 		mtx_unlock(&vm_domainset_lock);
3630 }
3631 
3632 static struct vm_pagequeue *
3633 _vm_page_pagequeue(vm_page_t m, uint8_t queue)
3634 {
3635 
3636 	return (&vm_pagequeue_domain(m)->vmd_pagequeues[queue]);
3637 }
3638 
3639 #ifdef INVARIANTS
3640 static struct vm_pagequeue *
3641 vm_page_pagequeue(vm_page_t m)
3642 {
3643 
3644 	return (_vm_page_pagequeue(m, vm_page_astate_load(m).queue));
3645 }
3646 #endif
3647 
3648 static __always_inline bool
3649 vm_page_pqstate_fcmpset(vm_page_t m, vm_page_astate_t *old, vm_page_astate_t new)
3650 {
3651 	vm_page_astate_t tmp;
3652 
3653 	tmp = *old;
3654 	do {
3655 		if (__predict_true(vm_page_astate_fcmpset(m, old, new)))
3656 			return (true);
3657 		counter_u64_add(pqstate_commit_retries, 1);
3658 	} while (old->_bits == tmp._bits);
3659 
3660 	return (false);
3661 }
3662 
3663 /*
3664  * Do the work of committing a queue state update that moves the page out of
3665  * its current queue.
3666  */
3667 static bool
3668 _vm_page_pqstate_commit_dequeue(struct vm_pagequeue *pq, vm_page_t m,
3669     vm_page_astate_t *old, vm_page_astate_t new)
3670 {
3671 	vm_page_t next;
3672 
3673 	vm_pagequeue_assert_locked(pq);
3674 	KASSERT(vm_page_pagequeue(m) == pq,
3675 	    ("%s: queue %p does not match page %p", __func__, pq, m));
3676 	KASSERT(old->queue != PQ_NONE && new.queue != old->queue,
3677 	    ("%s: invalid queue indices %d %d",
3678 	    __func__, old->queue, new.queue));
3679 
3680 	/*
3681 	 * Once the queue index of the page changes there is nothing
3682 	 * synchronizing with further updates to the page's physical
3683 	 * queue state.  Therefore we must speculatively remove the page
3684 	 * from the queue now and be prepared to roll back if the queue
3685 	 * state update fails.  If the page is not physically enqueued then
3686 	 * we just update its queue index.
3687 	 */
3688 	if ((old->flags & PGA_ENQUEUED) != 0) {
3689 		new.flags &= ~PGA_ENQUEUED;
3690 		next = TAILQ_NEXT(m, plinks.q);
3691 		TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
3692 		vm_pagequeue_cnt_dec(pq);
3693 		if (!vm_page_pqstate_fcmpset(m, old, new)) {
3694 			if (next == NULL)
3695 				TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
3696 			else
3697 				TAILQ_INSERT_BEFORE(next, m, plinks.q);
3698 			vm_pagequeue_cnt_inc(pq);
3699 			return (false);
3700 		} else {
3701 			return (true);
3702 		}
3703 	} else {
3704 		return (vm_page_pqstate_fcmpset(m, old, new));
3705 	}
3706 }
3707 
3708 static bool
3709 vm_page_pqstate_commit_dequeue(vm_page_t m, vm_page_astate_t *old,
3710     vm_page_astate_t new)
3711 {
3712 	struct vm_pagequeue *pq;
3713 	vm_page_astate_t as;
3714 	bool ret;
3715 
3716 	pq = _vm_page_pagequeue(m, old->queue);
3717 
3718 	/*
3719 	 * The queue field and PGA_ENQUEUED flag are stable only so long as the
3720 	 * corresponding page queue lock is held.
3721 	 */
3722 	vm_pagequeue_lock(pq);
3723 	as = vm_page_astate_load(m);
3724 	if (__predict_false(as._bits != old->_bits)) {
3725 		*old = as;
3726 		ret = false;
3727 	} else {
3728 		ret = _vm_page_pqstate_commit_dequeue(pq, m, old, new);
3729 	}
3730 	vm_pagequeue_unlock(pq);
3731 	return (ret);
3732 }
3733 
3734 /*
3735  * Commit a queue state update that enqueues or requeues a page.
3736  */
3737 static bool
3738 _vm_page_pqstate_commit_requeue(struct vm_pagequeue *pq, vm_page_t m,
3739     vm_page_astate_t *old, vm_page_astate_t new)
3740 {
3741 	struct vm_domain *vmd;
3742 
3743 	vm_pagequeue_assert_locked(pq);
3744 	KASSERT(old->queue != PQ_NONE && new.queue == old->queue,
3745 	    ("%s: invalid queue indices %d %d",
3746 	    __func__, old->queue, new.queue));
3747 
3748 	new.flags |= PGA_ENQUEUED;
3749 	if (!vm_page_pqstate_fcmpset(m, old, new))
3750 		return (false);
3751 
3752 	if ((old->flags & PGA_ENQUEUED) != 0)
3753 		TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
3754 	else
3755 		vm_pagequeue_cnt_inc(pq);
3756 
3757 	/*
3758 	 * Give PGA_REQUEUE_HEAD precedence over PGA_REQUEUE.  In particular, if
3759 	 * both flags are set in close succession, only PGA_REQUEUE_HEAD will be
3760 	 * applied, even if it was set first.
3761 	 */
3762 	if ((old->flags & PGA_REQUEUE_HEAD) != 0) {
3763 		vmd = vm_pagequeue_domain(m);
3764 		KASSERT(pq == &vmd->vmd_pagequeues[PQ_INACTIVE],
3765 		    ("%s: invalid page queue for page %p", __func__, m));
3766 		TAILQ_INSERT_BEFORE(&vmd->vmd_inacthead, m, plinks.q);
3767 	} else {
3768 		TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
3769 	}
3770 	return (true);
3771 }
3772 
3773 /*
3774  * Commit a queue state update that encodes a request for a deferred queue
3775  * operation.
3776  */
3777 static bool
3778 vm_page_pqstate_commit_request(vm_page_t m, vm_page_astate_t *old,
3779     vm_page_astate_t new)
3780 {
3781 
3782 	KASSERT(old->queue == new.queue || new.queue != PQ_NONE,
3783 	    ("%s: invalid state, queue %d flags %x",
3784 	    __func__, new.queue, new.flags));
3785 
3786 	if (old->_bits != new._bits &&
3787 	    !vm_page_pqstate_fcmpset(m, old, new))
3788 		return (false);
3789 	vm_page_pqbatch_submit(m, new.queue);
3790 	return (true);
3791 }
3792 
3793 /*
3794  * A generic queue state update function.  This handles more cases than the
3795  * specialized functions above.
3796  */
3797 bool
3798 vm_page_pqstate_commit(vm_page_t m, vm_page_astate_t *old, vm_page_astate_t new)
3799 {
3800 
3801 	if (old->_bits == new._bits)
3802 		return (true);
3803 
3804 	if (old->queue != PQ_NONE && new.queue != old->queue) {
3805 		if (!vm_page_pqstate_commit_dequeue(m, old, new))
3806 			return (false);
3807 		if (new.queue != PQ_NONE)
3808 			vm_page_pqbatch_submit(m, new.queue);
3809 	} else {
3810 		if (!vm_page_pqstate_fcmpset(m, old, new))
3811 			return (false);
3812 		if (new.queue != PQ_NONE &&
3813 		    ((new.flags & ~old->flags) & PGA_QUEUE_OP_MASK) != 0)
3814 			vm_page_pqbatch_submit(m, new.queue);
3815 	}
3816 	return (true);
3817 }
3818 
3819 /*
3820  * Apply deferred queue state updates to a page.
3821  */
3822 static inline void
3823 vm_pqbatch_process_page(struct vm_pagequeue *pq, vm_page_t m, uint8_t queue)
3824 {
3825 	vm_page_astate_t new, old;
3826 
3827 	CRITICAL_ASSERT(curthread);
3828 	vm_pagequeue_assert_locked(pq);
3829 	KASSERT(queue < PQ_COUNT,
3830 	    ("%s: invalid queue index %d", __func__, queue));
3831 	KASSERT(pq == _vm_page_pagequeue(m, queue),
3832 	    ("%s: page %p does not belong to queue %p", __func__, m, pq));
3833 
3834 	for (old = vm_page_astate_load(m);;) {
3835 		if (__predict_false(old.queue != queue ||
3836 		    (old.flags & PGA_QUEUE_OP_MASK) == 0)) {
3837 			counter_u64_add(queue_nops, 1);
3838 			break;
3839 		}
3840 		KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3841 		    ("%s: page %p is unmanaged", __func__, m));
3842 
3843 		new = old;
3844 		if ((old.flags & PGA_DEQUEUE) != 0) {
3845 			new.flags &= ~PGA_QUEUE_OP_MASK;
3846 			new.queue = PQ_NONE;
3847 			if (__predict_true(_vm_page_pqstate_commit_dequeue(pq,
3848 			    m, &old, new))) {
3849 				counter_u64_add(queue_ops, 1);
3850 				break;
3851 			}
3852 		} else {
3853 			new.flags &= ~(PGA_REQUEUE | PGA_REQUEUE_HEAD);
3854 			if (__predict_true(_vm_page_pqstate_commit_requeue(pq,
3855 			    m, &old, new))) {
3856 				counter_u64_add(queue_ops, 1);
3857 				break;
3858 			}
3859 		}
3860 	}
3861 }
3862 
3863 static void
3864 vm_pqbatch_process(struct vm_pagequeue *pq, struct vm_batchqueue *bq,
3865     uint8_t queue)
3866 {
3867 	int i;
3868 
3869 	for (i = 0; i < bq->bq_cnt; i++)
3870 		vm_pqbatch_process_page(pq, bq->bq_pa[i], queue);
3871 	vm_batchqueue_init(bq);
3872 }
3873 
3874 /*
3875  *	vm_page_pqbatch_submit:		[ internal use only ]
3876  *
3877  *	Enqueue a page in the specified page queue's batched work queue.
3878  *	The caller must have encoded the requested operation in the page
3879  *	structure's a.flags field.
3880  */
3881 void
3882 vm_page_pqbatch_submit(vm_page_t m, uint8_t queue)
3883 {
3884 	struct vm_batchqueue *bq;
3885 	struct vm_pagequeue *pq;
3886 	int domain, slots_remaining;
3887 
3888 	KASSERT(queue < PQ_COUNT, ("invalid queue %d", queue));
3889 
3890 	domain = vm_page_domain(m);
3891 	critical_enter();
3892 	bq = DPCPU_PTR(pqbatch[domain][queue]);
3893 	slots_remaining = vm_batchqueue_insert(bq, m);
3894 	if (slots_remaining > (VM_BATCHQUEUE_SIZE >> 1)) {
3895 		/* keep building the bq */
3896 		critical_exit();
3897 		return;
3898 	} else if (slots_remaining > 0 ) {
3899 		/* Try to process the bq if we can get the lock */
3900 		pq = &VM_DOMAIN(domain)->vmd_pagequeues[queue];
3901 		if (vm_pagequeue_trylock(pq)) {
3902 			vm_pqbatch_process(pq, bq, queue);
3903 			vm_pagequeue_unlock(pq);
3904 		}
3905 		critical_exit();
3906 		return;
3907 	}
3908 	critical_exit();
3909 
3910 	/* if we make it here, the bq is full so wait for the lock */
3911 
3912 	pq = &VM_DOMAIN(domain)->vmd_pagequeues[queue];
3913 	vm_pagequeue_lock(pq);
3914 	critical_enter();
3915 	bq = DPCPU_PTR(pqbatch[domain][queue]);
3916 	vm_pqbatch_process(pq, bq, queue);
3917 	vm_pqbatch_process_page(pq, m, queue);
3918 	vm_pagequeue_unlock(pq);
3919 	critical_exit();
3920 }
3921 
3922 /*
3923  *	vm_page_pqbatch_drain:		[ internal use only ]
3924  *
3925  *	Force all per-CPU page queue batch queues to be drained.  This is
3926  *	intended for use in severe memory shortages, to ensure that pages
3927  *	do not remain stuck in the batch queues.
3928  */
3929 void
3930 vm_page_pqbatch_drain(void)
3931 {
3932 	struct thread *td;
3933 	struct vm_domain *vmd;
3934 	struct vm_pagequeue *pq;
3935 	int cpu, domain, queue;
3936 
3937 	td = curthread;
3938 	CPU_FOREACH(cpu) {
3939 		thread_lock(td);
3940 		sched_bind(td, cpu);
3941 		thread_unlock(td);
3942 
3943 		for (domain = 0; domain < vm_ndomains; domain++) {
3944 			vmd = VM_DOMAIN(domain);
3945 			for (queue = 0; queue < PQ_COUNT; queue++) {
3946 				pq = &vmd->vmd_pagequeues[queue];
3947 				vm_pagequeue_lock(pq);
3948 				critical_enter();
3949 				vm_pqbatch_process(pq,
3950 				    DPCPU_PTR(pqbatch[domain][queue]), queue);
3951 				critical_exit();
3952 				vm_pagequeue_unlock(pq);
3953 			}
3954 		}
3955 	}
3956 	thread_lock(td);
3957 	sched_unbind(td);
3958 	thread_unlock(td);
3959 }
3960 
3961 /*
3962  *	vm_page_dequeue_deferred:	[ internal use only ]
3963  *
3964  *	Request removal of the given page from its current page
3965  *	queue.  Physical removal from the queue may be deferred
3966  *	indefinitely.
3967  */
3968 void
3969 vm_page_dequeue_deferred(vm_page_t m)
3970 {
3971 	vm_page_astate_t new, old;
3972 
3973 	old = vm_page_astate_load(m);
3974 	do {
3975 		if (old.queue == PQ_NONE) {
3976 			KASSERT((old.flags & PGA_QUEUE_STATE_MASK) == 0,
3977 			    ("%s: page %p has unexpected queue state",
3978 			    __func__, m));
3979 			break;
3980 		}
3981 		new = old;
3982 		new.flags |= PGA_DEQUEUE;
3983 	} while (!vm_page_pqstate_commit_request(m, &old, new));
3984 }
3985 
3986 /*
3987  *	vm_page_dequeue:
3988  *
3989  *	Remove the page from whichever page queue it's in, if any, before
3990  *	returning.
3991  */
3992 void
3993 vm_page_dequeue(vm_page_t m)
3994 {
3995 	vm_page_astate_t new, old;
3996 
3997 	old = vm_page_astate_load(m);
3998 	do {
3999 		if (old.queue == PQ_NONE) {
4000 			KASSERT((old.flags & PGA_QUEUE_STATE_MASK) == 0,
4001 			    ("%s: page %p has unexpected queue state",
4002 			    __func__, m));
4003 			break;
4004 		}
4005 		new = old;
4006 		new.flags &= ~PGA_QUEUE_OP_MASK;
4007 		new.queue = PQ_NONE;
4008 	} while (!vm_page_pqstate_commit_dequeue(m, &old, new));
4009 
4010 }
4011 
4012 /*
4013  * Schedule the given page for insertion into the specified page queue.
4014  * Physical insertion of the page may be deferred indefinitely.
4015  */
4016 static void
4017 vm_page_enqueue(vm_page_t m, uint8_t queue)
4018 {
4019 
4020 	KASSERT(m->a.queue == PQ_NONE &&
4021 	    (m->a.flags & PGA_QUEUE_STATE_MASK) == 0,
4022 	    ("%s: page %p is already enqueued", __func__, m));
4023 	KASSERT(m->ref_count > 0,
4024 	    ("%s: page %p does not carry any references", __func__, m));
4025 
4026 	m->a.queue = queue;
4027 	if ((m->a.flags & PGA_REQUEUE) == 0)
4028 		vm_page_aflag_set(m, PGA_REQUEUE);
4029 	vm_page_pqbatch_submit(m, queue);
4030 }
4031 
4032 /*
4033  *	vm_page_free_prep:
4034  *
4035  *	Prepares the given page to be put on the free list,
4036  *	disassociating it from any VM object. The caller may return
4037  *	the page to the free list only if this function returns true.
4038  *
4039  *	The object, if it exists, must be locked, and then the page must
4040  *	be xbusy.  Otherwise the page must be not busied.  A managed
4041  *	page must be unmapped.
4042  */
4043 static bool
4044 vm_page_free_prep(vm_page_t m)
4045 {
4046 
4047 	/*
4048 	 * Synchronize with threads that have dropped a reference to this
4049 	 * page.
4050 	 */
4051 	atomic_thread_fence_acq();
4052 
4053 #if defined(DIAGNOSTIC) && defined(PHYS_TO_DMAP)
4054 	if (PMAP_HAS_DMAP && (m->flags & PG_ZERO) != 0) {
4055 		uint64_t *p;
4056 		int i;
4057 		p = (uint64_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
4058 		for (i = 0; i < PAGE_SIZE / sizeof(uint64_t); i++, p++)
4059 			KASSERT(*p == 0, ("vm_page_free_prep %p PG_ZERO %d %jx",
4060 			    m, i, (uintmax_t)*p));
4061 	}
4062 #endif
4063 	KASSERT((m->flags & PG_NOFREE) == 0,
4064 	    ("%s: attempting to free a PG_NOFREE page", __func__));
4065 	if ((m->oflags & VPO_UNMANAGED) == 0) {
4066 		KASSERT(!pmap_page_is_mapped(m),
4067 		    ("vm_page_free_prep: freeing mapped page %p", m));
4068 		KASSERT((m->a.flags & (PGA_EXECUTABLE | PGA_WRITEABLE)) == 0,
4069 		    ("vm_page_free_prep: mapping flags set in page %p", m));
4070 	} else {
4071 		KASSERT(m->a.queue == PQ_NONE,
4072 		    ("vm_page_free_prep: unmanaged page %p is queued", m));
4073 	}
4074 	VM_CNT_INC(v_tfree);
4075 
4076 	if (m->object != NULL) {
4077 		KASSERT(((m->oflags & VPO_UNMANAGED) != 0) ==
4078 		    ((m->object->flags & OBJ_UNMANAGED) != 0),
4079 		    ("vm_page_free_prep: managed flag mismatch for page %p",
4080 		    m));
4081 		vm_page_assert_xbusied(m);
4082 
4083 		/*
4084 		 * The object reference can be released without an atomic
4085 		 * operation.
4086 		 */
4087 		KASSERT((m->flags & PG_FICTITIOUS) != 0 ||
4088 		    m->ref_count == VPRC_OBJREF,
4089 		    ("vm_page_free_prep: page %p has unexpected ref_count %u",
4090 		    m, m->ref_count));
4091 		vm_page_object_remove(m);
4092 		m->ref_count -= VPRC_OBJREF;
4093 	} else
4094 		vm_page_assert_unbusied(m);
4095 
4096 	vm_page_busy_free(m);
4097 
4098 	/*
4099 	 * If fictitious remove object association and
4100 	 * return.
4101 	 */
4102 	if ((m->flags & PG_FICTITIOUS) != 0) {
4103 		KASSERT(m->ref_count == 1,
4104 		    ("fictitious page %p is referenced", m));
4105 		KASSERT(m->a.queue == PQ_NONE,
4106 		    ("fictitious page %p is queued", m));
4107 		return (false);
4108 	}
4109 
4110 	/*
4111 	 * Pages need not be dequeued before they are returned to the physical
4112 	 * memory allocator, but they must at least be marked for a deferred
4113 	 * dequeue.
4114 	 */
4115 	if ((m->oflags & VPO_UNMANAGED) == 0)
4116 		vm_page_dequeue_deferred(m);
4117 
4118 	m->valid = 0;
4119 	vm_page_undirty(m);
4120 
4121 	if (m->ref_count != 0)
4122 		panic("vm_page_free_prep: page %p has references", m);
4123 
4124 	/*
4125 	 * Restore the default memory attribute to the page.
4126 	 */
4127 	if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
4128 		pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
4129 
4130 #if VM_NRESERVLEVEL > 0
4131 	/*
4132 	 * Determine whether the page belongs to a reservation.  If the page was
4133 	 * allocated from a per-CPU cache, it cannot belong to a reservation, so
4134 	 * as an optimization, we avoid the check in that case.
4135 	 */
4136 	if ((m->flags & PG_PCPU_CACHE) == 0 && vm_reserv_free_page(m))
4137 		return (false);
4138 #endif
4139 
4140 	return (true);
4141 }
4142 
4143 /*
4144  *	vm_page_free_toq:
4145  *
4146  *	Returns the given page to the free list, disassociating it
4147  *	from any VM object.
4148  *
4149  *	The object must be locked.  The page must be exclusively busied if it
4150  *	belongs to an object.
4151  */
4152 static void
4153 vm_page_free_toq(vm_page_t m)
4154 {
4155 	struct vm_domain *vmd;
4156 	uma_zone_t zone;
4157 
4158 	if (!vm_page_free_prep(m))
4159 		return;
4160 
4161 	vmd = vm_pagequeue_domain(m);
4162 	zone = vmd->vmd_pgcache[m->pool].zone;
4163 	if ((m->flags & PG_PCPU_CACHE) != 0 && zone != NULL) {
4164 		uma_zfree(zone, m);
4165 		return;
4166 	}
4167 	vm_domain_free_lock(vmd);
4168 	vm_phys_free_pages(m, 0);
4169 	vm_domain_free_unlock(vmd);
4170 	vm_domain_freecnt_inc(vmd, 1);
4171 }
4172 
4173 /*
4174  *	vm_page_free_pages_toq:
4175  *
4176  *	Returns a list of pages to the free list, disassociating it
4177  *	from any VM object.  In other words, this is equivalent to
4178  *	calling vm_page_free_toq() for each page of a list of VM objects.
4179  */
4180 void
4181 vm_page_free_pages_toq(struct spglist *free, bool update_wire_count)
4182 {
4183 	vm_page_t m;
4184 	int count;
4185 
4186 	if (SLIST_EMPTY(free))
4187 		return;
4188 
4189 	count = 0;
4190 	while ((m = SLIST_FIRST(free)) != NULL) {
4191 		count++;
4192 		SLIST_REMOVE_HEAD(free, plinks.s.ss);
4193 		vm_page_free_toq(m);
4194 	}
4195 
4196 	if (update_wire_count)
4197 		vm_wire_sub(count);
4198 }
4199 
4200 /*
4201  * Mark this page as wired down.  For managed pages, this prevents reclamation
4202  * by the page daemon, or when the containing object, if any, is destroyed.
4203  */
4204 void
4205 vm_page_wire(vm_page_t m)
4206 {
4207 	u_int old;
4208 
4209 #ifdef INVARIANTS
4210 	if (m->object != NULL && !vm_page_busied(m) &&
4211 	    !vm_object_busied(m->object))
4212 		VM_OBJECT_ASSERT_LOCKED(m->object);
4213 #endif
4214 	KASSERT((m->flags & PG_FICTITIOUS) == 0 ||
4215 	    VPRC_WIRE_COUNT(m->ref_count) >= 1,
4216 	    ("vm_page_wire: fictitious page %p has zero wirings", m));
4217 
4218 	old = atomic_fetchadd_int(&m->ref_count, 1);
4219 	KASSERT(VPRC_WIRE_COUNT(old) != VPRC_WIRE_COUNT_MAX,
4220 	    ("vm_page_wire: counter overflow for page %p", m));
4221 	if (VPRC_WIRE_COUNT(old) == 0) {
4222 		if ((m->oflags & VPO_UNMANAGED) == 0)
4223 			vm_page_aflag_set(m, PGA_DEQUEUE);
4224 		vm_wire_add(1);
4225 	}
4226 }
4227 
4228 /*
4229  * Attempt to wire a mapped page following a pmap lookup of that page.
4230  * This may fail if a thread is concurrently tearing down mappings of the page.
4231  * The transient failure is acceptable because it translates to the
4232  * failure of the caller pmap_extract_and_hold(), which should be then
4233  * followed by the vm_fault() fallback, see e.g. vm_fault_quick_hold_pages().
4234  */
4235 bool
4236 vm_page_wire_mapped(vm_page_t m)
4237 {
4238 	u_int old;
4239 
4240 	old = m->ref_count;
4241 	do {
4242 		KASSERT(old > 0,
4243 		    ("vm_page_wire_mapped: wiring unreferenced page %p", m));
4244 		if ((old & VPRC_BLOCKED) != 0)
4245 			return (false);
4246 	} while (!atomic_fcmpset_int(&m->ref_count, &old, old + 1));
4247 
4248 	if (VPRC_WIRE_COUNT(old) == 0) {
4249 		if ((m->oflags & VPO_UNMANAGED) == 0)
4250 			vm_page_aflag_set(m, PGA_DEQUEUE);
4251 		vm_wire_add(1);
4252 	}
4253 	return (true);
4254 }
4255 
4256 /*
4257  * Release a wiring reference to a managed page.  If the page still belongs to
4258  * an object, update its position in the page queues to reflect the reference.
4259  * If the wiring was the last reference to the page, free the page.
4260  */
4261 static void
4262 vm_page_unwire_managed(vm_page_t m, uint8_t nqueue, bool noreuse)
4263 {
4264 	u_int old;
4265 
4266 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4267 	    ("%s: page %p is unmanaged", __func__, m));
4268 
4269 	/*
4270 	 * Update LRU state before releasing the wiring reference.
4271 	 * Use a release store when updating the reference count to
4272 	 * synchronize with vm_page_free_prep().
4273 	 */
4274 	old = m->ref_count;
4275 	do {
4276 		KASSERT(VPRC_WIRE_COUNT(old) > 0,
4277 		    ("vm_page_unwire: wire count underflow for page %p", m));
4278 
4279 		if (old > VPRC_OBJREF + 1) {
4280 			/*
4281 			 * The page has at least one other wiring reference.  An
4282 			 * earlier iteration of this loop may have called
4283 			 * vm_page_release_toq() and cleared PGA_DEQUEUE, so
4284 			 * re-set it if necessary.
4285 			 */
4286 			if ((vm_page_astate_load(m).flags & PGA_DEQUEUE) == 0)
4287 				vm_page_aflag_set(m, PGA_DEQUEUE);
4288 		} else if (old == VPRC_OBJREF + 1) {
4289 			/*
4290 			 * This is the last wiring.  Clear PGA_DEQUEUE and
4291 			 * update the page's queue state to reflect the
4292 			 * reference.  If the page does not belong to an object
4293 			 * (i.e., the VPRC_OBJREF bit is clear), we only need to
4294 			 * clear leftover queue state.
4295 			 */
4296 			vm_page_release_toq(m, nqueue, noreuse);
4297 		} else if (old == 1) {
4298 			vm_page_aflag_clear(m, PGA_DEQUEUE);
4299 		}
4300 	} while (!atomic_fcmpset_rel_int(&m->ref_count, &old, old - 1));
4301 
4302 	if (VPRC_WIRE_COUNT(old) == 1) {
4303 		vm_wire_sub(1);
4304 		if (old == 1)
4305 			vm_page_free(m);
4306 	}
4307 }
4308 
4309 /*
4310  * Release one wiring of the specified page, potentially allowing it to be
4311  * paged out.
4312  *
4313  * Only managed pages belonging to an object can be paged out.  If the number
4314  * of wirings transitions to zero and the page is eligible for page out, then
4315  * the page is added to the specified paging queue.  If the released wiring
4316  * represented the last reference to the page, the page is freed.
4317  */
4318 void
4319 vm_page_unwire(vm_page_t m, uint8_t nqueue)
4320 {
4321 
4322 	KASSERT(nqueue < PQ_COUNT,
4323 	    ("vm_page_unwire: invalid queue %u request for page %p",
4324 	    nqueue, m));
4325 
4326 	if ((m->oflags & VPO_UNMANAGED) != 0) {
4327 		if (vm_page_unwire_noq(m) && m->ref_count == 0)
4328 			vm_page_free(m);
4329 		return;
4330 	}
4331 	vm_page_unwire_managed(m, nqueue, false);
4332 }
4333 
4334 /*
4335  * Unwire a page without (re-)inserting it into a page queue.  It is up
4336  * to the caller to enqueue, requeue, or free the page as appropriate.
4337  * In most cases involving managed pages, vm_page_unwire() should be used
4338  * instead.
4339  */
4340 bool
4341 vm_page_unwire_noq(vm_page_t m)
4342 {
4343 	u_int old;
4344 
4345 	old = vm_page_drop(m, 1);
4346 	KASSERT(VPRC_WIRE_COUNT(old) != 0,
4347 	    ("%s: counter underflow for page %p", __func__,  m));
4348 	KASSERT((m->flags & PG_FICTITIOUS) == 0 || VPRC_WIRE_COUNT(old) > 1,
4349 	    ("%s: missing ref on fictitious page %p", __func__, m));
4350 
4351 	if (VPRC_WIRE_COUNT(old) > 1)
4352 		return (false);
4353 	if ((m->oflags & VPO_UNMANAGED) == 0)
4354 		vm_page_aflag_clear(m, PGA_DEQUEUE);
4355 	vm_wire_sub(1);
4356 	return (true);
4357 }
4358 
4359 /*
4360  * Ensure that the page ends up in the specified page queue.  If the page is
4361  * active or being moved to the active queue, ensure that its act_count is
4362  * at least ACT_INIT but do not otherwise mess with it.
4363  */
4364 static __always_inline void
4365 vm_page_mvqueue(vm_page_t m, const uint8_t nqueue, const uint16_t nflag)
4366 {
4367 	vm_page_astate_t old, new;
4368 
4369 	KASSERT(m->ref_count > 0,
4370 	    ("%s: page %p does not carry any references", __func__, m));
4371 	KASSERT(nflag == PGA_REQUEUE || nflag == PGA_REQUEUE_HEAD,
4372 	    ("%s: invalid flags %x", __func__, nflag));
4373 
4374 	if ((m->oflags & VPO_UNMANAGED) != 0 || vm_page_wired(m))
4375 		return;
4376 
4377 	old = vm_page_astate_load(m);
4378 	do {
4379 		if ((old.flags & PGA_DEQUEUE) != 0)
4380 			break;
4381 		new = old;
4382 		new.flags &= ~PGA_QUEUE_OP_MASK;
4383 		if (nqueue == PQ_ACTIVE)
4384 			new.act_count = max(old.act_count, ACT_INIT);
4385 		if (old.queue == nqueue) {
4386 			/*
4387 			 * There is no need to requeue pages already in the
4388 			 * active queue.
4389 			 */
4390 			if (nqueue != PQ_ACTIVE ||
4391 			    (old.flags & PGA_ENQUEUED) == 0)
4392 				new.flags |= nflag;
4393 		} else {
4394 			new.flags |= nflag;
4395 			new.queue = nqueue;
4396 		}
4397 	} while (!vm_page_pqstate_commit(m, &old, new));
4398 }
4399 
4400 /*
4401  * Put the specified page on the active list (if appropriate).
4402  */
4403 void
4404 vm_page_activate(vm_page_t m)
4405 {
4406 
4407 	vm_page_mvqueue(m, PQ_ACTIVE, PGA_REQUEUE);
4408 }
4409 
4410 /*
4411  * Move the specified page to the tail of the inactive queue, or requeue
4412  * the page if it is already in the inactive queue.
4413  */
4414 void
4415 vm_page_deactivate(vm_page_t m)
4416 {
4417 
4418 	vm_page_mvqueue(m, PQ_INACTIVE, PGA_REQUEUE);
4419 }
4420 
4421 void
4422 vm_page_deactivate_noreuse(vm_page_t m)
4423 {
4424 
4425 	vm_page_mvqueue(m, PQ_INACTIVE, PGA_REQUEUE_HEAD);
4426 }
4427 
4428 /*
4429  * Put a page in the laundry, or requeue it if it is already there.
4430  */
4431 void
4432 vm_page_launder(vm_page_t m)
4433 {
4434 
4435 	vm_page_mvqueue(m, PQ_LAUNDRY, PGA_REQUEUE);
4436 }
4437 
4438 /*
4439  * Put a page in the PQ_UNSWAPPABLE holding queue.
4440  */
4441 void
4442 vm_page_unswappable(vm_page_t m)
4443 {
4444 
4445 	VM_OBJECT_ASSERT_LOCKED(m->object);
4446 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4447 	    ("page %p already unswappable", m));
4448 
4449 	vm_page_dequeue(m);
4450 	vm_page_enqueue(m, PQ_UNSWAPPABLE);
4451 }
4452 
4453 /*
4454  * Release a page back to the page queues in preparation for unwiring.
4455  */
4456 static void
4457 vm_page_release_toq(vm_page_t m, uint8_t nqueue, const bool noreuse)
4458 {
4459 	vm_page_astate_t old, new;
4460 	uint16_t nflag;
4461 
4462 	/*
4463 	 * Use a check of the valid bits to determine whether we should
4464 	 * accelerate reclamation of the page.  The object lock might not be
4465 	 * held here, in which case the check is racy.  At worst we will either
4466 	 * accelerate reclamation of a valid page and violate LRU, or
4467 	 * unnecessarily defer reclamation of an invalid page.
4468 	 *
4469 	 * If we were asked to not cache the page, place it near the head of the
4470 	 * inactive queue so that is reclaimed sooner.
4471 	 */
4472 	if (noreuse || vm_page_none_valid(m)) {
4473 		nqueue = PQ_INACTIVE;
4474 		nflag = PGA_REQUEUE_HEAD;
4475 	} else {
4476 		nflag = PGA_REQUEUE;
4477 	}
4478 
4479 	old = vm_page_astate_load(m);
4480 	do {
4481 		new = old;
4482 
4483 		/*
4484 		 * If the page is already in the active queue and we are not
4485 		 * trying to accelerate reclamation, simply mark it as
4486 		 * referenced and avoid any queue operations.
4487 		 */
4488 		new.flags &= ~PGA_QUEUE_OP_MASK;
4489 		if (nflag != PGA_REQUEUE_HEAD && old.queue == PQ_ACTIVE &&
4490 		    (old.flags & PGA_ENQUEUED) != 0)
4491 			new.flags |= PGA_REFERENCED;
4492 		else {
4493 			new.flags |= nflag;
4494 			new.queue = nqueue;
4495 		}
4496 	} while (!vm_page_pqstate_commit(m, &old, new));
4497 }
4498 
4499 /*
4500  * Unwire a page and either attempt to free it or re-add it to the page queues.
4501  */
4502 void
4503 vm_page_release(vm_page_t m, int flags)
4504 {
4505 	vm_object_t object;
4506 
4507 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4508 	    ("vm_page_release: page %p is unmanaged", m));
4509 
4510 	if ((flags & VPR_TRYFREE) != 0) {
4511 		for (;;) {
4512 			object = atomic_load_ptr(&m->object);
4513 			if (object == NULL)
4514 				break;
4515 			/* Depends on type-stability. */
4516 			if (vm_page_busied(m) || !VM_OBJECT_TRYWLOCK(object))
4517 				break;
4518 			if (object == m->object) {
4519 				vm_page_release_locked(m, flags);
4520 				VM_OBJECT_WUNLOCK(object);
4521 				return;
4522 			}
4523 			VM_OBJECT_WUNLOCK(object);
4524 		}
4525 	}
4526 	vm_page_unwire_managed(m, PQ_INACTIVE, flags != 0);
4527 }
4528 
4529 /* See vm_page_release(). */
4530 void
4531 vm_page_release_locked(vm_page_t m, int flags)
4532 {
4533 
4534 	VM_OBJECT_ASSERT_WLOCKED(m->object);
4535 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4536 	    ("vm_page_release_locked: page %p is unmanaged", m));
4537 
4538 	if (vm_page_unwire_noq(m)) {
4539 		if ((flags & VPR_TRYFREE) != 0 &&
4540 		    (m->object->ref_count == 0 || !pmap_page_is_mapped(m)) &&
4541 		    m->dirty == 0 && vm_page_tryxbusy(m)) {
4542 			/*
4543 			 * An unlocked lookup may have wired the page before the
4544 			 * busy lock was acquired, in which case the page must
4545 			 * not be freed.
4546 			 */
4547 			if (__predict_true(!vm_page_wired(m))) {
4548 				vm_page_free(m);
4549 				return;
4550 			}
4551 			vm_page_xunbusy(m);
4552 		} else {
4553 			vm_page_release_toq(m, PQ_INACTIVE, flags != 0);
4554 		}
4555 	}
4556 }
4557 
4558 static bool
4559 vm_page_try_blocked_op(vm_page_t m, void (*op)(vm_page_t))
4560 {
4561 	u_int old;
4562 
4563 	KASSERT(m->object != NULL && (m->oflags & VPO_UNMANAGED) == 0,
4564 	    ("vm_page_try_blocked_op: page %p has no object", m));
4565 	KASSERT(vm_page_busied(m),
4566 	    ("vm_page_try_blocked_op: page %p is not busy", m));
4567 	VM_OBJECT_ASSERT_LOCKED(m->object);
4568 
4569 	old = m->ref_count;
4570 	do {
4571 		KASSERT(old != 0,
4572 		    ("vm_page_try_blocked_op: page %p has no references", m));
4573 		if (VPRC_WIRE_COUNT(old) != 0)
4574 			return (false);
4575 	} while (!atomic_fcmpset_int(&m->ref_count, &old, old | VPRC_BLOCKED));
4576 
4577 	(op)(m);
4578 
4579 	/*
4580 	 * If the object is read-locked, new wirings may be created via an
4581 	 * object lookup.
4582 	 */
4583 	old = vm_page_drop(m, VPRC_BLOCKED);
4584 	KASSERT(!VM_OBJECT_WOWNED(m->object) ||
4585 	    old == (VPRC_BLOCKED | VPRC_OBJREF),
4586 	    ("vm_page_try_blocked_op: unexpected refcount value %u for %p",
4587 	    old, m));
4588 	return (true);
4589 }
4590 
4591 /*
4592  * Atomically check for wirings and remove all mappings of the page.
4593  */
4594 bool
4595 vm_page_try_remove_all(vm_page_t m)
4596 {
4597 
4598 	return (vm_page_try_blocked_op(m, pmap_remove_all));
4599 }
4600 
4601 /*
4602  * Atomically check for wirings and remove all writeable mappings of the page.
4603  */
4604 bool
4605 vm_page_try_remove_write(vm_page_t m)
4606 {
4607 
4608 	return (vm_page_try_blocked_op(m, pmap_remove_write));
4609 }
4610 
4611 /*
4612  * vm_page_advise
4613  *
4614  * 	Apply the specified advice to the given page.
4615  */
4616 void
4617 vm_page_advise(vm_page_t m, int advice)
4618 {
4619 
4620 	VM_OBJECT_ASSERT_WLOCKED(m->object);
4621 	vm_page_assert_xbusied(m);
4622 
4623 	if (advice == MADV_FREE)
4624 		/*
4625 		 * Mark the page clean.  This will allow the page to be freed
4626 		 * without first paging it out.  MADV_FREE pages are often
4627 		 * quickly reused by malloc(3), so we do not do anything that
4628 		 * would result in a page fault on a later access.
4629 		 */
4630 		vm_page_undirty(m);
4631 	else if (advice != MADV_DONTNEED) {
4632 		if (advice == MADV_WILLNEED)
4633 			vm_page_activate(m);
4634 		return;
4635 	}
4636 
4637 	if (advice != MADV_FREE && m->dirty == 0 && pmap_is_modified(m))
4638 		vm_page_dirty(m);
4639 
4640 	/*
4641 	 * Clear any references to the page.  Otherwise, the page daemon will
4642 	 * immediately reactivate the page.
4643 	 */
4644 	vm_page_aflag_clear(m, PGA_REFERENCED);
4645 
4646 	/*
4647 	 * Place clean pages near the head of the inactive queue rather than
4648 	 * the tail, thus defeating the queue's LRU operation and ensuring that
4649 	 * the page will be reused quickly.  Dirty pages not already in the
4650 	 * laundry are moved there.
4651 	 */
4652 	if (m->dirty == 0)
4653 		vm_page_deactivate_noreuse(m);
4654 	else if (!vm_page_in_laundry(m))
4655 		vm_page_launder(m);
4656 }
4657 
4658 /*
4659  *	vm_page_grab_release
4660  *
4661  *	Helper routine for grab functions to release busy on return.
4662  */
4663 static inline void
4664 vm_page_grab_release(vm_page_t m, int allocflags)
4665 {
4666 
4667 	if ((allocflags & VM_ALLOC_NOBUSY) != 0) {
4668 		if ((allocflags & VM_ALLOC_IGN_SBUSY) != 0)
4669 			vm_page_sunbusy(m);
4670 		else
4671 			vm_page_xunbusy(m);
4672 	}
4673 }
4674 
4675 /*
4676  *	vm_page_grab_sleep
4677  *
4678  *	Sleep for busy according to VM_ALLOC_ parameters.  Returns true
4679  *	if the caller should retry and false otherwise.
4680  *
4681  *	If the object is locked on entry the object will be unlocked with
4682  *	false returns and still locked but possibly having been dropped
4683  *	with true returns.
4684  */
4685 static bool
4686 vm_page_grab_sleep(vm_object_t object, vm_page_t m, vm_pindex_t pindex,
4687     const char *wmesg, int allocflags, bool locked)
4688 {
4689 
4690 	if ((allocflags & VM_ALLOC_NOWAIT) != 0)
4691 		return (false);
4692 
4693 	/*
4694 	 * Reference the page before unlocking and sleeping so that
4695 	 * the page daemon is less likely to reclaim it.
4696 	 */
4697 	if (locked && (allocflags & VM_ALLOC_NOCREAT) == 0)
4698 		vm_page_reference(m);
4699 
4700 	if (_vm_page_busy_sleep(object, m, pindex, wmesg, allocflags, locked) &&
4701 	    locked)
4702 		VM_OBJECT_WLOCK(object);
4703 	if ((allocflags & VM_ALLOC_WAITFAIL) != 0)
4704 		return (false);
4705 
4706 	return (true);
4707 }
4708 
4709 /*
4710  * Assert that the grab flags are valid.
4711  */
4712 static inline void
4713 vm_page_grab_check(int allocflags)
4714 {
4715 
4716 	KASSERT((allocflags & VM_ALLOC_NOBUSY) == 0 ||
4717 	    (allocflags & VM_ALLOC_WIRED) != 0,
4718 	    ("vm_page_grab*: the pages must be busied or wired"));
4719 
4720 	KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
4721 	    (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
4722 	    ("vm_page_grab*: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch"));
4723 }
4724 
4725 /*
4726  * Calculate the page allocation flags for grab.
4727  */
4728 static inline int
4729 vm_page_grab_pflags(int allocflags)
4730 {
4731 	int pflags;
4732 
4733 	pflags = allocflags &
4734 	    ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL |
4735 	    VM_ALLOC_NOBUSY | VM_ALLOC_IGN_SBUSY);
4736 	if ((allocflags & VM_ALLOC_NOWAIT) == 0)
4737 		pflags |= VM_ALLOC_WAITFAIL;
4738 	if ((allocflags & VM_ALLOC_IGN_SBUSY) != 0)
4739 		pflags |= VM_ALLOC_SBUSY;
4740 
4741 	return (pflags);
4742 }
4743 
4744 /*
4745  * Grab a page, waiting until we are waken up due to the page
4746  * changing state.  We keep on waiting, if the page continues
4747  * to be in the object.  If the page doesn't exist, first allocate it
4748  * and then conditionally zero it.
4749  *
4750  * This routine may sleep.
4751  *
4752  * The object must be locked on entry.  The lock will, however, be released
4753  * and reacquired if the routine sleeps.
4754  */
4755 vm_page_t
4756 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
4757 {
4758 	vm_page_t m;
4759 
4760 	VM_OBJECT_ASSERT_WLOCKED(object);
4761 	vm_page_grab_check(allocflags);
4762 
4763 retrylookup:
4764 	if ((m = vm_page_lookup(object, pindex)) != NULL) {
4765 		if (!vm_page_tryacquire(m, allocflags)) {
4766 			if (vm_page_grab_sleep(object, m, pindex, "pgrbwt",
4767 			    allocflags, true))
4768 				goto retrylookup;
4769 			return (NULL);
4770 		}
4771 		goto out;
4772 	}
4773 	if ((allocflags & VM_ALLOC_NOCREAT) != 0)
4774 		return (NULL);
4775 	m = vm_page_alloc(object, pindex, vm_page_grab_pflags(allocflags));
4776 	if (m == NULL) {
4777 		if ((allocflags & (VM_ALLOC_NOWAIT | VM_ALLOC_WAITFAIL)) != 0)
4778 			return (NULL);
4779 		goto retrylookup;
4780 	}
4781 	if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
4782 		pmap_zero_page(m);
4783 
4784 out:
4785 	vm_page_grab_release(m, allocflags);
4786 
4787 	return (m);
4788 }
4789 
4790 /*
4791  * Locklessly attempt to acquire a page given a (object, pindex) tuple
4792  * and an optional previous page to avoid the radix lookup.  The resulting
4793  * page will be validated against the identity tuple and busied or wired
4794  * as requested.  A NULL *mp return guarantees that the page was not in
4795  * radix at the time of the call but callers must perform higher level
4796  * synchronization or retry the operation under a lock if they require
4797  * an atomic answer.  This is the only lock free validation routine,
4798  * other routines can depend on the resulting page state.
4799  *
4800  * The return value indicates whether the operation failed due to caller
4801  * flags.  The return is tri-state with mp:
4802  *
4803  * (true, *mp != NULL) - The operation was successful.
4804  * (true, *mp == NULL) - The page was not found in tree.
4805  * (false, *mp == NULL) - WAITFAIL or NOWAIT prevented acquisition.
4806  */
4807 static bool
4808 vm_page_acquire_unlocked(vm_object_t object, vm_pindex_t pindex,
4809     vm_page_t prev, vm_page_t *mp, int allocflags)
4810 {
4811 	vm_page_t m;
4812 
4813 	vm_page_grab_check(allocflags);
4814 	MPASS(prev == NULL || vm_page_busied(prev) || vm_page_wired(prev));
4815 
4816 	*mp = NULL;
4817 	for (;;) {
4818 		/*
4819 		 * We may see a false NULL here because the previous page
4820 		 * has been removed or just inserted and the list is loaded
4821 		 * without barriers.  Switch to radix to verify.
4822 		 */
4823 		if (prev == NULL || (m = TAILQ_NEXT(prev, listq)) == NULL ||
4824 		    QMD_IS_TRASHED(m) || m->pindex != pindex ||
4825 		    atomic_load_ptr(&m->object) != object) {
4826 			prev = NULL;
4827 			/*
4828 			 * This guarantees the result is instantaneously
4829 			 * correct.
4830 			 */
4831 			m = vm_radix_lookup_unlocked(&object->rtree, pindex);
4832 		}
4833 		if (m == NULL)
4834 			return (true);
4835 		if (vm_page_trybusy(m, allocflags)) {
4836 			if (m->object == object && m->pindex == pindex)
4837 				break;
4838 			/* relookup. */
4839 			vm_page_busy_release(m);
4840 			cpu_spinwait();
4841 			continue;
4842 		}
4843 		if (!vm_page_grab_sleep(object, m, pindex, "pgnslp",
4844 		    allocflags, false))
4845 			return (false);
4846 	}
4847 	if ((allocflags & VM_ALLOC_WIRED) != 0)
4848 		vm_page_wire(m);
4849 	vm_page_grab_release(m, allocflags);
4850 	*mp = m;
4851 	return (true);
4852 }
4853 
4854 /*
4855  * Try to locklessly grab a page and fall back to the object lock if NOCREAT
4856  * is not set.
4857  */
4858 vm_page_t
4859 vm_page_grab_unlocked(vm_object_t object, vm_pindex_t pindex, int allocflags)
4860 {
4861 	vm_page_t m;
4862 
4863 	vm_page_grab_check(allocflags);
4864 
4865 	if (!vm_page_acquire_unlocked(object, pindex, NULL, &m, allocflags))
4866 		return (NULL);
4867 	if (m != NULL)
4868 		return (m);
4869 
4870 	/*
4871 	 * The radix lockless lookup should never return a false negative
4872 	 * errors.  If the user specifies NOCREAT they are guaranteed there
4873 	 * was no page present at the instant of the call.  A NOCREAT caller
4874 	 * must handle create races gracefully.
4875 	 */
4876 	if ((allocflags & VM_ALLOC_NOCREAT) != 0)
4877 		return (NULL);
4878 
4879 	VM_OBJECT_WLOCK(object);
4880 	m = vm_page_grab(object, pindex, allocflags);
4881 	VM_OBJECT_WUNLOCK(object);
4882 
4883 	return (m);
4884 }
4885 
4886 /*
4887  * Grab a page and make it valid, paging in if necessary.  Pages missing from
4888  * their pager are zero filled and validated.  If a VM_ALLOC_COUNT is supplied
4889  * and the page is not valid as many as VM_INITIAL_PAGEIN pages can be brought
4890  * in simultaneously.  Additional pages will be left on a paging queue but
4891  * will neither be wired nor busy regardless of allocflags.
4892  */
4893 int
4894 vm_page_grab_valid(vm_page_t *mp, vm_object_t object, vm_pindex_t pindex, int allocflags)
4895 {
4896 	vm_page_t m;
4897 	vm_page_t ma[VM_INITIAL_PAGEIN];
4898 	int after, i, pflags, rv;
4899 
4900 	KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
4901 	    (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
4902 	    ("vm_page_grab_valid: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch"));
4903 	KASSERT((allocflags &
4904 	    (VM_ALLOC_NOWAIT | VM_ALLOC_WAITFAIL | VM_ALLOC_ZERO)) == 0,
4905 	    ("vm_page_grab_valid: Invalid flags 0x%X", allocflags));
4906 	VM_OBJECT_ASSERT_WLOCKED(object);
4907 	pflags = allocflags & ~(VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY |
4908 	    VM_ALLOC_WIRED | VM_ALLOC_IGN_SBUSY);
4909 	pflags |= VM_ALLOC_WAITFAIL;
4910 
4911 retrylookup:
4912 	if ((m = vm_page_lookup(object, pindex)) != NULL) {
4913 		/*
4914 		 * If the page is fully valid it can only become invalid
4915 		 * with the object lock held.  If it is not valid it can
4916 		 * become valid with the busy lock held.  Therefore, we
4917 		 * may unnecessarily lock the exclusive busy here if we
4918 		 * race with I/O completion not using the object lock.
4919 		 * However, we will not end up with an invalid page and a
4920 		 * shared lock.
4921 		 */
4922 		if (!vm_page_trybusy(m,
4923 		    vm_page_all_valid(m) ? allocflags : 0)) {
4924 			(void)vm_page_grab_sleep(object, m, pindex, "pgrbwt",
4925 			    allocflags, true);
4926 			goto retrylookup;
4927 		}
4928 		if (vm_page_all_valid(m))
4929 			goto out;
4930 		if ((allocflags & VM_ALLOC_NOCREAT) != 0) {
4931 			vm_page_busy_release(m);
4932 			*mp = NULL;
4933 			return (VM_PAGER_FAIL);
4934 		}
4935 	} else if ((allocflags & VM_ALLOC_NOCREAT) != 0) {
4936 		*mp = NULL;
4937 		return (VM_PAGER_FAIL);
4938 	} else if ((m = vm_page_alloc(object, pindex, pflags)) == NULL) {
4939 		if (!vm_pager_can_alloc_page(object, pindex)) {
4940 			*mp = NULL;
4941 			return (VM_PAGER_AGAIN);
4942 		}
4943 		goto retrylookup;
4944 	}
4945 
4946 	vm_page_assert_xbusied(m);
4947 	if (vm_pager_has_page(object, pindex, NULL, &after)) {
4948 		after = MIN(after, VM_INITIAL_PAGEIN);
4949 		after = MIN(after, allocflags >> VM_ALLOC_COUNT_SHIFT);
4950 		after = MAX(after, 1);
4951 		ma[0] = m;
4952 		for (i = 1; i < after; i++) {
4953 			if ((ma[i] = vm_page_next(ma[i - 1])) != NULL) {
4954 				if (vm_page_any_valid(ma[i]) ||
4955 				    !vm_page_tryxbusy(ma[i]))
4956 					break;
4957 			} else {
4958 				ma[i] = vm_page_alloc(object, m->pindex + i,
4959 				    VM_ALLOC_NORMAL);
4960 				if (ma[i] == NULL)
4961 					break;
4962 			}
4963 		}
4964 		after = i;
4965 		vm_object_pip_add(object, after);
4966 		VM_OBJECT_WUNLOCK(object);
4967 		rv = vm_pager_get_pages(object, ma, after, NULL, NULL);
4968 		VM_OBJECT_WLOCK(object);
4969 		vm_object_pip_wakeupn(object, after);
4970 		/* Pager may have replaced a page. */
4971 		m = ma[0];
4972 		if (rv != VM_PAGER_OK) {
4973 			for (i = 0; i < after; i++) {
4974 				if (!vm_page_wired(ma[i]))
4975 					vm_page_free(ma[i]);
4976 				else
4977 					vm_page_xunbusy(ma[i]);
4978 			}
4979 			*mp = NULL;
4980 			return (rv);
4981 		}
4982 		for (i = 1; i < after; i++)
4983 			vm_page_readahead_finish(ma[i]);
4984 		MPASS(vm_page_all_valid(m));
4985 	} else {
4986 		vm_page_zero_invalid(m, TRUE);
4987 	}
4988 out:
4989 	if ((allocflags & VM_ALLOC_WIRED) != 0)
4990 		vm_page_wire(m);
4991 	if ((allocflags & VM_ALLOC_SBUSY) != 0 && vm_page_xbusied(m))
4992 		vm_page_busy_downgrade(m);
4993 	else if ((allocflags & VM_ALLOC_NOBUSY) != 0)
4994 		vm_page_busy_release(m);
4995 	*mp = m;
4996 	return (VM_PAGER_OK);
4997 }
4998 
4999 /*
5000  * Locklessly grab a valid page.  If the page is not valid or not yet
5001  * allocated this will fall back to the object lock method.
5002  */
5003 int
5004 vm_page_grab_valid_unlocked(vm_page_t *mp, vm_object_t object,
5005     vm_pindex_t pindex, int allocflags)
5006 {
5007 	vm_page_t m;
5008 	int flags;
5009 	int error;
5010 
5011 	KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
5012 	    (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
5013 	    ("vm_page_grab_valid_unlocked: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY "
5014 	    "mismatch"));
5015 	KASSERT((allocflags &
5016 	    (VM_ALLOC_NOWAIT | VM_ALLOC_WAITFAIL | VM_ALLOC_ZERO)) == 0,
5017 	    ("vm_page_grab_valid_unlocked: Invalid flags 0x%X", allocflags));
5018 
5019 	/*
5020 	 * Attempt a lockless lookup and busy.  We need at least an sbusy
5021 	 * before we can inspect the valid field and return a wired page.
5022 	 */
5023 	flags = allocflags & ~(VM_ALLOC_NOBUSY | VM_ALLOC_WIRED);
5024 	if (!vm_page_acquire_unlocked(object, pindex, NULL, mp, flags))
5025 		return (VM_PAGER_FAIL);
5026 	if ((m = *mp) != NULL) {
5027 		if (vm_page_all_valid(m)) {
5028 			if ((allocflags & VM_ALLOC_WIRED) != 0)
5029 				vm_page_wire(m);
5030 			vm_page_grab_release(m, allocflags);
5031 			return (VM_PAGER_OK);
5032 		}
5033 		vm_page_busy_release(m);
5034 	}
5035 	if ((allocflags & VM_ALLOC_NOCREAT) != 0) {
5036 		*mp = NULL;
5037 		return (VM_PAGER_FAIL);
5038 	}
5039 	VM_OBJECT_WLOCK(object);
5040 	error = vm_page_grab_valid(mp, object, pindex, allocflags);
5041 	VM_OBJECT_WUNLOCK(object);
5042 
5043 	return (error);
5044 }
5045 
5046 /*
5047  * Return the specified range of pages from the given object.  For each
5048  * page offset within the range, if a page already exists within the object
5049  * at that offset and it is busy, then wait for it to change state.  If,
5050  * instead, the page doesn't exist, then allocate it.
5051  *
5052  * The caller must always specify an allocation class.
5053  *
5054  * allocation classes:
5055  *	VM_ALLOC_NORMAL		normal process request
5056  *	VM_ALLOC_SYSTEM		system *really* needs the pages
5057  *
5058  * The caller must always specify that the pages are to be busied and/or
5059  * wired.
5060  *
5061  * optional allocation flags:
5062  *	VM_ALLOC_IGN_SBUSY	do not sleep on soft busy pages
5063  *	VM_ALLOC_NOBUSY		do not exclusive busy the page
5064  *	VM_ALLOC_NOWAIT		do not sleep
5065  *	VM_ALLOC_SBUSY		set page to sbusy state
5066  *	VM_ALLOC_WIRED		wire the pages
5067  *	VM_ALLOC_ZERO		zero and validate any invalid pages
5068  *
5069  * If VM_ALLOC_NOWAIT is not specified, this routine may sleep.  Otherwise, it
5070  * may return a partial prefix of the requested range.
5071  */
5072 int
5073 vm_page_grab_pages(vm_object_t object, vm_pindex_t pindex, int allocflags,
5074     vm_page_t *ma, int count)
5075 {
5076 	vm_page_t m, mpred;
5077 	int pflags;
5078 	int i;
5079 
5080 	VM_OBJECT_ASSERT_WLOCKED(object);
5081 	KASSERT(((u_int)allocflags >> VM_ALLOC_COUNT_SHIFT) == 0,
5082 	    ("vm_page_grap_pages: VM_ALLOC_COUNT() is not allowed"));
5083 	KASSERT(count > 0,
5084 	    ("vm_page_grab_pages: invalid page count %d", count));
5085 	vm_page_grab_check(allocflags);
5086 
5087 	pflags = vm_page_grab_pflags(allocflags);
5088 	i = 0;
5089 retrylookup:
5090 	m = vm_radix_lookup_le(&object->rtree, pindex + i);
5091 	if (m == NULL || m->pindex != pindex + i) {
5092 		mpred = m;
5093 		m = NULL;
5094 	} else
5095 		mpred = TAILQ_PREV(m, pglist, listq);
5096 	for (; i < count; i++) {
5097 		if (m != NULL) {
5098 			if (!vm_page_tryacquire(m, allocflags)) {
5099 				if (vm_page_grab_sleep(object, m, pindex + i,
5100 				    "grbmaw", allocflags, true))
5101 					goto retrylookup;
5102 				break;
5103 			}
5104 		} else {
5105 			if ((allocflags & VM_ALLOC_NOCREAT) != 0)
5106 				break;
5107 			m = vm_page_alloc_after(object, pindex + i,
5108 			    pflags | VM_ALLOC_COUNT(count - i), mpred);
5109 			if (m == NULL) {
5110 				if ((allocflags & (VM_ALLOC_NOWAIT |
5111 				    VM_ALLOC_WAITFAIL)) != 0)
5112 					break;
5113 				goto retrylookup;
5114 			}
5115 		}
5116 		if (vm_page_none_valid(m) &&
5117 		    (allocflags & VM_ALLOC_ZERO) != 0) {
5118 			if ((m->flags & PG_ZERO) == 0)
5119 				pmap_zero_page(m);
5120 			vm_page_valid(m);
5121 		}
5122 		vm_page_grab_release(m, allocflags);
5123 		ma[i] = mpred = m;
5124 		m = vm_page_next(m);
5125 	}
5126 	return (i);
5127 }
5128 
5129 /*
5130  * Unlocked variant of vm_page_grab_pages().  This accepts the same flags
5131  * and will fall back to the locked variant to handle allocation.
5132  */
5133 int
5134 vm_page_grab_pages_unlocked(vm_object_t object, vm_pindex_t pindex,
5135     int allocflags, vm_page_t *ma, int count)
5136 {
5137 	vm_page_t m, pred;
5138 	int flags;
5139 	int i;
5140 
5141 	KASSERT(count > 0,
5142 	    ("vm_page_grab_pages_unlocked: invalid page count %d", count));
5143 	vm_page_grab_check(allocflags);
5144 
5145 	/*
5146 	 * Modify flags for lockless acquire to hold the page until we
5147 	 * set it valid if necessary.
5148 	 */
5149 	flags = allocflags & ~VM_ALLOC_NOBUSY;
5150 	pred = NULL;
5151 	for (i = 0; i < count; i++, pindex++) {
5152 		if (!vm_page_acquire_unlocked(object, pindex, pred, &m, flags))
5153 			return (i);
5154 		if (m == NULL)
5155 			break;
5156 		if ((flags & VM_ALLOC_ZERO) != 0 && vm_page_none_valid(m)) {
5157 			if ((m->flags & PG_ZERO) == 0)
5158 				pmap_zero_page(m);
5159 			vm_page_valid(m);
5160 		}
5161 		/* m will still be wired or busy according to flags. */
5162 		vm_page_grab_release(m, allocflags);
5163 		pred = ma[i] = m;
5164 	}
5165 	if (i == count || (allocflags & VM_ALLOC_NOCREAT) != 0)
5166 		return (i);
5167 	count -= i;
5168 	VM_OBJECT_WLOCK(object);
5169 	i += vm_page_grab_pages(object, pindex, allocflags, &ma[i], count);
5170 	VM_OBJECT_WUNLOCK(object);
5171 
5172 	return (i);
5173 }
5174 
5175 /*
5176  * Mapping function for valid or dirty bits in a page.
5177  *
5178  * Inputs are required to range within a page.
5179  */
5180 vm_page_bits_t
5181 vm_page_bits(int base, int size)
5182 {
5183 	int first_bit;
5184 	int last_bit;
5185 
5186 	KASSERT(
5187 	    base + size <= PAGE_SIZE,
5188 	    ("vm_page_bits: illegal base/size %d/%d", base, size)
5189 	);
5190 
5191 	if (size == 0)		/* handle degenerate case */
5192 		return (0);
5193 
5194 	first_bit = base >> DEV_BSHIFT;
5195 	last_bit = (base + size - 1) >> DEV_BSHIFT;
5196 
5197 	return (((vm_page_bits_t)2 << last_bit) -
5198 	    ((vm_page_bits_t)1 << first_bit));
5199 }
5200 
5201 void
5202 vm_page_bits_set(vm_page_t m, vm_page_bits_t *bits, vm_page_bits_t set)
5203 {
5204 
5205 #if PAGE_SIZE == 32768
5206 	atomic_set_64((uint64_t *)bits, set);
5207 #elif PAGE_SIZE == 16384
5208 	atomic_set_32((uint32_t *)bits, set);
5209 #elif (PAGE_SIZE == 8192) && defined(atomic_set_16)
5210 	atomic_set_16((uint16_t *)bits, set);
5211 #elif (PAGE_SIZE == 4096) && defined(atomic_set_8)
5212 	atomic_set_8((uint8_t *)bits, set);
5213 #else		/* PAGE_SIZE <= 8192 */
5214 	uintptr_t addr;
5215 	int shift;
5216 
5217 	addr = (uintptr_t)bits;
5218 	/*
5219 	 * Use a trick to perform a 32-bit atomic on the
5220 	 * containing aligned word, to not depend on the existence
5221 	 * of atomic_{set, clear}_{8, 16}.
5222 	 */
5223 	shift = addr & (sizeof(uint32_t) - 1);
5224 #if BYTE_ORDER == BIG_ENDIAN
5225 	shift = (sizeof(uint32_t) - sizeof(vm_page_bits_t) - shift) * NBBY;
5226 #else
5227 	shift *= NBBY;
5228 #endif
5229 	addr &= ~(sizeof(uint32_t) - 1);
5230 	atomic_set_32((uint32_t *)addr, set << shift);
5231 #endif		/* PAGE_SIZE */
5232 }
5233 
5234 static inline void
5235 vm_page_bits_clear(vm_page_t m, vm_page_bits_t *bits, vm_page_bits_t clear)
5236 {
5237 
5238 #if PAGE_SIZE == 32768
5239 	atomic_clear_64((uint64_t *)bits, clear);
5240 #elif PAGE_SIZE == 16384
5241 	atomic_clear_32((uint32_t *)bits, clear);
5242 #elif (PAGE_SIZE == 8192) && defined(atomic_clear_16)
5243 	atomic_clear_16((uint16_t *)bits, clear);
5244 #elif (PAGE_SIZE == 4096) && defined(atomic_clear_8)
5245 	atomic_clear_8((uint8_t *)bits, clear);
5246 #else		/* PAGE_SIZE <= 8192 */
5247 	uintptr_t addr;
5248 	int shift;
5249 
5250 	addr = (uintptr_t)bits;
5251 	/*
5252 	 * Use a trick to perform a 32-bit atomic on the
5253 	 * containing aligned word, to not depend on the existence
5254 	 * of atomic_{set, clear}_{8, 16}.
5255 	 */
5256 	shift = addr & (sizeof(uint32_t) - 1);
5257 #if BYTE_ORDER == BIG_ENDIAN
5258 	shift = (sizeof(uint32_t) - sizeof(vm_page_bits_t) - shift) * NBBY;
5259 #else
5260 	shift *= NBBY;
5261 #endif
5262 	addr &= ~(sizeof(uint32_t) - 1);
5263 	atomic_clear_32((uint32_t *)addr, clear << shift);
5264 #endif		/* PAGE_SIZE */
5265 }
5266 
5267 static inline vm_page_bits_t
5268 vm_page_bits_swap(vm_page_t m, vm_page_bits_t *bits, vm_page_bits_t newbits)
5269 {
5270 #if PAGE_SIZE == 32768
5271 	uint64_t old;
5272 
5273 	old = *bits;
5274 	while (atomic_fcmpset_64(bits, &old, newbits) == 0);
5275 	return (old);
5276 #elif PAGE_SIZE == 16384
5277 	uint32_t old;
5278 
5279 	old = *bits;
5280 	while (atomic_fcmpset_32(bits, &old, newbits) == 0);
5281 	return (old);
5282 #elif (PAGE_SIZE == 8192) && defined(atomic_fcmpset_16)
5283 	uint16_t old;
5284 
5285 	old = *bits;
5286 	while (atomic_fcmpset_16(bits, &old, newbits) == 0);
5287 	return (old);
5288 #elif (PAGE_SIZE == 4096) && defined(atomic_fcmpset_8)
5289 	uint8_t old;
5290 
5291 	old = *bits;
5292 	while (atomic_fcmpset_8(bits, &old, newbits) == 0);
5293 	return (old);
5294 #else		/* PAGE_SIZE <= 4096*/
5295 	uintptr_t addr;
5296 	uint32_t old, new, mask;
5297 	int shift;
5298 
5299 	addr = (uintptr_t)bits;
5300 	/*
5301 	 * Use a trick to perform a 32-bit atomic on the
5302 	 * containing aligned word, to not depend on the existence
5303 	 * of atomic_{set, swap, clear}_{8, 16}.
5304 	 */
5305 	shift = addr & (sizeof(uint32_t) - 1);
5306 #if BYTE_ORDER == BIG_ENDIAN
5307 	shift = (sizeof(uint32_t) - sizeof(vm_page_bits_t) - shift) * NBBY;
5308 #else
5309 	shift *= NBBY;
5310 #endif
5311 	addr &= ~(sizeof(uint32_t) - 1);
5312 	mask = VM_PAGE_BITS_ALL << shift;
5313 
5314 	old = *bits;
5315 	do {
5316 		new = old & ~mask;
5317 		new |= newbits << shift;
5318 	} while (atomic_fcmpset_32((uint32_t *)addr, &old, new) == 0);
5319 	return (old >> shift);
5320 #endif		/* PAGE_SIZE */
5321 }
5322 
5323 /*
5324  *	vm_page_set_valid_range:
5325  *
5326  *	Sets portions of a page valid.  The arguments are expected
5327  *	to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
5328  *	of any partial chunks touched by the range.  The invalid portion of
5329  *	such chunks will be zeroed.
5330  *
5331  *	(base + size) must be less then or equal to PAGE_SIZE.
5332  */
5333 void
5334 vm_page_set_valid_range(vm_page_t m, int base, int size)
5335 {
5336 	int endoff, frag;
5337 	vm_page_bits_t pagebits;
5338 
5339 	vm_page_assert_busied(m);
5340 	if (size == 0)	/* handle degenerate case */
5341 		return;
5342 
5343 	/*
5344 	 * If the base is not DEV_BSIZE aligned and the valid
5345 	 * bit is clear, we have to zero out a portion of the
5346 	 * first block.
5347 	 */
5348 	if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
5349 	    (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
5350 		pmap_zero_page_area(m, frag, base - frag);
5351 
5352 	/*
5353 	 * If the ending offset is not DEV_BSIZE aligned and the
5354 	 * valid bit is clear, we have to zero out a portion of
5355 	 * the last block.
5356 	 */
5357 	endoff = base + size;
5358 	if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
5359 	    (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
5360 		pmap_zero_page_area(m, endoff,
5361 		    DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
5362 
5363 	/*
5364 	 * Assert that no previously invalid block that is now being validated
5365 	 * is already dirty.
5366 	 */
5367 	KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0,
5368 	    ("vm_page_set_valid_range: page %p is dirty", m));
5369 
5370 	/*
5371 	 * Set valid bits inclusive of any overlap.
5372 	 */
5373 	pagebits = vm_page_bits(base, size);
5374 	if (vm_page_xbusied(m))
5375 		m->valid |= pagebits;
5376 	else
5377 		vm_page_bits_set(m, &m->valid, pagebits);
5378 }
5379 
5380 /*
5381  * Set the page dirty bits and free the invalid swap space if
5382  * present.  Returns the previous dirty bits.
5383  */
5384 vm_page_bits_t
5385 vm_page_set_dirty(vm_page_t m)
5386 {
5387 	vm_page_bits_t old;
5388 
5389 	VM_PAGE_OBJECT_BUSY_ASSERT(m);
5390 
5391 	if (vm_page_xbusied(m) && !pmap_page_is_write_mapped(m)) {
5392 		old = m->dirty;
5393 		m->dirty = VM_PAGE_BITS_ALL;
5394 	} else
5395 		old = vm_page_bits_swap(m, &m->dirty, VM_PAGE_BITS_ALL);
5396 	if (old == 0 && (m->a.flags & PGA_SWAP_SPACE) != 0)
5397 		vm_pager_page_unswapped(m);
5398 
5399 	return (old);
5400 }
5401 
5402 /*
5403  * Clear the given bits from the specified page's dirty field.
5404  */
5405 static __inline void
5406 vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits)
5407 {
5408 
5409 	vm_page_assert_busied(m);
5410 
5411 	/*
5412 	 * If the page is xbusied and not write mapped we are the
5413 	 * only thread that can modify dirty bits.  Otherwise, The pmap
5414 	 * layer can call vm_page_dirty() without holding a distinguished
5415 	 * lock.  The combination of page busy and atomic operations
5416 	 * suffice to guarantee consistency of the page dirty field.
5417 	 */
5418 	if (vm_page_xbusied(m) && !pmap_page_is_write_mapped(m))
5419 		m->dirty &= ~pagebits;
5420 	else
5421 		vm_page_bits_clear(m, &m->dirty, pagebits);
5422 }
5423 
5424 /*
5425  *	vm_page_set_validclean:
5426  *
5427  *	Sets portions of a page valid and clean.  The arguments are expected
5428  *	to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
5429  *	of any partial chunks touched by the range.  The invalid portion of
5430  *	such chunks will be zero'd.
5431  *
5432  *	(base + size) must be less then or equal to PAGE_SIZE.
5433  */
5434 void
5435 vm_page_set_validclean(vm_page_t m, int base, int size)
5436 {
5437 	vm_page_bits_t oldvalid, pagebits;
5438 	int endoff, frag;
5439 
5440 	vm_page_assert_busied(m);
5441 	if (size == 0)	/* handle degenerate case */
5442 		return;
5443 
5444 	/*
5445 	 * If the base is not DEV_BSIZE aligned and the valid
5446 	 * bit is clear, we have to zero out a portion of the
5447 	 * first block.
5448 	 */
5449 	if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
5450 	    (m->valid & ((vm_page_bits_t)1 << (base >> DEV_BSHIFT))) == 0)
5451 		pmap_zero_page_area(m, frag, base - frag);
5452 
5453 	/*
5454 	 * If the ending offset is not DEV_BSIZE aligned and the
5455 	 * valid bit is clear, we have to zero out a portion of
5456 	 * the last block.
5457 	 */
5458 	endoff = base + size;
5459 	if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
5460 	    (m->valid & ((vm_page_bits_t)1 << (endoff >> DEV_BSHIFT))) == 0)
5461 		pmap_zero_page_area(m, endoff,
5462 		    DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
5463 
5464 	/*
5465 	 * Set valid, clear dirty bits.  If validating the entire
5466 	 * page we can safely clear the pmap modify bit.  We also
5467 	 * use this opportunity to clear the PGA_NOSYNC flag.  If a process
5468 	 * takes a write fault on a MAP_NOSYNC memory area the flag will
5469 	 * be set again.
5470 	 *
5471 	 * We set valid bits inclusive of any overlap, but we can only
5472 	 * clear dirty bits for DEV_BSIZE chunks that are fully within
5473 	 * the range.
5474 	 */
5475 	oldvalid = m->valid;
5476 	pagebits = vm_page_bits(base, size);
5477 	if (vm_page_xbusied(m))
5478 		m->valid |= pagebits;
5479 	else
5480 		vm_page_bits_set(m, &m->valid, pagebits);
5481 #if 0	/* NOT YET */
5482 	if ((frag = base & (DEV_BSIZE - 1)) != 0) {
5483 		frag = DEV_BSIZE - frag;
5484 		base += frag;
5485 		size -= frag;
5486 		if (size < 0)
5487 			size = 0;
5488 	}
5489 	pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
5490 #endif
5491 	if (base == 0 && size == PAGE_SIZE) {
5492 		/*
5493 		 * The page can only be modified within the pmap if it is
5494 		 * mapped, and it can only be mapped if it was previously
5495 		 * fully valid.
5496 		 */
5497 		if (oldvalid == VM_PAGE_BITS_ALL)
5498 			/*
5499 			 * Perform the pmap_clear_modify() first.  Otherwise,
5500 			 * a concurrent pmap operation, such as
5501 			 * pmap_protect(), could clear a modification in the
5502 			 * pmap and set the dirty field on the page before
5503 			 * pmap_clear_modify() had begun and after the dirty
5504 			 * field was cleared here.
5505 			 */
5506 			pmap_clear_modify(m);
5507 		m->dirty = 0;
5508 		vm_page_aflag_clear(m, PGA_NOSYNC);
5509 	} else if (oldvalid != VM_PAGE_BITS_ALL && vm_page_xbusied(m))
5510 		m->dirty &= ~pagebits;
5511 	else
5512 		vm_page_clear_dirty_mask(m, pagebits);
5513 }
5514 
5515 void
5516 vm_page_clear_dirty(vm_page_t m, int base, int size)
5517 {
5518 
5519 	vm_page_clear_dirty_mask(m, vm_page_bits(base, size));
5520 }
5521 
5522 /*
5523  *	vm_page_set_invalid:
5524  *
5525  *	Invalidates DEV_BSIZE'd chunks within a page.  Both the
5526  *	valid and dirty bits for the effected areas are cleared.
5527  */
5528 void
5529 vm_page_set_invalid(vm_page_t m, int base, int size)
5530 {
5531 	vm_page_bits_t bits;
5532 	vm_object_t object;
5533 
5534 	/*
5535 	 * The object lock is required so that pages can't be mapped
5536 	 * read-only while we're in the process of invalidating them.
5537 	 */
5538 	object = m->object;
5539 	VM_OBJECT_ASSERT_WLOCKED(object);
5540 	vm_page_assert_busied(m);
5541 
5542 	if (object->type == OBJT_VNODE && base == 0 && IDX_TO_OFF(m->pindex) +
5543 	    size >= object->un_pager.vnp.vnp_size)
5544 		bits = VM_PAGE_BITS_ALL;
5545 	else
5546 		bits = vm_page_bits(base, size);
5547 	if (object->ref_count != 0 && vm_page_all_valid(m) && bits != 0)
5548 		pmap_remove_all(m);
5549 	KASSERT((bits == 0 && vm_page_all_valid(m)) ||
5550 	    !pmap_page_is_mapped(m),
5551 	    ("vm_page_set_invalid: page %p is mapped", m));
5552 	if (vm_page_xbusied(m)) {
5553 		m->valid &= ~bits;
5554 		m->dirty &= ~bits;
5555 	} else {
5556 		vm_page_bits_clear(m, &m->valid, bits);
5557 		vm_page_bits_clear(m, &m->dirty, bits);
5558 	}
5559 }
5560 
5561 /*
5562  *	vm_page_invalid:
5563  *
5564  *	Invalidates the entire page.  The page must be busy, unmapped, and
5565  *	the enclosing object must be locked.  The object locks protects
5566  *	against concurrent read-only pmap enter which is done without
5567  *	busy.
5568  */
5569 void
5570 vm_page_invalid(vm_page_t m)
5571 {
5572 
5573 	vm_page_assert_busied(m);
5574 	VM_OBJECT_ASSERT_WLOCKED(m->object);
5575 	MPASS(!pmap_page_is_mapped(m));
5576 
5577 	if (vm_page_xbusied(m))
5578 		m->valid = 0;
5579 	else
5580 		vm_page_bits_clear(m, &m->valid, VM_PAGE_BITS_ALL);
5581 }
5582 
5583 /*
5584  * vm_page_zero_invalid()
5585  *
5586  *	The kernel assumes that the invalid portions of a page contain
5587  *	garbage, but such pages can be mapped into memory by user code.
5588  *	When this occurs, we must zero out the non-valid portions of the
5589  *	page so user code sees what it expects.
5590  *
5591  *	Pages are most often semi-valid when the end of a file is mapped
5592  *	into memory and the file's size is not page aligned.
5593  */
5594 void
5595 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
5596 {
5597 	int b;
5598 	int i;
5599 
5600 	/*
5601 	 * Scan the valid bits looking for invalid sections that
5602 	 * must be zeroed.  Invalid sub-DEV_BSIZE'd areas ( where the
5603 	 * valid bit may be set ) have already been zeroed by
5604 	 * vm_page_set_validclean().
5605 	 */
5606 	for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
5607 		if (i == (PAGE_SIZE / DEV_BSIZE) ||
5608 		    (m->valid & ((vm_page_bits_t)1 << i))) {
5609 			if (i > b) {
5610 				pmap_zero_page_area(m,
5611 				    b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
5612 			}
5613 			b = i + 1;
5614 		}
5615 	}
5616 
5617 	/*
5618 	 * setvalid is TRUE when we can safely set the zero'd areas
5619 	 * as being valid.  We can do this if there are no cache consistency
5620 	 * issues.  e.g. it is ok to do with UFS, but not ok to do with NFS.
5621 	 */
5622 	if (setvalid)
5623 		vm_page_valid(m);
5624 }
5625 
5626 /*
5627  *	vm_page_is_valid:
5628  *
5629  *	Is (partial) page valid?  Note that the case where size == 0
5630  *	will return FALSE in the degenerate case where the page is
5631  *	entirely invalid, and TRUE otherwise.
5632  *
5633  *	Some callers envoke this routine without the busy lock held and
5634  *	handle races via higher level locks.  Typical callers should
5635  *	hold a busy lock to prevent invalidation.
5636  */
5637 int
5638 vm_page_is_valid(vm_page_t m, int base, int size)
5639 {
5640 	vm_page_bits_t bits;
5641 
5642 	bits = vm_page_bits(base, size);
5643 	return (vm_page_any_valid(m) && (m->valid & bits) == bits);
5644 }
5645 
5646 /*
5647  * Returns true if all of the specified predicates are true for the entire
5648  * (super)page and false otherwise.
5649  */
5650 bool
5651 vm_page_ps_test(vm_page_t m, int psind, int flags, vm_page_t skip_m)
5652 {
5653 	vm_object_t object;
5654 	int i, npages;
5655 
5656 	object = m->object;
5657 	if (skip_m != NULL && skip_m->object != object)
5658 		return (false);
5659 	VM_OBJECT_ASSERT_LOCKED(object);
5660 	KASSERT(psind <= m->psind,
5661 	    ("psind %d > psind %d of m %p", psind, m->psind, m));
5662 	npages = atop(pagesizes[psind]);
5663 
5664 	/*
5665 	 * The physically contiguous pages that make up a superpage, i.e., a
5666 	 * page with a page size index ("psind") greater than zero, will
5667 	 * occupy adjacent entries in vm_page_array[].
5668 	 */
5669 	for (i = 0; i < npages; i++) {
5670 		/* Always test object consistency, including "skip_m". */
5671 		if (m[i].object != object)
5672 			return (false);
5673 		if (&m[i] == skip_m)
5674 			continue;
5675 		if ((flags & PS_NONE_BUSY) != 0 && vm_page_busied(&m[i]))
5676 			return (false);
5677 		if ((flags & PS_ALL_DIRTY) != 0) {
5678 			/*
5679 			 * Calling vm_page_test_dirty() or pmap_is_modified()
5680 			 * might stop this case from spuriously returning
5681 			 * "false".  However, that would require a write lock
5682 			 * on the object containing "m[i]".
5683 			 */
5684 			if (m[i].dirty != VM_PAGE_BITS_ALL)
5685 				return (false);
5686 		}
5687 		if ((flags & PS_ALL_VALID) != 0 &&
5688 		    m[i].valid != VM_PAGE_BITS_ALL)
5689 			return (false);
5690 	}
5691 	return (true);
5692 }
5693 
5694 /*
5695  * Set the page's dirty bits if the page is modified.
5696  */
5697 void
5698 vm_page_test_dirty(vm_page_t m)
5699 {
5700 
5701 	vm_page_assert_busied(m);
5702 	if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m))
5703 		vm_page_dirty(m);
5704 }
5705 
5706 void
5707 vm_page_valid(vm_page_t m)
5708 {
5709 
5710 	vm_page_assert_busied(m);
5711 	if (vm_page_xbusied(m))
5712 		m->valid = VM_PAGE_BITS_ALL;
5713 	else
5714 		vm_page_bits_set(m, &m->valid, VM_PAGE_BITS_ALL);
5715 }
5716 
5717 void
5718 vm_page_lock_KBI(vm_page_t m, const char *file, int line)
5719 {
5720 
5721 	mtx_lock_flags_(vm_page_lockptr(m), 0, file, line);
5722 }
5723 
5724 void
5725 vm_page_unlock_KBI(vm_page_t m, const char *file, int line)
5726 {
5727 
5728 	mtx_unlock_flags_(vm_page_lockptr(m), 0, file, line);
5729 }
5730 
5731 int
5732 vm_page_trylock_KBI(vm_page_t m, const char *file, int line)
5733 {
5734 
5735 	return (mtx_trylock_flags_(vm_page_lockptr(m), 0, file, line));
5736 }
5737 
5738 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
5739 void
5740 vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line)
5741 {
5742 
5743 	vm_page_lock_assert_KBI(m, MA_OWNED, file, line);
5744 }
5745 
5746 void
5747 vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line)
5748 {
5749 
5750 	mtx_assert_(vm_page_lockptr(m), a, file, line);
5751 }
5752 #endif
5753 
5754 #ifdef INVARIANTS
5755 void
5756 vm_page_object_busy_assert(vm_page_t m)
5757 {
5758 
5759 	/*
5760 	 * Certain of the page's fields may only be modified by the
5761 	 * holder of a page or object busy.
5762 	 */
5763 	if (m->object != NULL && !vm_page_busied(m))
5764 		VM_OBJECT_ASSERT_BUSY(m->object);
5765 }
5766 
5767 void
5768 vm_page_assert_pga_writeable(vm_page_t m, uint16_t bits)
5769 {
5770 
5771 	if ((bits & PGA_WRITEABLE) == 0)
5772 		return;
5773 
5774 	/*
5775 	 * The PGA_WRITEABLE flag can only be set if the page is
5776 	 * managed, is exclusively busied or the object is locked.
5777 	 * Currently, this flag is only set by pmap_enter().
5778 	 */
5779 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5780 	    ("PGA_WRITEABLE on unmanaged page"));
5781 	if (!vm_page_xbusied(m))
5782 		VM_OBJECT_ASSERT_BUSY(m->object);
5783 }
5784 #endif
5785 
5786 #include "opt_ddb.h"
5787 #ifdef DDB
5788 #include <sys/kernel.h>
5789 
5790 #include <ddb/ddb.h>
5791 
5792 DB_SHOW_COMMAND_FLAGS(page, vm_page_print_page_info, DB_CMD_MEMSAFE)
5793 {
5794 
5795 	db_printf("vm_cnt.v_free_count: %d\n", vm_free_count());
5796 	db_printf("vm_cnt.v_inactive_count: %d\n", vm_inactive_count());
5797 	db_printf("vm_cnt.v_active_count: %d\n", vm_active_count());
5798 	db_printf("vm_cnt.v_laundry_count: %d\n", vm_laundry_count());
5799 	db_printf("vm_cnt.v_wire_count: %d\n", vm_wire_count());
5800 	db_printf("vm_cnt.v_free_reserved: %d\n", vm_cnt.v_free_reserved);
5801 	db_printf("vm_cnt.v_free_min: %d\n", vm_cnt.v_free_min);
5802 	db_printf("vm_cnt.v_free_target: %d\n", vm_cnt.v_free_target);
5803 	db_printf("vm_cnt.v_inactive_target: %d\n", vm_cnt.v_inactive_target);
5804 }
5805 
5806 DB_SHOW_COMMAND_FLAGS(pageq, vm_page_print_pageq_info, DB_CMD_MEMSAFE)
5807 {
5808 	int dom;
5809 
5810 	db_printf("pq_free %d\n", vm_free_count());
5811 	for (dom = 0; dom < vm_ndomains; dom++) {
5812 		db_printf(
5813     "dom %d page_cnt %d free %d pq_act %d pq_inact %d pq_laund %d pq_unsw %d\n",
5814 		    dom,
5815 		    vm_dom[dom].vmd_page_count,
5816 		    vm_dom[dom].vmd_free_count,
5817 		    vm_dom[dom].vmd_pagequeues[PQ_ACTIVE].pq_cnt,
5818 		    vm_dom[dom].vmd_pagequeues[PQ_INACTIVE].pq_cnt,
5819 		    vm_dom[dom].vmd_pagequeues[PQ_LAUNDRY].pq_cnt,
5820 		    vm_dom[dom].vmd_pagequeues[PQ_UNSWAPPABLE].pq_cnt);
5821 	}
5822 }
5823 
5824 DB_SHOW_COMMAND(pginfo, vm_page_print_pginfo)
5825 {
5826 	vm_page_t m;
5827 	boolean_t phys, virt;
5828 
5829 	if (!have_addr) {
5830 		db_printf("show pginfo addr\n");
5831 		return;
5832 	}
5833 
5834 	phys = strchr(modif, 'p') != NULL;
5835 	virt = strchr(modif, 'v') != NULL;
5836 	if (virt)
5837 		m = PHYS_TO_VM_PAGE(pmap_kextract(addr));
5838 	else if (phys)
5839 		m = PHYS_TO_VM_PAGE(addr);
5840 	else
5841 		m = (vm_page_t)addr;
5842 	db_printf(
5843     "page %p obj %p pidx 0x%jx phys 0x%jx q %d ref 0x%x\n"
5844     "  af 0x%x of 0x%x f 0x%x act %d busy %x valid 0x%x dirty 0x%x\n",
5845 	    m, m->object, (uintmax_t)m->pindex, (uintmax_t)m->phys_addr,
5846 	    m->a.queue, m->ref_count, m->a.flags, m->oflags,
5847 	    m->flags, m->a.act_count, m->busy_lock, m->valid, m->dirty);
5848 }
5849 #endif /* DDB */
5850