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