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