xref: /freebsd/sys/vm/vm_page.c (revision 10b9d77bf1ccf2f3affafa6261692cb92cf7e992)
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 pageq mutex is required when adding or removing a page from a
67  *	  page queue (vm_page_queue[]), regardless of other mutexes or the
68  *	  busy state of a page.
69  *
70  *	- a hash chain mutex is required when associating or disassociating
71  *	  a page from the VM PAGE CACHE hash table (vm_page_buckets),
72  *	  regardless of other mutexes or the busy state of a page.
73  *
74  *	- either a hash chain mutex OR a busied page is required in order
75  *	  to modify the page flags.  A hash chain mutex must be obtained in
76  *	  order to busy a page.  A page's flags cannot be modified by a
77  *	  hash chain mutex if the page is marked busy.
78  *
79  *	- The object memq mutex is held when inserting or removing
80  *	  pages from an object (vm_page_insert() or vm_page_remove()).  This
81  *	  is different from the object's main mutex.
82  *
83  *	Generally speaking, you have to be aware of side effects when running
84  *	vm_page ops.  A vm_page_lookup() will return with the hash chain
85  *	locked, whether it was able to lookup the page or not.  vm_page_free(),
86  *	vm_page_cache(), vm_page_activate(), and a number of other routines
87  *	will release the hash chain mutex for you.  Intermediate manipulation
88  *	routines such as vm_page_flag_set() expect the hash chain to be held
89  *	on entry and the hash chain will remain held on return.
90  *
91  *	pageq scanning can only occur with the pageq in question locked.
92  *	We have a known bottleneck with the active queue, but the cache
93  *	and free queues are actually arrays already.
94  */
95 
96 /*
97  *	Resident memory management module.
98  */
99 
100 #include <sys/cdefs.h>
101 __FBSDID("$FreeBSD$");
102 
103 #include "opt_vm.h"
104 
105 #include <sys/param.h>
106 #include <sys/systm.h>
107 #include <sys/lock.h>
108 #include <sys/kernel.h>
109 #include <sys/limits.h>
110 #include <sys/malloc.h>
111 #include <sys/msgbuf.h>
112 #include <sys/mutex.h>
113 #include <sys/proc.h>
114 #include <sys/sysctl.h>
115 #include <sys/vmmeter.h>
116 #include <sys/vnode.h>
117 
118 #include <vm/vm.h>
119 #include <vm/pmap.h>
120 #include <vm/vm_param.h>
121 #include <vm/vm_kern.h>
122 #include <vm/vm_object.h>
123 #include <vm/vm_page.h>
124 #include <vm/vm_pageout.h>
125 #include <vm/vm_pager.h>
126 #include <vm/vm_phys.h>
127 #include <vm/vm_reserv.h>
128 #include <vm/vm_extern.h>
129 #include <vm/uma.h>
130 #include <vm/uma_int.h>
131 
132 #include <machine/md_var.h>
133 
134 /*
135  *	Associated with page of user-allocatable memory is a
136  *	page structure.
137  */
138 
139 struct vpgqueues vm_page_queues[PQ_COUNT];
140 struct vpglocks vm_page_queue_lock;
141 struct vpglocks vm_page_queue_free_lock;
142 
143 struct vpglocks	pa_lock[PA_LOCK_COUNT];
144 
145 vm_page_t vm_page_array = 0;
146 int vm_page_array_size = 0;
147 long first_page = 0;
148 int vm_page_zero_count = 0;
149 
150 static int boot_pages = UMA_BOOT_PAGES;
151 TUNABLE_INT("vm.boot_pages", &boot_pages);
152 SYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RD, &boot_pages, 0,
153 	"number of pages allocated for bootstrapping the VM system");
154 
155 static int pa_tryrelock_restart;
156 SYSCTL_INT(_vm, OID_AUTO, tryrelock_restart, CTLFLAG_RD,
157     &pa_tryrelock_restart, 0, "Number of tryrelock restarts");
158 
159 static void vm_page_clear_dirty_mask(vm_page_t m, int pagebits);
160 static void vm_page_queue_remove(int queue, vm_page_t m);
161 static void vm_page_enqueue(int queue, vm_page_t m);
162 
163 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */
164 #if PAGE_SIZE == 32768
165 #ifdef CTASSERT
166 CTASSERT(sizeof(u_long) >= 8);
167 #endif
168 #endif
169 
170 /*
171  * Try to acquire a physical address lock while a pmap is locked.  If we
172  * fail to trylock we unlock and lock the pmap directly and cache the
173  * locked pa in *locked.  The caller should then restart their loop in case
174  * the virtual to physical mapping has changed.
175  */
176 int
177 vm_page_pa_tryrelock(pmap_t pmap, vm_paddr_t pa, vm_paddr_t *locked)
178 {
179 	vm_paddr_t lockpa;
180 
181 	lockpa = *locked;
182 	*locked = pa;
183 	if (lockpa) {
184 		PA_LOCK_ASSERT(lockpa, MA_OWNED);
185 		if (PA_LOCKPTR(pa) == PA_LOCKPTR(lockpa))
186 			return (0);
187 		PA_UNLOCK(lockpa);
188 	}
189 	if (PA_TRYLOCK(pa))
190 		return (0);
191 	PMAP_UNLOCK(pmap);
192 	atomic_add_int(&pa_tryrelock_restart, 1);
193 	PA_LOCK(pa);
194 	PMAP_LOCK(pmap);
195 	return (EAGAIN);
196 }
197 
198 /*
199  *	vm_set_page_size:
200  *
201  *	Sets the page size, perhaps based upon the memory
202  *	size.  Must be called before any use of page-size
203  *	dependent functions.
204  */
205 void
206 vm_set_page_size(void)
207 {
208 	if (cnt.v_page_size == 0)
209 		cnt.v_page_size = PAGE_SIZE;
210 	if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0)
211 		panic("vm_set_page_size: page size not a power of two");
212 }
213 
214 /*
215  *	vm_page_blacklist_lookup:
216  *
217  *	See if a physical address in this page has been listed
218  *	in the blacklist tunable.  Entries in the tunable are
219  *	separated by spaces or commas.  If an invalid integer is
220  *	encountered then the rest of the string is skipped.
221  */
222 static int
223 vm_page_blacklist_lookup(char *list, vm_paddr_t pa)
224 {
225 	vm_paddr_t bad;
226 	char *cp, *pos;
227 
228 	for (pos = list; *pos != '\0'; pos = cp) {
229 		bad = strtoq(pos, &cp, 0);
230 		if (*cp != '\0') {
231 			if (*cp == ' ' || *cp == ',') {
232 				cp++;
233 				if (cp == pos)
234 					continue;
235 			} else
236 				break;
237 		}
238 		if (pa == trunc_page(bad))
239 			return (1);
240 	}
241 	return (0);
242 }
243 
244 /*
245  *	vm_page_startup:
246  *
247  *	Initializes the resident memory module.
248  *
249  *	Allocates memory for the page cells, and
250  *	for the object/offset-to-page hash table headers.
251  *	Each page cell is initialized and placed on the free list.
252  */
253 vm_offset_t
254 vm_page_startup(vm_offset_t vaddr)
255 {
256 	vm_offset_t mapped;
257 	vm_paddr_t page_range;
258 	vm_paddr_t new_end;
259 	int i;
260 	vm_paddr_t pa;
261 	vm_paddr_t last_pa;
262 	char *list;
263 
264 	/* the biggest memory array is the second group of pages */
265 	vm_paddr_t end;
266 	vm_paddr_t biggestsize;
267 	vm_paddr_t low_water, high_water;
268 	int biggestone;
269 
270 	biggestsize = 0;
271 	biggestone = 0;
272 	vaddr = round_page(vaddr);
273 
274 	for (i = 0; phys_avail[i + 1]; i += 2) {
275 		phys_avail[i] = round_page(phys_avail[i]);
276 		phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
277 	}
278 
279 	low_water = phys_avail[0];
280 	high_water = phys_avail[1];
281 
282 	for (i = 0; phys_avail[i + 1]; i += 2) {
283 		vm_paddr_t size = phys_avail[i + 1] - phys_avail[i];
284 
285 		if (size > biggestsize) {
286 			biggestone = i;
287 			biggestsize = size;
288 		}
289 		if (phys_avail[i] < low_water)
290 			low_water = phys_avail[i];
291 		if (phys_avail[i + 1] > high_water)
292 			high_water = phys_avail[i + 1];
293 	}
294 
295 #ifdef XEN
296 	low_water = 0;
297 #endif
298 
299 	end = phys_avail[biggestone+1];
300 
301 	/*
302 	 * Initialize the locks.
303 	 */
304 	mtx_init(&vm_page_queue_mtx, "vm page queue mutex", NULL, MTX_DEF |
305 	    MTX_RECURSE);
306 	mtx_init(&vm_page_queue_free_mtx, "vm page queue free mutex", NULL,
307 	    MTX_DEF);
308 
309 	/* Setup page locks. */
310 	for (i = 0; i < PA_LOCK_COUNT; i++)
311 		mtx_init(&pa_lock[i].data, "page lock", NULL, MTX_DEF);
312 
313 	/*
314 	 * Initialize the queue headers for the hold queue, the active queue,
315 	 * and the inactive queue.
316 	 */
317 	for (i = 0; i < PQ_COUNT; i++)
318 		TAILQ_INIT(&vm_page_queues[i].pl);
319 	vm_page_queues[PQ_INACTIVE].cnt = &cnt.v_inactive_count;
320 	vm_page_queues[PQ_ACTIVE].cnt = &cnt.v_active_count;
321 	vm_page_queues[PQ_HOLD].cnt = &cnt.v_active_count;
322 
323 	/*
324 	 * Allocate memory for use when boot strapping the kernel memory
325 	 * allocator.
326 	 */
327 	new_end = end - (boot_pages * UMA_SLAB_SIZE);
328 	new_end = trunc_page(new_end);
329 	mapped = pmap_map(&vaddr, new_end, end,
330 	    VM_PROT_READ | VM_PROT_WRITE);
331 	bzero((void *)mapped, end - new_end);
332 	uma_startup((void *)mapped, boot_pages);
333 
334 #if defined(__amd64__) || defined(__i386__) || defined(__arm__) || \
335     defined(__mips__)
336 	/*
337 	 * Allocate a bitmap to indicate that a random physical page
338 	 * needs to be included in a minidump.
339 	 *
340 	 * The amd64 port needs this to indicate which direct map pages
341 	 * need to be dumped, via calls to dump_add_page()/dump_drop_page().
342 	 *
343 	 * However, i386 still needs this workspace internally within the
344 	 * minidump code.  In theory, they are not needed on i386, but are
345 	 * included should the sf_buf code decide to use them.
346 	 */
347 	last_pa = 0;
348 	for (i = 0; dump_avail[i + 1] != 0; i += 2)
349 		if (dump_avail[i + 1] > last_pa)
350 			last_pa = dump_avail[i + 1];
351 	page_range = last_pa / PAGE_SIZE;
352 	vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY);
353 	new_end -= vm_page_dump_size;
354 	vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
355 	    new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
356 	bzero((void *)vm_page_dump, vm_page_dump_size);
357 #endif
358 #ifdef __amd64__
359 	/*
360 	 * Request that the physical pages underlying the message buffer be
361 	 * included in a crash dump.  Since the message buffer is accessed
362 	 * through the direct map, they are not automatically included.
363 	 */
364 	pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr);
365 	last_pa = pa + round_page(msgbufsize);
366 	while (pa < last_pa) {
367 		dump_add_page(pa);
368 		pa += PAGE_SIZE;
369 	}
370 #endif
371 	/*
372 	 * Compute the number of pages of memory that will be available for
373 	 * use (taking into account the overhead of a page structure per
374 	 * page).
375 	 */
376 	first_page = low_water / PAGE_SIZE;
377 #ifdef VM_PHYSSEG_SPARSE
378 	page_range = 0;
379 	for (i = 0; phys_avail[i + 1] != 0; i += 2)
380 		page_range += atop(phys_avail[i + 1] - phys_avail[i]);
381 #elif defined(VM_PHYSSEG_DENSE)
382 	page_range = high_water / PAGE_SIZE - first_page;
383 #else
384 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
385 #endif
386 	end = new_end;
387 
388 	/*
389 	 * Reserve an unmapped guard page to trap access to vm_page_array[-1].
390 	 */
391 	vaddr += PAGE_SIZE;
392 
393 	/*
394 	 * Initialize the mem entry structures now, and put them in the free
395 	 * queue.
396 	 */
397 	new_end = trunc_page(end - page_range * sizeof(struct vm_page));
398 	mapped = pmap_map(&vaddr, new_end, end,
399 	    VM_PROT_READ | VM_PROT_WRITE);
400 	vm_page_array = (vm_page_t) mapped;
401 #if VM_NRESERVLEVEL > 0
402 	/*
403 	 * Allocate memory for the reservation management system's data
404 	 * structures.
405 	 */
406 	new_end = vm_reserv_startup(&vaddr, new_end, high_water);
407 #endif
408 #if defined(__amd64__) || defined(__mips__)
409 	/*
410 	 * pmap_map on amd64 and mips can come out of the direct-map, not kvm
411 	 * like i386, so the pages must be tracked for a crashdump to include
412 	 * this data.  This includes the vm_page_array and the early UMA
413 	 * bootstrap pages.
414 	 */
415 	for (pa = new_end; pa < phys_avail[biggestone + 1]; pa += PAGE_SIZE)
416 		dump_add_page(pa);
417 #endif
418 	phys_avail[biggestone + 1] = new_end;
419 
420 	/*
421 	 * Clear all of the page structures
422 	 */
423 	bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
424 	for (i = 0; i < page_range; i++)
425 		vm_page_array[i].order = VM_NFREEORDER;
426 	vm_page_array_size = page_range;
427 
428 	/*
429 	 * Initialize the physical memory allocator.
430 	 */
431 	vm_phys_init();
432 
433 	/*
434 	 * Add every available physical page that is not blacklisted to
435 	 * the free lists.
436 	 */
437 	cnt.v_page_count = 0;
438 	cnt.v_free_count = 0;
439 	list = getenv("vm.blacklist");
440 	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
441 		pa = phys_avail[i];
442 		last_pa = phys_avail[i + 1];
443 		while (pa < last_pa) {
444 			if (list != NULL &&
445 			    vm_page_blacklist_lookup(list, pa))
446 				printf("Skipping page with pa 0x%jx\n",
447 				    (uintmax_t)pa);
448 			else
449 				vm_phys_add_page(pa);
450 			pa += PAGE_SIZE;
451 		}
452 	}
453 	freeenv(list);
454 #if VM_NRESERVLEVEL > 0
455 	/*
456 	 * Initialize the reservation management system.
457 	 */
458 	vm_reserv_init();
459 #endif
460 	return (vaddr);
461 }
462 
463 void
464 vm_page_flag_set(vm_page_t m, unsigned short bits)
465 {
466 
467 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
468 	/*
469 	 * The PG_WRITEABLE flag can only be set if the page is managed and
470 	 * VPO_BUSY.  Currently, this flag is only set by pmap_enter().
471 	 */
472 	KASSERT((bits & PG_WRITEABLE) == 0 ||
473 	    ((m->flags & (PG_UNMANAGED | PG_FICTITIOUS)) == 0 &&
474 	    (m->oflags & VPO_BUSY) != 0), ("PG_WRITEABLE and !VPO_BUSY"));
475 	m->flags |= bits;
476 }
477 
478 void
479 vm_page_flag_clear(vm_page_t m, unsigned short bits)
480 {
481 
482 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
483 	/*
484 	 * The PG_REFERENCED flag can only be cleared if the object
485 	 * containing the page is locked.
486 	 */
487 	KASSERT((bits & PG_REFERENCED) == 0 || VM_OBJECT_LOCKED(m->object),
488 	    ("PG_REFERENCED and !VM_OBJECT_LOCKED"));
489 	m->flags &= ~bits;
490 }
491 
492 void
493 vm_page_busy(vm_page_t m)
494 {
495 
496 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
497 	KASSERT((m->oflags & VPO_BUSY) == 0,
498 	    ("vm_page_busy: page already busy!!!"));
499 	m->oflags |= VPO_BUSY;
500 }
501 
502 /*
503  *      vm_page_flash:
504  *
505  *      wakeup anyone waiting for the page.
506  */
507 void
508 vm_page_flash(vm_page_t m)
509 {
510 
511 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
512 	if (m->oflags & VPO_WANTED) {
513 		m->oflags &= ~VPO_WANTED;
514 		wakeup(m);
515 	}
516 }
517 
518 /*
519  *      vm_page_wakeup:
520  *
521  *      clear the VPO_BUSY flag and wakeup anyone waiting for the
522  *      page.
523  *
524  */
525 void
526 vm_page_wakeup(vm_page_t m)
527 {
528 
529 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
530 	KASSERT(m->oflags & VPO_BUSY, ("vm_page_wakeup: page not busy!!!"));
531 	m->oflags &= ~VPO_BUSY;
532 	vm_page_flash(m);
533 }
534 
535 void
536 vm_page_io_start(vm_page_t m)
537 {
538 
539 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
540 	m->busy++;
541 }
542 
543 void
544 vm_page_io_finish(vm_page_t m)
545 {
546 
547 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
548 	KASSERT(m->busy > 0, ("vm_page_io_finish: page %p is not busy", m));
549 	m->busy--;
550 	if (m->busy == 0)
551 		vm_page_flash(m);
552 }
553 
554 /*
555  * Keep page from being freed by the page daemon
556  * much of the same effect as wiring, except much lower
557  * overhead and should be used only for *very* temporary
558  * holding ("wiring").
559  */
560 void
561 vm_page_hold(vm_page_t mem)
562 {
563 
564 	vm_page_lock_assert(mem, MA_OWNED);
565         mem->hold_count++;
566 }
567 
568 void
569 vm_page_unhold(vm_page_t mem)
570 {
571 
572 	vm_page_lock_assert(mem, MA_OWNED);
573 	--mem->hold_count;
574 	KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!"));
575 	if (mem->hold_count == 0 && mem->queue == PQ_HOLD)
576 		vm_page_free_toq(mem);
577 }
578 
579 /*
580  *	vm_page_unhold_pages:
581  *
582  *	Unhold each of the pages that is referenced by the given array.
583  */
584 void
585 vm_page_unhold_pages(vm_page_t *ma, int count)
586 {
587 	struct mtx *mtx, *new_mtx;
588 
589 	mtx = NULL;
590 	for (; count != 0; count--) {
591 		/*
592 		 * Avoid releasing and reacquiring the same page lock.
593 		 */
594 		new_mtx = vm_page_lockptr(*ma);
595 		if (mtx != new_mtx) {
596 			if (mtx != NULL)
597 				mtx_unlock(mtx);
598 			mtx = new_mtx;
599 			mtx_lock(mtx);
600 		}
601 		vm_page_unhold(*ma);
602 		ma++;
603 	}
604 	if (mtx != NULL)
605 		mtx_unlock(mtx);
606 }
607 
608 /*
609  *	vm_page_free:
610  *
611  *	Free a page.
612  */
613 void
614 vm_page_free(vm_page_t m)
615 {
616 
617 	m->flags &= ~PG_ZERO;
618 	vm_page_free_toq(m);
619 }
620 
621 /*
622  *	vm_page_free_zero:
623  *
624  *	Free a page to the zerod-pages queue
625  */
626 void
627 vm_page_free_zero(vm_page_t m)
628 {
629 
630 	m->flags |= PG_ZERO;
631 	vm_page_free_toq(m);
632 }
633 
634 /*
635  *	vm_page_sleep:
636  *
637  *	Sleep and release the page and page queues locks.
638  *
639  *	The object containing the given page must be locked.
640  */
641 void
642 vm_page_sleep(vm_page_t m, const char *msg)
643 {
644 
645 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
646 	if (mtx_owned(&vm_page_queue_mtx))
647 		vm_page_unlock_queues();
648 	if (mtx_owned(vm_page_lockptr(m)))
649 		vm_page_unlock(m);
650 
651 	/*
652 	 * It's possible that while we sleep, the page will get
653 	 * unbusied and freed.  If we are holding the object
654 	 * lock, we will assume we hold a reference to the object
655 	 * such that even if m->object changes, we can re-lock
656 	 * it.
657 	 */
658 	m->oflags |= VPO_WANTED;
659 	msleep(m, VM_OBJECT_MTX(m->object), PVM, msg, 0);
660 }
661 
662 /*
663  *	vm_page_dirty:
664  *
665  *	make page all dirty
666  */
667 void
668 vm_page_dirty(vm_page_t m)
669 {
670 
671 	KASSERT((m->flags & PG_CACHED) == 0,
672 	    ("vm_page_dirty: page in cache!"));
673 	KASSERT(!VM_PAGE_IS_FREE(m),
674 	    ("vm_page_dirty: page is free!"));
675 	KASSERT(m->valid == VM_PAGE_BITS_ALL,
676 	    ("vm_page_dirty: page is invalid!"));
677 	m->dirty = VM_PAGE_BITS_ALL;
678 }
679 
680 /*
681  *	vm_page_splay:
682  *
683  *	Implements Sleator and Tarjan's top-down splay algorithm.  Returns
684  *	the vm_page containing the given pindex.  If, however, that
685  *	pindex is not found in the vm_object, returns a vm_page that is
686  *	adjacent to the pindex, coming before or after it.
687  */
688 vm_page_t
689 vm_page_splay(vm_pindex_t pindex, vm_page_t root)
690 {
691 	struct vm_page dummy;
692 	vm_page_t lefttreemax, righttreemin, y;
693 
694 	if (root == NULL)
695 		return (root);
696 	lefttreemax = righttreemin = &dummy;
697 	for (;; root = y) {
698 		if (pindex < root->pindex) {
699 			if ((y = root->left) == NULL)
700 				break;
701 			if (pindex < y->pindex) {
702 				/* Rotate right. */
703 				root->left = y->right;
704 				y->right = root;
705 				root = y;
706 				if ((y = root->left) == NULL)
707 					break;
708 			}
709 			/* Link into the new root's right tree. */
710 			righttreemin->left = root;
711 			righttreemin = root;
712 		} else if (pindex > root->pindex) {
713 			if ((y = root->right) == NULL)
714 				break;
715 			if (pindex > y->pindex) {
716 				/* Rotate left. */
717 				root->right = y->left;
718 				y->left = root;
719 				root = y;
720 				if ((y = root->right) == NULL)
721 					break;
722 			}
723 			/* Link into the new root's left tree. */
724 			lefttreemax->right = root;
725 			lefttreemax = root;
726 		} else
727 			break;
728 	}
729 	/* Assemble the new root. */
730 	lefttreemax->right = root->left;
731 	righttreemin->left = root->right;
732 	root->left = dummy.right;
733 	root->right = dummy.left;
734 	return (root);
735 }
736 
737 /*
738  *	vm_page_insert:		[ internal use only ]
739  *
740  *	Inserts the given mem entry into the object and object list.
741  *
742  *	The pagetables are not updated but will presumably fault the page
743  *	in if necessary, or if a kernel page the caller will at some point
744  *	enter the page into the kernel's pmap.  We are not allowed to block
745  *	here so we *can't* do this anyway.
746  *
747  *	The object and page must be locked.
748  *	This routine may not block.
749  */
750 void
751 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
752 {
753 	vm_page_t root;
754 
755 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
756 	if (m->object != NULL)
757 		panic("vm_page_insert: page already inserted");
758 
759 	/*
760 	 * Record the object/offset pair in this page
761 	 */
762 	m->object = object;
763 	m->pindex = pindex;
764 
765 	/*
766 	 * Now link into the object's ordered list of backed pages.
767 	 */
768 	root = object->root;
769 	if (root == NULL) {
770 		m->left = NULL;
771 		m->right = NULL;
772 		TAILQ_INSERT_TAIL(&object->memq, m, listq);
773 	} else {
774 		root = vm_page_splay(pindex, root);
775 		if (pindex < root->pindex) {
776 			m->left = root->left;
777 			m->right = root;
778 			root->left = NULL;
779 			TAILQ_INSERT_BEFORE(root, m, listq);
780 		} else if (pindex == root->pindex)
781 			panic("vm_page_insert: offset already allocated");
782 		else {
783 			m->right = root->right;
784 			m->left = root;
785 			root->right = NULL;
786 			TAILQ_INSERT_AFTER(&object->memq, root, m, listq);
787 		}
788 	}
789 	object->root = m;
790 
791 	/*
792 	 * show that the object has one more resident page.
793 	 */
794 	object->resident_page_count++;
795 	/*
796 	 * Hold the vnode until the last page is released.
797 	 */
798 	if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
799 		vhold((struct vnode *)object->handle);
800 
801 	/*
802 	 * Since we are inserting a new and possibly dirty page,
803 	 * update the object's OBJ_MIGHTBEDIRTY flag.
804 	 */
805 	if (m->flags & PG_WRITEABLE)
806 		vm_object_set_writeable_dirty(object);
807 }
808 
809 /*
810  *	vm_page_remove:
811  *				NOTE: used by device pager as well -wfj
812  *
813  *	Removes the given mem entry from the object/offset-page
814  *	table and the object page list, but do not invalidate/terminate
815  *	the backing store.
816  *
817  *	The object and page must be locked.
818  *	The underlying pmap entry (if any) is NOT removed here.
819  *	This routine may not block.
820  */
821 void
822 vm_page_remove(vm_page_t m)
823 {
824 	vm_object_t object;
825 	vm_page_t root;
826 
827 	if ((m->flags & PG_UNMANAGED) == 0)
828 		vm_page_lock_assert(m, MA_OWNED);
829 	if ((object = m->object) == NULL)
830 		return;
831 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
832 	if (m->oflags & VPO_BUSY) {
833 		m->oflags &= ~VPO_BUSY;
834 		vm_page_flash(m);
835 	}
836 
837 	/*
838 	 * Now remove from the object's list of backed pages.
839 	 */
840 	if (m != object->root)
841 		vm_page_splay(m->pindex, object->root);
842 	if (m->left == NULL)
843 		root = m->right;
844 	else {
845 		root = vm_page_splay(m->pindex, m->left);
846 		root->right = m->right;
847 	}
848 	object->root = root;
849 	TAILQ_REMOVE(&object->memq, m, listq);
850 
851 	/*
852 	 * And show that the object has one fewer resident page.
853 	 */
854 	object->resident_page_count--;
855 	/*
856 	 * The vnode may now be recycled.
857 	 */
858 	if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
859 		vdrop((struct vnode *)object->handle);
860 
861 	m->object = NULL;
862 }
863 
864 /*
865  *	vm_page_lookup:
866  *
867  *	Returns the page associated with the object/offset
868  *	pair specified; if none is found, NULL is returned.
869  *
870  *	The object must be locked.
871  *	This routine may not block.
872  *	This is a critical path routine
873  */
874 vm_page_t
875 vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
876 {
877 	vm_page_t m;
878 
879 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
880 	if ((m = object->root) != NULL && m->pindex != pindex) {
881 		m = vm_page_splay(pindex, m);
882 		if ((object->root = m)->pindex != pindex)
883 			m = NULL;
884 	}
885 	return (m);
886 }
887 
888 /*
889  *	vm_page_find_least:
890  *
891  *	Returns the page associated with the object with least pindex
892  *	greater than or equal to the parameter pindex, or NULL.
893  *
894  *	The object must be locked.
895  *	The routine may not block.
896  */
897 vm_page_t
898 vm_page_find_least(vm_object_t object, vm_pindex_t pindex)
899 {
900 	vm_page_t m;
901 
902 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
903 	if ((m = TAILQ_FIRST(&object->memq)) != NULL) {
904 		if (m->pindex < pindex) {
905 			m = vm_page_splay(pindex, object->root);
906 			if ((object->root = m)->pindex < pindex)
907 				m = TAILQ_NEXT(m, listq);
908 		}
909 	}
910 	return (m);
911 }
912 
913 /*
914  * Returns the given page's successor (by pindex) within the object if it is
915  * resident; if none is found, NULL is returned.
916  *
917  * The object must be locked.
918  */
919 vm_page_t
920 vm_page_next(vm_page_t m)
921 {
922 	vm_page_t next;
923 
924 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
925 	if ((next = TAILQ_NEXT(m, listq)) != NULL &&
926 	    next->pindex != m->pindex + 1)
927 		next = NULL;
928 	return (next);
929 }
930 
931 /*
932  * Returns the given page's predecessor (by pindex) within the object if it is
933  * resident; if none is found, NULL is returned.
934  *
935  * The object must be locked.
936  */
937 vm_page_t
938 vm_page_prev(vm_page_t m)
939 {
940 	vm_page_t prev;
941 
942 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
943 	if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL &&
944 	    prev->pindex != m->pindex - 1)
945 		prev = NULL;
946 	return (prev);
947 }
948 
949 /*
950  *	vm_page_rename:
951  *
952  *	Move the given memory entry from its
953  *	current object to the specified target object/offset.
954  *
955  *	The object must be locked.
956  *	This routine may not block.
957  *
958  *	Note: swap associated with the page must be invalidated by the move.  We
959  *	      have to do this for several reasons:  (1) we aren't freeing the
960  *	      page, (2) we are dirtying the page, (3) the VM system is probably
961  *	      moving the page from object A to B, and will then later move
962  *	      the backing store from A to B and we can't have a conflict.
963  *
964  *	Note: we *always* dirty the page.  It is necessary both for the
965  *	      fact that we moved it, and because we may be invalidating
966  *	      swap.  If the page is on the cache, we have to deactivate it
967  *	      or vm_page_dirty() will panic.  Dirty pages are not allowed
968  *	      on the cache.
969  */
970 void
971 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
972 {
973 
974 	vm_page_remove(m);
975 	vm_page_insert(m, new_object, new_pindex);
976 	vm_page_dirty(m);
977 }
978 
979 /*
980  *	Convert all of the given object's cached pages that have a
981  *	pindex within the given range into free pages.  If the value
982  *	zero is given for "end", then the range's upper bound is
983  *	infinity.  If the given object is backed by a vnode and it
984  *	transitions from having one or more cached pages to none, the
985  *	vnode's hold count is reduced.
986  */
987 void
988 vm_page_cache_free(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
989 {
990 	vm_page_t m, m_next;
991 	boolean_t empty;
992 
993 	mtx_lock(&vm_page_queue_free_mtx);
994 	if (__predict_false(object->cache == NULL)) {
995 		mtx_unlock(&vm_page_queue_free_mtx);
996 		return;
997 	}
998 	m = object->cache = vm_page_splay(start, object->cache);
999 	if (m->pindex < start) {
1000 		if (m->right == NULL)
1001 			m = NULL;
1002 		else {
1003 			m_next = vm_page_splay(start, m->right);
1004 			m_next->left = m;
1005 			m->right = NULL;
1006 			m = object->cache = m_next;
1007 		}
1008 	}
1009 
1010 	/*
1011 	 * At this point, "m" is either (1) a reference to the page
1012 	 * with the least pindex that is greater than or equal to
1013 	 * "start" or (2) NULL.
1014 	 */
1015 	for (; m != NULL && (m->pindex < end || end == 0); m = m_next) {
1016 		/*
1017 		 * Find "m"'s successor and remove "m" from the
1018 		 * object's cache.
1019 		 */
1020 		if (m->right == NULL) {
1021 			object->cache = m->left;
1022 			m_next = NULL;
1023 		} else {
1024 			m_next = vm_page_splay(start, m->right);
1025 			m_next->left = m->left;
1026 			object->cache = m_next;
1027 		}
1028 		/* Convert "m" to a free page. */
1029 		m->object = NULL;
1030 		m->valid = 0;
1031 		/* Clear PG_CACHED and set PG_FREE. */
1032 		m->flags ^= PG_CACHED | PG_FREE;
1033 		KASSERT((m->flags & (PG_CACHED | PG_FREE)) == PG_FREE,
1034 		    ("vm_page_cache_free: page %p has inconsistent flags", m));
1035 		cnt.v_cache_count--;
1036 		cnt.v_free_count++;
1037 	}
1038 	empty = object->cache == NULL;
1039 	mtx_unlock(&vm_page_queue_free_mtx);
1040 	if (object->type == OBJT_VNODE && empty)
1041 		vdrop(object->handle);
1042 }
1043 
1044 /*
1045  *	Returns the cached page that is associated with the given
1046  *	object and offset.  If, however, none exists, returns NULL.
1047  *
1048  *	The free page queue must be locked.
1049  */
1050 static inline vm_page_t
1051 vm_page_cache_lookup(vm_object_t object, vm_pindex_t pindex)
1052 {
1053 	vm_page_t m;
1054 
1055 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1056 	if ((m = object->cache) != NULL && m->pindex != pindex) {
1057 		m = vm_page_splay(pindex, m);
1058 		if ((object->cache = m)->pindex != pindex)
1059 			m = NULL;
1060 	}
1061 	return (m);
1062 }
1063 
1064 /*
1065  *	Remove the given cached page from its containing object's
1066  *	collection of cached pages.
1067  *
1068  *	The free page queue must be locked.
1069  */
1070 void
1071 vm_page_cache_remove(vm_page_t m)
1072 {
1073 	vm_object_t object;
1074 	vm_page_t root;
1075 
1076 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1077 	KASSERT((m->flags & PG_CACHED) != 0,
1078 	    ("vm_page_cache_remove: page %p is not cached", m));
1079 	object = m->object;
1080 	if (m != object->cache) {
1081 		root = vm_page_splay(m->pindex, object->cache);
1082 		KASSERT(root == m,
1083 		    ("vm_page_cache_remove: page %p is not cached in object %p",
1084 		    m, object));
1085 	}
1086 	if (m->left == NULL)
1087 		root = m->right;
1088 	else if (m->right == NULL)
1089 		root = m->left;
1090 	else {
1091 		root = vm_page_splay(m->pindex, m->left);
1092 		root->right = m->right;
1093 	}
1094 	object->cache = root;
1095 	m->object = NULL;
1096 	cnt.v_cache_count--;
1097 }
1098 
1099 /*
1100  *	Transfer all of the cached pages with offset greater than or
1101  *	equal to 'offidxstart' from the original object's cache to the
1102  *	new object's cache.  However, any cached pages with offset
1103  *	greater than or equal to the new object's size are kept in the
1104  *	original object.  Initially, the new object's cache must be
1105  *	empty.  Offset 'offidxstart' in the original object must
1106  *	correspond to offset zero in the new object.
1107  *
1108  *	The new object must be locked.
1109  */
1110 void
1111 vm_page_cache_transfer(vm_object_t orig_object, vm_pindex_t offidxstart,
1112     vm_object_t new_object)
1113 {
1114 	vm_page_t m, m_next;
1115 
1116 	/*
1117 	 * Insertion into an object's collection of cached pages
1118 	 * requires the object to be locked.  In contrast, removal does
1119 	 * not.
1120 	 */
1121 	VM_OBJECT_LOCK_ASSERT(new_object, MA_OWNED);
1122 	KASSERT(new_object->cache == NULL,
1123 	    ("vm_page_cache_transfer: object %p has cached pages",
1124 	    new_object));
1125 	mtx_lock(&vm_page_queue_free_mtx);
1126 	if ((m = orig_object->cache) != NULL) {
1127 		/*
1128 		 * Transfer all of the pages with offset greater than or
1129 		 * equal to 'offidxstart' from the original object's
1130 		 * cache to the new object's cache.
1131 		 */
1132 		m = vm_page_splay(offidxstart, m);
1133 		if (m->pindex < offidxstart) {
1134 			orig_object->cache = m;
1135 			new_object->cache = m->right;
1136 			m->right = NULL;
1137 		} else {
1138 			orig_object->cache = m->left;
1139 			new_object->cache = m;
1140 			m->left = NULL;
1141 		}
1142 		while ((m = new_object->cache) != NULL) {
1143 			if ((m->pindex - offidxstart) >= new_object->size) {
1144 				/*
1145 				 * Return all of the cached pages with
1146 				 * offset greater than or equal to the
1147 				 * new object's size to the original
1148 				 * object's cache.
1149 				 */
1150 				new_object->cache = m->left;
1151 				m->left = orig_object->cache;
1152 				orig_object->cache = m;
1153 				break;
1154 			}
1155 			m_next = vm_page_splay(m->pindex, m->right);
1156 			/* Update the page's object and offset. */
1157 			m->object = new_object;
1158 			m->pindex -= offidxstart;
1159 			if (m_next == NULL)
1160 				break;
1161 			m->right = NULL;
1162 			m_next->left = m;
1163 			new_object->cache = m_next;
1164 		}
1165 		KASSERT(new_object->cache == NULL ||
1166 		    new_object->type == OBJT_SWAP,
1167 		    ("vm_page_cache_transfer: object %p's type is incompatible"
1168 		    " with cached pages", new_object));
1169 	}
1170 	mtx_unlock(&vm_page_queue_free_mtx);
1171 }
1172 
1173 /*
1174  *	vm_page_alloc:
1175  *
1176  *	Allocate and return a memory cell associated
1177  *	with this VM object/offset pair.
1178  *
1179  *	The caller must always specify an allocation class.
1180  *
1181  *	allocation classes:
1182  *	VM_ALLOC_NORMAL		normal process request
1183  *	VM_ALLOC_SYSTEM		system *really* needs a page
1184  *	VM_ALLOC_INTERRUPT	interrupt time request
1185  *
1186  *	optional allocation flags:
1187  *	VM_ALLOC_ZERO		prefer a zeroed page
1188  *	VM_ALLOC_WIRED		wire the allocated page
1189  *	VM_ALLOC_NOOBJ		page is not associated with a vm object
1190  *	VM_ALLOC_NOBUSY		do not set the page busy
1191  *	VM_ALLOC_IFCACHED	return page only if it is cached
1192  *	VM_ALLOC_IFNOTCACHED	return NULL, do not reactivate if the page
1193  *				is cached
1194  *
1195  *	This routine may not sleep.
1196  */
1197 vm_page_t
1198 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
1199 {
1200 	struct vnode *vp = NULL;
1201 	vm_object_t m_object;
1202 	vm_page_t m;
1203 	int flags, page_req;
1204 
1205 	if ((req & VM_ALLOC_NOOBJ) == 0) {
1206 		KASSERT(object != NULL,
1207 		    ("vm_page_alloc: NULL object."));
1208 		VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1209 	}
1210 
1211 	page_req = req & VM_ALLOC_CLASS_MASK;
1212 
1213 	/*
1214 	 * The pager is allowed to eat deeper into the free page list.
1215 	 */
1216 	if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT))
1217 		page_req = VM_ALLOC_SYSTEM;
1218 
1219 	mtx_lock(&vm_page_queue_free_mtx);
1220 	if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved ||
1221 	    (page_req == VM_ALLOC_SYSTEM &&
1222 	    cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) ||
1223 	    (page_req == VM_ALLOC_INTERRUPT &&
1224 	    cnt.v_free_count + cnt.v_cache_count > 0)) {
1225 		/*
1226 		 * Allocate from the free queue if the number of free pages
1227 		 * exceeds the minimum for the request class.
1228 		 */
1229 		if (object != NULL &&
1230 		    (m = vm_page_cache_lookup(object, pindex)) != NULL) {
1231 			if ((req & VM_ALLOC_IFNOTCACHED) != 0) {
1232 				mtx_unlock(&vm_page_queue_free_mtx);
1233 				return (NULL);
1234 			}
1235 			if (vm_phys_unfree_page(m))
1236 				vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, 0);
1237 #if VM_NRESERVLEVEL > 0
1238 			else if (!vm_reserv_reactivate_page(m))
1239 #else
1240 			else
1241 #endif
1242 				panic("vm_page_alloc: cache page %p is missing"
1243 				    " from the free queue", m);
1244 		} else if ((req & VM_ALLOC_IFCACHED) != 0) {
1245 			mtx_unlock(&vm_page_queue_free_mtx);
1246 			return (NULL);
1247 #if VM_NRESERVLEVEL > 0
1248 		} else if (object == NULL || object->type == OBJT_DEVICE ||
1249 		    object->type == OBJT_SG ||
1250 		    (object->flags & OBJ_COLORED) == 0 ||
1251 		    (m = vm_reserv_alloc_page(object, pindex)) == NULL) {
1252 #else
1253 		} else {
1254 #endif
1255 			m = vm_phys_alloc_pages(object != NULL ?
1256 			    VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0);
1257 #if VM_NRESERVLEVEL > 0
1258 			if (m == NULL && vm_reserv_reclaim_inactive()) {
1259 				m = vm_phys_alloc_pages(object != NULL ?
1260 				    VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT,
1261 				    0);
1262 			}
1263 #endif
1264 		}
1265 	} else {
1266 		/*
1267 		 * Not allocatable, give up.
1268 		 */
1269 		mtx_unlock(&vm_page_queue_free_mtx);
1270 		atomic_add_int(&vm_pageout_deficit,
1271 		    MAX((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
1272 		pagedaemon_wakeup();
1273 		return (NULL);
1274 	}
1275 
1276 	/*
1277 	 *  At this point we had better have found a good page.
1278 	 */
1279 
1280 	KASSERT(m != NULL, ("vm_page_alloc: missing page"));
1281 	KASSERT(m->queue == PQ_NONE,
1282 	    ("vm_page_alloc: page %p has unexpected queue %d", m, m->queue));
1283 	KASSERT(m->wire_count == 0, ("vm_page_alloc: page %p is wired", m));
1284 	KASSERT(m->hold_count == 0, ("vm_page_alloc: page %p is held", m));
1285 	KASSERT(m->busy == 0, ("vm_page_alloc: page %p is busy", m));
1286 	KASSERT(m->dirty == 0, ("vm_page_alloc: page %p is dirty", m));
1287 	KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1288 	    ("vm_page_alloc: page %p has unexpected memattr %d", m,
1289 	    pmap_page_get_memattr(m)));
1290 	if ((m->flags & PG_CACHED) != 0) {
1291 		KASSERT(m->valid != 0,
1292 		    ("vm_page_alloc: cached page %p is invalid", m));
1293 		if (m->object == object && m->pindex == pindex)
1294 	  		cnt.v_reactivated++;
1295 		else
1296 			m->valid = 0;
1297 		m_object = m->object;
1298 		vm_page_cache_remove(m);
1299 		if (m_object->type == OBJT_VNODE && m_object->cache == NULL)
1300 			vp = m_object->handle;
1301 	} else {
1302 		KASSERT(VM_PAGE_IS_FREE(m),
1303 		    ("vm_page_alloc: page %p is not free", m));
1304 		KASSERT(m->valid == 0,
1305 		    ("vm_page_alloc: free page %p is valid", m));
1306 		cnt.v_free_count--;
1307 	}
1308 
1309 	/*
1310 	 * Only the PG_ZERO flag is inherited.  The PG_CACHED or PG_FREE flag
1311 	 * must be cleared before the free page queues lock is released.
1312 	 */
1313 	flags = 0;
1314 	if (m->flags & PG_ZERO) {
1315 		vm_page_zero_count--;
1316 		if (req & VM_ALLOC_ZERO)
1317 			flags = PG_ZERO;
1318 	}
1319 	if (object == NULL || object->type == OBJT_PHYS)
1320 		flags |= PG_UNMANAGED;
1321 	m->flags = flags;
1322 	mtx_unlock(&vm_page_queue_free_mtx);
1323 	if (req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ))
1324 		m->oflags = 0;
1325 	else
1326 		m->oflags = VPO_BUSY;
1327 	if (req & VM_ALLOC_WIRED) {
1328 		/*
1329 		 * The page lock is not required for wiring a page until that
1330 		 * page is inserted into the object.
1331 		 */
1332 		atomic_add_int(&cnt.v_wire_count, 1);
1333 		m->wire_count = 1;
1334 	}
1335 	m->act_count = 0;
1336 
1337 	if (object != NULL) {
1338 		/* Ignore device objects; the pager sets "memattr" for them. */
1339 		if (object->memattr != VM_MEMATTR_DEFAULT &&
1340 		    object->type != OBJT_DEVICE && object->type != OBJT_SG)
1341 			pmap_page_set_memattr(m, object->memattr);
1342 		vm_page_insert(m, object, pindex);
1343 	} else
1344 		m->pindex = pindex;
1345 
1346 	/*
1347 	 * The following call to vdrop() must come after the above call
1348 	 * to vm_page_insert() in case both affect the same object and
1349 	 * vnode.  Otherwise, the affected vnode's hold count could
1350 	 * temporarily become zero.
1351 	 */
1352 	if (vp != NULL)
1353 		vdrop(vp);
1354 
1355 	/*
1356 	 * Don't wakeup too often - wakeup the pageout daemon when
1357 	 * we would be nearly out of memory.
1358 	 */
1359 	if (vm_paging_needed())
1360 		pagedaemon_wakeup();
1361 
1362 	return (m);
1363 }
1364 
1365 /*
1366  * Initialize a page that has been freshly dequeued from a freelist.
1367  * The caller has to drop the vnode returned, if it is not NULL.
1368  *
1369  * To be called with vm_page_queue_free_mtx held.
1370  */
1371 struct vnode *
1372 vm_page_alloc_init(vm_page_t m)
1373 {
1374 	struct vnode *drop;
1375 	vm_object_t m_object;
1376 
1377 	KASSERT(m->queue == PQ_NONE,
1378 	    ("vm_page_alloc_init: page %p has unexpected queue %d",
1379 	    m, m->queue));
1380 	KASSERT(m->wire_count == 0,
1381 	    ("vm_page_alloc_init: page %p is wired", m));
1382 	KASSERT(m->hold_count == 0,
1383 	    ("vm_page_alloc_init: page %p is held", m));
1384 	KASSERT(m->busy == 0,
1385 	    ("vm_page_alloc_init: page %p is busy", m));
1386 	KASSERT(m->dirty == 0,
1387 	    ("vm_page_alloc_init: page %p is dirty", m));
1388 	KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1389 	    ("vm_page_alloc_init: page %p has unexpected memattr %d",
1390 	    m, pmap_page_get_memattr(m)));
1391 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1392 	drop = NULL;
1393 	if ((m->flags & PG_CACHED) != 0) {
1394 		m->valid = 0;
1395 		m_object = m->object;
1396 		vm_page_cache_remove(m);
1397 		if (m_object->type == OBJT_VNODE &&
1398 		    m_object->cache == NULL)
1399 			drop = m_object->handle;
1400 	} else {
1401 		KASSERT(VM_PAGE_IS_FREE(m),
1402 		    ("vm_page_alloc_init: page %p is not free", m));
1403 		KASSERT(m->valid == 0,
1404 		    ("vm_page_alloc_init: free page %p is valid", m));
1405 		cnt.v_free_count--;
1406 	}
1407 	if (m->flags & PG_ZERO)
1408 		vm_page_zero_count--;
1409 	/* Don't clear the PG_ZERO flag; we'll need it later. */
1410 	m->flags = PG_UNMANAGED | (m->flags & PG_ZERO);
1411 	m->oflags = 0;
1412 	/* Unmanaged pages don't use "act_count". */
1413 	return (drop);
1414 }
1415 
1416 /*
1417  * 	vm_page_alloc_freelist:
1418  *
1419  *	Allocate a page from the specified freelist.
1420  *	Only the ALLOC_CLASS values in req are honored, other request flags
1421  *	are ignored.
1422  */
1423 vm_page_t
1424 vm_page_alloc_freelist(int flind, int req)
1425 {
1426 	struct vnode *drop;
1427 	vm_page_t m;
1428 	int page_req;
1429 
1430 	m = NULL;
1431 	page_req = req & VM_ALLOC_CLASS_MASK;
1432 	mtx_lock(&vm_page_queue_free_mtx);
1433 	/*
1434 	 * Do not allocate reserved pages unless the req has asked for it.
1435 	 */
1436 	if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved ||
1437 	    (page_req == VM_ALLOC_SYSTEM &&
1438 	    cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) ||
1439 	    (page_req == VM_ALLOC_INTERRUPT &&
1440 	    cnt.v_free_count + cnt.v_cache_count > 0)) {
1441 		m = vm_phys_alloc_freelist_pages(flind, VM_FREEPOOL_DIRECT, 0);
1442 	}
1443 	if (m == NULL) {
1444 		mtx_unlock(&vm_page_queue_free_mtx);
1445 		return (NULL);
1446 	}
1447 	drop = vm_page_alloc_init(m);
1448 	mtx_unlock(&vm_page_queue_free_mtx);
1449 	if (drop)
1450 		vdrop(drop);
1451 	return (m);
1452 }
1453 
1454 /*
1455  *	vm_wait:	(also see VM_WAIT macro)
1456  *
1457  *	Block until free pages are available for allocation
1458  *	- Called in various places before memory allocations.
1459  */
1460 void
1461 vm_wait(void)
1462 {
1463 
1464 	mtx_lock(&vm_page_queue_free_mtx);
1465 	if (curproc == pageproc) {
1466 		vm_pageout_pages_needed = 1;
1467 		msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx,
1468 		    PDROP | PSWP, "VMWait", 0);
1469 	} else {
1470 		if (!vm_pages_needed) {
1471 			vm_pages_needed = 1;
1472 			wakeup(&vm_pages_needed);
1473 		}
1474 		msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM,
1475 		    "vmwait", 0);
1476 	}
1477 }
1478 
1479 /*
1480  *	vm_waitpfault:	(also see VM_WAITPFAULT macro)
1481  *
1482  *	Block until free pages are available for allocation
1483  *	- Called only in vm_fault so that processes page faulting
1484  *	  can be easily tracked.
1485  *	- Sleeps at a lower priority than vm_wait() so that vm_wait()ing
1486  *	  processes will be able to grab memory first.  Do not change
1487  *	  this balance without careful testing first.
1488  */
1489 void
1490 vm_waitpfault(void)
1491 {
1492 
1493 	mtx_lock(&vm_page_queue_free_mtx);
1494 	if (!vm_pages_needed) {
1495 		vm_pages_needed = 1;
1496 		wakeup(&vm_pages_needed);
1497 	}
1498 	msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER,
1499 	    "pfault", 0);
1500 }
1501 
1502 /*
1503  *	vm_page_requeue:
1504  *
1505  *	Move the given page to the tail of its present page queue.
1506  *
1507  *	The page queues must be locked.
1508  */
1509 void
1510 vm_page_requeue(vm_page_t m)
1511 {
1512 	struct vpgqueues *vpq;
1513 	int queue;
1514 
1515 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1516 	queue = m->queue;
1517 	KASSERT(queue != PQ_NONE,
1518 	    ("vm_page_requeue: page %p is not queued", m));
1519 	vpq = &vm_page_queues[queue];
1520 	TAILQ_REMOVE(&vpq->pl, m, pageq);
1521 	TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
1522 }
1523 
1524 /*
1525  *	vm_page_queue_remove:
1526  *
1527  *	Remove the given page from the specified queue.
1528  *
1529  *	The page and page queues must be locked.
1530  */
1531 static __inline void
1532 vm_page_queue_remove(int queue, vm_page_t m)
1533 {
1534 	struct vpgqueues *pq;
1535 
1536 	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1537 	vm_page_lock_assert(m, MA_OWNED);
1538 	pq = &vm_page_queues[queue];
1539 	TAILQ_REMOVE(&pq->pl, m, pageq);
1540 	(*pq->cnt)--;
1541 }
1542 
1543 /*
1544  *	vm_pageq_remove:
1545  *
1546  *	Remove a page from its queue.
1547  *
1548  *	The given page must be locked.
1549  *	This routine may not block.
1550  */
1551 void
1552 vm_pageq_remove(vm_page_t m)
1553 {
1554 	int queue;
1555 
1556 	vm_page_lock_assert(m, MA_OWNED);
1557 	if ((queue = m->queue) != PQ_NONE) {
1558 		vm_page_lock_queues();
1559 		m->queue = PQ_NONE;
1560 		vm_page_queue_remove(queue, m);
1561 		vm_page_unlock_queues();
1562 	}
1563 }
1564 
1565 /*
1566  *	vm_page_enqueue:
1567  *
1568  *	Add the given page to the specified queue.
1569  *
1570  *	The page queues must be locked.
1571  */
1572 static void
1573 vm_page_enqueue(int queue, vm_page_t m)
1574 {
1575 	struct vpgqueues *vpq;
1576 
1577 	vpq = &vm_page_queues[queue];
1578 	m->queue = queue;
1579 	TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
1580 	++*vpq->cnt;
1581 }
1582 
1583 /*
1584  *	vm_page_activate:
1585  *
1586  *	Put the specified page on the active list (if appropriate).
1587  *	Ensure that act_count is at least ACT_INIT but do not otherwise
1588  *	mess with it.
1589  *
1590  *	The page must be locked.
1591  *	This routine may not block.
1592  */
1593 void
1594 vm_page_activate(vm_page_t m)
1595 {
1596 	int queue;
1597 
1598 	vm_page_lock_assert(m, MA_OWNED);
1599 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1600 	if ((queue = m->queue) != PQ_ACTIVE) {
1601 		if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
1602 			if (m->act_count < ACT_INIT)
1603 				m->act_count = ACT_INIT;
1604 			vm_page_lock_queues();
1605 			if (queue != PQ_NONE)
1606 				vm_page_queue_remove(queue, m);
1607 			vm_page_enqueue(PQ_ACTIVE, m);
1608 			vm_page_unlock_queues();
1609 		} else
1610 			KASSERT(queue == PQ_NONE,
1611 			    ("vm_page_activate: wired page %p is queued", m));
1612 	} else {
1613 		if (m->act_count < ACT_INIT)
1614 			m->act_count = ACT_INIT;
1615 	}
1616 }
1617 
1618 /*
1619  *	vm_page_free_wakeup:
1620  *
1621  *	Helper routine for vm_page_free_toq() and vm_page_cache().  This
1622  *	routine is called when a page has been added to the cache or free
1623  *	queues.
1624  *
1625  *	The page queues must be locked.
1626  *	This routine may not block.
1627  */
1628 static inline void
1629 vm_page_free_wakeup(void)
1630 {
1631 
1632 	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1633 	/*
1634 	 * if pageout daemon needs pages, then tell it that there are
1635 	 * some free.
1636 	 */
1637 	if (vm_pageout_pages_needed &&
1638 	    cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) {
1639 		wakeup(&vm_pageout_pages_needed);
1640 		vm_pageout_pages_needed = 0;
1641 	}
1642 	/*
1643 	 * wakeup processes that are waiting on memory if we hit a
1644 	 * high water mark. And wakeup scheduler process if we have
1645 	 * lots of memory. this process will swapin processes.
1646 	 */
1647 	if (vm_pages_needed && !vm_page_count_min()) {
1648 		vm_pages_needed = 0;
1649 		wakeup(&cnt.v_free_count);
1650 	}
1651 }
1652 
1653 /*
1654  *	vm_page_free_toq:
1655  *
1656  *	Returns the given page to the free list,
1657  *	disassociating it with any VM object.
1658  *
1659  *	Object and page must be locked prior to entry.
1660  *	This routine may not block.
1661  */
1662 
1663 void
1664 vm_page_free_toq(vm_page_t m)
1665 {
1666 
1667 	if ((m->flags & PG_UNMANAGED) == 0) {
1668 		vm_page_lock_assert(m, MA_OWNED);
1669 		KASSERT(!pmap_page_is_mapped(m),
1670 		    ("vm_page_free_toq: freeing mapped page %p", m));
1671 	}
1672 	PCPU_INC(cnt.v_tfree);
1673 
1674 	if (VM_PAGE_IS_FREE(m))
1675 		panic("vm_page_free: freeing free page %p", m);
1676 	else if (m->busy != 0)
1677 		panic("vm_page_free: freeing busy page %p", m);
1678 
1679 	/*
1680 	 * unqueue, then remove page.  Note that we cannot destroy
1681 	 * the page here because we do not want to call the pager's
1682 	 * callback routine until after we've put the page on the
1683 	 * appropriate free queue.
1684 	 */
1685 	if ((m->flags & PG_UNMANAGED) == 0)
1686 		vm_pageq_remove(m);
1687 	vm_page_remove(m);
1688 
1689 	/*
1690 	 * If fictitious remove object association and
1691 	 * return, otherwise delay object association removal.
1692 	 */
1693 	if ((m->flags & PG_FICTITIOUS) != 0) {
1694 		return;
1695 	}
1696 
1697 	m->valid = 0;
1698 	vm_page_undirty(m);
1699 
1700 	if (m->wire_count != 0)
1701 		panic("vm_page_free: freeing wired page %p", m);
1702 	if (m->hold_count != 0) {
1703 		m->flags &= ~PG_ZERO;
1704 		vm_page_lock_queues();
1705 		vm_page_enqueue(PQ_HOLD, m);
1706 		vm_page_unlock_queues();
1707 	} else {
1708 		/*
1709 		 * Restore the default memory attribute to the page.
1710 		 */
1711 		if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
1712 			pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
1713 
1714 		/*
1715 		 * Insert the page into the physical memory allocator's
1716 		 * cache/free page queues.
1717 		 */
1718 		mtx_lock(&vm_page_queue_free_mtx);
1719 		m->flags |= PG_FREE;
1720 		cnt.v_free_count++;
1721 #if VM_NRESERVLEVEL > 0
1722 		if (!vm_reserv_free_page(m))
1723 #else
1724 		if (TRUE)
1725 #endif
1726 			vm_phys_free_pages(m, 0);
1727 		if ((m->flags & PG_ZERO) != 0)
1728 			++vm_page_zero_count;
1729 		else
1730 			vm_page_zero_idle_wakeup();
1731 		vm_page_free_wakeup();
1732 		mtx_unlock(&vm_page_queue_free_mtx);
1733 	}
1734 }
1735 
1736 /*
1737  *	vm_page_wire:
1738  *
1739  *	Mark this page as wired down by yet
1740  *	another map, removing it from paging queues
1741  *	as necessary.
1742  *
1743  *	If the page is fictitious, then its wire count must remain one.
1744  *
1745  *	The page must be locked.
1746  *	This routine may not block.
1747  */
1748 void
1749 vm_page_wire(vm_page_t m)
1750 {
1751 
1752 	/*
1753 	 * Only bump the wire statistics if the page is not already wired,
1754 	 * and only unqueue the page if it is on some queue (if it is unmanaged
1755 	 * it is already off the queues).
1756 	 */
1757 	vm_page_lock_assert(m, MA_OWNED);
1758 	if ((m->flags & PG_FICTITIOUS) != 0) {
1759 		KASSERT(m->wire_count == 1,
1760 		    ("vm_page_wire: fictitious page %p's wire count isn't one",
1761 		    m));
1762 		return;
1763 	}
1764 	if (m->wire_count == 0) {
1765 		if ((m->flags & PG_UNMANAGED) == 0)
1766 			vm_pageq_remove(m);
1767 		atomic_add_int(&cnt.v_wire_count, 1);
1768 	}
1769 	m->wire_count++;
1770 	KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
1771 }
1772 
1773 /*
1774  * vm_page_unwire:
1775  *
1776  * Release one wiring of the specified page, potentially enabling it to be
1777  * paged again.  If paging is enabled, then the value of the parameter
1778  * "activate" determines to which queue the page is added.  If "activate" is
1779  * non-zero, then the page is added to the active queue.  Otherwise, it is
1780  * added to the inactive queue.
1781  *
1782  * However, unless the page belongs to an object, it is not enqueued because
1783  * it cannot be paged out.
1784  *
1785  * If a page is fictitious, then its wire count must alway be one.
1786  *
1787  * A managed page must be locked.
1788  */
1789 void
1790 vm_page_unwire(vm_page_t m, int activate)
1791 {
1792 
1793 	if ((m->flags & PG_UNMANAGED) == 0)
1794 		vm_page_lock_assert(m, MA_OWNED);
1795 	if ((m->flags & PG_FICTITIOUS) != 0) {
1796 		KASSERT(m->wire_count == 1,
1797 	    ("vm_page_unwire: fictitious page %p's wire count isn't one", m));
1798 		return;
1799 	}
1800 	if (m->wire_count > 0) {
1801 		m->wire_count--;
1802 		if (m->wire_count == 0) {
1803 			atomic_subtract_int(&cnt.v_wire_count, 1);
1804 			if ((m->flags & PG_UNMANAGED) != 0 ||
1805 			    m->object == NULL)
1806 				return;
1807 			vm_page_lock_queues();
1808 			if (activate)
1809 				vm_page_enqueue(PQ_ACTIVE, m);
1810 			else {
1811 				vm_page_flag_clear(m, PG_WINATCFLS);
1812 				vm_page_enqueue(PQ_INACTIVE, m);
1813 			}
1814 			vm_page_unlock_queues();
1815 		}
1816 	} else
1817 		panic("vm_page_unwire: page %p's wire count is zero", m);
1818 }
1819 
1820 /*
1821  * Move the specified page to the inactive queue.
1822  *
1823  * Many pages placed on the inactive queue should actually go
1824  * into the cache, but it is difficult to figure out which.  What
1825  * we do instead, if the inactive target is well met, is to put
1826  * clean pages at the head of the inactive queue instead of the tail.
1827  * This will cause them to be moved to the cache more quickly and
1828  * if not actively re-referenced, reclaimed more quickly.  If we just
1829  * stick these pages at the end of the inactive queue, heavy filesystem
1830  * meta-data accesses can cause an unnecessary paging load on memory bound
1831  * processes.  This optimization causes one-time-use metadata to be
1832  * reused more quickly.
1833  *
1834  * Normally athead is 0 resulting in LRU operation.  athead is set
1835  * to 1 if we want this page to be 'as if it were placed in the cache',
1836  * except without unmapping it from the process address space.
1837  *
1838  * This routine may not block.
1839  */
1840 static inline void
1841 _vm_page_deactivate(vm_page_t m, int athead)
1842 {
1843 	int queue;
1844 
1845 	vm_page_lock_assert(m, MA_OWNED);
1846 
1847 	/*
1848 	 * Ignore if already inactive.
1849 	 */
1850 	if ((queue = m->queue) == PQ_INACTIVE)
1851 		return;
1852 	if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
1853 		vm_page_lock_queues();
1854 		vm_page_flag_clear(m, PG_WINATCFLS);
1855 		if (queue != PQ_NONE)
1856 			vm_page_queue_remove(queue, m);
1857 		if (athead)
1858 			TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m,
1859 			    pageq);
1860 		else
1861 			TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m,
1862 			    pageq);
1863 		m->queue = PQ_INACTIVE;
1864 		cnt.v_inactive_count++;
1865 		vm_page_unlock_queues();
1866 	}
1867 }
1868 
1869 /*
1870  * Move the specified page to the inactive queue.
1871  *
1872  * The page must be locked.
1873  */
1874 void
1875 vm_page_deactivate(vm_page_t m)
1876 {
1877 
1878 	_vm_page_deactivate(m, 0);
1879 }
1880 
1881 /*
1882  * vm_page_try_to_cache:
1883  *
1884  * Returns 0 on failure, 1 on success
1885  */
1886 int
1887 vm_page_try_to_cache(vm_page_t m)
1888 {
1889 
1890 	vm_page_lock_assert(m, MA_OWNED);
1891 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1892 	if (m->dirty || m->hold_count || m->busy || m->wire_count ||
1893 	    (m->oflags & VPO_BUSY) || (m->flags & PG_UNMANAGED))
1894 		return (0);
1895 	pmap_remove_all(m);
1896 	if (m->dirty)
1897 		return (0);
1898 	vm_page_cache(m);
1899 	return (1);
1900 }
1901 
1902 /*
1903  * vm_page_try_to_free()
1904  *
1905  *	Attempt to free the page.  If we cannot free it, we do nothing.
1906  *	1 is returned on success, 0 on failure.
1907  */
1908 int
1909 vm_page_try_to_free(vm_page_t m)
1910 {
1911 
1912 	vm_page_lock_assert(m, MA_OWNED);
1913 	if (m->object != NULL)
1914 		VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1915 	if (m->dirty || m->hold_count || m->busy || m->wire_count ||
1916 	    (m->oflags & VPO_BUSY) || (m->flags & PG_UNMANAGED))
1917 		return (0);
1918 	pmap_remove_all(m);
1919 	if (m->dirty)
1920 		return (0);
1921 	vm_page_free(m);
1922 	return (1);
1923 }
1924 
1925 /*
1926  * vm_page_cache
1927  *
1928  * Put the specified page onto the page cache queue (if appropriate).
1929  *
1930  * This routine may not block.
1931  */
1932 void
1933 vm_page_cache(vm_page_t m)
1934 {
1935 	vm_object_t object;
1936 	vm_page_t root;
1937 
1938 	vm_page_lock_assert(m, MA_OWNED);
1939 	object = m->object;
1940 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1941 	if ((m->flags & PG_UNMANAGED) || (m->oflags & VPO_BUSY) || m->busy ||
1942 	    m->hold_count || m->wire_count)
1943 		panic("vm_page_cache: attempting to cache busy page");
1944 	pmap_remove_all(m);
1945 	if (m->dirty != 0)
1946 		panic("vm_page_cache: page %p is dirty", m);
1947 	if (m->valid == 0 || object->type == OBJT_DEFAULT ||
1948 	    (object->type == OBJT_SWAP &&
1949 	    !vm_pager_has_page(object, m->pindex, NULL, NULL))) {
1950 		/*
1951 		 * Hypothesis: A cache-elgible page belonging to a
1952 		 * default object or swap object but without a backing
1953 		 * store must be zero filled.
1954 		 */
1955 		vm_page_free(m);
1956 		return;
1957 	}
1958 	KASSERT((m->flags & PG_CACHED) == 0,
1959 	    ("vm_page_cache: page %p is already cached", m));
1960 	PCPU_INC(cnt.v_tcached);
1961 
1962 	/*
1963 	 * Remove the page from the paging queues.
1964 	 */
1965 	vm_pageq_remove(m);
1966 
1967 	/*
1968 	 * Remove the page from the object's collection of resident
1969 	 * pages.
1970 	 */
1971 	if (m != object->root)
1972 		vm_page_splay(m->pindex, object->root);
1973 	if (m->left == NULL)
1974 		root = m->right;
1975 	else {
1976 		root = vm_page_splay(m->pindex, m->left);
1977 		root->right = m->right;
1978 	}
1979 	object->root = root;
1980 	TAILQ_REMOVE(&object->memq, m, listq);
1981 	object->resident_page_count--;
1982 
1983 	/*
1984 	 * Restore the default memory attribute to the page.
1985 	 */
1986 	if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
1987 		pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
1988 
1989 	/*
1990 	 * Insert the page into the object's collection of cached pages
1991 	 * and the physical memory allocator's cache/free page queues.
1992 	 */
1993 	m->flags &= ~PG_ZERO;
1994 	mtx_lock(&vm_page_queue_free_mtx);
1995 	m->flags |= PG_CACHED;
1996 	cnt.v_cache_count++;
1997 	root = object->cache;
1998 	if (root == NULL) {
1999 		m->left = NULL;
2000 		m->right = NULL;
2001 	} else {
2002 		root = vm_page_splay(m->pindex, root);
2003 		if (m->pindex < root->pindex) {
2004 			m->left = root->left;
2005 			m->right = root;
2006 			root->left = NULL;
2007 		} else if (__predict_false(m->pindex == root->pindex))
2008 			panic("vm_page_cache: offset already cached");
2009 		else {
2010 			m->right = root->right;
2011 			m->left = root;
2012 			root->right = NULL;
2013 		}
2014 	}
2015 	object->cache = m;
2016 #if VM_NRESERVLEVEL > 0
2017 	if (!vm_reserv_free_page(m)) {
2018 #else
2019 	if (TRUE) {
2020 #endif
2021 		vm_phys_set_pool(VM_FREEPOOL_CACHE, m, 0);
2022 		vm_phys_free_pages(m, 0);
2023 	}
2024 	vm_page_free_wakeup();
2025 	mtx_unlock(&vm_page_queue_free_mtx);
2026 
2027 	/*
2028 	 * Increment the vnode's hold count if this is the object's only
2029 	 * cached page.  Decrement the vnode's hold count if this was
2030 	 * the object's only resident page.
2031 	 */
2032 	if (object->type == OBJT_VNODE) {
2033 		if (root == NULL && object->resident_page_count != 0)
2034 			vhold(object->handle);
2035 		else if (root != NULL && object->resident_page_count == 0)
2036 			vdrop(object->handle);
2037 	}
2038 }
2039 
2040 /*
2041  * vm_page_dontneed
2042  *
2043  *	Cache, deactivate, or do nothing as appropriate.  This routine
2044  *	is typically used by madvise() MADV_DONTNEED.
2045  *
2046  *	Generally speaking we want to move the page into the cache so
2047  *	it gets reused quickly.  However, this can result in a silly syndrome
2048  *	due to the page recycling too quickly.  Small objects will not be
2049  *	fully cached.  On the otherhand, if we move the page to the inactive
2050  *	queue we wind up with a problem whereby very large objects
2051  *	unnecessarily blow away our inactive and cache queues.
2052  *
2053  *	The solution is to move the pages based on a fixed weighting.  We
2054  *	either leave them alone, deactivate them, or move them to the cache,
2055  *	where moving them to the cache has the highest weighting.
2056  *	By forcing some pages into other queues we eventually force the
2057  *	system to balance the queues, potentially recovering other unrelated
2058  *	space from active.  The idea is to not force this to happen too
2059  *	often.
2060  */
2061 void
2062 vm_page_dontneed(vm_page_t m)
2063 {
2064 	int dnw;
2065 	int head;
2066 
2067 	vm_page_lock_assert(m, MA_OWNED);
2068 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2069 	dnw = PCPU_GET(dnweight);
2070 	PCPU_INC(dnweight);
2071 
2072 	/*
2073 	 * Occasionally leave the page alone.
2074 	 */
2075 	if ((dnw & 0x01F0) == 0 || m->queue == PQ_INACTIVE) {
2076 		if (m->act_count >= ACT_INIT)
2077 			--m->act_count;
2078 		return;
2079 	}
2080 
2081 	/*
2082 	 * Clear any references to the page.  Otherwise, the page daemon will
2083 	 * immediately reactivate the page.
2084 	 *
2085 	 * Perform the pmap_clear_reference() first.  Otherwise, a concurrent
2086 	 * pmap operation, such as pmap_remove(), could clear a reference in
2087 	 * the pmap and set PG_REFERENCED on the page before the
2088 	 * pmap_clear_reference() had completed.  Consequently, the page would
2089 	 * appear referenced based upon an old reference that occurred before
2090 	 * this function ran.
2091 	 */
2092 	pmap_clear_reference(m);
2093 	vm_page_lock_queues();
2094 	vm_page_flag_clear(m, PG_REFERENCED);
2095 	vm_page_unlock_queues();
2096 
2097 	if (m->dirty == 0 && pmap_is_modified(m))
2098 		vm_page_dirty(m);
2099 
2100 	if (m->dirty || (dnw & 0x0070) == 0) {
2101 		/*
2102 		 * Deactivate the page 3 times out of 32.
2103 		 */
2104 		head = 0;
2105 	} else {
2106 		/*
2107 		 * Cache the page 28 times out of every 32.  Note that
2108 		 * the page is deactivated instead of cached, but placed
2109 		 * at the head of the queue instead of the tail.
2110 		 */
2111 		head = 1;
2112 	}
2113 	_vm_page_deactivate(m, head);
2114 }
2115 
2116 /*
2117  * Grab a page, waiting until we are waken up due to the page
2118  * changing state.  We keep on waiting, if the page continues
2119  * to be in the object.  If the page doesn't exist, first allocate it
2120  * and then conditionally zero it.
2121  *
2122  * The caller must always specify the VM_ALLOC_RETRY flag.  This is intended
2123  * to facilitate its eventual removal.
2124  *
2125  * This routine may block.
2126  */
2127 vm_page_t
2128 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
2129 {
2130 	vm_page_t m;
2131 
2132 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2133 	KASSERT((allocflags & VM_ALLOC_RETRY) != 0,
2134 	    ("vm_page_grab: VM_ALLOC_RETRY is required"));
2135 retrylookup:
2136 	if ((m = vm_page_lookup(object, pindex)) != NULL) {
2137 		if ((m->oflags & VPO_BUSY) != 0 ||
2138 		    ((allocflags & VM_ALLOC_IGN_SBUSY) == 0 && m->busy != 0)) {
2139 			/*
2140 			 * Reference the page before unlocking and
2141 			 * sleeping so that the page daemon is less
2142 			 * likely to reclaim it.
2143 			 */
2144 			vm_page_lock_queues();
2145 			vm_page_flag_set(m, PG_REFERENCED);
2146 			vm_page_sleep(m, "pgrbwt");
2147 			goto retrylookup;
2148 		} else {
2149 			if ((allocflags & VM_ALLOC_WIRED) != 0) {
2150 				vm_page_lock(m);
2151 				vm_page_wire(m);
2152 				vm_page_unlock(m);
2153 			}
2154 			if ((allocflags & VM_ALLOC_NOBUSY) == 0)
2155 				vm_page_busy(m);
2156 			return (m);
2157 		}
2158 	}
2159 	m = vm_page_alloc(object, pindex, allocflags & ~(VM_ALLOC_RETRY |
2160 	    VM_ALLOC_IGN_SBUSY));
2161 	if (m == NULL) {
2162 		VM_OBJECT_UNLOCK(object);
2163 		VM_WAIT;
2164 		VM_OBJECT_LOCK(object);
2165 		goto retrylookup;
2166 	} else if (m->valid != 0)
2167 		return (m);
2168 	if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
2169 		pmap_zero_page(m);
2170 	return (m);
2171 }
2172 
2173 /*
2174  * Mapping function for valid bits or for dirty bits in
2175  * a page.  May not block.
2176  *
2177  * Inputs are required to range within a page.
2178  */
2179 int
2180 vm_page_bits(int base, int size)
2181 {
2182 	int first_bit;
2183 	int last_bit;
2184 
2185 	KASSERT(
2186 	    base + size <= PAGE_SIZE,
2187 	    ("vm_page_bits: illegal base/size %d/%d", base, size)
2188 	);
2189 
2190 	if (size == 0)		/* handle degenerate case */
2191 		return (0);
2192 
2193 	first_bit = base >> DEV_BSHIFT;
2194 	last_bit = (base + size - 1) >> DEV_BSHIFT;
2195 
2196 	return ((2 << last_bit) - (1 << first_bit));
2197 }
2198 
2199 /*
2200  *	vm_page_set_valid:
2201  *
2202  *	Sets portions of a page valid.  The arguments are expected
2203  *	to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
2204  *	of any partial chunks touched by the range.  The invalid portion of
2205  *	such chunks will be zeroed.
2206  *
2207  *	(base + size) must be less then or equal to PAGE_SIZE.
2208  */
2209 void
2210 vm_page_set_valid(vm_page_t m, int base, int size)
2211 {
2212 	int endoff, frag;
2213 
2214 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2215 	if (size == 0)	/* handle degenerate case */
2216 		return;
2217 
2218 	/*
2219 	 * If the base is not DEV_BSIZE aligned and the valid
2220 	 * bit is clear, we have to zero out a portion of the
2221 	 * first block.
2222 	 */
2223 	if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
2224 	    (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
2225 		pmap_zero_page_area(m, frag, base - frag);
2226 
2227 	/*
2228 	 * If the ending offset is not DEV_BSIZE aligned and the
2229 	 * valid bit is clear, we have to zero out a portion of
2230 	 * the last block.
2231 	 */
2232 	endoff = base + size;
2233 	if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
2234 	    (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
2235 		pmap_zero_page_area(m, endoff,
2236 		    DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
2237 
2238 	/*
2239 	 * Assert that no previously invalid block that is now being validated
2240 	 * is already dirty.
2241 	 */
2242 	KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0,
2243 	    ("vm_page_set_valid: page %p is dirty", m));
2244 
2245 	/*
2246 	 * Set valid bits inclusive of any overlap.
2247 	 */
2248 	m->valid |= vm_page_bits(base, size);
2249 }
2250 
2251 /*
2252  * Clear the given bits from the specified page's dirty field.
2253  */
2254 static __inline void
2255 vm_page_clear_dirty_mask(vm_page_t m, int pagebits)
2256 {
2257 
2258 	/*
2259 	 * If the object is locked and the page is neither VPO_BUSY nor
2260 	 * PG_WRITEABLE, then the page's dirty field cannot possibly be
2261 	 * modified by a concurrent pmap operation.
2262 	 */
2263 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2264 	if ((m->oflags & VPO_BUSY) == 0 && (m->flags & PG_WRITEABLE) == 0)
2265 		m->dirty &= ~pagebits;
2266 	else {
2267 		vm_page_lock_queues();
2268 		m->dirty &= ~pagebits;
2269 		vm_page_unlock_queues();
2270 	}
2271 }
2272 
2273 /*
2274  *	vm_page_set_validclean:
2275  *
2276  *	Sets portions of a page valid and clean.  The arguments are expected
2277  *	to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
2278  *	of any partial chunks touched by the range.  The invalid portion of
2279  *	such chunks will be zero'd.
2280  *
2281  *	This routine may not block.
2282  *
2283  *	(base + size) must be less then or equal to PAGE_SIZE.
2284  */
2285 void
2286 vm_page_set_validclean(vm_page_t m, int base, int size)
2287 {
2288 	u_long oldvalid;
2289 	int endoff, frag, pagebits;
2290 
2291 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2292 	if (size == 0)	/* handle degenerate case */
2293 		return;
2294 
2295 	/*
2296 	 * If the base is not DEV_BSIZE aligned and the valid
2297 	 * bit is clear, we have to zero out a portion of the
2298 	 * first block.
2299 	 */
2300 	if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
2301 	    (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
2302 		pmap_zero_page_area(m, frag, base - frag);
2303 
2304 	/*
2305 	 * If the ending offset is not DEV_BSIZE aligned and the
2306 	 * valid bit is clear, we have to zero out a portion of
2307 	 * the last block.
2308 	 */
2309 	endoff = base + size;
2310 	if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
2311 	    (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
2312 		pmap_zero_page_area(m, endoff,
2313 		    DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
2314 
2315 	/*
2316 	 * Set valid, clear dirty bits.  If validating the entire
2317 	 * page we can safely clear the pmap modify bit.  We also
2318 	 * use this opportunity to clear the VPO_NOSYNC flag.  If a process
2319 	 * takes a write fault on a MAP_NOSYNC memory area the flag will
2320 	 * be set again.
2321 	 *
2322 	 * We set valid bits inclusive of any overlap, but we can only
2323 	 * clear dirty bits for DEV_BSIZE chunks that are fully within
2324 	 * the range.
2325 	 */
2326 	oldvalid = m->valid;
2327 	pagebits = vm_page_bits(base, size);
2328 	m->valid |= pagebits;
2329 #if 0	/* NOT YET */
2330 	if ((frag = base & (DEV_BSIZE - 1)) != 0) {
2331 		frag = DEV_BSIZE - frag;
2332 		base += frag;
2333 		size -= frag;
2334 		if (size < 0)
2335 			size = 0;
2336 	}
2337 	pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
2338 #endif
2339 	if (base == 0 && size == PAGE_SIZE) {
2340 		/*
2341 		 * The page can only be modified within the pmap if it is
2342 		 * mapped, and it can only be mapped if it was previously
2343 		 * fully valid.
2344 		 */
2345 		if (oldvalid == VM_PAGE_BITS_ALL)
2346 			/*
2347 			 * Perform the pmap_clear_modify() first.  Otherwise,
2348 			 * a concurrent pmap operation, such as
2349 			 * pmap_protect(), could clear a modification in the
2350 			 * pmap and set the dirty field on the page before
2351 			 * pmap_clear_modify() had begun and after the dirty
2352 			 * field was cleared here.
2353 			 */
2354 			pmap_clear_modify(m);
2355 		m->dirty = 0;
2356 		m->oflags &= ~VPO_NOSYNC;
2357 	} else if (oldvalid != VM_PAGE_BITS_ALL)
2358 		m->dirty &= ~pagebits;
2359 	else
2360 		vm_page_clear_dirty_mask(m, pagebits);
2361 }
2362 
2363 void
2364 vm_page_clear_dirty(vm_page_t m, int base, int size)
2365 {
2366 
2367 	vm_page_clear_dirty_mask(m, vm_page_bits(base, size));
2368 }
2369 
2370 /*
2371  *	vm_page_set_invalid:
2372  *
2373  *	Invalidates DEV_BSIZE'd chunks within a page.  Both the
2374  *	valid and dirty bits for the effected areas are cleared.
2375  *
2376  *	May not block.
2377  */
2378 void
2379 vm_page_set_invalid(vm_page_t m, int base, int size)
2380 {
2381 	int bits;
2382 
2383 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2384 	KASSERT((m->oflags & VPO_BUSY) == 0,
2385 	    ("vm_page_set_invalid: page %p is busy", m));
2386 	bits = vm_page_bits(base, size);
2387 	if (m->valid == VM_PAGE_BITS_ALL && bits != 0)
2388 		pmap_remove_all(m);
2389 	KASSERT(!pmap_page_is_mapped(m),
2390 	    ("vm_page_set_invalid: page %p is mapped", m));
2391 	m->valid &= ~bits;
2392 	m->dirty &= ~bits;
2393 }
2394 
2395 /*
2396  * vm_page_zero_invalid()
2397  *
2398  *	The kernel assumes that the invalid portions of a page contain
2399  *	garbage, but such pages can be mapped into memory by user code.
2400  *	When this occurs, we must zero out the non-valid portions of the
2401  *	page so user code sees what it expects.
2402  *
2403  *	Pages are most often semi-valid when the end of a file is mapped
2404  *	into memory and the file's size is not page aligned.
2405  */
2406 void
2407 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
2408 {
2409 	int b;
2410 	int i;
2411 
2412 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2413 	/*
2414 	 * Scan the valid bits looking for invalid sections that
2415 	 * must be zerod.  Invalid sub-DEV_BSIZE'd areas ( where the
2416 	 * valid bit may be set ) have already been zerod by
2417 	 * vm_page_set_validclean().
2418 	 */
2419 	for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
2420 		if (i == (PAGE_SIZE / DEV_BSIZE) ||
2421 		    (m->valid & (1 << i))
2422 		) {
2423 			if (i > b) {
2424 				pmap_zero_page_area(m,
2425 				    b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
2426 			}
2427 			b = i + 1;
2428 		}
2429 	}
2430 
2431 	/*
2432 	 * setvalid is TRUE when we can safely set the zero'd areas
2433 	 * as being valid.  We can do this if there are no cache consistancy
2434 	 * issues.  e.g. it is ok to do with UFS, but not ok to do with NFS.
2435 	 */
2436 	if (setvalid)
2437 		m->valid = VM_PAGE_BITS_ALL;
2438 }
2439 
2440 /*
2441  *	vm_page_is_valid:
2442  *
2443  *	Is (partial) page valid?  Note that the case where size == 0
2444  *	will return FALSE in the degenerate case where the page is
2445  *	entirely invalid, and TRUE otherwise.
2446  *
2447  *	May not block.
2448  */
2449 int
2450 vm_page_is_valid(vm_page_t m, int base, int size)
2451 {
2452 	int bits = vm_page_bits(base, size);
2453 
2454 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2455 	if (m->valid && ((m->valid & bits) == bits))
2456 		return 1;
2457 	else
2458 		return 0;
2459 }
2460 
2461 /*
2462  * update dirty bits from pmap/mmu.  May not block.
2463  */
2464 void
2465 vm_page_test_dirty(vm_page_t m)
2466 {
2467 
2468 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2469 	if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m))
2470 		vm_page_dirty(m);
2471 }
2472 
2473 int so_zerocp_fullpage = 0;
2474 
2475 /*
2476  *	Replace the given page with a copy.  The copied page assumes
2477  *	the portion of the given page's "wire_count" that is not the
2478  *	responsibility of this copy-on-write mechanism.
2479  *
2480  *	The object containing the given page must have a non-zero
2481  *	paging-in-progress count and be locked.
2482  */
2483 void
2484 vm_page_cowfault(vm_page_t m)
2485 {
2486 	vm_page_t mnew;
2487 	vm_object_t object;
2488 	vm_pindex_t pindex;
2489 
2490 	mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED);
2491 	vm_page_lock_assert(m, MA_OWNED);
2492 	object = m->object;
2493 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2494 	KASSERT(object->paging_in_progress != 0,
2495 	    ("vm_page_cowfault: object %p's paging-in-progress count is zero.",
2496 	    object));
2497 	pindex = m->pindex;
2498 
2499  retry_alloc:
2500 	pmap_remove_all(m);
2501 	vm_page_remove(m);
2502 	mnew = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY);
2503 	if (mnew == NULL) {
2504 		vm_page_insert(m, object, pindex);
2505 		vm_page_unlock(m);
2506 		VM_OBJECT_UNLOCK(object);
2507 		VM_WAIT;
2508 		VM_OBJECT_LOCK(object);
2509 		if (m == vm_page_lookup(object, pindex)) {
2510 			vm_page_lock(m);
2511 			goto retry_alloc;
2512 		} else {
2513 			/*
2514 			 * Page disappeared during the wait.
2515 			 */
2516 			return;
2517 		}
2518 	}
2519 
2520 	if (m->cow == 0) {
2521 		/*
2522 		 * check to see if we raced with an xmit complete when
2523 		 * waiting to allocate a page.  If so, put things back
2524 		 * the way they were
2525 		 */
2526 		vm_page_unlock(m);
2527 		vm_page_lock(mnew);
2528 		vm_page_free(mnew);
2529 		vm_page_unlock(mnew);
2530 		vm_page_insert(m, object, pindex);
2531 	} else { /* clear COW & copy page */
2532 		if (!so_zerocp_fullpage)
2533 			pmap_copy_page(m, mnew);
2534 		mnew->valid = VM_PAGE_BITS_ALL;
2535 		vm_page_dirty(mnew);
2536 		mnew->wire_count = m->wire_count - m->cow;
2537 		m->wire_count = m->cow;
2538 		vm_page_unlock(m);
2539 	}
2540 }
2541 
2542 void
2543 vm_page_cowclear(vm_page_t m)
2544 {
2545 
2546 	vm_page_lock_assert(m, MA_OWNED);
2547 	if (m->cow) {
2548 		m->cow--;
2549 		/*
2550 		 * let vm_fault add back write permission  lazily
2551 		 */
2552 	}
2553 	/*
2554 	 *  sf_buf_free() will free the page, so we needn't do it here
2555 	 */
2556 }
2557 
2558 int
2559 vm_page_cowsetup(vm_page_t m)
2560 {
2561 
2562 	vm_page_lock_assert(m, MA_OWNED);
2563 	if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0 ||
2564 	    m->cow == USHRT_MAX - 1 || !VM_OBJECT_TRYLOCK(m->object))
2565 		return (EBUSY);
2566 	m->cow++;
2567 	pmap_remove_write(m);
2568 	VM_OBJECT_UNLOCK(m->object);
2569 	return (0);
2570 }
2571 
2572 #include "opt_ddb.h"
2573 #ifdef DDB
2574 #include <sys/kernel.h>
2575 
2576 #include <ddb/ddb.h>
2577 
2578 DB_SHOW_COMMAND(page, vm_page_print_page_info)
2579 {
2580 	db_printf("cnt.v_free_count: %d\n", cnt.v_free_count);
2581 	db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count);
2582 	db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count);
2583 	db_printf("cnt.v_active_count: %d\n", cnt.v_active_count);
2584 	db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count);
2585 	db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved);
2586 	db_printf("cnt.v_free_min: %d\n", cnt.v_free_min);
2587 	db_printf("cnt.v_free_target: %d\n", cnt.v_free_target);
2588 	db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min);
2589 	db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target);
2590 }
2591 
2592 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
2593 {
2594 
2595 	db_printf("PQ_FREE:");
2596 	db_printf(" %d", cnt.v_free_count);
2597 	db_printf("\n");
2598 
2599 	db_printf("PQ_CACHE:");
2600 	db_printf(" %d", cnt.v_cache_count);
2601 	db_printf("\n");
2602 
2603 	db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n",
2604 		*vm_page_queues[PQ_ACTIVE].cnt,
2605 		*vm_page_queues[PQ_INACTIVE].cnt);
2606 }
2607 #endif /* DDB */
2608