xref: /freebsd/sys/vm/vm_page.c (revision 2c8d04d0228871c24017509cf039e7c5d97d97be)
1 /*-
2  * Copyright (c) 1991 Regents of the University of California.
3  * All rights reserved.
4  * Copyright (c) 1998 Matthew Dillon.  All Rights Reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * The Mach Operating System project at Carnegie-Mellon University.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	from: @(#)vm_page.c	7.4 (Berkeley) 5/7/91
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  *			GENERAL RULES ON VM_PAGE MANIPULATION
65  *
66  *	- A page queue lock is required when adding or removing a page from a
67  *	  page queue regardless of other locks or the busy state of a page.
68  *
69  *		* In general, no thread besides the page daemon can acquire or
70  *		  hold more than one page queue lock at a time.
71  *
72  *		* The page daemon can acquire and hold any pair of page queue
73  *		  locks in any order.
74  *
75  *	- The object lock is required when inserting or removing
76  *	  pages from an object (vm_page_insert() or vm_page_remove()).
77  *
78  */
79 
80 /*
81  *	Resident memory management module.
82  */
83 
84 #include <sys/cdefs.h>
85 __FBSDID("$FreeBSD$");
86 
87 #include "opt_vm.h"
88 
89 #include <sys/param.h>
90 #include <sys/systm.h>
91 #include <sys/lock.h>
92 #include <sys/kernel.h>
93 #include <sys/limits.h>
94 #include <sys/linker.h>
95 #include <sys/malloc.h>
96 #include <sys/mman.h>
97 #include <sys/msgbuf.h>
98 #include <sys/mutex.h>
99 #include <sys/proc.h>
100 #include <sys/rwlock.h>
101 #include <sys/sbuf.h>
102 #include <sys/smp.h>
103 #include <sys/sysctl.h>
104 #include <sys/vmmeter.h>
105 #include <sys/vnode.h>
106 
107 #include <vm/vm.h>
108 #include <vm/pmap.h>
109 #include <vm/vm_param.h>
110 #include <vm/vm_kern.h>
111 #include <vm/vm_object.h>
112 #include <vm/vm_page.h>
113 #include <vm/vm_pageout.h>
114 #include <vm/vm_pager.h>
115 #include <vm/vm_phys.h>
116 #include <vm/vm_radix.h>
117 #include <vm/vm_reserv.h>
118 #include <vm/vm_extern.h>
119 #include <vm/uma.h>
120 #include <vm/uma_int.h>
121 
122 #include <machine/md_var.h>
123 
124 /*
125  *	Associated with page of user-allocatable memory is a
126  *	page structure.
127  */
128 
129 struct vm_domain vm_dom[MAXMEMDOM];
130 struct mtx_padalign vm_page_queue_free_mtx;
131 
132 struct mtx_padalign pa_lock[PA_LOCK_COUNT];
133 
134 vm_page_t vm_page_array;
135 long vm_page_array_size;
136 long first_page;
137 int vm_page_zero_count;
138 
139 static int boot_pages = UMA_BOOT_PAGES;
140 SYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
141     &boot_pages, 0,
142     "number of pages allocated for bootstrapping the VM system");
143 
144 static int pa_tryrelock_restart;
145 SYSCTL_INT(_vm, OID_AUTO, tryrelock_restart, CTLFLAG_RD,
146     &pa_tryrelock_restart, 0, "Number of tryrelock restarts");
147 
148 static TAILQ_HEAD(, vm_page) blacklist_head;
149 static int sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS);
150 SYSCTL_PROC(_vm, OID_AUTO, page_blacklist, CTLTYPE_STRING | CTLFLAG_RD |
151     CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_page_blacklist, "A", "Blacklist pages");
152 
153 /* Is the page daemon waiting for free pages? */
154 static int vm_pageout_pages_needed;
155 
156 static uma_zone_t fakepg_zone;
157 
158 static struct vnode *vm_page_alloc_init(vm_page_t m);
159 static void vm_page_cache_turn_free(vm_page_t m);
160 static void vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits);
161 static void vm_page_enqueue(uint8_t queue, vm_page_t m);
162 static void vm_page_free_wakeup(void);
163 static void vm_page_init_fakepg(void *dummy);
164 static int vm_page_insert_after(vm_page_t m, vm_object_t object,
165     vm_pindex_t pindex, vm_page_t mpred);
166 static void vm_page_insert_radixdone(vm_page_t m, vm_object_t object,
167     vm_page_t mpred);
168 static int vm_page_reclaim_run(int req_class, u_long npages, vm_page_t m_run,
169     vm_paddr_t high);
170 
171 SYSINIT(vm_page, SI_SUB_VM, SI_ORDER_SECOND, vm_page_init_fakepg, NULL);
172 
173 static void
174 vm_page_init_fakepg(void *dummy)
175 {
176 
177 	fakepg_zone = uma_zcreate("fakepg", sizeof(struct vm_page), NULL, NULL,
178 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM);
179 }
180 
181 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */
182 #if PAGE_SIZE == 32768
183 #ifdef CTASSERT
184 CTASSERT(sizeof(u_long) >= 8);
185 #endif
186 #endif
187 
188 /*
189  * Try to acquire a physical address lock while a pmap is locked.  If we
190  * fail to trylock we unlock and lock the pmap directly and cache the
191  * locked pa in *locked.  The caller should then restart their loop in case
192  * the virtual to physical mapping has changed.
193  */
194 int
195 vm_page_pa_tryrelock(pmap_t pmap, vm_paddr_t pa, vm_paddr_t *locked)
196 {
197 	vm_paddr_t lockpa;
198 
199 	lockpa = *locked;
200 	*locked = pa;
201 	if (lockpa) {
202 		PA_LOCK_ASSERT(lockpa, MA_OWNED);
203 		if (PA_LOCKPTR(pa) == PA_LOCKPTR(lockpa))
204 			return (0);
205 		PA_UNLOCK(lockpa);
206 	}
207 	if (PA_TRYLOCK(pa))
208 		return (0);
209 	PMAP_UNLOCK(pmap);
210 	atomic_add_int(&pa_tryrelock_restart, 1);
211 	PA_LOCK(pa);
212 	PMAP_LOCK(pmap);
213 	return (EAGAIN);
214 }
215 
216 /*
217  *	vm_set_page_size:
218  *
219  *	Sets the page size, perhaps based upon the memory
220  *	size.  Must be called before any use of page-size
221  *	dependent functions.
222  */
223 void
224 vm_set_page_size(void)
225 {
226 	if (vm_cnt.v_page_size == 0)
227 		vm_cnt.v_page_size = PAGE_SIZE;
228 	if (((vm_cnt.v_page_size - 1) & vm_cnt.v_page_size) != 0)
229 		panic("vm_set_page_size: page size not a power of two");
230 }
231 
232 /*
233  *	vm_page_blacklist_next:
234  *
235  *	Find the next entry in the provided string of blacklist
236  *	addresses.  Entries are separated by space, comma, or newline.
237  *	If an invalid integer is encountered then the rest of the
238  *	string is skipped.  Updates the list pointer to the next
239  *	character, or NULL if the string is exhausted or invalid.
240  */
241 static vm_paddr_t
242 vm_page_blacklist_next(char **list, char *end)
243 {
244 	vm_paddr_t bad;
245 	char *cp, *pos;
246 
247 	if (list == NULL || *list == NULL)
248 		return (0);
249 	if (**list =='\0') {
250 		*list = NULL;
251 		return (0);
252 	}
253 
254 	/*
255 	 * If there's no end pointer then the buffer is coming from
256 	 * the kenv and we know it's null-terminated.
257 	 */
258 	if (end == NULL)
259 		end = *list + strlen(*list);
260 
261 	/* Ensure that strtoq() won't walk off the end */
262 	if (*end != '\0') {
263 		if (*end == '\n' || *end == ' ' || *end  == ',')
264 			*end = '\0';
265 		else {
266 			printf("Blacklist not terminated, skipping\n");
267 			*list = NULL;
268 			return (0);
269 		}
270 	}
271 
272 	for (pos = *list; *pos != '\0'; pos = cp) {
273 		bad = strtoq(pos, &cp, 0);
274 		if (*cp == '\0' || *cp == ' ' || *cp == ',' || *cp == '\n') {
275 			if (bad == 0) {
276 				if (++cp < end)
277 					continue;
278 				else
279 					break;
280 			}
281 		} else
282 			break;
283 		if (*cp == '\0' || ++cp >= end)
284 			*list = NULL;
285 		else
286 			*list = cp;
287 		return (trunc_page(bad));
288 	}
289 	printf("Garbage in RAM blacklist, skipping\n");
290 	*list = NULL;
291 	return (0);
292 }
293 
294 /*
295  *	vm_page_blacklist_check:
296  *
297  *	Iterate through the provided string of blacklist addresses, pulling
298  *	each entry out of the physical allocator free list and putting it
299  *	onto a list for reporting via the vm.page_blacklist sysctl.
300  */
301 static void
302 vm_page_blacklist_check(char *list, char *end)
303 {
304 	vm_paddr_t pa;
305 	vm_page_t m;
306 	char *next;
307 	int ret;
308 
309 	next = list;
310 	while (next != NULL) {
311 		if ((pa = vm_page_blacklist_next(&next, end)) == 0)
312 			continue;
313 		m = vm_phys_paddr_to_vm_page(pa);
314 		if (m == NULL)
315 			continue;
316 		mtx_lock(&vm_page_queue_free_mtx);
317 		ret = vm_phys_unfree_page(m);
318 		mtx_unlock(&vm_page_queue_free_mtx);
319 		if (ret == TRUE) {
320 			TAILQ_INSERT_TAIL(&blacklist_head, m, listq);
321 			if (bootverbose)
322 				printf("Skipping page with pa 0x%jx\n",
323 				    (uintmax_t)pa);
324 		}
325 	}
326 }
327 
328 /*
329  *	vm_page_blacklist_load:
330  *
331  *	Search for a special module named "ram_blacklist".  It'll be a
332  *	plain text file provided by the user via the loader directive
333  *	of the same name.
334  */
335 static void
336 vm_page_blacklist_load(char **list, char **end)
337 {
338 	void *mod;
339 	u_char *ptr;
340 	u_int len;
341 
342 	mod = NULL;
343 	ptr = NULL;
344 
345 	mod = preload_search_by_type("ram_blacklist");
346 	if (mod != NULL) {
347 		ptr = preload_fetch_addr(mod);
348 		len = preload_fetch_size(mod);
349         }
350 	*list = ptr;
351 	if (ptr != NULL)
352 		*end = ptr + len;
353 	else
354 		*end = NULL;
355 	return;
356 }
357 
358 static int
359 sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS)
360 {
361 	vm_page_t m;
362 	struct sbuf sbuf;
363 	int error, first;
364 
365 	first = 1;
366 	error = sysctl_wire_old_buffer(req, 0);
367 	if (error != 0)
368 		return (error);
369 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
370 	TAILQ_FOREACH(m, &blacklist_head, listq) {
371 		sbuf_printf(&sbuf, "%s%#jx", first ? "" : ",",
372 		    (uintmax_t)m->phys_addr);
373 		first = 0;
374 	}
375 	error = sbuf_finish(&sbuf);
376 	sbuf_delete(&sbuf);
377 	return (error);
378 }
379 
380 static void
381 vm_page_domain_init(struct vm_domain *vmd)
382 {
383 	struct vm_pagequeue *pq;
384 	int i;
385 
386 	*__DECONST(char **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_name) =
387 	    "vm inactive pagequeue";
388 	*__DECONST(u_int **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_vcnt) =
389 	    &vm_cnt.v_inactive_count;
390 	*__DECONST(char **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_name) =
391 	    "vm active pagequeue";
392 	*__DECONST(u_int **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_vcnt) =
393 	    &vm_cnt.v_active_count;
394 	vmd->vmd_page_count = 0;
395 	vmd->vmd_free_count = 0;
396 	vmd->vmd_segs = 0;
397 	vmd->vmd_oom = FALSE;
398 	vmd->vmd_pass = 0;
399 	for (i = 0; i < PQ_COUNT; i++) {
400 		pq = &vmd->vmd_pagequeues[i];
401 		TAILQ_INIT(&pq->pq_pl);
402 		mtx_init(&pq->pq_mutex, pq->pq_name, "vm pagequeue",
403 		    MTX_DEF | MTX_DUPOK);
404 	}
405 }
406 
407 /*
408  *	vm_page_startup:
409  *
410  *	Initializes the resident memory module.
411  *
412  *	Allocates memory for the page cells, and
413  *	for the object/offset-to-page hash table headers.
414  *	Each page cell is initialized and placed on the free list.
415  */
416 vm_offset_t
417 vm_page_startup(vm_offset_t vaddr)
418 {
419 	vm_offset_t mapped;
420 	vm_paddr_t page_range;
421 	vm_paddr_t new_end;
422 	int i;
423 	vm_paddr_t pa;
424 	vm_paddr_t last_pa;
425 	char *list, *listend;
426 	vm_paddr_t end;
427 	vm_paddr_t biggestsize;
428 	vm_paddr_t low_water, high_water;
429 	int biggestone;
430 	int pages_per_zone;
431 
432 	biggestsize = 0;
433 	biggestone = 0;
434 	vaddr = round_page(vaddr);
435 
436 	for (i = 0; phys_avail[i + 1]; i += 2) {
437 		phys_avail[i] = round_page(phys_avail[i]);
438 		phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
439 	}
440 
441 	low_water = phys_avail[0];
442 	high_water = phys_avail[1];
443 
444 	for (i = 0; i < vm_phys_nsegs; i++) {
445 		if (vm_phys_segs[i].start < low_water)
446 			low_water = vm_phys_segs[i].start;
447 		if (vm_phys_segs[i].end > high_water)
448 			high_water = vm_phys_segs[i].end;
449 	}
450 	for (i = 0; phys_avail[i + 1]; i += 2) {
451 		vm_paddr_t size = phys_avail[i + 1] - phys_avail[i];
452 
453 		if (size > biggestsize) {
454 			biggestone = i;
455 			biggestsize = size;
456 		}
457 		if (phys_avail[i] < low_water)
458 			low_water = phys_avail[i];
459 		if (phys_avail[i + 1] > high_water)
460 			high_water = phys_avail[i + 1];
461 	}
462 
463 	end = phys_avail[biggestone+1];
464 
465 	/*
466 	 * Initialize the page and queue locks.
467 	 */
468 	mtx_init(&vm_page_queue_free_mtx, "vm page free queue", NULL, MTX_DEF);
469 	for (i = 0; i < PA_LOCK_COUNT; i++)
470 		mtx_init(&pa_lock[i], "vm page", NULL, MTX_DEF);
471 	for (i = 0; i < vm_ndomains; i++)
472 		vm_page_domain_init(&vm_dom[i]);
473 
474 	/*
475 	 * Almost all of the pages needed for boot strapping UMA are used
476 	 * for zone structures, so if the number of CPUs results in those
477 	 * structures taking more than one page each, we set aside more pages
478 	 * in proportion to the zone structure size.
479 	 */
480 	pages_per_zone = howmany(sizeof(struct uma_zone) +
481 	    sizeof(struct uma_cache) * (mp_maxid + 1), UMA_SLAB_SIZE);
482 	if (pages_per_zone > 1) {
483 		/* Reserve more pages so that we don't run out. */
484 		boot_pages = UMA_BOOT_PAGES_ZONES * pages_per_zone;
485 	}
486 
487 	/*
488 	 * Allocate memory for use when boot strapping the kernel memory
489 	 * allocator.
490 	 *
491 	 * CTFLAG_RDTUN doesn't work during the early boot process, so we must
492 	 * manually fetch the value.
493 	 */
494 	TUNABLE_INT_FETCH("vm.boot_pages", &boot_pages);
495 	new_end = end - (boot_pages * UMA_SLAB_SIZE);
496 	new_end = trunc_page(new_end);
497 	mapped = pmap_map(&vaddr, new_end, end,
498 	    VM_PROT_READ | VM_PROT_WRITE);
499 	bzero((void *)mapped, end - new_end);
500 	uma_startup((void *)mapped, boot_pages);
501 
502 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \
503     defined(__i386__) || defined(__mips__)
504 	/*
505 	 * Allocate a bitmap to indicate that a random physical page
506 	 * needs to be included in a minidump.
507 	 *
508 	 * The amd64 port needs this to indicate which direct map pages
509 	 * need to be dumped, via calls to dump_add_page()/dump_drop_page().
510 	 *
511 	 * However, i386 still needs this workspace internally within the
512 	 * minidump code.  In theory, they are not needed on i386, but are
513 	 * included should the sf_buf code decide to use them.
514 	 */
515 	last_pa = 0;
516 	for (i = 0; dump_avail[i + 1] != 0; i += 2)
517 		if (dump_avail[i + 1] > last_pa)
518 			last_pa = dump_avail[i + 1];
519 	page_range = last_pa / PAGE_SIZE;
520 	vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY);
521 	new_end -= vm_page_dump_size;
522 	vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
523 	    new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
524 	bzero((void *)vm_page_dump, vm_page_dump_size);
525 #endif
526 #ifdef __amd64__
527 	/*
528 	 * Request that the physical pages underlying the message buffer be
529 	 * included in a crash dump.  Since the message buffer is accessed
530 	 * through the direct map, they are not automatically included.
531 	 */
532 	pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr);
533 	last_pa = pa + round_page(msgbufsize);
534 	while (pa < last_pa) {
535 		dump_add_page(pa);
536 		pa += PAGE_SIZE;
537 	}
538 #endif
539 	/*
540 	 * Compute the number of pages of memory that will be available for
541 	 * use (taking into account the overhead of a page structure per
542 	 * page).
543 	 */
544 	first_page = low_water / PAGE_SIZE;
545 #ifdef VM_PHYSSEG_SPARSE
546 	page_range = 0;
547 	for (i = 0; i < vm_phys_nsegs; i++) {
548 		page_range += atop(vm_phys_segs[i].end -
549 		    vm_phys_segs[i].start);
550 	}
551 	for (i = 0; phys_avail[i + 1] != 0; i += 2)
552 		page_range += atop(phys_avail[i + 1] - phys_avail[i]);
553 #elif defined(VM_PHYSSEG_DENSE)
554 	page_range = high_water / PAGE_SIZE - first_page;
555 #else
556 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
557 #endif
558 	end = new_end;
559 
560 	/*
561 	 * Reserve an unmapped guard page to trap access to vm_page_array[-1].
562 	 */
563 	vaddr += PAGE_SIZE;
564 
565 	/*
566 	 * Initialize the mem entry structures now, and put them in the free
567 	 * queue.
568 	 */
569 	new_end = trunc_page(end - page_range * sizeof(struct vm_page));
570 	mapped = pmap_map(&vaddr, new_end, end,
571 	    VM_PROT_READ | VM_PROT_WRITE);
572 	vm_page_array = (vm_page_t) mapped;
573 #if VM_NRESERVLEVEL > 0
574 	/*
575 	 * Allocate memory for the reservation management system's data
576 	 * structures.
577 	 */
578 	new_end = vm_reserv_startup(&vaddr, new_end, high_water);
579 #endif
580 #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__)
581 	/*
582 	 * pmap_map on arm64, amd64, and mips can come out of the direct-map,
583 	 * not kvm like i386, so the pages must be tracked for a crashdump to
584 	 * include this data.  This includes the vm_page_array and the early
585 	 * UMA bootstrap pages.
586 	 */
587 	for (pa = new_end; pa < phys_avail[biggestone + 1]; pa += PAGE_SIZE)
588 		dump_add_page(pa);
589 #endif
590 	phys_avail[biggestone + 1] = new_end;
591 
592 	/*
593 	 * Add physical memory segments corresponding to the available
594 	 * physical pages.
595 	 */
596 	for (i = 0; phys_avail[i + 1] != 0; i += 2)
597 		vm_phys_add_seg(phys_avail[i], phys_avail[i + 1]);
598 
599 	/*
600 	 * Clear all of the page structures
601 	 */
602 	bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
603 	for (i = 0; i < page_range; i++)
604 		vm_page_array[i].order = VM_NFREEORDER;
605 	vm_page_array_size = page_range;
606 
607 	/*
608 	 * Initialize the physical memory allocator.
609 	 */
610 	vm_phys_init();
611 
612 	/*
613 	 * Add every available physical page that is not blacklisted to
614 	 * the free lists.
615 	 */
616 	vm_cnt.v_page_count = 0;
617 	vm_cnt.v_free_count = 0;
618 	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
619 		pa = phys_avail[i];
620 		last_pa = phys_avail[i + 1];
621 		while (pa < last_pa) {
622 			vm_phys_add_page(pa);
623 			pa += PAGE_SIZE;
624 		}
625 	}
626 
627 	TAILQ_INIT(&blacklist_head);
628 	vm_page_blacklist_load(&list, &listend);
629 	vm_page_blacklist_check(list, listend);
630 
631 	list = kern_getenv("vm.blacklist");
632 	vm_page_blacklist_check(list, NULL);
633 
634 	freeenv(list);
635 #if VM_NRESERVLEVEL > 0
636 	/*
637 	 * Initialize the reservation management system.
638 	 */
639 	vm_reserv_init();
640 #endif
641 	return (vaddr);
642 }
643 
644 void
645 vm_page_reference(vm_page_t m)
646 {
647 
648 	vm_page_aflag_set(m, PGA_REFERENCED);
649 }
650 
651 /*
652  *	vm_page_busy_downgrade:
653  *
654  *	Downgrade an exclusive busy page into a single shared busy page.
655  */
656 void
657 vm_page_busy_downgrade(vm_page_t m)
658 {
659 	u_int x;
660 
661 	vm_page_assert_xbusied(m);
662 
663 	for (;;) {
664 		x = m->busy_lock;
665 		x &= VPB_BIT_WAITERS;
666 		if (atomic_cmpset_rel_int(&m->busy_lock,
667 		    VPB_SINGLE_EXCLUSIVER | x, VPB_SHARERS_WORD(1) | x))
668 			break;
669 	}
670 }
671 
672 /*
673  *	vm_page_sbusied:
674  *
675  *	Return a positive value if the page is shared busied, 0 otherwise.
676  */
677 int
678 vm_page_sbusied(vm_page_t m)
679 {
680 	u_int x;
681 
682 	x = m->busy_lock;
683 	return ((x & VPB_BIT_SHARED) != 0 && x != VPB_UNBUSIED);
684 }
685 
686 /*
687  *	vm_page_sunbusy:
688  *
689  *	Shared unbusy a page.
690  */
691 void
692 vm_page_sunbusy(vm_page_t m)
693 {
694 	u_int x;
695 
696 	vm_page_assert_sbusied(m);
697 
698 	for (;;) {
699 		x = m->busy_lock;
700 		if (VPB_SHARERS(x) > 1) {
701 			if (atomic_cmpset_int(&m->busy_lock, x,
702 			    x - VPB_ONE_SHARER))
703 				break;
704 			continue;
705 		}
706 		if ((x & VPB_BIT_WAITERS) == 0) {
707 			KASSERT(x == VPB_SHARERS_WORD(1),
708 			    ("vm_page_sunbusy: invalid lock state"));
709 			if (atomic_cmpset_int(&m->busy_lock,
710 			    VPB_SHARERS_WORD(1), VPB_UNBUSIED))
711 				break;
712 			continue;
713 		}
714 		KASSERT(x == (VPB_SHARERS_WORD(1) | VPB_BIT_WAITERS),
715 		    ("vm_page_sunbusy: invalid lock state for waiters"));
716 
717 		vm_page_lock(m);
718 		if (!atomic_cmpset_int(&m->busy_lock, x, VPB_UNBUSIED)) {
719 			vm_page_unlock(m);
720 			continue;
721 		}
722 		wakeup(m);
723 		vm_page_unlock(m);
724 		break;
725 	}
726 }
727 
728 /*
729  *	vm_page_busy_sleep:
730  *
731  *	Sleep and release the page lock, using the page pointer as wchan.
732  *	This is used to implement the hard-path of busying mechanism.
733  *
734  *	The given page must be locked.
735  */
736 void
737 vm_page_busy_sleep(vm_page_t m, const char *wmesg)
738 {
739 	u_int x;
740 
741 	vm_page_lock_assert(m, MA_OWNED);
742 
743 	x = m->busy_lock;
744 	if (x == VPB_UNBUSIED) {
745 		vm_page_unlock(m);
746 		return;
747 	}
748 	if ((x & VPB_BIT_WAITERS) == 0 &&
749 	    !atomic_cmpset_int(&m->busy_lock, x, x | VPB_BIT_WAITERS)) {
750 		vm_page_unlock(m);
751 		return;
752 	}
753 	msleep(m, vm_page_lockptr(m), PVM | PDROP, wmesg, 0);
754 }
755 
756 /*
757  *	vm_page_trysbusy:
758  *
759  *	Try to shared busy a page.
760  *	If the operation succeeds 1 is returned otherwise 0.
761  *	The operation never sleeps.
762  */
763 int
764 vm_page_trysbusy(vm_page_t m)
765 {
766 	u_int x;
767 
768 	for (;;) {
769 		x = m->busy_lock;
770 		if ((x & VPB_BIT_SHARED) == 0)
771 			return (0);
772 		if (atomic_cmpset_acq_int(&m->busy_lock, x, x + VPB_ONE_SHARER))
773 			return (1);
774 	}
775 }
776 
777 static void
778 vm_page_xunbusy_locked(vm_page_t m)
779 {
780 
781 	vm_page_assert_xbusied(m);
782 	vm_page_assert_locked(m);
783 
784 	atomic_store_rel_int(&m->busy_lock, VPB_UNBUSIED);
785 	/* There is a waiter, do wakeup() instead of vm_page_flash(). */
786 	wakeup(m);
787 }
788 
789 static void
790 vm_page_xunbusy_maybelocked(vm_page_t m)
791 {
792 	bool lockacq;
793 
794 	vm_page_assert_xbusied(m);
795 
796 	/*
797 	 * Fast path for unbusy.  If it succeeds, we know that there
798 	 * are no waiters, so we do not need a wakeup.
799 	 */
800 	if (atomic_cmpset_rel_int(&m->busy_lock, VPB_SINGLE_EXCLUSIVER,
801 	    VPB_UNBUSIED))
802 		return;
803 
804 	lockacq = !mtx_owned(vm_page_lockptr(m));
805 	if (lockacq)
806 		vm_page_lock(m);
807 	vm_page_xunbusy_locked(m);
808 	if (lockacq)
809 		vm_page_unlock(m);
810 }
811 
812 /*
813  *	vm_page_xunbusy_hard:
814  *
815  *	Called after the first try the exclusive unbusy of a page failed.
816  *	It is assumed that the waiters bit is on.
817  */
818 void
819 vm_page_xunbusy_hard(vm_page_t m)
820 {
821 
822 	vm_page_assert_xbusied(m);
823 
824 	vm_page_lock(m);
825 	vm_page_xunbusy_locked(m);
826 	vm_page_unlock(m);
827 }
828 
829 /*
830  *	vm_page_flash:
831  *
832  *	Wakeup anyone waiting for the page.
833  *	The ownership bits do not change.
834  *
835  *	The given page must be locked.
836  */
837 void
838 vm_page_flash(vm_page_t m)
839 {
840 	u_int x;
841 
842 	vm_page_lock_assert(m, MA_OWNED);
843 
844 	for (;;) {
845 		x = m->busy_lock;
846 		if ((x & VPB_BIT_WAITERS) == 0)
847 			return;
848 		if (atomic_cmpset_int(&m->busy_lock, x,
849 		    x & (~VPB_BIT_WAITERS)))
850 			break;
851 	}
852 	wakeup(m);
853 }
854 
855 /*
856  * Keep page from being freed by the page daemon
857  * much of the same effect as wiring, except much lower
858  * overhead and should be used only for *very* temporary
859  * holding ("wiring").
860  */
861 void
862 vm_page_hold(vm_page_t mem)
863 {
864 
865 	vm_page_lock_assert(mem, MA_OWNED);
866         mem->hold_count++;
867 }
868 
869 void
870 vm_page_unhold(vm_page_t mem)
871 {
872 
873 	vm_page_lock_assert(mem, MA_OWNED);
874 	KASSERT(mem->hold_count >= 1, ("vm_page_unhold: hold count < 0!!!"));
875 	--mem->hold_count;
876 	if (mem->hold_count == 0 && (mem->flags & PG_UNHOLDFREE) != 0)
877 		vm_page_free_toq(mem);
878 }
879 
880 /*
881  *	vm_page_unhold_pages:
882  *
883  *	Unhold each of the pages that is referenced by the given array.
884  */
885 void
886 vm_page_unhold_pages(vm_page_t *ma, int count)
887 {
888 	struct mtx *mtx, *new_mtx;
889 
890 	mtx = NULL;
891 	for (; count != 0; count--) {
892 		/*
893 		 * Avoid releasing and reacquiring the same page lock.
894 		 */
895 		new_mtx = vm_page_lockptr(*ma);
896 		if (mtx != new_mtx) {
897 			if (mtx != NULL)
898 				mtx_unlock(mtx);
899 			mtx = new_mtx;
900 			mtx_lock(mtx);
901 		}
902 		vm_page_unhold(*ma);
903 		ma++;
904 	}
905 	if (mtx != NULL)
906 		mtx_unlock(mtx);
907 }
908 
909 vm_page_t
910 PHYS_TO_VM_PAGE(vm_paddr_t pa)
911 {
912 	vm_page_t m;
913 
914 #ifdef VM_PHYSSEG_SPARSE
915 	m = vm_phys_paddr_to_vm_page(pa);
916 	if (m == NULL)
917 		m = vm_phys_fictitious_to_vm_page(pa);
918 	return (m);
919 #elif defined(VM_PHYSSEG_DENSE)
920 	long pi;
921 
922 	pi = atop(pa);
923 	if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
924 		m = &vm_page_array[pi - first_page];
925 		return (m);
926 	}
927 	return (vm_phys_fictitious_to_vm_page(pa));
928 #else
929 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
930 #endif
931 }
932 
933 /*
934  *	vm_page_getfake:
935  *
936  *	Create a fictitious page with the specified physical address and
937  *	memory attribute.  The memory attribute is the only the machine-
938  *	dependent aspect of a fictitious page that must be initialized.
939  */
940 vm_page_t
941 vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
942 {
943 	vm_page_t m;
944 
945 	m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
946 	vm_page_initfake(m, paddr, memattr);
947 	return (m);
948 }
949 
950 void
951 vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
952 {
953 
954 	if ((m->flags & PG_FICTITIOUS) != 0) {
955 		/*
956 		 * The page's memattr might have changed since the
957 		 * previous initialization.  Update the pmap to the
958 		 * new memattr.
959 		 */
960 		goto memattr;
961 	}
962 	m->phys_addr = paddr;
963 	m->queue = PQ_NONE;
964 	/* Fictitious pages don't use "segind". */
965 	m->flags = PG_FICTITIOUS;
966 	/* Fictitious pages don't use "order" or "pool". */
967 	m->oflags = VPO_UNMANAGED;
968 	m->busy_lock = VPB_SINGLE_EXCLUSIVER;
969 	m->wire_count = 1;
970 	pmap_page_init(m);
971 memattr:
972 	pmap_page_set_memattr(m, memattr);
973 }
974 
975 /*
976  *	vm_page_putfake:
977  *
978  *	Release a fictitious page.
979  */
980 void
981 vm_page_putfake(vm_page_t m)
982 {
983 
984 	KASSERT((m->oflags & VPO_UNMANAGED) != 0, ("managed %p", m));
985 	KASSERT((m->flags & PG_FICTITIOUS) != 0,
986 	    ("vm_page_putfake: bad page %p", m));
987 	uma_zfree(fakepg_zone, m);
988 }
989 
990 /*
991  *	vm_page_updatefake:
992  *
993  *	Update the given fictitious page to the specified physical address and
994  *	memory attribute.
995  */
996 void
997 vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
998 {
999 
1000 	KASSERT((m->flags & PG_FICTITIOUS) != 0,
1001 	    ("vm_page_updatefake: bad page %p", m));
1002 	m->phys_addr = paddr;
1003 	pmap_page_set_memattr(m, memattr);
1004 }
1005 
1006 /*
1007  *	vm_page_free:
1008  *
1009  *	Free a page.
1010  */
1011 void
1012 vm_page_free(vm_page_t m)
1013 {
1014 
1015 	m->flags &= ~PG_ZERO;
1016 	vm_page_free_toq(m);
1017 }
1018 
1019 /*
1020  *	vm_page_free_zero:
1021  *
1022  *	Free a page to the zerod-pages queue
1023  */
1024 void
1025 vm_page_free_zero(vm_page_t m)
1026 {
1027 
1028 	m->flags |= PG_ZERO;
1029 	vm_page_free_toq(m);
1030 }
1031 
1032 /*
1033  * Unbusy and handle the page queueing for a page from the VOP_GETPAGES()
1034  * array which was optionally read ahead or behind.
1035  */
1036 void
1037 vm_page_readahead_finish(vm_page_t m)
1038 {
1039 
1040 	/* We shouldn't put invalid pages on queues. */
1041 	KASSERT(m->valid != 0, ("%s: %p is invalid", __func__, m));
1042 
1043 	/*
1044 	 * Since the page is not the actually needed one, whether it should
1045 	 * be activated or deactivated is not obvious.  Empirical results
1046 	 * have shown that deactivating the page is usually the best choice,
1047 	 * unless the page is wanted by another thread.
1048 	 */
1049 	vm_page_lock(m);
1050 	if ((m->busy_lock & VPB_BIT_WAITERS) != 0)
1051 		vm_page_activate(m);
1052 	else
1053 		vm_page_deactivate(m);
1054 	vm_page_unlock(m);
1055 	vm_page_xunbusy(m);
1056 }
1057 
1058 /*
1059  *	vm_page_sleep_if_busy:
1060  *
1061  *	Sleep and release the page queues lock if the page is busied.
1062  *	Returns TRUE if the thread slept.
1063  *
1064  *	The given page must be unlocked and object containing it must
1065  *	be locked.
1066  */
1067 int
1068 vm_page_sleep_if_busy(vm_page_t m, const char *msg)
1069 {
1070 	vm_object_t obj;
1071 
1072 	vm_page_lock_assert(m, MA_NOTOWNED);
1073 	VM_OBJECT_ASSERT_WLOCKED(m->object);
1074 
1075 	if (vm_page_busied(m)) {
1076 		/*
1077 		 * The page-specific object must be cached because page
1078 		 * identity can change during the sleep, causing the
1079 		 * re-lock of a different object.
1080 		 * It is assumed that a reference to the object is already
1081 		 * held by the callers.
1082 		 */
1083 		obj = m->object;
1084 		vm_page_lock(m);
1085 		VM_OBJECT_WUNLOCK(obj);
1086 		vm_page_busy_sleep(m, msg);
1087 		VM_OBJECT_WLOCK(obj);
1088 		return (TRUE);
1089 	}
1090 	return (FALSE);
1091 }
1092 
1093 /*
1094  *	vm_page_dirty_KBI:		[ internal use only ]
1095  *
1096  *	Set all bits in the page's dirty field.
1097  *
1098  *	The object containing the specified page must be locked if the
1099  *	call is made from the machine-independent layer.
1100  *
1101  *	See vm_page_clear_dirty_mask().
1102  *
1103  *	This function should only be called by vm_page_dirty().
1104  */
1105 void
1106 vm_page_dirty_KBI(vm_page_t m)
1107 {
1108 
1109 	/* These assertions refer to this operation by its public name. */
1110 	KASSERT((m->flags & PG_CACHED) == 0,
1111 	    ("vm_page_dirty: page in cache!"));
1112 	KASSERT(m->valid == VM_PAGE_BITS_ALL,
1113 	    ("vm_page_dirty: page is invalid!"));
1114 	m->dirty = VM_PAGE_BITS_ALL;
1115 }
1116 
1117 /*
1118  *	vm_page_insert:		[ internal use only ]
1119  *
1120  *	Inserts the given mem entry into the object and object list.
1121  *
1122  *	The object must be locked.
1123  */
1124 int
1125 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
1126 {
1127 	vm_page_t mpred;
1128 
1129 	VM_OBJECT_ASSERT_WLOCKED(object);
1130 	mpred = vm_radix_lookup_le(&object->rtree, pindex);
1131 	return (vm_page_insert_after(m, object, pindex, mpred));
1132 }
1133 
1134 /*
1135  *	vm_page_insert_after:
1136  *
1137  *	Inserts the page "m" into the specified object at offset "pindex".
1138  *
1139  *	The page "mpred" must immediately precede the offset "pindex" within
1140  *	the specified object.
1141  *
1142  *	The object must be locked.
1143  */
1144 static int
1145 vm_page_insert_after(vm_page_t m, vm_object_t object, vm_pindex_t pindex,
1146     vm_page_t mpred)
1147 {
1148 	vm_page_t msucc;
1149 
1150 	VM_OBJECT_ASSERT_WLOCKED(object);
1151 	KASSERT(m->object == NULL,
1152 	    ("vm_page_insert_after: page already inserted"));
1153 	if (mpred != NULL) {
1154 		KASSERT(mpred->object == object,
1155 		    ("vm_page_insert_after: object doesn't contain mpred"));
1156 		KASSERT(mpred->pindex < pindex,
1157 		    ("vm_page_insert_after: mpred doesn't precede pindex"));
1158 		msucc = TAILQ_NEXT(mpred, listq);
1159 	} else
1160 		msucc = TAILQ_FIRST(&object->memq);
1161 	if (msucc != NULL)
1162 		KASSERT(msucc->pindex > pindex,
1163 		    ("vm_page_insert_after: msucc doesn't succeed pindex"));
1164 
1165 	/*
1166 	 * Record the object/offset pair in this page
1167 	 */
1168 	m->object = object;
1169 	m->pindex = pindex;
1170 
1171 	/*
1172 	 * Now link into the object's ordered list of backed pages.
1173 	 */
1174 	if (vm_radix_insert(&object->rtree, m)) {
1175 		m->object = NULL;
1176 		m->pindex = 0;
1177 		return (1);
1178 	}
1179 	vm_page_insert_radixdone(m, object, mpred);
1180 	return (0);
1181 }
1182 
1183 /*
1184  *	vm_page_insert_radixdone:
1185  *
1186  *	Complete page "m" insertion into the specified object after the
1187  *	radix trie hooking.
1188  *
1189  *	The page "mpred" must precede the offset "m->pindex" within the
1190  *	specified object.
1191  *
1192  *	The object must be locked.
1193  */
1194 static void
1195 vm_page_insert_radixdone(vm_page_t m, vm_object_t object, vm_page_t mpred)
1196 {
1197 
1198 	VM_OBJECT_ASSERT_WLOCKED(object);
1199 	KASSERT(object != NULL && m->object == object,
1200 	    ("vm_page_insert_radixdone: page %p has inconsistent object", m));
1201 	if (mpred != NULL) {
1202 		KASSERT(mpred->object == object,
1203 		    ("vm_page_insert_after: object doesn't contain mpred"));
1204 		KASSERT(mpred->pindex < m->pindex,
1205 		    ("vm_page_insert_after: mpred doesn't precede pindex"));
1206 	}
1207 
1208 	if (mpred != NULL)
1209 		TAILQ_INSERT_AFTER(&object->memq, mpred, m, listq);
1210 	else
1211 		TAILQ_INSERT_HEAD(&object->memq, m, listq);
1212 
1213 	/*
1214 	 * Show that the object has one more resident page.
1215 	 */
1216 	object->resident_page_count++;
1217 
1218 	/*
1219 	 * Hold the vnode until the last page is released.
1220 	 */
1221 	if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
1222 		vhold(object->handle);
1223 
1224 	/*
1225 	 * Since we are inserting a new and possibly dirty page,
1226 	 * update the object's OBJ_MIGHTBEDIRTY flag.
1227 	 */
1228 	if (pmap_page_is_write_mapped(m))
1229 		vm_object_set_writeable_dirty(object);
1230 }
1231 
1232 /*
1233  *	vm_page_remove:
1234  *
1235  *	Removes the given mem entry from the object/offset-page
1236  *	table and the object page list, but do not invalidate/terminate
1237  *	the backing store.
1238  *
1239  *	The object must be locked.  The page must be locked if it is managed.
1240  */
1241 void
1242 vm_page_remove(vm_page_t m)
1243 {
1244 	vm_object_t object;
1245 
1246 	if ((m->oflags & VPO_UNMANAGED) == 0)
1247 		vm_page_assert_locked(m);
1248 	if ((object = m->object) == NULL)
1249 		return;
1250 	VM_OBJECT_ASSERT_WLOCKED(object);
1251 	if (vm_page_xbusied(m))
1252 		vm_page_xunbusy_maybelocked(m);
1253 
1254 	/*
1255 	 * Now remove from the object's list of backed pages.
1256 	 */
1257 	vm_radix_remove(&object->rtree, m->pindex);
1258 	TAILQ_REMOVE(&object->memq, m, listq);
1259 
1260 	/*
1261 	 * And show that the object has one fewer resident page.
1262 	 */
1263 	object->resident_page_count--;
1264 
1265 	/*
1266 	 * The vnode may now be recycled.
1267 	 */
1268 	if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
1269 		vdrop(object->handle);
1270 
1271 	m->object = NULL;
1272 }
1273 
1274 /*
1275  *	vm_page_lookup:
1276  *
1277  *	Returns the page associated with the object/offset
1278  *	pair specified; if none is found, NULL is returned.
1279  *
1280  *	The object must be locked.
1281  */
1282 vm_page_t
1283 vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
1284 {
1285 
1286 	VM_OBJECT_ASSERT_LOCKED(object);
1287 	return (vm_radix_lookup(&object->rtree, pindex));
1288 }
1289 
1290 /*
1291  *	vm_page_find_least:
1292  *
1293  *	Returns the page associated with the object with least pindex
1294  *	greater than or equal to the parameter pindex, or NULL.
1295  *
1296  *	The object must be locked.
1297  */
1298 vm_page_t
1299 vm_page_find_least(vm_object_t object, vm_pindex_t pindex)
1300 {
1301 	vm_page_t m;
1302 
1303 	VM_OBJECT_ASSERT_LOCKED(object);
1304 	if ((m = TAILQ_FIRST(&object->memq)) != NULL && m->pindex < pindex)
1305 		m = vm_radix_lookup_ge(&object->rtree, pindex);
1306 	return (m);
1307 }
1308 
1309 /*
1310  * Returns the given page's successor (by pindex) within the object if it is
1311  * resident; if none is found, NULL is returned.
1312  *
1313  * The object must be locked.
1314  */
1315 vm_page_t
1316 vm_page_next(vm_page_t m)
1317 {
1318 	vm_page_t next;
1319 
1320 	VM_OBJECT_ASSERT_LOCKED(m->object);
1321 	if ((next = TAILQ_NEXT(m, listq)) != NULL &&
1322 	    next->pindex != m->pindex + 1)
1323 		next = NULL;
1324 	return (next);
1325 }
1326 
1327 /*
1328  * Returns the given page's predecessor (by pindex) within the object if it is
1329  * resident; if none is found, NULL is returned.
1330  *
1331  * The object must be locked.
1332  */
1333 vm_page_t
1334 vm_page_prev(vm_page_t m)
1335 {
1336 	vm_page_t prev;
1337 
1338 	VM_OBJECT_ASSERT_LOCKED(m->object);
1339 	if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL &&
1340 	    prev->pindex != m->pindex - 1)
1341 		prev = NULL;
1342 	return (prev);
1343 }
1344 
1345 /*
1346  * Uses the page mnew as a replacement for an existing page at index
1347  * pindex which must be already present in the object.
1348  *
1349  * The existing page must not be on a paging queue.
1350  */
1351 vm_page_t
1352 vm_page_replace(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex)
1353 {
1354 	vm_page_t mold;
1355 
1356 	VM_OBJECT_ASSERT_WLOCKED(object);
1357 	KASSERT(mnew->object == NULL,
1358 	    ("vm_page_replace: page already in object"));
1359 
1360 	/*
1361 	 * This function mostly follows vm_page_insert() and
1362 	 * vm_page_remove() without the radix, object count and vnode
1363 	 * dance.  Double check such functions for more comments.
1364 	 */
1365 
1366 	mnew->object = object;
1367 	mnew->pindex = pindex;
1368 	mold = vm_radix_replace(&object->rtree, mnew);
1369 	KASSERT(mold->queue == PQ_NONE,
1370 	    ("vm_page_replace: mold is on a paging queue"));
1371 
1372 	/* Keep the resident page list in sorted order. */
1373 	TAILQ_INSERT_AFTER(&object->memq, mold, mnew, listq);
1374 	TAILQ_REMOVE(&object->memq, mold, listq);
1375 
1376 	mold->object = NULL;
1377 	vm_page_xunbusy_maybelocked(mold);
1378 
1379 	/*
1380 	 * The object's resident_page_count does not change because we have
1381 	 * swapped one page for another, but OBJ_MIGHTBEDIRTY.
1382 	 */
1383 	if (pmap_page_is_write_mapped(mnew))
1384 		vm_object_set_writeable_dirty(object);
1385 	return (mold);
1386 }
1387 
1388 /*
1389  *	vm_page_rename:
1390  *
1391  *	Move the given memory entry from its
1392  *	current object to the specified target object/offset.
1393  *
1394  *	Note: swap associated with the page must be invalidated by the move.  We
1395  *	      have to do this for several reasons:  (1) we aren't freeing the
1396  *	      page, (2) we are dirtying the page, (3) the VM system is probably
1397  *	      moving the page from object A to B, and will then later move
1398  *	      the backing store from A to B and we can't have a conflict.
1399  *
1400  *	Note: we *always* dirty the page.  It is necessary both for the
1401  *	      fact that we moved it, and because we may be invalidating
1402  *	      swap.  If the page is on the cache, we have to deactivate it
1403  *	      or vm_page_dirty() will panic.  Dirty pages are not allowed
1404  *	      on the cache.
1405  *
1406  *	The objects must be locked.
1407  */
1408 int
1409 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
1410 {
1411 	vm_page_t mpred;
1412 	vm_pindex_t opidx;
1413 
1414 	VM_OBJECT_ASSERT_WLOCKED(new_object);
1415 
1416 	mpred = vm_radix_lookup_le(&new_object->rtree, new_pindex);
1417 	KASSERT(mpred == NULL || mpred->pindex != new_pindex,
1418 	    ("vm_page_rename: pindex already renamed"));
1419 
1420 	/*
1421 	 * Create a custom version of vm_page_insert() which does not depend
1422 	 * by m_prev and can cheat on the implementation aspects of the
1423 	 * function.
1424 	 */
1425 	opidx = m->pindex;
1426 	m->pindex = new_pindex;
1427 	if (vm_radix_insert(&new_object->rtree, m)) {
1428 		m->pindex = opidx;
1429 		return (1);
1430 	}
1431 
1432 	/*
1433 	 * The operation cannot fail anymore.  The removal must happen before
1434 	 * the listq iterator is tainted.
1435 	 */
1436 	m->pindex = opidx;
1437 	vm_page_lock(m);
1438 	vm_page_remove(m);
1439 
1440 	/* Return back to the new pindex to complete vm_page_insert(). */
1441 	m->pindex = new_pindex;
1442 	m->object = new_object;
1443 	vm_page_unlock(m);
1444 	vm_page_insert_radixdone(m, new_object, mpred);
1445 	vm_page_dirty(m);
1446 	return (0);
1447 }
1448 
1449 /*
1450  *	Convert all of the given object's cached pages that have a
1451  *	pindex within the given range into free pages.  If the value
1452  *	zero is given for "end", then the range's upper bound is
1453  *	infinity.  If the given object is backed by a vnode and it
1454  *	transitions from having one or more cached pages to none, the
1455  *	vnode's hold count is reduced.
1456  */
1457 void
1458 vm_page_cache_free(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1459 {
1460 	vm_page_t m;
1461 	boolean_t empty;
1462 
1463 	mtx_lock(&vm_page_queue_free_mtx);
1464 	if (__predict_false(vm_radix_is_empty(&object->cache))) {
1465 		mtx_unlock(&vm_page_queue_free_mtx);
1466 		return;
1467 	}
1468 	while ((m = vm_radix_lookup_ge(&object->cache, start)) != NULL) {
1469 		if (end != 0 && m->pindex >= end)
1470 			break;
1471 		vm_radix_remove(&object->cache, m->pindex);
1472 		vm_page_cache_turn_free(m);
1473 	}
1474 	empty = vm_radix_is_empty(&object->cache);
1475 	mtx_unlock(&vm_page_queue_free_mtx);
1476 	if (object->type == OBJT_VNODE && empty)
1477 		vdrop(object->handle);
1478 }
1479 
1480 /*
1481  *	Returns the cached page that is associated with the given
1482  *	object and offset.  If, however, none exists, returns NULL.
1483  *
1484  *	The free page queue must be locked.
1485  */
1486 static inline vm_page_t
1487 vm_page_cache_lookup(vm_object_t object, vm_pindex_t pindex)
1488 {
1489 
1490 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1491 	return (vm_radix_lookup(&object->cache, pindex));
1492 }
1493 
1494 /*
1495  *	Remove the given cached page from its containing object's
1496  *	collection of cached pages.
1497  *
1498  *	The free page queue must be locked.
1499  */
1500 static void
1501 vm_page_cache_remove(vm_page_t m)
1502 {
1503 
1504 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1505 	KASSERT((m->flags & PG_CACHED) != 0,
1506 	    ("vm_page_cache_remove: page %p is not cached", m));
1507 	vm_radix_remove(&m->object->cache, m->pindex);
1508 	m->object = NULL;
1509 	vm_cnt.v_cache_count--;
1510 }
1511 
1512 /*
1513  *	Transfer all of the cached pages with offset greater than or
1514  *	equal to 'offidxstart' from the original object's cache to the
1515  *	new object's cache.  However, any cached pages with offset
1516  *	greater than or equal to the new object's size are kept in the
1517  *	original object.  Initially, the new object's cache must be
1518  *	empty.  Offset 'offidxstart' in the original object must
1519  *	correspond to offset zero in the new object.
1520  *
1521  *	The new object must be locked.
1522  */
1523 void
1524 vm_page_cache_transfer(vm_object_t orig_object, vm_pindex_t offidxstart,
1525     vm_object_t new_object)
1526 {
1527 	vm_page_t m;
1528 
1529 	/*
1530 	 * Insertion into an object's collection of cached pages
1531 	 * requires the object to be locked.  In contrast, removal does
1532 	 * not.
1533 	 */
1534 	VM_OBJECT_ASSERT_WLOCKED(new_object);
1535 	KASSERT(vm_radix_is_empty(&new_object->cache),
1536 	    ("vm_page_cache_transfer: object %p has cached pages",
1537 	    new_object));
1538 	mtx_lock(&vm_page_queue_free_mtx);
1539 	while ((m = vm_radix_lookup_ge(&orig_object->cache,
1540 	    offidxstart)) != NULL) {
1541 		/*
1542 		 * Transfer all of the pages with offset greater than or
1543 		 * equal to 'offidxstart' from the original object's
1544 		 * cache to the new object's cache.
1545 		 */
1546 		if ((m->pindex - offidxstart) >= new_object->size)
1547 			break;
1548 		vm_radix_remove(&orig_object->cache, m->pindex);
1549 		/* Update the page's object and offset. */
1550 		m->object = new_object;
1551 		m->pindex -= offidxstart;
1552 		if (vm_radix_insert(&new_object->cache, m))
1553 			vm_page_cache_turn_free(m);
1554 	}
1555 	mtx_unlock(&vm_page_queue_free_mtx);
1556 }
1557 
1558 /*
1559  *	Returns TRUE if a cached page is associated with the given object and
1560  *	offset, and FALSE otherwise.
1561  *
1562  *	The object must be locked.
1563  */
1564 boolean_t
1565 vm_page_is_cached(vm_object_t object, vm_pindex_t pindex)
1566 {
1567 	vm_page_t m;
1568 
1569 	/*
1570 	 * Insertion into an object's collection of cached pages requires the
1571 	 * object to be locked.  Therefore, if the object is locked and the
1572 	 * object's collection is empty, there is no need to acquire the free
1573 	 * page queues lock in order to prove that the specified page doesn't
1574 	 * exist.
1575 	 */
1576 	VM_OBJECT_ASSERT_WLOCKED(object);
1577 	if (__predict_true(vm_object_cache_is_empty(object)))
1578 		return (FALSE);
1579 	mtx_lock(&vm_page_queue_free_mtx);
1580 	m = vm_page_cache_lookup(object, pindex);
1581 	mtx_unlock(&vm_page_queue_free_mtx);
1582 	return (m != NULL);
1583 }
1584 
1585 /*
1586  *	vm_page_alloc:
1587  *
1588  *	Allocate and return a page that is associated with the specified
1589  *	object and offset pair.  By default, this page is exclusive busied.
1590  *
1591  *	The caller must always specify an allocation class.
1592  *
1593  *	allocation classes:
1594  *	VM_ALLOC_NORMAL		normal process request
1595  *	VM_ALLOC_SYSTEM		system *really* needs a page
1596  *	VM_ALLOC_INTERRUPT	interrupt time request
1597  *
1598  *	optional allocation flags:
1599  *	VM_ALLOC_COUNT(number)	the number of additional pages that the caller
1600  *				intends to allocate
1601  *	VM_ALLOC_IFCACHED	return page only if it is cached
1602  *	VM_ALLOC_IFNOTCACHED	return NULL, do not reactivate if the page
1603  *				is cached
1604  *	VM_ALLOC_NOBUSY		do not exclusive busy the page
1605  *	VM_ALLOC_NODUMP		do not include the page in a kernel core dump
1606  *	VM_ALLOC_NOOBJ		page is not associated with an object and
1607  *				should not be exclusive busy
1608  *	VM_ALLOC_SBUSY		shared busy the allocated page
1609  *	VM_ALLOC_WIRED		wire the allocated page
1610  *	VM_ALLOC_ZERO		prefer a zeroed page
1611  *
1612  *	This routine may not sleep.
1613  */
1614 vm_page_t
1615 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
1616 {
1617 	struct vnode *vp = NULL;
1618 	vm_object_t m_object;
1619 	vm_page_t m, mpred;
1620 	int flags, req_class;
1621 
1622 	mpred = 0;	/* XXX: pacify gcc */
1623 	KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) &&
1624 	    (object != NULL || (req & VM_ALLOC_SBUSY) == 0) &&
1625 	    ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
1626 	    (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
1627 	    ("vm_page_alloc: inconsistent object(%p)/req(%x)", (void *)object,
1628 	    req));
1629 	if (object != NULL)
1630 		VM_OBJECT_ASSERT_WLOCKED(object);
1631 
1632 	req_class = req & VM_ALLOC_CLASS_MASK;
1633 
1634 	/*
1635 	 * The page daemon is allowed to dig deeper into the free page list.
1636 	 */
1637 	if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1638 		req_class = VM_ALLOC_SYSTEM;
1639 
1640 	if (object != NULL) {
1641 		mpred = vm_radix_lookup_le(&object->rtree, pindex);
1642 		KASSERT(mpred == NULL || mpred->pindex != pindex,
1643 		   ("vm_page_alloc: pindex already allocated"));
1644 	}
1645 
1646 	/*
1647 	 * The page allocation request can came from consumers which already
1648 	 * hold the free page queue mutex, like vm_page_insert() in
1649 	 * vm_page_cache().
1650 	 */
1651 	mtx_lock_flags(&vm_page_queue_free_mtx, MTX_RECURSE);
1652 	if (vm_cnt.v_free_count + vm_cnt.v_cache_count > vm_cnt.v_free_reserved ||
1653 	    (req_class == VM_ALLOC_SYSTEM &&
1654 	    vm_cnt.v_free_count + vm_cnt.v_cache_count > vm_cnt.v_interrupt_free_min) ||
1655 	    (req_class == VM_ALLOC_INTERRUPT &&
1656 	    vm_cnt.v_free_count + vm_cnt.v_cache_count > 0)) {
1657 		/*
1658 		 * Allocate from the free queue if the number of free pages
1659 		 * exceeds the minimum for the request class.
1660 		 */
1661 		if (object != NULL &&
1662 		    (m = vm_page_cache_lookup(object, pindex)) != NULL) {
1663 			if ((req & VM_ALLOC_IFNOTCACHED) != 0) {
1664 				mtx_unlock(&vm_page_queue_free_mtx);
1665 				return (NULL);
1666 			}
1667 			if (vm_phys_unfree_page(m))
1668 				vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, 0);
1669 #if VM_NRESERVLEVEL > 0
1670 			else if (!vm_reserv_reactivate_page(m))
1671 #else
1672 			else
1673 #endif
1674 				panic("vm_page_alloc: cache page %p is missing"
1675 				    " from the free queue", m);
1676 		} else if ((req & VM_ALLOC_IFCACHED) != 0) {
1677 			mtx_unlock(&vm_page_queue_free_mtx);
1678 			return (NULL);
1679 #if VM_NRESERVLEVEL > 0
1680 		} else if (object == NULL || (object->flags & (OBJ_COLORED |
1681 		    OBJ_FICTITIOUS)) != OBJ_COLORED || (m =
1682 		    vm_reserv_alloc_page(object, pindex, mpred)) == NULL) {
1683 #else
1684 		} else {
1685 #endif
1686 			m = vm_phys_alloc_pages(object != NULL ?
1687 			    VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0);
1688 #if VM_NRESERVLEVEL > 0
1689 			if (m == NULL && vm_reserv_reclaim_inactive()) {
1690 				m = vm_phys_alloc_pages(object != NULL ?
1691 				    VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT,
1692 				    0);
1693 			}
1694 #endif
1695 		}
1696 	} else {
1697 		/*
1698 		 * Not allocatable, give up.
1699 		 */
1700 		mtx_unlock(&vm_page_queue_free_mtx);
1701 		atomic_add_int(&vm_pageout_deficit,
1702 		    max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
1703 		pagedaemon_wakeup();
1704 		return (NULL);
1705 	}
1706 
1707 	/*
1708 	 *  At this point we had better have found a good page.
1709 	 */
1710 	KASSERT(m != NULL, ("vm_page_alloc: missing page"));
1711 	KASSERT(m->queue == PQ_NONE,
1712 	    ("vm_page_alloc: page %p has unexpected queue %d", m, m->queue));
1713 	KASSERT(m->wire_count == 0, ("vm_page_alloc: page %p is wired", m));
1714 	KASSERT(m->hold_count == 0, ("vm_page_alloc: page %p is held", m));
1715 	KASSERT(!vm_page_sbusied(m),
1716 	    ("vm_page_alloc: page %p is busy", m));
1717 	KASSERT(m->dirty == 0, ("vm_page_alloc: page %p is dirty", m));
1718 	KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1719 	    ("vm_page_alloc: page %p has unexpected memattr %d", m,
1720 	    pmap_page_get_memattr(m)));
1721 	if ((m->flags & PG_CACHED) != 0) {
1722 		KASSERT((m->flags & PG_ZERO) == 0,
1723 		    ("vm_page_alloc: cached page %p is PG_ZERO", m));
1724 		KASSERT(m->valid != 0,
1725 		    ("vm_page_alloc: cached page %p is invalid", m));
1726 		if (m->object == object && m->pindex == pindex)
1727 			vm_cnt.v_reactivated++;
1728 		else
1729 			m->valid = 0;
1730 		m_object = m->object;
1731 		vm_page_cache_remove(m);
1732 		if (m_object->type == OBJT_VNODE &&
1733 		    vm_object_cache_is_empty(m_object))
1734 			vp = m_object->handle;
1735 	} else {
1736 		KASSERT(m->valid == 0,
1737 		    ("vm_page_alloc: free page %p is valid", m));
1738 		vm_phys_freecnt_adj(m, -1);
1739 		if ((m->flags & PG_ZERO) != 0)
1740 			vm_page_zero_count--;
1741 	}
1742 	mtx_unlock(&vm_page_queue_free_mtx);
1743 
1744 	/*
1745 	 * Initialize the page.  Only the PG_ZERO flag is inherited.
1746 	 */
1747 	flags = 0;
1748 	if ((req & VM_ALLOC_ZERO) != 0)
1749 		flags = PG_ZERO;
1750 	flags &= m->flags;
1751 	if ((req & VM_ALLOC_NODUMP) != 0)
1752 		flags |= PG_NODUMP;
1753 	m->flags = flags;
1754 	m->aflags = 0;
1755 	m->oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ?
1756 	    VPO_UNMANAGED : 0;
1757 	m->busy_lock = VPB_UNBUSIED;
1758 	if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ | VM_ALLOC_SBUSY)) == 0)
1759 		m->busy_lock = VPB_SINGLE_EXCLUSIVER;
1760 	if ((req & VM_ALLOC_SBUSY) != 0)
1761 		m->busy_lock = VPB_SHARERS_WORD(1);
1762 	if (req & VM_ALLOC_WIRED) {
1763 		/*
1764 		 * The page lock is not required for wiring a page until that
1765 		 * page is inserted into the object.
1766 		 */
1767 		atomic_add_int(&vm_cnt.v_wire_count, 1);
1768 		m->wire_count = 1;
1769 	}
1770 	m->act_count = 0;
1771 
1772 	if (object != NULL) {
1773 		if (vm_page_insert_after(m, object, pindex, mpred)) {
1774 			/* See the comment below about hold count. */
1775 			if (vp != NULL)
1776 				vdrop(vp);
1777 			pagedaemon_wakeup();
1778 			if (req & VM_ALLOC_WIRED) {
1779 				atomic_subtract_int(&vm_cnt.v_wire_count, 1);
1780 				m->wire_count = 0;
1781 			}
1782 			m->object = NULL;
1783 			m->oflags = VPO_UNMANAGED;
1784 			m->busy_lock = VPB_UNBUSIED;
1785 			vm_page_free(m);
1786 			return (NULL);
1787 		}
1788 
1789 		/* Ignore device objects; the pager sets "memattr" for them. */
1790 		if (object->memattr != VM_MEMATTR_DEFAULT &&
1791 		    (object->flags & OBJ_FICTITIOUS) == 0)
1792 			pmap_page_set_memattr(m, object->memattr);
1793 	} else
1794 		m->pindex = pindex;
1795 
1796 	/*
1797 	 * The following call to vdrop() must come after the above call
1798 	 * to vm_page_insert() in case both affect the same object and
1799 	 * vnode.  Otherwise, the affected vnode's hold count could
1800 	 * temporarily become zero.
1801 	 */
1802 	if (vp != NULL)
1803 		vdrop(vp);
1804 
1805 	/*
1806 	 * Don't wakeup too often - wakeup the pageout daemon when
1807 	 * we would be nearly out of memory.
1808 	 */
1809 	if (vm_paging_needed())
1810 		pagedaemon_wakeup();
1811 
1812 	return (m);
1813 }
1814 
1815 static void
1816 vm_page_alloc_contig_vdrop(struct spglist *lst)
1817 {
1818 
1819 	while (!SLIST_EMPTY(lst)) {
1820 		vdrop((struct vnode *)SLIST_FIRST(lst)-> plinks.s.pv);
1821 		SLIST_REMOVE_HEAD(lst, plinks.s.ss);
1822 	}
1823 }
1824 
1825 /*
1826  *	vm_page_alloc_contig:
1827  *
1828  *	Allocate a contiguous set of physical pages of the given size "npages"
1829  *	from the free lists.  All of the physical pages must be at or above
1830  *	the given physical address "low" and below the given physical address
1831  *	"high".  The given value "alignment" determines the alignment of the
1832  *	first physical page in the set.  If the given value "boundary" is
1833  *	non-zero, then the set of physical pages cannot cross any physical
1834  *	address boundary that is a multiple of that value.  Both "alignment"
1835  *	and "boundary" must be a power of two.
1836  *
1837  *	If the specified memory attribute, "memattr", is VM_MEMATTR_DEFAULT,
1838  *	then the memory attribute setting for the physical pages is configured
1839  *	to the object's memory attribute setting.  Otherwise, the memory
1840  *	attribute setting for the physical pages is configured to "memattr",
1841  *	overriding the object's memory attribute setting.  However, if the
1842  *	object's memory attribute setting is not VM_MEMATTR_DEFAULT, then the
1843  *	memory attribute setting for the physical pages cannot be configured
1844  *	to VM_MEMATTR_DEFAULT.
1845  *
1846  *	The caller must always specify an allocation class.
1847  *
1848  *	allocation classes:
1849  *	VM_ALLOC_NORMAL		normal process request
1850  *	VM_ALLOC_SYSTEM		system *really* needs a page
1851  *	VM_ALLOC_INTERRUPT	interrupt time request
1852  *
1853  *	optional allocation flags:
1854  *	VM_ALLOC_NOBUSY		do not exclusive busy the page
1855  *	VM_ALLOC_NODUMP		do not include the page in a kernel core dump
1856  *	VM_ALLOC_NOOBJ		page is not associated with an object and
1857  *				should not be exclusive busy
1858  *	VM_ALLOC_SBUSY		shared busy the allocated page
1859  *	VM_ALLOC_WIRED		wire the allocated page
1860  *	VM_ALLOC_ZERO		prefer a zeroed page
1861  *
1862  *	This routine may not sleep.
1863  */
1864 vm_page_t
1865 vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req,
1866     u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
1867     vm_paddr_t boundary, vm_memattr_t memattr)
1868 {
1869 	struct vnode *drop;
1870 	struct spglist deferred_vdrop_list;
1871 	vm_page_t m, m_tmp, m_ret;
1872 	u_int flags;
1873 	int req_class;
1874 
1875 	KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) &&
1876 	    (object != NULL || (req & VM_ALLOC_SBUSY) == 0) &&
1877 	    ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
1878 	    (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
1879 	    ("vm_page_alloc: inconsistent object(%p)/req(%x)", (void *)object,
1880 	    req));
1881 	if (object != NULL) {
1882 		VM_OBJECT_ASSERT_WLOCKED(object);
1883 		KASSERT(object->type == OBJT_PHYS,
1884 		    ("vm_page_alloc_contig: object %p isn't OBJT_PHYS",
1885 		    object));
1886 	}
1887 	KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero"));
1888 	req_class = req & VM_ALLOC_CLASS_MASK;
1889 
1890 	/*
1891 	 * The page daemon is allowed to dig deeper into the free page list.
1892 	 */
1893 	if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1894 		req_class = VM_ALLOC_SYSTEM;
1895 
1896 	SLIST_INIT(&deferred_vdrop_list);
1897 	mtx_lock(&vm_page_queue_free_mtx);
1898 	if (vm_cnt.v_free_count + vm_cnt.v_cache_count >= npages +
1899 	    vm_cnt.v_free_reserved || (req_class == VM_ALLOC_SYSTEM &&
1900 	    vm_cnt.v_free_count + vm_cnt.v_cache_count >= npages +
1901 	    vm_cnt.v_interrupt_free_min) || (req_class == VM_ALLOC_INTERRUPT &&
1902 	    vm_cnt.v_free_count + vm_cnt.v_cache_count >= npages)) {
1903 #if VM_NRESERVLEVEL > 0
1904 retry:
1905 		if (object == NULL || (object->flags & OBJ_COLORED) == 0 ||
1906 		    (m_ret = vm_reserv_alloc_contig(object, pindex, npages,
1907 		    low, high, alignment, boundary)) == NULL)
1908 #endif
1909 			m_ret = vm_phys_alloc_contig(npages, low, high,
1910 			    alignment, boundary);
1911 	} else {
1912 		mtx_unlock(&vm_page_queue_free_mtx);
1913 		atomic_add_int(&vm_pageout_deficit, npages);
1914 		pagedaemon_wakeup();
1915 		return (NULL);
1916 	}
1917 	if (m_ret != NULL)
1918 		for (m = m_ret; m < &m_ret[npages]; m++) {
1919 			drop = vm_page_alloc_init(m);
1920 			if (drop != NULL) {
1921 				/*
1922 				 * Enqueue the vnode for deferred vdrop().
1923 				 */
1924 				m->plinks.s.pv = drop;
1925 				SLIST_INSERT_HEAD(&deferred_vdrop_list, m,
1926 				    plinks.s.ss);
1927 			}
1928 		}
1929 	else {
1930 #if VM_NRESERVLEVEL > 0
1931 		if (vm_reserv_reclaim_contig(npages, low, high, alignment,
1932 		    boundary))
1933 			goto retry;
1934 #endif
1935 	}
1936 	mtx_unlock(&vm_page_queue_free_mtx);
1937 	if (m_ret == NULL)
1938 		return (NULL);
1939 
1940 	/*
1941 	 * Initialize the pages.  Only the PG_ZERO flag is inherited.
1942 	 */
1943 	flags = 0;
1944 	if ((req & VM_ALLOC_ZERO) != 0)
1945 		flags = PG_ZERO;
1946 	if ((req & VM_ALLOC_NODUMP) != 0)
1947 		flags |= PG_NODUMP;
1948 	if ((req & VM_ALLOC_WIRED) != 0)
1949 		atomic_add_int(&vm_cnt.v_wire_count, npages);
1950 	if (object != NULL) {
1951 		if (object->memattr != VM_MEMATTR_DEFAULT &&
1952 		    memattr == VM_MEMATTR_DEFAULT)
1953 			memattr = object->memattr;
1954 	}
1955 	for (m = m_ret; m < &m_ret[npages]; m++) {
1956 		m->aflags = 0;
1957 		m->flags = (m->flags | PG_NODUMP) & flags;
1958 		m->busy_lock = VPB_UNBUSIED;
1959 		if (object != NULL) {
1960 			if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0)
1961 				m->busy_lock = VPB_SINGLE_EXCLUSIVER;
1962 			if ((req & VM_ALLOC_SBUSY) != 0)
1963 				m->busy_lock = VPB_SHARERS_WORD(1);
1964 		}
1965 		if ((req & VM_ALLOC_WIRED) != 0)
1966 			m->wire_count = 1;
1967 		/* Unmanaged pages don't use "act_count". */
1968 		m->oflags = VPO_UNMANAGED;
1969 		if (object != NULL) {
1970 			if (vm_page_insert(m, object, pindex)) {
1971 				vm_page_alloc_contig_vdrop(
1972 				    &deferred_vdrop_list);
1973 				if (vm_paging_needed())
1974 					pagedaemon_wakeup();
1975 				if ((req & VM_ALLOC_WIRED) != 0)
1976 					atomic_subtract_int(&vm_cnt.v_wire_count,
1977 					    npages);
1978 				for (m_tmp = m, m = m_ret;
1979 				    m < &m_ret[npages]; m++) {
1980 					if ((req & VM_ALLOC_WIRED) != 0)
1981 						m->wire_count = 0;
1982 					if (m >= m_tmp) {
1983 						m->object = NULL;
1984 						m->oflags |= VPO_UNMANAGED;
1985 					}
1986 					m->busy_lock = VPB_UNBUSIED;
1987 					vm_page_free(m);
1988 				}
1989 				return (NULL);
1990 			}
1991 		} else
1992 			m->pindex = pindex;
1993 		if (memattr != VM_MEMATTR_DEFAULT)
1994 			pmap_page_set_memattr(m, memattr);
1995 		pindex++;
1996 	}
1997 	vm_page_alloc_contig_vdrop(&deferred_vdrop_list);
1998 	if (vm_paging_needed())
1999 		pagedaemon_wakeup();
2000 	return (m_ret);
2001 }
2002 
2003 /*
2004  * Initialize a page that has been freshly dequeued from a freelist.
2005  * The caller has to drop the vnode returned, if it is not NULL.
2006  *
2007  * This function may only be used to initialize unmanaged pages.
2008  *
2009  * To be called with vm_page_queue_free_mtx held.
2010  */
2011 static struct vnode *
2012 vm_page_alloc_init(vm_page_t m)
2013 {
2014 	struct vnode *drop;
2015 	vm_object_t m_object;
2016 
2017 	KASSERT(m->queue == PQ_NONE,
2018 	    ("vm_page_alloc_init: page %p has unexpected queue %d",
2019 	    m, m->queue));
2020 	KASSERT(m->wire_count == 0,
2021 	    ("vm_page_alloc_init: page %p is wired", m));
2022 	KASSERT(m->hold_count == 0,
2023 	    ("vm_page_alloc_init: page %p is held", m));
2024 	KASSERT(!vm_page_sbusied(m),
2025 	    ("vm_page_alloc_init: page %p is busy", m));
2026 	KASSERT(m->dirty == 0,
2027 	    ("vm_page_alloc_init: page %p is dirty", m));
2028 	KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
2029 	    ("vm_page_alloc_init: page %p has unexpected memattr %d",
2030 	    m, pmap_page_get_memattr(m)));
2031 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
2032 	drop = NULL;
2033 	if ((m->flags & PG_CACHED) != 0) {
2034 		KASSERT((m->flags & PG_ZERO) == 0,
2035 		    ("vm_page_alloc_init: cached page %p is PG_ZERO", m));
2036 		m->valid = 0;
2037 		m_object = m->object;
2038 		vm_page_cache_remove(m);
2039 		if (m_object->type == OBJT_VNODE &&
2040 		    vm_object_cache_is_empty(m_object))
2041 			drop = m_object->handle;
2042 	} else {
2043 		KASSERT(m->valid == 0,
2044 		    ("vm_page_alloc_init: free page %p is valid", m));
2045 		vm_phys_freecnt_adj(m, -1);
2046 		if ((m->flags & PG_ZERO) != 0)
2047 			vm_page_zero_count--;
2048 	}
2049 	return (drop);
2050 }
2051 
2052 /*
2053  * 	vm_page_alloc_freelist:
2054  *
2055  *	Allocate a physical page from the specified free page list.
2056  *
2057  *	The caller must always specify an allocation class.
2058  *
2059  *	allocation classes:
2060  *	VM_ALLOC_NORMAL		normal process request
2061  *	VM_ALLOC_SYSTEM		system *really* needs a page
2062  *	VM_ALLOC_INTERRUPT	interrupt time request
2063  *
2064  *	optional allocation flags:
2065  *	VM_ALLOC_COUNT(number)	the number of additional pages that the caller
2066  *				intends to allocate
2067  *	VM_ALLOC_WIRED		wire the allocated page
2068  *	VM_ALLOC_ZERO		prefer a zeroed page
2069  *
2070  *	This routine may not sleep.
2071  */
2072 vm_page_t
2073 vm_page_alloc_freelist(int flind, int req)
2074 {
2075 	struct vnode *drop;
2076 	vm_page_t m;
2077 	u_int flags;
2078 	int req_class;
2079 
2080 	req_class = req & VM_ALLOC_CLASS_MASK;
2081 
2082 	/*
2083 	 * The page daemon is allowed to dig deeper into the free page list.
2084 	 */
2085 	if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
2086 		req_class = VM_ALLOC_SYSTEM;
2087 
2088 	/*
2089 	 * Do not allocate reserved pages unless the req has asked for it.
2090 	 */
2091 	mtx_lock_flags(&vm_page_queue_free_mtx, MTX_RECURSE);
2092 	if (vm_cnt.v_free_count + vm_cnt.v_cache_count > vm_cnt.v_free_reserved ||
2093 	    (req_class == VM_ALLOC_SYSTEM &&
2094 	    vm_cnt.v_free_count + vm_cnt.v_cache_count > vm_cnt.v_interrupt_free_min) ||
2095 	    (req_class == VM_ALLOC_INTERRUPT &&
2096 	    vm_cnt.v_free_count + vm_cnt.v_cache_count > 0))
2097 		m = vm_phys_alloc_freelist_pages(flind, VM_FREEPOOL_DIRECT, 0);
2098 	else {
2099 		mtx_unlock(&vm_page_queue_free_mtx);
2100 		atomic_add_int(&vm_pageout_deficit,
2101 		    max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
2102 		pagedaemon_wakeup();
2103 		return (NULL);
2104 	}
2105 	if (m == NULL) {
2106 		mtx_unlock(&vm_page_queue_free_mtx);
2107 		return (NULL);
2108 	}
2109 	drop = vm_page_alloc_init(m);
2110 	mtx_unlock(&vm_page_queue_free_mtx);
2111 
2112 	/*
2113 	 * Initialize the page.  Only the PG_ZERO flag is inherited.
2114 	 */
2115 	m->aflags = 0;
2116 	flags = 0;
2117 	if ((req & VM_ALLOC_ZERO) != 0)
2118 		flags = PG_ZERO;
2119 	m->flags &= flags;
2120 	if ((req & VM_ALLOC_WIRED) != 0) {
2121 		/*
2122 		 * The page lock is not required for wiring a page that does
2123 		 * not belong to an object.
2124 		 */
2125 		atomic_add_int(&vm_cnt.v_wire_count, 1);
2126 		m->wire_count = 1;
2127 	}
2128 	/* Unmanaged pages don't use "act_count". */
2129 	m->oflags = VPO_UNMANAGED;
2130 	if (drop != NULL)
2131 		vdrop(drop);
2132 	if (vm_paging_needed())
2133 		pagedaemon_wakeup();
2134 	return (m);
2135 }
2136 
2137 #define	VPSC_ANY	0	/* No restrictions. */
2138 #define	VPSC_NORESERV	1	/* Skip reservations; implies VPSC_NOSUPER. */
2139 #define	VPSC_NOSUPER	2	/* Skip superpages. */
2140 
2141 /*
2142  *	vm_page_scan_contig:
2143  *
2144  *	Scan vm_page_array[] between the specified entries "m_start" and
2145  *	"m_end" for a run of contiguous physical pages that satisfy the
2146  *	specified conditions, and return the lowest page in the run.  The
2147  *	specified "alignment" determines the alignment of the lowest physical
2148  *	page in the run.  If the specified "boundary" is non-zero, then the
2149  *	run of physical pages cannot span a physical address that is a
2150  *	multiple of "boundary".
2151  *
2152  *	"m_end" is never dereferenced, so it need not point to a vm_page
2153  *	structure within vm_page_array[].
2154  *
2155  *	"npages" must be greater than zero.  "m_start" and "m_end" must not
2156  *	span a hole (or discontiguity) in the physical address space.  Both
2157  *	"alignment" and "boundary" must be a power of two.
2158  */
2159 vm_page_t
2160 vm_page_scan_contig(u_long npages, vm_page_t m_start, vm_page_t m_end,
2161     u_long alignment, vm_paddr_t boundary, int options)
2162 {
2163 	struct mtx *m_mtx, *new_mtx;
2164 	vm_object_t object;
2165 	vm_paddr_t pa;
2166 	vm_page_t m, m_run;
2167 #if VM_NRESERVLEVEL > 0
2168 	int level;
2169 #endif
2170 	int m_inc, order, run_ext, run_len;
2171 
2172 	KASSERT(npages > 0, ("npages is 0"));
2173 	KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
2174 	KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
2175 	m_run = NULL;
2176 	run_len = 0;
2177 	m_mtx = NULL;
2178 	for (m = m_start; m < m_end && run_len < npages; m += m_inc) {
2179 		KASSERT((m->flags & (PG_FICTITIOUS | PG_MARKER)) == 0,
2180 		    ("page %p is PG_FICTITIOUS or PG_MARKER", m));
2181 
2182 		/*
2183 		 * If the current page would be the start of a run, check its
2184 		 * physical address against the end, alignment, and boundary
2185 		 * conditions.  If it doesn't satisfy these conditions, either
2186 		 * terminate the scan or advance to the next page that
2187 		 * satisfies the failed condition.
2188 		 */
2189 		if (run_len == 0) {
2190 			KASSERT(m_run == NULL, ("m_run != NULL"));
2191 			if (m + npages > m_end)
2192 				break;
2193 			pa = VM_PAGE_TO_PHYS(m);
2194 			if ((pa & (alignment - 1)) != 0) {
2195 				m_inc = atop(roundup2(pa, alignment) - pa);
2196 				continue;
2197 			}
2198 			if (rounddown2(pa ^ (pa + ptoa(npages) - 1),
2199 			    boundary) != 0) {
2200 				m_inc = atop(roundup2(pa, boundary) - pa);
2201 				continue;
2202 			}
2203 		} else
2204 			KASSERT(m_run != NULL, ("m_run == NULL"));
2205 
2206 		/*
2207 		 * Avoid releasing and reacquiring the same page lock.
2208 		 */
2209 		new_mtx = vm_page_lockptr(m);
2210 		if (m_mtx != new_mtx) {
2211 			if (m_mtx != NULL)
2212 				mtx_unlock(m_mtx);
2213 			m_mtx = new_mtx;
2214 			mtx_lock(m_mtx);
2215 		}
2216 		m_inc = 1;
2217 retry:
2218 		if (m->wire_count != 0 || m->hold_count != 0)
2219 			run_ext = 0;
2220 #if VM_NRESERVLEVEL > 0
2221 		else if ((level = vm_reserv_level(m)) >= 0 &&
2222 		    (options & VPSC_NORESERV) != 0) {
2223 			run_ext = 0;
2224 			/* Advance to the end of the reservation. */
2225 			pa = VM_PAGE_TO_PHYS(m);
2226 			m_inc = atop(roundup2(pa + 1, vm_reserv_size(level)) -
2227 			    pa);
2228 		}
2229 #endif
2230 		else if ((object = m->object) != NULL) {
2231 			/*
2232 			 * The page is considered eligible for relocation if
2233 			 * and only if it could be laundered or reclaimed by
2234 			 * the page daemon.
2235 			 */
2236 			if (!VM_OBJECT_TRYRLOCK(object)) {
2237 				mtx_unlock(m_mtx);
2238 				VM_OBJECT_RLOCK(object);
2239 				mtx_lock(m_mtx);
2240 				if (m->object != object) {
2241 					/*
2242 					 * The page may have been freed.
2243 					 */
2244 					VM_OBJECT_RUNLOCK(object);
2245 					goto retry;
2246 				} else if (m->wire_count != 0 ||
2247 				    m->hold_count != 0) {
2248 					run_ext = 0;
2249 					goto unlock;
2250 				}
2251 			}
2252 			KASSERT((m->flags & PG_UNHOLDFREE) == 0,
2253 			    ("page %p is PG_UNHOLDFREE", m));
2254 			/* Don't care: PG_NODUMP, PG_WINATCFLS, PG_ZERO. */
2255 			if (object->type != OBJT_DEFAULT &&
2256 			    object->type != OBJT_SWAP &&
2257 			    object->type != OBJT_VNODE)
2258 				run_ext = 0;
2259 			else if ((m->flags & PG_CACHED) != 0 ||
2260 			    m != vm_page_lookup(object, m->pindex)) {
2261 				/*
2262 				 * The page is cached or recently converted
2263 				 * from cached to free.
2264 				 */
2265 #if VM_NRESERVLEVEL > 0
2266 				if (level >= 0) {
2267 					/*
2268 					 * The page is reserved.  Extend the
2269 					 * current run by one page.
2270 					 */
2271 					run_ext = 1;
2272 				} else
2273 #endif
2274 				if ((order = m->order) < VM_NFREEORDER) {
2275 					/*
2276 					 * The page is enqueued in the
2277 					 * physical memory allocator's cache/
2278 					 * free page queues.  Moreover, it is
2279 					 * the first page in a power-of-two-
2280 					 * sized run of contiguous cache/free
2281 					 * pages.  Add these pages to the end
2282 					 * of the current run, and jump
2283 					 * ahead.
2284 					 */
2285 					run_ext = 1 << order;
2286 					m_inc = 1 << order;
2287 				} else
2288 					run_ext = 0;
2289 #if VM_NRESERVLEVEL > 0
2290 			} else if ((options & VPSC_NOSUPER) != 0 &&
2291 			    (level = vm_reserv_level_iffullpop(m)) >= 0) {
2292 				run_ext = 0;
2293 				/* Advance to the end of the superpage. */
2294 				pa = VM_PAGE_TO_PHYS(m);
2295 				m_inc = atop(roundup2(pa + 1,
2296 				    vm_reserv_size(level)) - pa);
2297 #endif
2298 			} else if (object->memattr == VM_MEMATTR_DEFAULT &&
2299 			    m->queue != PQ_NONE && !vm_page_busied(m)) {
2300 				/*
2301 				 * The page is allocated but eligible for
2302 				 * relocation.  Extend the current run by one
2303 				 * page.
2304 				 */
2305 				KASSERT(pmap_page_get_memattr(m) ==
2306 				    VM_MEMATTR_DEFAULT,
2307 				    ("page %p has an unexpected memattr", m));
2308 				KASSERT((m->oflags & (VPO_SWAPINPROG |
2309 				    VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0,
2310 				    ("page %p has unexpected oflags", m));
2311 				/* Don't care: VPO_NOSYNC. */
2312 				run_ext = 1;
2313 			} else
2314 				run_ext = 0;
2315 unlock:
2316 			VM_OBJECT_RUNLOCK(object);
2317 #if VM_NRESERVLEVEL > 0
2318 		} else if (level >= 0) {
2319 			/*
2320 			 * The page is reserved but not yet allocated.  In
2321 			 * other words, it is still cached or free.  Extend
2322 			 * the current run by one page.
2323 			 */
2324 			run_ext = 1;
2325 #endif
2326 		} else if ((order = m->order) < VM_NFREEORDER) {
2327 			/*
2328 			 * The page is enqueued in the physical memory
2329 			 * allocator's cache/free page queues.  Moreover, it
2330 			 * is the first page in a power-of-two-sized run of
2331 			 * contiguous cache/free pages.  Add these pages to
2332 			 * the end of the current run, and jump ahead.
2333 			 */
2334 			run_ext = 1 << order;
2335 			m_inc = 1 << order;
2336 		} else {
2337 			/*
2338 			 * Skip the page for one of the following reasons: (1)
2339 			 * It is enqueued in the physical memory allocator's
2340 			 * cache/free page queues.  However, it is not the
2341 			 * first page in a run of contiguous cache/free pages.
2342 			 * (This case rarely occurs because the scan is
2343 			 * performed in ascending order.) (2) It is not
2344 			 * reserved, and it is transitioning from free to
2345 			 * allocated.  (Conversely, the transition from
2346 			 * allocated to free for managed pages is blocked by
2347 			 * the page lock.) (3) It is allocated but not
2348 			 * contained by an object and not wired, e.g.,
2349 			 * allocated by Xen's balloon driver.
2350 			 */
2351 			run_ext = 0;
2352 		}
2353 
2354 		/*
2355 		 * Extend or reset the current run of pages.
2356 		 */
2357 		if (run_ext > 0) {
2358 			if (run_len == 0)
2359 				m_run = m;
2360 			run_len += run_ext;
2361 		} else {
2362 			if (run_len > 0) {
2363 				m_run = NULL;
2364 				run_len = 0;
2365 			}
2366 		}
2367 	}
2368 	if (m_mtx != NULL)
2369 		mtx_unlock(m_mtx);
2370 	if (run_len >= npages)
2371 		return (m_run);
2372 	return (NULL);
2373 }
2374 
2375 /*
2376  *	vm_page_reclaim_run:
2377  *
2378  *	Try to relocate each of the allocated virtual pages within the
2379  *	specified run of physical pages to a new physical address.  Free the
2380  *	physical pages underlying the relocated virtual pages.  A virtual page
2381  *	is relocatable if and only if it could be laundered or reclaimed by
2382  *	the page daemon.  Whenever possible, a virtual page is relocated to a
2383  *	physical address above "high".
2384  *
2385  *	Returns 0 if every physical page within the run was already free or
2386  *	just freed by a successful relocation.  Otherwise, returns a non-zero
2387  *	value indicating why the last attempt to relocate a virtual page was
2388  *	unsuccessful.
2389  *
2390  *	"req_class" must be an allocation class.
2391  */
2392 static int
2393 vm_page_reclaim_run(int req_class, u_long npages, vm_page_t m_run,
2394     vm_paddr_t high)
2395 {
2396 	struct mtx *m_mtx, *new_mtx;
2397 	struct spglist free;
2398 	vm_object_t object;
2399 	vm_paddr_t pa;
2400 	vm_page_t m, m_end, m_new;
2401 	int error, order, req;
2402 
2403 	KASSERT((req_class & VM_ALLOC_CLASS_MASK) == req_class,
2404 	    ("req_class is not an allocation class"));
2405 	SLIST_INIT(&free);
2406 	error = 0;
2407 	m = m_run;
2408 	m_end = m_run + npages;
2409 	m_mtx = NULL;
2410 	for (; error == 0 && m < m_end; m++) {
2411 		KASSERT((m->flags & (PG_FICTITIOUS | PG_MARKER)) == 0,
2412 		    ("page %p is PG_FICTITIOUS or PG_MARKER", m));
2413 
2414 		/*
2415 		 * Avoid releasing and reacquiring the same page lock.
2416 		 */
2417 		new_mtx = vm_page_lockptr(m);
2418 		if (m_mtx != new_mtx) {
2419 			if (m_mtx != NULL)
2420 				mtx_unlock(m_mtx);
2421 			m_mtx = new_mtx;
2422 			mtx_lock(m_mtx);
2423 		}
2424 retry:
2425 		if (m->wire_count != 0 || m->hold_count != 0)
2426 			error = EBUSY;
2427 		else if ((object = m->object) != NULL) {
2428 			/*
2429 			 * The page is relocated if and only if it could be
2430 			 * laundered or reclaimed by the page daemon.
2431 			 */
2432 			if (!VM_OBJECT_TRYWLOCK(object)) {
2433 				mtx_unlock(m_mtx);
2434 				VM_OBJECT_WLOCK(object);
2435 				mtx_lock(m_mtx);
2436 				if (m->object != object) {
2437 					/*
2438 					 * The page may have been freed.
2439 					 */
2440 					VM_OBJECT_WUNLOCK(object);
2441 					goto retry;
2442 				} else if (m->wire_count != 0 ||
2443 				    m->hold_count != 0) {
2444 					error = EBUSY;
2445 					goto unlock;
2446 				}
2447 			}
2448 			KASSERT((m->flags & PG_UNHOLDFREE) == 0,
2449 			    ("page %p is PG_UNHOLDFREE", m));
2450 			/* Don't care: PG_NODUMP, PG_WINATCFLS, PG_ZERO. */
2451 			if (object->type != OBJT_DEFAULT &&
2452 			    object->type != OBJT_SWAP &&
2453 			    object->type != OBJT_VNODE)
2454 				error = EINVAL;
2455 			else if ((m->flags & PG_CACHED) != 0 ||
2456 			    m != vm_page_lookup(object, m->pindex)) {
2457 				/*
2458 				 * The page is cached or recently converted
2459 				 * from cached to free.
2460 				 */
2461 				VM_OBJECT_WUNLOCK(object);
2462 				goto cached;
2463 			} else if (object->memattr != VM_MEMATTR_DEFAULT)
2464 				error = EINVAL;
2465 			else if (m->queue != PQ_NONE && !vm_page_busied(m)) {
2466 				KASSERT(pmap_page_get_memattr(m) ==
2467 				    VM_MEMATTR_DEFAULT,
2468 				    ("page %p has an unexpected memattr", m));
2469 				KASSERT((m->oflags & (VPO_SWAPINPROG |
2470 				    VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0,
2471 				    ("page %p has unexpected oflags", m));
2472 				/* Don't care: VPO_NOSYNC. */
2473 				if (m->valid != 0) {
2474 					/*
2475 					 * First, try to allocate a new page
2476 					 * that is above "high".  Failing
2477 					 * that, try to allocate a new page
2478 					 * that is below "m_run".  Allocate
2479 					 * the new page between the end of
2480 					 * "m_run" and "high" only as a last
2481 					 * resort.
2482 					 */
2483 					req = req_class | VM_ALLOC_NOOBJ;
2484 					if ((m->flags & PG_NODUMP) != 0)
2485 						req |= VM_ALLOC_NODUMP;
2486 					if (trunc_page(high) !=
2487 					    ~(vm_paddr_t)PAGE_MASK) {
2488 						m_new = vm_page_alloc_contig(
2489 						    NULL, 0, req, 1,
2490 						    round_page(high),
2491 						    ~(vm_paddr_t)0,
2492 						    PAGE_SIZE, 0,
2493 						    VM_MEMATTR_DEFAULT);
2494 					} else
2495 						m_new = NULL;
2496 					if (m_new == NULL) {
2497 						pa = VM_PAGE_TO_PHYS(m_run);
2498 						m_new = vm_page_alloc_contig(
2499 						    NULL, 0, req, 1,
2500 						    0, pa - 1, PAGE_SIZE, 0,
2501 						    VM_MEMATTR_DEFAULT);
2502 					}
2503 					if (m_new == NULL) {
2504 						pa += ptoa(npages);
2505 						m_new = vm_page_alloc_contig(
2506 						    NULL, 0, req, 1,
2507 						    pa, high, PAGE_SIZE, 0,
2508 						    VM_MEMATTR_DEFAULT);
2509 					}
2510 					if (m_new == NULL) {
2511 						error = ENOMEM;
2512 						goto unlock;
2513 					}
2514 					KASSERT(m_new->wire_count == 0,
2515 					    ("page %p is wired", m));
2516 
2517 					/*
2518 					 * Replace "m" with the new page.  For
2519 					 * vm_page_replace(), "m" must be busy
2520 					 * and dequeued.  Finally, change "m"
2521 					 * as if vm_page_free() was called.
2522 					 */
2523 					if (object->ref_count != 0)
2524 						pmap_remove_all(m);
2525 					m_new->aflags = m->aflags;
2526 					KASSERT(m_new->oflags == VPO_UNMANAGED,
2527 					    ("page %p is managed", m));
2528 					m_new->oflags = m->oflags & VPO_NOSYNC;
2529 					pmap_copy_page(m, m_new);
2530 					m_new->valid = m->valid;
2531 					m_new->dirty = m->dirty;
2532 					m->flags &= ~PG_ZERO;
2533 					vm_page_xbusy(m);
2534 					vm_page_remque(m);
2535 					vm_page_replace_checked(m_new, object,
2536 					    m->pindex, m);
2537 					m->valid = 0;
2538 					vm_page_undirty(m);
2539 
2540 					/*
2541 					 * The new page must be deactivated
2542 					 * before the object is unlocked.
2543 					 */
2544 					new_mtx = vm_page_lockptr(m_new);
2545 					if (m_mtx != new_mtx) {
2546 						mtx_unlock(m_mtx);
2547 						m_mtx = new_mtx;
2548 						mtx_lock(m_mtx);
2549 					}
2550 					vm_page_deactivate(m_new);
2551 				} else {
2552 					m->flags &= ~PG_ZERO;
2553 					vm_page_remque(m);
2554 					vm_page_remove(m);
2555 					KASSERT(m->dirty == 0,
2556 					    ("page %p is dirty", m));
2557 				}
2558 				SLIST_INSERT_HEAD(&free, m, plinks.s.ss);
2559 			} else
2560 				error = EBUSY;
2561 unlock:
2562 			VM_OBJECT_WUNLOCK(object);
2563 		} else {
2564 cached:
2565 			mtx_lock(&vm_page_queue_free_mtx);
2566 			order = m->order;
2567 			if (order < VM_NFREEORDER) {
2568 				/*
2569 				 * The page is enqueued in the physical memory
2570 				 * allocator's cache/free page queues.
2571 				 * Moreover, it is the first page in a power-
2572 				 * of-two-sized run of contiguous cache/free
2573 				 * pages.  Jump ahead to the last page within
2574 				 * that run, and continue from there.
2575 				 */
2576 				m += (1 << order) - 1;
2577 			}
2578 #if VM_NRESERVLEVEL > 0
2579 			else if (vm_reserv_is_page_free(m))
2580 				order = 0;
2581 #endif
2582 			mtx_unlock(&vm_page_queue_free_mtx);
2583 			if (order == VM_NFREEORDER)
2584 				error = EINVAL;
2585 		}
2586 	}
2587 	if (m_mtx != NULL)
2588 		mtx_unlock(m_mtx);
2589 	if ((m = SLIST_FIRST(&free)) != NULL) {
2590 		mtx_lock(&vm_page_queue_free_mtx);
2591 		do {
2592 			SLIST_REMOVE_HEAD(&free, plinks.s.ss);
2593 			vm_phys_freecnt_adj(m, 1);
2594 #if VM_NRESERVLEVEL > 0
2595 			if (!vm_reserv_free_page(m))
2596 #else
2597 			if (true)
2598 #endif
2599 				vm_phys_free_pages(m, 0);
2600 		} while ((m = SLIST_FIRST(&free)) != NULL);
2601 		vm_page_zero_idle_wakeup();
2602 		vm_page_free_wakeup();
2603 		mtx_unlock(&vm_page_queue_free_mtx);
2604 	}
2605 	return (error);
2606 }
2607 
2608 #define	NRUNS	16
2609 
2610 CTASSERT(powerof2(NRUNS));
2611 
2612 #define	RUN_INDEX(count)	((count) & (NRUNS - 1))
2613 
2614 #define	MIN_RECLAIM	8
2615 
2616 /*
2617  *	vm_page_reclaim_contig:
2618  *
2619  *	Reclaim allocated, contiguous physical memory satisfying the specified
2620  *	conditions by relocating the virtual pages using that physical memory.
2621  *	Returns true if reclamation is successful and false otherwise.  Since
2622  *	relocation requires the allocation of physical pages, reclamation may
2623  *	fail due to a shortage of cache/free pages.  When reclamation fails,
2624  *	callers are expected to perform VM_WAIT before retrying a failed
2625  *	allocation operation, e.g., vm_page_alloc_contig().
2626  *
2627  *	The caller must always specify an allocation class through "req".
2628  *
2629  *	allocation classes:
2630  *	VM_ALLOC_NORMAL		normal process request
2631  *	VM_ALLOC_SYSTEM		system *really* needs a page
2632  *	VM_ALLOC_INTERRUPT	interrupt time request
2633  *
2634  *	The optional allocation flags are ignored.
2635  *
2636  *	"npages" must be greater than zero.  Both "alignment" and "boundary"
2637  *	must be a power of two.
2638  */
2639 bool
2640 vm_page_reclaim_contig(int req, u_long npages, vm_paddr_t low, vm_paddr_t high,
2641     u_long alignment, vm_paddr_t boundary)
2642 {
2643 	vm_paddr_t curr_low;
2644 	vm_page_t m_run, m_runs[NRUNS];
2645 	u_long count, reclaimed;
2646 	int error, i, options, req_class;
2647 
2648 	KASSERT(npages > 0, ("npages is 0"));
2649 	KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
2650 	KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
2651 	req_class = req & VM_ALLOC_CLASS_MASK;
2652 
2653 	/*
2654 	 * The page daemon is allowed to dig deeper into the free page list.
2655 	 */
2656 	if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
2657 		req_class = VM_ALLOC_SYSTEM;
2658 
2659 	/*
2660 	 * Return if the number of cached and free pages cannot satisfy the
2661 	 * requested allocation.
2662 	 */
2663 	count = vm_cnt.v_free_count + vm_cnt.v_cache_count;
2664 	if (count < npages + vm_cnt.v_free_reserved || (count < npages +
2665 	    vm_cnt.v_interrupt_free_min && req_class == VM_ALLOC_SYSTEM) ||
2666 	    (count < npages && req_class == VM_ALLOC_INTERRUPT))
2667 		return (false);
2668 
2669 	/*
2670 	 * Scan up to three times, relaxing the restrictions ("options") on
2671 	 * the reclamation of reservations and superpages each time.
2672 	 */
2673 	for (options = VPSC_NORESERV;;) {
2674 		/*
2675 		 * Find the highest runs that satisfy the given constraints
2676 		 * and restrictions, and record them in "m_runs".
2677 		 */
2678 		curr_low = low;
2679 		count = 0;
2680 		for (;;) {
2681 			m_run = vm_phys_scan_contig(npages, curr_low, high,
2682 			    alignment, boundary, options);
2683 			if (m_run == NULL)
2684 				break;
2685 			curr_low = VM_PAGE_TO_PHYS(m_run) + ptoa(npages);
2686 			m_runs[RUN_INDEX(count)] = m_run;
2687 			count++;
2688 		}
2689 
2690 		/*
2691 		 * Reclaim the highest runs in LIFO (descending) order until
2692 		 * the number of reclaimed pages, "reclaimed", is at least
2693 		 * MIN_RECLAIM.  Reset "reclaimed" each time because each
2694 		 * reclamation is idempotent, and runs will (likely) recur
2695 		 * from one scan to the next as restrictions are relaxed.
2696 		 */
2697 		reclaimed = 0;
2698 		for (i = 0; count > 0 && i < NRUNS; i++) {
2699 			count--;
2700 			m_run = m_runs[RUN_INDEX(count)];
2701 			error = vm_page_reclaim_run(req_class, npages, m_run,
2702 			    high);
2703 			if (error == 0) {
2704 				reclaimed += npages;
2705 				if (reclaimed >= MIN_RECLAIM)
2706 					return (true);
2707 			}
2708 		}
2709 
2710 		/*
2711 		 * Either relax the restrictions on the next scan or return if
2712 		 * the last scan had no restrictions.
2713 		 */
2714 		if (options == VPSC_NORESERV)
2715 			options = VPSC_NOSUPER;
2716 		else if (options == VPSC_NOSUPER)
2717 			options = VPSC_ANY;
2718 		else if (options == VPSC_ANY)
2719 			return (reclaimed != 0);
2720 	}
2721 }
2722 
2723 /*
2724  *	vm_wait:	(also see VM_WAIT macro)
2725  *
2726  *	Sleep until free pages are available for allocation.
2727  *	- Called in various places before memory allocations.
2728  */
2729 void
2730 vm_wait(void)
2731 {
2732 
2733 	mtx_lock(&vm_page_queue_free_mtx);
2734 	if (curproc == pageproc) {
2735 		vm_pageout_pages_needed = 1;
2736 		msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx,
2737 		    PDROP | PSWP, "VMWait", 0);
2738 	} else {
2739 		if (!vm_pageout_wanted) {
2740 			vm_pageout_wanted = true;
2741 			wakeup(&vm_pageout_wanted);
2742 		}
2743 		vm_pages_needed = true;
2744 		msleep(&vm_cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM,
2745 		    "vmwait", 0);
2746 	}
2747 }
2748 
2749 /*
2750  *	vm_waitpfault:	(also see VM_WAITPFAULT macro)
2751  *
2752  *	Sleep until free pages are available for allocation.
2753  *	- Called only in vm_fault so that processes page faulting
2754  *	  can be easily tracked.
2755  *	- Sleeps at a lower priority than vm_wait() so that vm_wait()ing
2756  *	  processes will be able to grab memory first.  Do not change
2757  *	  this balance without careful testing first.
2758  */
2759 void
2760 vm_waitpfault(void)
2761 {
2762 
2763 	mtx_lock(&vm_page_queue_free_mtx);
2764 	if (!vm_pageout_wanted) {
2765 		vm_pageout_wanted = true;
2766 		wakeup(&vm_pageout_wanted);
2767 	}
2768 	vm_pages_needed = true;
2769 	msleep(&vm_cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER,
2770 	    "pfault", 0);
2771 }
2772 
2773 struct vm_pagequeue *
2774 vm_page_pagequeue(vm_page_t m)
2775 {
2776 
2777 	return (&vm_phys_domain(m)->vmd_pagequeues[m->queue]);
2778 }
2779 
2780 /*
2781  *	vm_page_dequeue:
2782  *
2783  *	Remove the given page from its current page queue.
2784  *
2785  *	The page must be locked.
2786  */
2787 void
2788 vm_page_dequeue(vm_page_t m)
2789 {
2790 	struct vm_pagequeue *pq;
2791 
2792 	vm_page_assert_locked(m);
2793 	KASSERT(m->queue < PQ_COUNT, ("vm_page_dequeue: page %p is not queued",
2794 	    m));
2795 	pq = vm_page_pagequeue(m);
2796 	vm_pagequeue_lock(pq);
2797 	m->queue = PQ_NONE;
2798 	TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2799 	vm_pagequeue_cnt_dec(pq);
2800 	vm_pagequeue_unlock(pq);
2801 }
2802 
2803 /*
2804  *	vm_page_dequeue_locked:
2805  *
2806  *	Remove the given page from its current page queue.
2807  *
2808  *	The page and page queue must be locked.
2809  */
2810 void
2811 vm_page_dequeue_locked(vm_page_t m)
2812 {
2813 	struct vm_pagequeue *pq;
2814 
2815 	vm_page_lock_assert(m, MA_OWNED);
2816 	pq = vm_page_pagequeue(m);
2817 	vm_pagequeue_assert_locked(pq);
2818 	m->queue = PQ_NONE;
2819 	TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2820 	vm_pagequeue_cnt_dec(pq);
2821 }
2822 
2823 /*
2824  *	vm_page_enqueue:
2825  *
2826  *	Add the given page to the specified page queue.
2827  *
2828  *	The page must be locked.
2829  */
2830 static void
2831 vm_page_enqueue(uint8_t queue, vm_page_t m)
2832 {
2833 	struct vm_pagequeue *pq;
2834 
2835 	vm_page_lock_assert(m, MA_OWNED);
2836 	KASSERT(queue < PQ_COUNT,
2837 	    ("vm_page_enqueue: invalid queue %u request for page %p",
2838 	    queue, m));
2839 	pq = &vm_phys_domain(m)->vmd_pagequeues[queue];
2840 	vm_pagequeue_lock(pq);
2841 	m->queue = queue;
2842 	TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
2843 	vm_pagequeue_cnt_inc(pq);
2844 	vm_pagequeue_unlock(pq);
2845 }
2846 
2847 /*
2848  *	vm_page_requeue:
2849  *
2850  *	Move the given page to the tail of its current page queue.
2851  *
2852  *	The page must be locked.
2853  */
2854 void
2855 vm_page_requeue(vm_page_t m)
2856 {
2857 	struct vm_pagequeue *pq;
2858 
2859 	vm_page_lock_assert(m, MA_OWNED);
2860 	KASSERT(m->queue != PQ_NONE,
2861 	    ("vm_page_requeue: page %p is not queued", m));
2862 	pq = vm_page_pagequeue(m);
2863 	vm_pagequeue_lock(pq);
2864 	TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2865 	TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
2866 	vm_pagequeue_unlock(pq);
2867 }
2868 
2869 /*
2870  *	vm_page_requeue_locked:
2871  *
2872  *	Move the given page to the tail of its current page queue.
2873  *
2874  *	The page queue must be locked.
2875  */
2876 void
2877 vm_page_requeue_locked(vm_page_t m)
2878 {
2879 	struct vm_pagequeue *pq;
2880 
2881 	KASSERT(m->queue != PQ_NONE,
2882 	    ("vm_page_requeue_locked: page %p is not queued", m));
2883 	pq = vm_page_pagequeue(m);
2884 	vm_pagequeue_assert_locked(pq);
2885 	TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2886 	TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
2887 }
2888 
2889 /*
2890  *	vm_page_activate:
2891  *
2892  *	Put the specified page on the active list (if appropriate).
2893  *	Ensure that act_count is at least ACT_INIT but do not otherwise
2894  *	mess with it.
2895  *
2896  *	The page must be locked.
2897  */
2898 void
2899 vm_page_activate(vm_page_t m)
2900 {
2901 	int queue;
2902 
2903 	vm_page_lock_assert(m, MA_OWNED);
2904 	if ((queue = m->queue) != PQ_ACTIVE) {
2905 		if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
2906 			if (m->act_count < ACT_INIT)
2907 				m->act_count = ACT_INIT;
2908 			if (queue != PQ_NONE)
2909 				vm_page_dequeue(m);
2910 			vm_page_enqueue(PQ_ACTIVE, m);
2911 		} else
2912 			KASSERT(queue == PQ_NONE,
2913 			    ("vm_page_activate: wired page %p is queued", m));
2914 	} else {
2915 		if (m->act_count < ACT_INIT)
2916 			m->act_count = ACT_INIT;
2917 	}
2918 }
2919 
2920 /*
2921  *	vm_page_free_wakeup:
2922  *
2923  *	Helper routine for vm_page_free_toq() and vm_page_cache().  This
2924  *	routine is called when a page has been added to the cache or free
2925  *	queues.
2926  *
2927  *	The page queues must be locked.
2928  */
2929 static inline void
2930 vm_page_free_wakeup(void)
2931 {
2932 
2933 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
2934 	/*
2935 	 * if pageout daemon needs pages, then tell it that there are
2936 	 * some free.
2937 	 */
2938 	if (vm_pageout_pages_needed &&
2939 	    vm_cnt.v_cache_count + vm_cnt.v_free_count >= vm_cnt.v_pageout_free_min) {
2940 		wakeup(&vm_pageout_pages_needed);
2941 		vm_pageout_pages_needed = 0;
2942 	}
2943 	/*
2944 	 * wakeup processes that are waiting on memory if we hit a
2945 	 * high water mark. And wakeup scheduler process if we have
2946 	 * lots of memory. this process will swapin processes.
2947 	 */
2948 	if (vm_pages_needed && !vm_page_count_min()) {
2949 		vm_pages_needed = false;
2950 		wakeup(&vm_cnt.v_free_count);
2951 	}
2952 }
2953 
2954 /*
2955  *	Turn a cached page into a free page, by changing its attributes.
2956  *	Keep the statistics up-to-date.
2957  *
2958  *	The free page queue must be locked.
2959  */
2960 static void
2961 vm_page_cache_turn_free(vm_page_t m)
2962 {
2963 
2964 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
2965 
2966 	m->object = NULL;
2967 	m->valid = 0;
2968 	KASSERT((m->flags & PG_CACHED) != 0,
2969 	    ("vm_page_cache_turn_free: page %p is not cached", m));
2970 	m->flags &= ~PG_CACHED;
2971 	vm_cnt.v_cache_count--;
2972 	vm_phys_freecnt_adj(m, 1);
2973 }
2974 
2975 /*
2976  *	vm_page_free_toq:
2977  *
2978  *	Returns the given page to the free list,
2979  *	disassociating it with any VM object.
2980  *
2981  *	The object must be locked.  The page must be locked if it is managed.
2982  */
2983 void
2984 vm_page_free_toq(vm_page_t m)
2985 {
2986 
2987 	if ((m->oflags & VPO_UNMANAGED) == 0) {
2988 		vm_page_lock_assert(m, MA_OWNED);
2989 		KASSERT(!pmap_page_is_mapped(m),
2990 		    ("vm_page_free_toq: freeing mapped page %p", m));
2991 	} else
2992 		KASSERT(m->queue == PQ_NONE,
2993 		    ("vm_page_free_toq: unmanaged page %p is queued", m));
2994 	PCPU_INC(cnt.v_tfree);
2995 
2996 	if (vm_page_sbusied(m))
2997 		panic("vm_page_free: freeing busy page %p", m);
2998 
2999 	/*
3000 	 * Unqueue, then remove page.  Note that we cannot destroy
3001 	 * the page here because we do not want to call the pager's
3002 	 * callback routine until after we've put the page on the
3003 	 * appropriate free queue.
3004 	 */
3005 	vm_page_remque(m);
3006 	vm_page_remove(m);
3007 
3008 	/*
3009 	 * If fictitious remove object association and
3010 	 * return, otherwise delay object association removal.
3011 	 */
3012 	if ((m->flags & PG_FICTITIOUS) != 0) {
3013 		return;
3014 	}
3015 
3016 	m->valid = 0;
3017 	vm_page_undirty(m);
3018 
3019 	if (m->wire_count != 0)
3020 		panic("vm_page_free: freeing wired page %p", m);
3021 	if (m->hold_count != 0) {
3022 		m->flags &= ~PG_ZERO;
3023 		KASSERT((m->flags & PG_UNHOLDFREE) == 0,
3024 		    ("vm_page_free: freeing PG_UNHOLDFREE page %p", m));
3025 		m->flags |= PG_UNHOLDFREE;
3026 	} else {
3027 		/*
3028 		 * Restore the default memory attribute to the page.
3029 		 */
3030 		if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
3031 			pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
3032 
3033 		/*
3034 		 * Insert the page into the physical memory allocator's
3035 		 * cache/free page queues.
3036 		 */
3037 		mtx_lock(&vm_page_queue_free_mtx);
3038 		vm_phys_freecnt_adj(m, 1);
3039 #if VM_NRESERVLEVEL > 0
3040 		if (!vm_reserv_free_page(m))
3041 #else
3042 		if (TRUE)
3043 #endif
3044 			vm_phys_free_pages(m, 0);
3045 		if ((m->flags & PG_ZERO) != 0)
3046 			++vm_page_zero_count;
3047 		else
3048 			vm_page_zero_idle_wakeup();
3049 		vm_page_free_wakeup();
3050 		mtx_unlock(&vm_page_queue_free_mtx);
3051 	}
3052 }
3053 
3054 /*
3055  *	vm_page_wire:
3056  *
3057  *	Mark this page as wired down by yet
3058  *	another map, removing it from paging queues
3059  *	as necessary.
3060  *
3061  *	If the page is fictitious, then its wire count must remain one.
3062  *
3063  *	The page must be locked.
3064  */
3065 void
3066 vm_page_wire(vm_page_t m)
3067 {
3068 
3069 	/*
3070 	 * Only bump the wire statistics if the page is not already wired,
3071 	 * and only unqueue the page if it is on some queue (if it is unmanaged
3072 	 * it is already off the queues).
3073 	 */
3074 	vm_page_lock_assert(m, MA_OWNED);
3075 	if ((m->flags & PG_FICTITIOUS) != 0) {
3076 		KASSERT(m->wire_count == 1,
3077 		    ("vm_page_wire: fictitious page %p's wire count isn't one",
3078 		    m));
3079 		return;
3080 	}
3081 	if (m->wire_count == 0) {
3082 		KASSERT((m->oflags & VPO_UNMANAGED) == 0 ||
3083 		    m->queue == PQ_NONE,
3084 		    ("vm_page_wire: unmanaged page %p is queued", m));
3085 		vm_page_remque(m);
3086 		atomic_add_int(&vm_cnt.v_wire_count, 1);
3087 	}
3088 	m->wire_count++;
3089 	KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
3090 }
3091 
3092 /*
3093  * vm_page_unwire:
3094  *
3095  * Release one wiring of the specified page, potentially allowing it to be
3096  * paged out.  Returns TRUE if the number of wirings transitions to zero and
3097  * FALSE otherwise.
3098  *
3099  * Only managed pages belonging to an object can be paged out.  If the number
3100  * of wirings transitions to zero and the page is eligible for page out, then
3101  * the page is added to the specified paging queue (unless PQ_NONE is
3102  * specified).
3103  *
3104  * If a page is fictitious, then its wire count must always be one.
3105  *
3106  * A managed page must be locked.
3107  */
3108 boolean_t
3109 vm_page_unwire(vm_page_t m, uint8_t queue)
3110 {
3111 
3112 	KASSERT(queue < PQ_COUNT || queue == PQ_NONE,
3113 	    ("vm_page_unwire: invalid queue %u request for page %p",
3114 	    queue, m));
3115 	if ((m->oflags & VPO_UNMANAGED) == 0)
3116 		vm_page_assert_locked(m);
3117 	if ((m->flags & PG_FICTITIOUS) != 0) {
3118 		KASSERT(m->wire_count == 1,
3119 	    ("vm_page_unwire: fictitious page %p's wire count isn't one", m));
3120 		return (FALSE);
3121 	}
3122 	if (m->wire_count > 0) {
3123 		m->wire_count--;
3124 		if (m->wire_count == 0) {
3125 			atomic_subtract_int(&vm_cnt.v_wire_count, 1);
3126 			if ((m->oflags & VPO_UNMANAGED) == 0 &&
3127 			    m->object != NULL && queue != PQ_NONE) {
3128 				if (queue == PQ_INACTIVE)
3129 					m->flags &= ~PG_WINATCFLS;
3130 				vm_page_enqueue(queue, m);
3131 			}
3132 			return (TRUE);
3133 		} else
3134 			return (FALSE);
3135 	} else
3136 		panic("vm_page_unwire: page %p's wire count is zero", m);
3137 }
3138 
3139 /*
3140  * Move the specified page to the inactive queue.
3141  *
3142  * Many pages placed on the inactive queue should actually go
3143  * into the cache, but it is difficult to figure out which.  What
3144  * we do instead, if the inactive target is well met, is to put
3145  * clean pages at the head of the inactive queue instead of the tail.
3146  * This will cause them to be moved to the cache more quickly and
3147  * if not actively re-referenced, reclaimed more quickly.  If we just
3148  * stick these pages at the end of the inactive queue, heavy filesystem
3149  * meta-data accesses can cause an unnecessary paging load on memory bound
3150  * processes.  This optimization causes one-time-use metadata to be
3151  * reused more quickly.
3152  *
3153  * Normally noreuse is FALSE, resulting in LRU operation.  noreuse is set
3154  * to TRUE if we want this page to be 'as if it were placed in the cache',
3155  * except without unmapping it from the process address space.  In
3156  * practice this is implemented by inserting the page at the head of the
3157  * queue, using a marker page to guide FIFO insertion ordering.
3158  *
3159  * The page must be locked.
3160  */
3161 static inline void
3162 _vm_page_deactivate(vm_page_t m, boolean_t noreuse)
3163 {
3164 	struct vm_pagequeue *pq;
3165 	int queue;
3166 
3167 	vm_page_assert_locked(m);
3168 
3169 	/*
3170 	 * Ignore if the page is already inactive, unless it is unlikely to be
3171 	 * reactivated.
3172 	 */
3173 	if ((queue = m->queue) == PQ_INACTIVE && !noreuse)
3174 		return;
3175 	if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
3176 		pq = &vm_phys_domain(m)->vmd_pagequeues[PQ_INACTIVE];
3177 		/* Avoid multiple acquisitions of the inactive queue lock. */
3178 		if (queue == PQ_INACTIVE) {
3179 			vm_pagequeue_lock(pq);
3180 			vm_page_dequeue_locked(m);
3181 		} else {
3182 			if (queue != PQ_NONE)
3183 				vm_page_dequeue(m);
3184 			m->flags &= ~PG_WINATCFLS;
3185 			vm_pagequeue_lock(pq);
3186 		}
3187 		m->queue = PQ_INACTIVE;
3188 		if (noreuse)
3189 			TAILQ_INSERT_BEFORE(&vm_phys_domain(m)->vmd_inacthead,
3190 			    m, plinks.q);
3191 		else
3192 			TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
3193 		vm_pagequeue_cnt_inc(pq);
3194 		vm_pagequeue_unlock(pq);
3195 	}
3196 }
3197 
3198 /*
3199  * Move the specified page to the inactive queue.
3200  *
3201  * The page must be locked.
3202  */
3203 void
3204 vm_page_deactivate(vm_page_t m)
3205 {
3206 
3207 	_vm_page_deactivate(m, FALSE);
3208 }
3209 
3210 /*
3211  * Move the specified page to the inactive queue with the expectation
3212  * that it is unlikely to be reused.
3213  *
3214  * The page must be locked.
3215  */
3216 void
3217 vm_page_deactivate_noreuse(vm_page_t m)
3218 {
3219 
3220 	_vm_page_deactivate(m, TRUE);
3221 }
3222 
3223 /*
3224  * vm_page_try_to_cache:
3225  *
3226  * Returns 0 on failure, 1 on success
3227  */
3228 int
3229 vm_page_try_to_cache(vm_page_t m)
3230 {
3231 
3232 	vm_page_lock_assert(m, MA_OWNED);
3233 	VM_OBJECT_ASSERT_WLOCKED(m->object);
3234 	if (m->dirty || m->hold_count || m->wire_count ||
3235 	    (m->oflags & VPO_UNMANAGED) != 0 || vm_page_busied(m))
3236 		return (0);
3237 	pmap_remove_all(m);
3238 	if (m->dirty)
3239 		return (0);
3240 	vm_page_cache(m);
3241 	return (1);
3242 }
3243 
3244 /*
3245  * vm_page_try_to_free()
3246  *
3247  *	Attempt to free the page.  If we cannot free it, we do nothing.
3248  *	1 is returned on success, 0 on failure.
3249  */
3250 int
3251 vm_page_try_to_free(vm_page_t m)
3252 {
3253 
3254 	vm_page_lock_assert(m, MA_OWNED);
3255 	if (m->object != NULL)
3256 		VM_OBJECT_ASSERT_WLOCKED(m->object);
3257 	if (m->dirty || m->hold_count || m->wire_count ||
3258 	    (m->oflags & VPO_UNMANAGED) != 0 || vm_page_busied(m))
3259 		return (0);
3260 	pmap_remove_all(m);
3261 	if (m->dirty)
3262 		return (0);
3263 	vm_page_free(m);
3264 	return (1);
3265 }
3266 
3267 /*
3268  * vm_page_cache
3269  *
3270  * Put the specified page onto the page cache queue (if appropriate).
3271  *
3272  * The object and page must be locked.
3273  */
3274 void
3275 vm_page_cache(vm_page_t m)
3276 {
3277 	vm_object_t object;
3278 	boolean_t cache_was_empty;
3279 
3280 	vm_page_lock_assert(m, MA_OWNED);
3281 	object = m->object;
3282 	VM_OBJECT_ASSERT_WLOCKED(object);
3283 	if (vm_page_busied(m) || (m->oflags & VPO_UNMANAGED) ||
3284 	    m->hold_count || m->wire_count)
3285 		panic("vm_page_cache: attempting to cache busy page");
3286 	KASSERT(!pmap_page_is_mapped(m),
3287 	    ("vm_page_cache: page %p is mapped", m));
3288 	KASSERT(m->dirty == 0, ("vm_page_cache: page %p is dirty", m));
3289 	if (m->valid == 0 || object->type == OBJT_DEFAULT ||
3290 	    (object->type == OBJT_SWAP &&
3291 	    !vm_pager_has_page(object, m->pindex, NULL, NULL))) {
3292 		/*
3293 		 * Hypothesis: A cache-eligible page belonging to a
3294 		 * default object or swap object but without a backing
3295 		 * store must be zero filled.
3296 		 */
3297 		vm_page_free(m);
3298 		return;
3299 	}
3300 	KASSERT((m->flags & PG_CACHED) == 0,
3301 	    ("vm_page_cache: page %p is already cached", m));
3302 
3303 	/*
3304 	 * Remove the page from the paging queues.
3305 	 */
3306 	vm_page_remque(m);
3307 
3308 	/*
3309 	 * Remove the page from the object's collection of resident
3310 	 * pages.
3311 	 */
3312 	vm_radix_remove(&object->rtree, m->pindex);
3313 	TAILQ_REMOVE(&object->memq, m, listq);
3314 	object->resident_page_count--;
3315 
3316 	/*
3317 	 * Restore the default memory attribute to the page.
3318 	 */
3319 	if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
3320 		pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
3321 
3322 	/*
3323 	 * Insert the page into the object's collection of cached pages
3324 	 * and the physical memory allocator's cache/free page queues.
3325 	 */
3326 	m->flags &= ~PG_ZERO;
3327 	mtx_lock(&vm_page_queue_free_mtx);
3328 	cache_was_empty = vm_radix_is_empty(&object->cache);
3329 	if (vm_radix_insert(&object->cache, m)) {
3330 		mtx_unlock(&vm_page_queue_free_mtx);
3331 		if (object->type == OBJT_VNODE &&
3332 		    object->resident_page_count == 0)
3333 			vdrop(object->handle);
3334 		m->object = NULL;
3335 		vm_page_free(m);
3336 		return;
3337 	}
3338 
3339 	/*
3340 	 * The above call to vm_radix_insert() could reclaim the one pre-
3341 	 * existing cached page from this object, resulting in a call to
3342 	 * vdrop().
3343 	 */
3344 	if (!cache_was_empty)
3345 		cache_was_empty = vm_radix_is_singleton(&object->cache);
3346 
3347 	m->flags |= PG_CACHED;
3348 	vm_cnt.v_cache_count++;
3349 	PCPU_INC(cnt.v_tcached);
3350 #if VM_NRESERVLEVEL > 0
3351 	if (!vm_reserv_free_page(m)) {
3352 #else
3353 	if (TRUE) {
3354 #endif
3355 		vm_phys_free_pages(m, 0);
3356 	}
3357 	vm_page_free_wakeup();
3358 	mtx_unlock(&vm_page_queue_free_mtx);
3359 
3360 	/*
3361 	 * Increment the vnode's hold count if this is the object's only
3362 	 * cached page.  Decrement the vnode's hold count if this was
3363 	 * the object's only resident page.
3364 	 */
3365 	if (object->type == OBJT_VNODE) {
3366 		if (cache_was_empty && object->resident_page_count != 0)
3367 			vhold(object->handle);
3368 		else if (!cache_was_empty && object->resident_page_count == 0)
3369 			vdrop(object->handle);
3370 	}
3371 }
3372 
3373 /*
3374  * vm_page_advise
3375  *
3376  * 	Deactivate or do nothing, as appropriate.
3377  *
3378  *	The object and page must be locked.
3379  */
3380 void
3381 vm_page_advise(vm_page_t m, int advice)
3382 {
3383 
3384 	vm_page_assert_locked(m);
3385 	VM_OBJECT_ASSERT_WLOCKED(m->object);
3386 	if (advice == MADV_FREE)
3387 		/*
3388 		 * Mark the page clean.  This will allow the page to be freed
3389 		 * up by the system.  However, such pages are often reused
3390 		 * quickly by malloc() so we do not do anything that would
3391 		 * cause a page fault if we can help it.
3392 		 *
3393 		 * Specifically, we do not try to actually free the page now
3394 		 * nor do we try to put it in the cache (which would cause a
3395 		 * page fault on reuse).
3396 		 *
3397 		 * But we do make the page as freeable as we can without
3398 		 * actually taking the step of unmapping it.
3399 		 */
3400 		m->dirty = 0;
3401 	else if (advice != MADV_DONTNEED)
3402 		return;
3403 
3404 	/*
3405 	 * Clear any references to the page.  Otherwise, the page daemon will
3406 	 * immediately reactivate the page.
3407 	 */
3408 	vm_page_aflag_clear(m, PGA_REFERENCED);
3409 
3410 	if (advice != MADV_FREE && m->dirty == 0 && pmap_is_modified(m))
3411 		vm_page_dirty(m);
3412 
3413 	/*
3414 	 * Place clean pages near the head of the inactive queue rather than
3415 	 * the tail, thus defeating the queue's LRU operation and ensuring that
3416 	 * the page will be reused quickly.  Dirty pages are given a chance to
3417 	 * cycle once through the inactive queue before becoming eligible for
3418 	 * laundering.
3419 	 */
3420 	_vm_page_deactivate(m, m->dirty == 0);
3421 }
3422 
3423 /*
3424  * Grab a page, waiting until we are waken up due to the page
3425  * changing state.  We keep on waiting, if the page continues
3426  * to be in the object.  If the page doesn't exist, first allocate it
3427  * and then conditionally zero it.
3428  *
3429  * This routine may sleep.
3430  *
3431  * The object must be locked on entry.  The lock will, however, be released
3432  * and reacquired if the routine sleeps.
3433  */
3434 vm_page_t
3435 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
3436 {
3437 	vm_page_t m;
3438 	int sleep;
3439 
3440 	VM_OBJECT_ASSERT_WLOCKED(object);
3441 	KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
3442 	    (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
3443 	    ("vm_page_grab: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch"));
3444 retrylookup:
3445 	if ((m = vm_page_lookup(object, pindex)) != NULL) {
3446 		sleep = (allocflags & VM_ALLOC_IGN_SBUSY) != 0 ?
3447 		    vm_page_xbusied(m) : vm_page_busied(m);
3448 		if (sleep) {
3449 			if ((allocflags & VM_ALLOC_NOWAIT) != 0)
3450 				return (NULL);
3451 			/*
3452 			 * Reference the page before unlocking and
3453 			 * sleeping so that the page daemon is less
3454 			 * likely to reclaim it.
3455 			 */
3456 			vm_page_aflag_set(m, PGA_REFERENCED);
3457 			vm_page_lock(m);
3458 			VM_OBJECT_WUNLOCK(object);
3459 			vm_page_busy_sleep(m, "pgrbwt");
3460 			VM_OBJECT_WLOCK(object);
3461 			goto retrylookup;
3462 		} else {
3463 			if ((allocflags & VM_ALLOC_WIRED) != 0) {
3464 				vm_page_lock(m);
3465 				vm_page_wire(m);
3466 				vm_page_unlock(m);
3467 			}
3468 			if ((allocflags &
3469 			    (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0)
3470 				vm_page_xbusy(m);
3471 			if ((allocflags & VM_ALLOC_SBUSY) != 0)
3472 				vm_page_sbusy(m);
3473 			return (m);
3474 		}
3475 	}
3476 	m = vm_page_alloc(object, pindex, allocflags);
3477 	if (m == NULL) {
3478 		if ((allocflags & VM_ALLOC_NOWAIT) != 0)
3479 			return (NULL);
3480 		VM_OBJECT_WUNLOCK(object);
3481 		VM_WAIT;
3482 		VM_OBJECT_WLOCK(object);
3483 		goto retrylookup;
3484 	} else if (m->valid != 0)
3485 		return (m);
3486 	if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
3487 		pmap_zero_page(m);
3488 	return (m);
3489 }
3490 
3491 /*
3492  * Mapping function for valid or dirty bits in a page.
3493  *
3494  * Inputs are required to range within a page.
3495  */
3496 vm_page_bits_t
3497 vm_page_bits(int base, int size)
3498 {
3499 	int first_bit;
3500 	int last_bit;
3501 
3502 	KASSERT(
3503 	    base + size <= PAGE_SIZE,
3504 	    ("vm_page_bits: illegal base/size %d/%d", base, size)
3505 	);
3506 
3507 	if (size == 0)		/* handle degenerate case */
3508 		return (0);
3509 
3510 	first_bit = base >> DEV_BSHIFT;
3511 	last_bit = (base + size - 1) >> DEV_BSHIFT;
3512 
3513 	return (((vm_page_bits_t)2 << last_bit) -
3514 	    ((vm_page_bits_t)1 << first_bit));
3515 }
3516 
3517 /*
3518  *	vm_page_set_valid_range:
3519  *
3520  *	Sets portions of a page valid.  The arguments are expected
3521  *	to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
3522  *	of any partial chunks touched by the range.  The invalid portion of
3523  *	such chunks will be zeroed.
3524  *
3525  *	(base + size) must be less then or equal to PAGE_SIZE.
3526  */
3527 void
3528 vm_page_set_valid_range(vm_page_t m, int base, int size)
3529 {
3530 	int endoff, frag;
3531 
3532 	VM_OBJECT_ASSERT_WLOCKED(m->object);
3533 	if (size == 0)	/* handle degenerate case */
3534 		return;
3535 
3536 	/*
3537 	 * If the base is not DEV_BSIZE aligned and the valid
3538 	 * bit is clear, we have to zero out a portion of the
3539 	 * first block.
3540 	 */
3541 	if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
3542 	    (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
3543 		pmap_zero_page_area(m, frag, base - frag);
3544 
3545 	/*
3546 	 * If the ending offset is not DEV_BSIZE aligned and the
3547 	 * valid bit is clear, we have to zero out a portion of
3548 	 * the last block.
3549 	 */
3550 	endoff = base + size;
3551 	if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
3552 	    (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
3553 		pmap_zero_page_area(m, endoff,
3554 		    DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
3555 
3556 	/*
3557 	 * Assert that no previously invalid block that is now being validated
3558 	 * is already dirty.
3559 	 */
3560 	KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0,
3561 	    ("vm_page_set_valid_range: page %p is dirty", m));
3562 
3563 	/*
3564 	 * Set valid bits inclusive of any overlap.
3565 	 */
3566 	m->valid |= vm_page_bits(base, size);
3567 }
3568 
3569 /*
3570  * Clear the given bits from the specified page's dirty field.
3571  */
3572 static __inline void
3573 vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits)
3574 {
3575 	uintptr_t addr;
3576 #if PAGE_SIZE < 16384
3577 	int shift;
3578 #endif
3579 
3580 	/*
3581 	 * If the object is locked and the page is neither exclusive busy nor
3582 	 * write mapped, then the page's dirty field cannot possibly be
3583 	 * set by a concurrent pmap operation.
3584 	 */
3585 	VM_OBJECT_ASSERT_WLOCKED(m->object);
3586 	if (!vm_page_xbusied(m) && !pmap_page_is_write_mapped(m))
3587 		m->dirty &= ~pagebits;
3588 	else {
3589 		/*
3590 		 * The pmap layer can call vm_page_dirty() without
3591 		 * holding a distinguished lock.  The combination of
3592 		 * the object's lock and an atomic operation suffice
3593 		 * to guarantee consistency of the page dirty field.
3594 		 *
3595 		 * For PAGE_SIZE == 32768 case, compiler already
3596 		 * properly aligns the dirty field, so no forcible
3597 		 * alignment is needed. Only require existence of
3598 		 * atomic_clear_64 when page size is 32768.
3599 		 */
3600 		addr = (uintptr_t)&m->dirty;
3601 #if PAGE_SIZE == 32768
3602 		atomic_clear_64((uint64_t *)addr, pagebits);
3603 #elif PAGE_SIZE == 16384
3604 		atomic_clear_32((uint32_t *)addr, pagebits);
3605 #else		/* PAGE_SIZE <= 8192 */
3606 		/*
3607 		 * Use a trick to perform a 32-bit atomic on the
3608 		 * containing aligned word, to not depend on the existence
3609 		 * of atomic_clear_{8, 16}.
3610 		 */
3611 		shift = addr & (sizeof(uint32_t) - 1);
3612 #if BYTE_ORDER == BIG_ENDIAN
3613 		shift = (sizeof(uint32_t) - sizeof(m->dirty) - shift) * NBBY;
3614 #else
3615 		shift *= NBBY;
3616 #endif
3617 		addr &= ~(sizeof(uint32_t) - 1);
3618 		atomic_clear_32((uint32_t *)addr, pagebits << shift);
3619 #endif		/* PAGE_SIZE */
3620 	}
3621 }
3622 
3623 /*
3624  *	vm_page_set_validclean:
3625  *
3626  *	Sets portions of a page valid and clean.  The arguments are expected
3627  *	to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
3628  *	of any partial chunks touched by the range.  The invalid portion of
3629  *	such chunks will be zero'd.
3630  *
3631  *	(base + size) must be less then or equal to PAGE_SIZE.
3632  */
3633 void
3634 vm_page_set_validclean(vm_page_t m, int base, int size)
3635 {
3636 	vm_page_bits_t oldvalid, pagebits;
3637 	int endoff, frag;
3638 
3639 	VM_OBJECT_ASSERT_WLOCKED(m->object);
3640 	if (size == 0)	/* handle degenerate case */
3641 		return;
3642 
3643 	/*
3644 	 * If the base is not DEV_BSIZE aligned and the valid
3645 	 * bit is clear, we have to zero out a portion of the
3646 	 * first block.
3647 	 */
3648 	if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
3649 	    (m->valid & ((vm_page_bits_t)1 << (base >> DEV_BSHIFT))) == 0)
3650 		pmap_zero_page_area(m, frag, base - frag);
3651 
3652 	/*
3653 	 * If the ending offset is not DEV_BSIZE aligned and the
3654 	 * valid bit is clear, we have to zero out a portion of
3655 	 * the last block.
3656 	 */
3657 	endoff = base + size;
3658 	if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
3659 	    (m->valid & ((vm_page_bits_t)1 << (endoff >> DEV_BSHIFT))) == 0)
3660 		pmap_zero_page_area(m, endoff,
3661 		    DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
3662 
3663 	/*
3664 	 * Set valid, clear dirty bits.  If validating the entire
3665 	 * page we can safely clear the pmap modify bit.  We also
3666 	 * use this opportunity to clear the VPO_NOSYNC flag.  If a process
3667 	 * takes a write fault on a MAP_NOSYNC memory area the flag will
3668 	 * be set again.
3669 	 *
3670 	 * We set valid bits inclusive of any overlap, but we can only
3671 	 * clear dirty bits for DEV_BSIZE chunks that are fully within
3672 	 * the range.
3673 	 */
3674 	oldvalid = m->valid;
3675 	pagebits = vm_page_bits(base, size);
3676 	m->valid |= pagebits;
3677 #if 0	/* NOT YET */
3678 	if ((frag = base & (DEV_BSIZE - 1)) != 0) {
3679 		frag = DEV_BSIZE - frag;
3680 		base += frag;
3681 		size -= frag;
3682 		if (size < 0)
3683 			size = 0;
3684 	}
3685 	pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
3686 #endif
3687 	if (base == 0 && size == PAGE_SIZE) {
3688 		/*
3689 		 * The page can only be modified within the pmap if it is
3690 		 * mapped, and it can only be mapped if it was previously
3691 		 * fully valid.
3692 		 */
3693 		if (oldvalid == VM_PAGE_BITS_ALL)
3694 			/*
3695 			 * Perform the pmap_clear_modify() first.  Otherwise,
3696 			 * a concurrent pmap operation, such as
3697 			 * pmap_protect(), could clear a modification in the
3698 			 * pmap and set the dirty field on the page before
3699 			 * pmap_clear_modify() had begun and after the dirty
3700 			 * field was cleared here.
3701 			 */
3702 			pmap_clear_modify(m);
3703 		m->dirty = 0;
3704 		m->oflags &= ~VPO_NOSYNC;
3705 	} else if (oldvalid != VM_PAGE_BITS_ALL)
3706 		m->dirty &= ~pagebits;
3707 	else
3708 		vm_page_clear_dirty_mask(m, pagebits);
3709 }
3710 
3711 void
3712 vm_page_clear_dirty(vm_page_t m, int base, int size)
3713 {
3714 
3715 	vm_page_clear_dirty_mask(m, vm_page_bits(base, size));
3716 }
3717 
3718 /*
3719  *	vm_page_set_invalid:
3720  *
3721  *	Invalidates DEV_BSIZE'd chunks within a page.  Both the
3722  *	valid and dirty bits for the effected areas are cleared.
3723  */
3724 void
3725 vm_page_set_invalid(vm_page_t m, int base, int size)
3726 {
3727 	vm_page_bits_t bits;
3728 	vm_object_t object;
3729 
3730 	object = m->object;
3731 	VM_OBJECT_ASSERT_WLOCKED(object);
3732 	if (object->type == OBJT_VNODE && base == 0 && IDX_TO_OFF(m->pindex) +
3733 	    size >= object->un_pager.vnp.vnp_size)
3734 		bits = VM_PAGE_BITS_ALL;
3735 	else
3736 		bits = vm_page_bits(base, size);
3737 	if (object->ref_count != 0 && m->valid == VM_PAGE_BITS_ALL &&
3738 	    bits != 0)
3739 		pmap_remove_all(m);
3740 	KASSERT((bits == 0 && m->valid == VM_PAGE_BITS_ALL) ||
3741 	    !pmap_page_is_mapped(m),
3742 	    ("vm_page_set_invalid: page %p is mapped", m));
3743 	m->valid &= ~bits;
3744 	m->dirty &= ~bits;
3745 }
3746 
3747 /*
3748  * vm_page_zero_invalid()
3749  *
3750  *	The kernel assumes that the invalid portions of a page contain
3751  *	garbage, but such pages can be mapped into memory by user code.
3752  *	When this occurs, we must zero out the non-valid portions of the
3753  *	page so user code sees what it expects.
3754  *
3755  *	Pages are most often semi-valid when the end of a file is mapped
3756  *	into memory and the file's size is not page aligned.
3757  */
3758 void
3759 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
3760 {
3761 	int b;
3762 	int i;
3763 
3764 	VM_OBJECT_ASSERT_WLOCKED(m->object);
3765 	/*
3766 	 * Scan the valid bits looking for invalid sections that
3767 	 * must be zeroed.  Invalid sub-DEV_BSIZE'd areas ( where the
3768 	 * valid bit may be set ) have already been zeroed by
3769 	 * vm_page_set_validclean().
3770 	 */
3771 	for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
3772 		if (i == (PAGE_SIZE / DEV_BSIZE) ||
3773 		    (m->valid & ((vm_page_bits_t)1 << i))) {
3774 			if (i > b) {
3775 				pmap_zero_page_area(m,
3776 				    b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
3777 			}
3778 			b = i + 1;
3779 		}
3780 	}
3781 
3782 	/*
3783 	 * setvalid is TRUE when we can safely set the zero'd areas
3784 	 * as being valid.  We can do this if there are no cache consistancy
3785 	 * issues.  e.g. it is ok to do with UFS, but not ok to do with NFS.
3786 	 */
3787 	if (setvalid)
3788 		m->valid = VM_PAGE_BITS_ALL;
3789 }
3790 
3791 /*
3792  *	vm_page_is_valid:
3793  *
3794  *	Is (partial) page valid?  Note that the case where size == 0
3795  *	will return FALSE in the degenerate case where the page is
3796  *	entirely invalid, and TRUE otherwise.
3797  */
3798 int
3799 vm_page_is_valid(vm_page_t m, int base, int size)
3800 {
3801 	vm_page_bits_t bits;
3802 
3803 	VM_OBJECT_ASSERT_LOCKED(m->object);
3804 	bits = vm_page_bits(base, size);
3805 	return (m->valid != 0 && (m->valid & bits) == bits);
3806 }
3807 
3808 /*
3809  *	vm_page_ps_is_valid:
3810  *
3811  *	Returns TRUE if the entire (super)page is valid and FALSE otherwise.
3812  */
3813 boolean_t
3814 vm_page_ps_is_valid(vm_page_t m)
3815 {
3816 	int i, npages;
3817 
3818 	VM_OBJECT_ASSERT_LOCKED(m->object);
3819 	npages = atop(pagesizes[m->psind]);
3820 
3821 	/*
3822 	 * The physically contiguous pages that make up a superpage, i.e., a
3823 	 * page with a page size index ("psind") greater than zero, will
3824 	 * occupy adjacent entries in vm_page_array[].
3825 	 */
3826 	for (i = 0; i < npages; i++) {
3827 		if (m[i].valid != VM_PAGE_BITS_ALL)
3828 			return (FALSE);
3829 	}
3830 	return (TRUE);
3831 }
3832 
3833 /*
3834  * Set the page's dirty bits if the page is modified.
3835  */
3836 void
3837 vm_page_test_dirty(vm_page_t m)
3838 {
3839 
3840 	VM_OBJECT_ASSERT_WLOCKED(m->object);
3841 	if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m))
3842 		vm_page_dirty(m);
3843 }
3844 
3845 void
3846 vm_page_lock_KBI(vm_page_t m, const char *file, int line)
3847 {
3848 
3849 	mtx_lock_flags_(vm_page_lockptr(m), 0, file, line);
3850 }
3851 
3852 void
3853 vm_page_unlock_KBI(vm_page_t m, const char *file, int line)
3854 {
3855 
3856 	mtx_unlock_flags_(vm_page_lockptr(m), 0, file, line);
3857 }
3858 
3859 int
3860 vm_page_trylock_KBI(vm_page_t m, const char *file, int line)
3861 {
3862 
3863 	return (mtx_trylock_flags_(vm_page_lockptr(m), 0, file, line));
3864 }
3865 
3866 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
3867 void
3868 vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line)
3869 {
3870 
3871 	vm_page_lock_assert_KBI(m, MA_OWNED, file, line);
3872 }
3873 
3874 void
3875 vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line)
3876 {
3877 
3878 	mtx_assert_(vm_page_lockptr(m), a, file, line);
3879 }
3880 #endif
3881 
3882 #ifdef INVARIANTS
3883 void
3884 vm_page_object_lock_assert(vm_page_t m)
3885 {
3886 
3887 	/*
3888 	 * Certain of the page's fields may only be modified by the
3889 	 * holder of the containing object's lock or the exclusive busy.
3890 	 * holder.  Unfortunately, the holder of the write busy is
3891 	 * not recorded, and thus cannot be checked here.
3892 	 */
3893 	if (m->object != NULL && !vm_page_xbusied(m))
3894 		VM_OBJECT_ASSERT_WLOCKED(m->object);
3895 }
3896 
3897 void
3898 vm_page_assert_pga_writeable(vm_page_t m, uint8_t bits)
3899 {
3900 
3901 	if ((bits & PGA_WRITEABLE) == 0)
3902 		return;
3903 
3904 	/*
3905 	 * The PGA_WRITEABLE flag can only be set if the page is
3906 	 * managed, is exclusively busied or the object is locked.
3907 	 * Currently, this flag is only set by pmap_enter().
3908 	 */
3909 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3910 	    ("PGA_WRITEABLE on unmanaged page"));
3911 	if (!vm_page_xbusied(m))
3912 		VM_OBJECT_ASSERT_LOCKED(m->object);
3913 }
3914 #endif
3915 
3916 #include "opt_ddb.h"
3917 #ifdef DDB
3918 #include <sys/kernel.h>
3919 
3920 #include <ddb/ddb.h>
3921 
3922 DB_SHOW_COMMAND(page, vm_page_print_page_info)
3923 {
3924 	db_printf("vm_cnt.v_free_count: %d\n", vm_cnt.v_free_count);
3925 	db_printf("vm_cnt.v_cache_count: %d\n", vm_cnt.v_cache_count);
3926 	db_printf("vm_cnt.v_inactive_count: %d\n", vm_cnt.v_inactive_count);
3927 	db_printf("vm_cnt.v_active_count: %d\n", vm_cnt.v_active_count);
3928 	db_printf("vm_cnt.v_wire_count: %d\n", vm_cnt.v_wire_count);
3929 	db_printf("vm_cnt.v_free_reserved: %d\n", vm_cnt.v_free_reserved);
3930 	db_printf("vm_cnt.v_free_min: %d\n", vm_cnt.v_free_min);
3931 	db_printf("vm_cnt.v_free_target: %d\n", vm_cnt.v_free_target);
3932 	db_printf("vm_cnt.v_inactive_target: %d\n", vm_cnt.v_inactive_target);
3933 }
3934 
3935 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
3936 {
3937 	int dom;
3938 
3939 	db_printf("pq_free %d pq_cache %d\n",
3940 	    vm_cnt.v_free_count, vm_cnt.v_cache_count);
3941 	for (dom = 0; dom < vm_ndomains; dom++) {
3942 		db_printf(
3943 	"dom %d page_cnt %d free %d pq_act %d pq_inact %d pass %d\n",
3944 		    dom,
3945 		    vm_dom[dom].vmd_page_count,
3946 		    vm_dom[dom].vmd_free_count,
3947 		    vm_dom[dom].vmd_pagequeues[PQ_ACTIVE].pq_cnt,
3948 		    vm_dom[dom].vmd_pagequeues[PQ_INACTIVE].pq_cnt,
3949 		    vm_dom[dom].vmd_pass);
3950 	}
3951 }
3952 
3953 DB_SHOW_COMMAND(pginfo, vm_page_print_pginfo)
3954 {
3955 	vm_page_t m;
3956 	boolean_t phys;
3957 
3958 	if (!have_addr) {
3959 		db_printf("show pginfo addr\n");
3960 		return;
3961 	}
3962 
3963 	phys = strchr(modif, 'p') != NULL;
3964 	if (phys)
3965 		m = PHYS_TO_VM_PAGE(addr);
3966 	else
3967 		m = (vm_page_t)addr;
3968 	db_printf(
3969     "page %p obj %p pidx 0x%jx phys 0x%jx q %d hold %d wire %d\n"
3970     "  af 0x%x of 0x%x f 0x%x act %d busy %x valid 0x%x dirty 0x%x\n",
3971 	    m, m->object, (uintmax_t)m->pindex, (uintmax_t)m->phys_addr,
3972 	    m->queue, m->hold_count, m->wire_count, m->aflags, m->oflags,
3973 	    m->flags, m->act_count, m->busy_lock, m->valid, m->dirty);
3974 }
3975 #endif /* DDB */
3976