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