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