vm_page.c revision 136627
11927Swollman/*
21927Swollman * Copyright (c) 1991 Regents of the University of California.
31927Swollman * All rights reserved.
41927Swollman *
51927Swollman * This code is derived from software contributed to Berkeley by
61927Swollman * The Mach Operating System project at Carnegie-Mellon University.
71927Swollman *
81927Swollman * Redistribution and use in source and binary forms, with or without
91927Swollman * modification, are permitted provided that the following conditions
101927Swollman * are met:
111927Swollman * 1. Redistributions of source code must retain the above copyright
121927Swollman *    notice, this list of conditions and the following disclaimer.
131927Swollman * 2. Redistributions in binary form must reproduce the above copyright
141927Swollman *    notice, this list of conditions and the following disclaimer in the
151927Swollman *    documentation and/or other materials provided with the distribution.
161927Swollman * 4. Neither the name of the University nor the names of its contributors
171927Swollman *    may be used to endorse or promote products derived from this software
181927Swollman *    without specific prior written permission.
191927Swollman *
201927Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211927Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221927Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231927Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241927Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251927Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261927Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271927Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281927Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291927Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3030762Scharnier * SUCH DAMAGE.
3130762Scharnier *
3243854Swpaul *	from: @(#)vm_page.c	7.4 (Berkeley) 5/7/91
3330762Scharnier */
341927Swollman
351927Swollman/*
361927Swollman * Copyright (c) 1987, 1990 Carnegie-Mellon University.
378091Swpaul * All rights reserved.
381927Swollman *
391927Swollman * Authors: Avadis Tevanian, Jr., Michael Wayne Young
401927Swollman *
411927Swollman * Permission to use, copy, modify and distribute this software and
421927Swollman * its documentation is hereby granted, provided that both the copyright
438091Swpaul * notice and this permission notice appear in all copies of the
441927Swollman * software, derivative works or modified versions, and any portions
451927Swollman * thereof, and that both notices appear in supporting documentation.
461927Swollman *
4730762Scharnier * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
4830762Scharnier * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
491927Swollman * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
5030762Scharnier *
5130762Scharnier * Carnegie Mellon requests users of this software to return to
5230762Scharnier *
531927Swollman *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
5430762Scharnier *  School of Computer Science
5530762Scharnier *  Carnegie Mellon University
561927Swollman *  Pittsburgh PA 15213-3890
571927Swollman *
581927Swollman * any improvements or extensions that they make and grant Carnegie the
596732Swpaul * rights to redistribute these changes.
601927Swollman */
611927Swollman
621927Swollman/*
631927Swollman *			GENERAL RULES ON VM_PAGE MANIPULATION
6430762Scharnier *
6512862Swpaul *	- a pageq mutex is required when adding or removing a page from a
6612862Swpaul *	  page queue (vm_page_queue[]), regardless of other mutexes or the
671927Swollman *	  busy state of a page.
6826135Swpaul *
691927Swollman *	- a hash chain mutex is required when associating or disassociating
701927Swollman *	  a page from the VM PAGE CACHE hash table (vm_page_buckets),
711927Swollman *	  regardless of other mutexes or the busy state of a page.
721927Swollman *
731927Swollman *	- either a hash chain mutex OR a busied page is required in order
748474Swpaul *	  to modify the page flags.  A hash chain mutex must be obtained in
758474Swpaul *	  order to busy a page.  A page's flags cannot be modified by a
768474Swpaul *	  hash chain mutex if the page is marked busy.
778474Swpaul *
781927Swollman *	- The object memq mutex is held when inserting or removing
791927Swollman *	  pages from an object (vm_page_insert() or vm_page_remove()).  This
801927Swollman *	  is different from the object's main mutex.
811927Swollman *
821927Swollman *	Generally speaking, you have to be aware of side effects when running
831927Swollman *	vm_page ops.  A vm_page_lookup() will return with the hash chain
841927Swollman *	locked, whether it was able to lookup the page or not.  vm_page_free(),
858755Swpaul *	vm_page_cache(), vm_page_activate(), and a number of other routines
868755Swpaul *	will release the hash chain mutex for you.  Intermediate manipulation
878091Swpaul *	routines such as vm_page_flag_set() expect the hash chain to be held
881927Swollman *	on entry and the hash chain will remain held on return.
891927Swollman *
908755Swpaul *	pageq scanning can only occur with the pageq in question locked.
918755Swpaul *	We have a known bottleneck with the active queue, but the cache
928755Swpaul *	and free queues are actually arrays already.
938755Swpaul */
941927Swollman
951927Swollman/*
961927Swollman *	Resident memory management module.
971927Swollman */
988091Swpaul
9932631Swpaul#include <sys/cdefs.h>
10032631Swpaul__FBSDID("$FreeBSD: head/sys/vm/vm_page.c 136627 2004-10-17 22:33:40Z alc $");
1018091Swpaul
1028091Swpaul#include <sys/param.h>
1038425Swpaul#include <sys/systm.h>
1048755Swpaul#include <sys/lock.h>
1058853Swpaul#include <sys/malloc.h>
1068425Swpaul#include <sys/mutex.h>
1078425Swpaul#include <sys/proc.h>
1089600Swpaul#include <sys/vmmeter.h>
1099600Swpaul#include <sys/vnode.h>
1108091Swpaul
11112862Swpaul#include <vm/vm.h>
1121927Swollman#include <vm/vm_param.h>
1138755Swpaul#include <vm/vm_kern.h>
1141927Swollman#include <vm/vm_object.h>
1151927Swollman#include <vm/vm_page.h>
1161927Swollman#include <vm/vm_pageout.h>
1171927Swollman#include <vm/vm_pager.h>
1181927Swollman#include <vm/vm_extern.h>
1196732Swpaul#include <vm/uma.h>
12021581Swpaul#include <vm/uma_int.h>
1216732Swpaul
1229600Swpaul/*
1239600Swpaul *	Associated with page of user-allocatable memory is a
1249600Swpaul *	page structure.
1259600Swpaul */
1269600Swpaul
1279600Swpaulstruct mtx vm_page_queue_mtx;
1289600Swpaulstruct mtx vm_page_queue_free_mtx;
12926135Swpaul
1309600Swpaulvm_page_t vm_page_array = 0;
1319600Swpaulint vm_page_array_size = 0;
1328091Swpaullong first_page = 0;
1338755Swpaulint vm_page_zero_count = 0;
1348091Swpaul
1358755Swpaul/*
1368425Swpaul *	vm_set_page_size:
1378755Swpaul *
1388425Swpaul *	Sets the page size, perhaps based upon the memory
1398755Swpaul *	size.  Must be called before any use of page-size
1408755Swpaul *	dependent functions.
1418425Swpaul */
14221539Swpaulvoid
1438425Swpaulvm_set_page_size(void)
1448425Swpaul{
1459600Swpaul	if (cnt.v_page_size == 0)
1469600Swpaul		cnt.v_page_size = PAGE_SIZE;
1479600Swpaul	if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0)
1489600Swpaul		panic("vm_set_page_size: page size not a power of two");
1499600Swpaul}
1509600Swpaul
1518091Swpaul/*
1528425Swpaul *	vm_page_startup:
1538474Swpaul *
1548755Swpaul *	Initializes the resident memory module.
1558091Swpaul *
1561927Swollman *	Allocates memory for the page cells, and
1571927Swollman *	for the object/offset-to-page hash table headers.
1581927Swollman *	Each page cell is initialized and placed on the free list.
15932631Swpaul */
1601927Swollmanvm_offset_t
1611927Swollmanvm_page_startup(vm_offset_t vaddr)
1621927Swollman{
1631927Swollman	vm_offset_t mapped;
1641927Swollman	vm_size_t npages;
1651927Swollman	vm_paddr_t page_range;
1661927Swollman	vm_paddr_t new_end;
1671927Swollman	int i;
1681927Swollman	vm_paddr_t pa;
1691927Swollman	int nblocks;
1701927Swollman	vm_paddr_t last_pa;
17132631Swpaul
1721927Swollman	/* the biggest memory array is the second group of pages */
17312862Swpaul	vm_paddr_t end;
1741927Swollman	vm_paddr_t biggestsize;
1751927Swollman	int biggestone;
1761927Swollman
1771927Swollman	vm_paddr_t total;
1781927Swollman	vm_size_t bootpages;
1791927Swollman
1801927Swollman	total = 0;
1811927Swollman	biggestsize = 0;
18212862Swpaul	biggestone = 0;
1831927Swollman	nblocks = 0;
18424782Swpaul	vaddr = round_page(vaddr);
18524782Swpaul
18624782Swpaul	for (i = 0; phys_avail[i + 1]; i += 2) {
18724782Swpaul		phys_avail[i] = round_page(phys_avail[i]);
18824782Swpaul		phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
18924782Swpaul	}
1908755Swpaul
19112862Swpaul	for (i = 0; phys_avail[i + 1]; i += 2) {
1921927Swollman		vm_paddr_t size = phys_avail[i + 1] - phys_avail[i];
1938755Swpaul
1941927Swollman		if (size > biggestsize) {
1951927Swollman			biggestone = i;
1969600Swpaul			biggestsize = size;
19712862Swpaul		}
1989600Swpaul		++nblocks;
1999600Swpaul		total += size;
2009600Swpaul	}
2018474Swpaul
2028425Swpaul	end = phys_avail[biggestone+1];
2038425Swpaul
20412862Swpaul	/*
2058428Swpaul	 * Initialize the locks.
2068425Swpaul	 */
2071927Swollman	mtx_init(&vm_page_queue_mtx, "vm page queue mutex", NULL, MTX_DEF |
2088857Srgrimes	    MTX_RECURSE);
20921539Swpaul	mtx_init(&vm_page_queue_free_mtx, "vm page queue free mutex", NULL,
21012862Swpaul	    MTX_SPIN);
2118428Swpaul
2128246Swpaul	/*
2131927Swollman	 * Initialize the queue headers for the free queue, the active queue
21412862Swpaul	 * and the inactive queue.
2151927Swollman	 */
2161927Swollman	vm_pageq_init();
2178091Swpaul
2181927Swollman	/*
2198425Swpaul	 * Allocate memory for use when boot strapping the kernel memory
2208425Swpaul	 * allocator.
2211927Swollman	 */
2221927Swollman	bootpages = UMA_BOOT_PAGES * UMA_SLAB_SIZE;
2231927Swollman	new_end = end - bootpages;
2248425Swpaul	new_end = trunc_page(new_end);
2251927Swollman	mapped = pmap_map(&vaddr, new_end, end,
2261927Swollman	    VM_PROT_READ | VM_PROT_WRITE);
2278755Swpaul	bzero((caddr_t) mapped, end - new_end);
2287982Swpaul	uma_startup((caddr_t)mapped);
2298755Swpaul
2301927Swollman	/*
2311927Swollman	 * Compute the number of pages of memory that will be available for
23212862Swpaul	 * use (taking into account the overhead of a page structure per
23312862Swpaul	 * page).
2341927Swollman	 */
23512862Swpaul	first_page = phys_avail[0] / PAGE_SIZE;
2368091Swpaul	page_range = phys_avail[(nblocks - 1) * 2 + 1] / PAGE_SIZE - first_page;
2371927Swollman	npages = (total - (page_range * sizeof(struct vm_page)) -
2381927Swollman	    (end - new_end)) / PAGE_SIZE;
2391927Swollman	end = new_end;
2401927Swollman
2411927Swollman	/*
2421927Swollman	 * Reserve an unmapped guard page to trap access to vm_page_array[-1].
2438755Swpaul	 */
24432631Swpaul	vaddr += PAGE_SIZE;
2451927Swollman
24612862Swpaul	/*
2471927Swollman	 * Initialize the mem entry structures now, and put them in the free
2481927Swollman	 * queue.
2491927Swollman	 */
25043854Swpaul	new_end = trunc_page(end - page_range * sizeof(struct vm_page));
2511927Swollman	mapped = pmap_map(&vaddr, new_end, end,
25224782Swpaul	    VM_PROT_READ | VM_PROT_WRITE);
25324782Swpaul	vm_page_array = (vm_page_t) mapped;
25424782Swpaul	phys_avail[biggestone + 1] = new_end;
25530762Scharnier
25624782Swpaul	/*
2571927Swollman	 * Clear all of the page structures
2581927Swollman	 */
2591927Swollman	bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
2601927Swollman	vm_page_array_size = page_range;
2618755Swpaul
2628755Swpaul	/*
26330762Scharnier	 * Construct the free queue(s) in descending order (by physical
2648755Swpaul	 * address) so that the first 16MB of physical memory is allocated
2651927Swollman	 * last rather than first.  On large-memory machines, this avoids
2661927Swollman	 * the exhaustion of low physical memory before isa_dma_init has run.
2671927Swollman	 */
2681927Swollman	cnt.v_page_count = 0;
2691927Swollman	cnt.v_free_count = 0;
2708755Swpaul	for (i = 0; phys_avail[i + 1] && npages > 0; i += 2) {
27130762Scharnier		pa = phys_avail[i];
2721927Swollman		last_pa = phys_avail[i + 1];
2731927Swollman		while (pa < last_pa && npages-- > 0) {
2748755Swpaul			vm_pageq_add_new_page(pa);
2758755Swpaul			pa += PAGE_SIZE;
27630762Scharnier		}
2778755Swpaul	}
2781927Swollman	return (vaddr);
2798755Swpaul}
2808755Swpaul
28130762Scharniervoid
2828755Swpaulvm_page_flag_set(vm_page_t m, unsigned short bits)
2831927Swollman{
2841927Swollman
2851927Swollman	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
28612862Swpaul	m->flags |= bits;
28712862Swpaul}
2881927Swollman
2891927Swollmanvoid
29043854Swpaulvm_page_flag_clear(vm_page_t m, unsigned short bits)
2911927Swollman{
2921927Swollman
2931927Swollman	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2941927Swollman	m->flags &= ~bits;
2951927Swollman}
2961927Swollman
2971927Swollmanvoid
2981927Swollmanvm_page_busy(vm_page_t m)
29912862Swpaul{
3001927Swollman	KASSERT((m->flags & PG_BUSY) == 0,
3011927Swollman	    ("vm_page_busy: page already busy!!!"));
3021927Swollman	vm_page_flag_set(m, PG_BUSY);
3031927Swollman}
3041927Swollman
3051927Swollman/*
3061927Swollman *      vm_page_flash:
3071927Swollman *
3081927Swollman *      wakeup anyone waiting for the page.
3091927Swollman */
3101927Swollmanvoid
31132631Swpaulvm_page_flash(vm_page_t m)
3121927Swollman{
3131927Swollman	if (m->flags & PG_WANTED) {
3141927Swollman		vm_page_flag_clear(m, PG_WANTED);
3151927Swollman		wakeup(m);
3161927Swollman	}
31732631Swpaul}
3181927Swollman
3191927Swollman/*
3201927Swollman *      vm_page_wakeup:
3211927Swollman *
3221927Swollman *      clear the PG_BUSY flag and wakeup anyone waiting for the
3231927Swollman *      page.
3241927Swollman *
3251927Swollman */
3261927Swollmanvoid
3271927Swollmanvm_page_wakeup(vm_page_t m)
3281927Swollman{
3291927Swollman	KASSERT(m->flags & PG_BUSY, ("vm_page_wakeup: page not busy!!!"));
3301927Swollman	vm_page_flag_clear(m, PG_BUSY);
3311927Swollman	vm_page_flash(m);
3321927Swollman}
3331927Swollman
3341927Swollmanvoid
3351927Swollmanvm_page_io_start(vm_page_t m)
33632631Swpaul{
3371927Swollman
3381927Swollman	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
3391927Swollman	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3401927Swollman	m->busy++;
3411927Swollman}
3421927Swollman
3431927Swollmanvoid
34421096Spetervm_page_io_finish(vm_page_t m)
3451927Swollman{
3461927Swollman
3471927Swollman	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
3481927Swollman	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3491927Swollman	m->busy--;
3501927Swollman	if (m->busy == 0)
3511927Swollman		vm_page_flash(m);
3521927Swollman}
3531927Swollman
3541927Swollman/*
3558091Swpaul * Keep page from being freed by the page daemon
3568091Swpaul * much of the same effect as wiring, except much lower
3578091Swpaul * overhead and should be used only for *very* temporary
3588091Swpaul * holding ("wiring").
3598091Swpaul */
3608091Swpaulvoid
3619532Swpaulvm_page_hold(vm_page_t mem)
3629532Swpaul{
3638091Swpaul
3648091Swpaul	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3658425Swpaul        mem->hold_count++;
3668425Swpaul}
3678425Swpaul
3688425Swpaulvoid
3698425Swpaulvm_page_unhold(vm_page_t mem)
3708425Swpaul{
37121581Swpaul
37221581Swpaul	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
37321581Swpaul	--mem->hold_count;
3748425Swpaul	KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!"));
3758425Swpaul	if (mem->hold_count == 0 && mem->queue == PQ_HOLD)
3768755Swpaul		vm_page_free_toq(mem);
3778755Swpaul}
3788425Swpaul
3798425Swpaul/*
3808425Swpaul *	vm_page_free:
3818425Swpaul *
3828474Swpaul *	Free a page
3838474Swpaul *
3848425Swpaul *	The clearing of PG_ZERO is a temporary safety until the code can be
3858425Swpaul *	reviewed to determine that PG_ZERO is being properly cleared on
3868425Swpaul *	write faults or maps.  PG_ZERO was previously cleared in
3878857Srgrimes *	vm_page_alloc().
38821581Swpaul */
3891927Swollmanvoid
3908091Swpaulvm_page_free(vm_page_t m)
3911927Swollman{
3921927Swollman	vm_page_flag_clear(m, PG_ZERO);
3931927Swollman	vm_page_free_toq(m);
3941927Swollman	vm_page_zero_idle_wakeup();
3958425Swpaul}
3968425Swpaul
3978755Swpaul/*
3981927Swollman *	vm_page_free_zero:
3998474Swpaul *
40030762Scharnier *	Free a page to the zerod-pages queue
40130762Scharnier */
4028474Swpaulvoid
40330762Scharniervm_page_free_zero(vm_page_t m)
40430762Scharnier{
4058474Swpaul	vm_page_flag_set(m, PG_ZERO);
4069600Swpaul	vm_page_free_toq(m);
40712862Swpaul}
40830762Scharnier
40930762Scharnier/*
4101927Swollman *	vm_page_sleep_if_busy:
4111927Swollman *
4121927Swollman *	Sleep and release the page queues lock if PG_BUSY is set or,
4131927Swollman *	if also_m_busy is TRUE, busy is non-zero.  Returns TRUE if the
4141927Swollman *	thread slept and the page queues lock was released.
4156732Swpaul *	Otherwise, retains the page queues lock and returns FALSE.
4166732Swpaul */
4176732Swpaulint
4189600Swpaulvm_page_sleep_if_busy(vm_page_t m, int also_m_busy, const char *msg)
4199600Swpaul{
42026135Swpaul	vm_object_t object;
42126135Swpaul	int is_object_locked;
4221927Swollman
4231927Swollman	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4248425Swpaul	if ((m->flags & PG_BUSY) || (also_m_busy && m->busy)) {
4251927Swollman		vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
4268425Swpaul		/*
4278425Swpaul		 * It's possible that while we sleep, the page will get
4288425Swpaul		 * unbusied and freed.  If we are holding the object
4298425Swpaul		 * lock, we will assume we hold a reference to the object
4308425Swpaul		 * such that even if m->object changes, we can re-lock
4318425Swpaul		 * it.
4328425Swpaul		 *
4338425Swpaul		 * Remove mtx_owned() after vm_object locking is finished.
4348425Swpaul		 */
4358425Swpaul		object = m->object;
4361927Swollman		if ((is_object_locked = object != NULL &&
4371927Swollman		     mtx_owned(&object->mtx)))
43830762Scharnier			mtx_unlock(&object->mtx);
43930762Scharnier		msleep(m, &vm_page_queue_mtx, PDROP | PVM, msg, 0);
4401927Swollman		if (is_object_locked)
4411927Swollman			mtx_lock(&object->mtx);
4421927Swollman		return (TRUE);
4431927Swollman	}
4441927Swollman	return (FALSE);
44530762Scharnier}
44630762Scharnier
4471927Swollman/*
44830762Scharnier *	vm_page_dirty:
44930762Scharnier *
4501927Swollman *	make page all dirty
4511927Swollman */
45230762Scharniervoid
45330762Scharniervm_page_dirty(vm_page_t m)
4541927Swollman{
4551927Swollman	KASSERT(m->queue - m->pc != PQ_CACHE,
45630762Scharnier	    ("vm_page_dirty: page in cache!"));
45730762Scharnier	KASSERT(m->queue - m->pc != PQ_FREE,
4581927Swollman	    ("vm_page_dirty: page is free!"));
4591927Swollman	m->dirty = VM_PAGE_BITS_ALL;
4601927Swollman}
46130762Scharnier
46230762Scharnier/*
4631927Swollman *	vm_page_splay:
46412862Swpaul *
4651927Swollman *	Implements Sleator and Tarjan's top-down splay algorithm.  Returns
4661927Swollman *	the vm_page containing the given pindex.  If, however, that
4671927Swollman *	pindex is not found in the vm_object, returns a vm_page that is
4688091Swpaul *	adjacent to the pindex, coming before or after it.
4698425Swpaul */
4701927Swollmanvm_page_t
4718425Swpaulvm_page_splay(vm_pindex_t pindex, vm_page_t root)
4728425Swpaul{
4738425Swpaul	struct vm_page dummy;
47421581Swpaul	vm_page_t lefttreemax, righttreemin, y;
47521581Swpaul
4768246Swpaul	if (root == NULL)
4776478Swpaul		return (root);
4788425Swpaul	lefttreemax = righttreemin = &dummy;
4798425Swpaul	for (;; root = y) {
4808425Swpaul		if (pindex < root->pindex) {
4811927Swollman			if ((y = root->left) == NULL)
4821927Swollman				break;
4838091Swpaul			if (pindex < y->pindex) {
4848425Swpaul				/* Rotate right. */
4851927Swollman				root->left = y->right;
4861927Swollman				y->right = root;
4878091Swpaul				root = y;
4881927Swollman				if ((y = root->left) == NULL)
4891927Swollman					break;
4901927Swollman			}
4911927Swollman			/* Link into the new root's right tree. */
4928755Swpaul			righttreemin->left = root;
49321539Swpaul			righttreemin = root;
4948755Swpaul		} else if (pindex > root->pindex) {
4951927Swollman			if ((y = root->right) == NULL)
4968755Swpaul				break;
4978755Swpaul			if (pindex > y->pindex) {
4988853Swpaul				/* Rotate left. */
4998755Swpaul				root->right = y->left;
5008755Swpaul				y->left = root;
5018091Swpaul				root = y;
5021927Swollman				if ((y = root->right) == NULL)
5031927Swollman					break;
5041927Swollman			}
5051927Swollman			/* Link into the new root's left tree. */
5061927Swollman			lefttreemax->right = root;
50721581Swpaul			lefttreemax = root;
50821581Swpaul		} else
50921581Swpaul			break;
5101927Swollman	}
5111927Swollman	/* Assemble the new root. */
5128091Swpaul	lefttreemax->right = root->left;
5131927Swollman	righttreemin->left = root->right;
5141927Swollman	root->left = dummy.right;
5151927Swollman	root->right = dummy.left;
5161927Swollman	return (root);
5178425Swpaul}
5188425Swpaul
5191927Swollman/*
5201927Swollman *	vm_page_insert:		[ internal use only ]
5218091Swpaul *
5228091Swpaul *	Inserts the given mem entry into the object and object list.
5238091Swpaul *
5248091Swpaul *	The pagetables are not updated but will presumably fault the page
5258853Swpaul *	in if necessary, or if a kernel page the caller will at some point
5268853Swpaul *	enter the page into the kernel's pmap.  We are not allowed to block
5278853Swpaul *	here so we *can't* do this anyway.
5288853Swpaul *
5298853Swpaul *	The object and page must be locked.
5308853Swpaul *	This routine may not block.
5318853Swpaul */
5328091Swpaulvoid
5338853Swpaulvm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
5348853Swpaul{
5358091Swpaul	vm_page_t root;
5368091Swpaul
5378091Swpaul	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
53821539Swpaul	if (m->object != NULL)
53921539Swpaul		panic("vm_page_insert: page already inserted");
54021539Swpaul
5418091Swpaul	/*
54221539Swpaul	 * Record the object/offset pair in this page
54321539Swpaul	 */
5448755Swpaul	m->object = object;
54521539Swpaul	m->pindex = pindex;
54621539Swpaul
54721539Swpaul	/*
5488853Swpaul	 * Now link into the object's ordered list of backed pages.
5498853Swpaul	 */
5508853Swpaul	root = object->root;
5518853Swpaul	if (root == NULL) {
55221539Swpaul		m->left = NULL;
55321539Swpaul		m->right = NULL;
55421539Swpaul		TAILQ_INSERT_TAIL(&object->memq, m, listq);
55521539Swpaul	} else {
55621539Swpaul		root = vm_page_splay(pindex, root);
55721539Swpaul		if (pindex < root->pindex) {
55821539Swpaul			m->left = root->left;
55921539Swpaul			m->right = root;
56021539Swpaul			root->left = NULL;
56121539Swpaul			TAILQ_INSERT_BEFORE(root, m, listq);
56221539Swpaul		} else if (pindex == root->pindex)
56321539Swpaul			panic("vm_page_insert: offset already allocated");
56421539Swpaul		else {
56521539Swpaul			m->right = root->right;
56621539Swpaul			m->left = root;
56721539Swpaul			root->right = NULL;
56821539Swpaul			TAILQ_INSERT_AFTER(&object->memq, root, m, listq);
56921539Swpaul		}
57021539Swpaul	}
57121539Swpaul	object->root = m;
57221539Swpaul	object->generation++;
57321539Swpaul
57421539Swpaul	/*
57521539Swpaul	 * show that the object has one more resident page.
57621539Swpaul	 */
57721539Swpaul	object->resident_page_count++;
57821539Swpaul
57921539Swpaul	/*
58021539Swpaul	 * Since we are inserting a new and possibly dirty page,
58121539Swpaul	 * update the object's OBJ_WRITEABLE and OBJ_MIGHTBEDIRTY flags.
58221539Swpaul	 */
58321539Swpaul	if (m->flags & PG_WRITEABLE)
5848091Swpaul		vm_object_set_writeable_dirty(object);
5858091Swpaul}
5868091Swpaul
5878091Swpaul/*
5888091Swpaul *	vm_page_remove:
5898091Swpaul *				NOTE: used by device pager as well -wfj
5908091Swpaul *
5911927Swollman *	Removes the given mem entry from the object/offset-page
5928091Swpaul *	table and the object page list, but do not invalidate/terminate
5931927Swollman *	the backing store.
5948091Swpaul *
5958091Swpaul *	The object and page must be locked.
5968091Swpaul *	The underlying pmap entry (if any) is NOT removed here.
5971927Swollman *	This routine may not block.
5988091Swpaul */
5998091Swpaulvoid
6001927Swollmanvm_page_remove(vm_page_t m)
6018755Swpaul{
6028755Swpaul	vm_object_t object;
6038091Swpaul	vm_page_t root;
6041927Swollman
6058091Swpaul	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
6068091Swpaul	if (m->object == NULL)
6078091Swpaul		return;
6088091Swpaul	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
6091927Swollman	if ((m->flags & PG_BUSY) == 0) {
6108091Swpaul		panic("vm_page_remove: page not busy");
6118755Swpaul	}
6128091Swpaul
6138091Swpaul	/*
6148755Swpaul	 * Basically destroy the page.
6158755Swpaul	 */
6168091Swpaul	vm_page_wakeup(m);
6178091Swpaul
6188091Swpaul	object = m->object;
6191927Swollman
6201927Swollman	/*
6218755Swpaul	 * Now remove from the object's list of backed pages.
6228091Swpaul	 */
6238091Swpaul	if (m != object->root)
6241927Swollman		vm_page_splay(m->pindex, object->root);
6258091Swpaul	if (m->left == NULL)
6268091Swpaul		root = m->right;
6278091Swpaul	else {
6288091Swpaul		root = vm_page_splay(m->pindex, m->left);
6299600Swpaul		root->right = m->right;
6309600Swpaul	}
6319600Swpaul	object->root = root;
6329600Swpaul	TAILQ_REMOVE(&object->memq, m, listq);
6339600Swpaul
6349600Swpaul	/*
6359600Swpaul	 * And show that the object has one fewer resident page.
6369600Swpaul	 */
6379600Swpaul	object->resident_page_count--;
6389600Swpaul	object->generation++;
6399600Swpaul
6409600Swpaul	m->object = NULL;
6419600Swpaul}
6429600Swpaul
6439600Swpaul/*
6449600Swpaul *	vm_page_lookup:
6458091Swpaul *
6468091Swpaul *	Returns the page associated with the object/offset
6478091Swpaul *	pair specified; if none is found, NULL is returned.
6488091Swpaul *
6498091Swpaul *	The object must be locked.
6508091Swpaul *	This routine may not block.
6518091Swpaul *	This is a critical path routine
6528091Swpaul */
6538091Swpaulvm_page_t
6548091Swpaulvm_page_lookup(vm_object_t object, vm_pindex_t pindex)
6558091Swpaul{
6568091Swpaul	vm_page_t m;
6578091Swpaul
6588091Swpaul	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
6598091Swpaul	if ((m = object->root) != NULL && m->pindex != pindex) {
6608091Swpaul		m = vm_page_splay(pindex, m);
6618091Swpaul		if ((object->root = m)->pindex != pindex)
6628091Swpaul			m = NULL;
6638091Swpaul	}
6648755Swpaul	return (m);
6658091Swpaul}
6668091Swpaul
6678755Swpaul/*
66821539Swpaul *	vm_page_rename:
6698091Swpaul *
6701927Swollman *	Move the given memory entry from its
6711927Swollman *	current object to the specified target object/offset.
67230762Scharnier *
6738755Swpaul *	The object must be locked.
6748425Swpaul *	This routine may not block.
6758425Swpaul *
6768755Swpaul *	Note: swap associated with the page must be invalidated by the move.  We
6778425Swpaul *	      have to do this for several reasons:  (1) we aren't freeing the
6788425Swpaul *	      page, (2) we are dirtying the page, (3) the VM system is probably
6798755Swpaul *	      moving the page from object A to B, and will then later move
6808091Swpaul *	      the backing store from A to B and we can't have a conflict.
6818755Swpaul *
68221539Swpaul *	Note: we *always* dirty the page.  It is necessary both for the
68321539Swpaul *	      fact that we moved it, and because we may be invalidating
6848091Swpaul *	      swap.  If the page is on the cache, we have to deactivate it
6858091Swpaul *	      or vm_page_dirty() will panic.  Dirty pages are not allowed
68621539Swpaul *	      on the cache.
6878755Swpaul */
6888755Swpaulvoid
6898091Swpaulvm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
6908091Swpaul{
6918755Swpaul
6928755Swpaul	vm_page_remove(m);
6938091Swpaul	vm_page_insert(m, new_object, new_pindex);
6948091Swpaul	if (m->queue - m->pc == PQ_CACHE)
6951927Swollman		vm_page_deactivate(m);
6966478Swpaul	vm_page_dirty(m);
6978755Swpaul}
6988755Swpaul
6998755Swpaul/*
7008755Swpaul *	vm_page_select_cache:
7018755Swpaul *
7028755Swpaul *	Find a page on the cache queue with color optimization.  As pages
7038755Swpaul *	might be found, but not applicable, they are deactivated.  This
70426135Swpaul *	keeps us from using potentially busy cached pages.
70526135Swpaul *
70626135Swpaul *	This routine may not block.
70726135Swpaul */
70826135Swpaulvm_page_t
70926135Swpaulvm_page_select_cache(int color)
71026135Swpaul{
71126135Swpaul	vm_page_t m;
71226135Swpaul
71326135Swpaul	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
71426135Swpaul	while ((m = vm_pageq_find(PQ_CACHE, color, FALSE)) != NULL) {
71526135Swpaul		if ((m->flags & PG_BUSY) == 0 && m->busy == 0 &&
71626135Swpaul		    m->hold_count == 0 && (VM_OBJECT_TRYLOCK(m->object) ||
71726135Swpaul		    VM_OBJECT_LOCKED(m->object))) {
71826135Swpaul			KASSERT(m->dirty == 0,
71926135Swpaul			    ("Found dirty cache page %p", m));
72026135Swpaul			KASSERT(!pmap_page_is_mapped(m),
72126135Swpaul			    ("Found mapped cache page %p", m));
72226135Swpaul			KASSERT((m->flags & PG_UNMANAGED) == 0,
72326135Swpaul			    ("Found unmanaged cache page %p", m));
72426135Swpaul			KASSERT(m->wire_count == 0,
72526135Swpaul			    ("Found wired cache page %p", m));
72626135Swpaul			break;
72726135Swpaul		}
72826135Swpaul		vm_page_deactivate(m);
72926135Swpaul	}
73026135Swpaul	return (m);
73126135Swpaul}
73226135Swpaul
73326135Swpaul/*
73426135Swpaul *	vm_page_alloc:
73526135Swpaul *
73626135Swpaul *	Allocate and return a memory cell associated
73726135Swpaul *	with this VM object/offset pair.
7389600Swpaul *
7399600Swpaul *	page_req classes:
74012862Swpaul *	VM_ALLOC_NORMAL		normal process request
74112862Swpaul *	VM_ALLOC_SYSTEM		system *really* needs a page
7428091Swpaul *	VM_ALLOC_INTERRUPT	interrupt time request
74312862Swpaul *	VM_ALLOC_ZERO		zero page
74412862Swpaul *
74512862Swpaul *	This routine may not block.
74612862Swpaul *
74712862Swpaul *	Additional special handling is required when called from an
74812862Swpaul *	interrupt (VM_ALLOC_INTERRUPT).  We are not allowed to mess with
7498091Swpaul *	the page cache in this case.
7508091Swpaul */
7518091Swpaulvm_page_t
7528755Swpaulvm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
7538091Swpaul{
7548091Swpaul	vm_object_t m_object;
7558755Swpaul	vm_page_t m = NULL;
75626135Swpaul	int color, flags, page_req;
7571927Swollman
7581927Swollman	page_req = req & VM_ALLOC_CLASS_MASK;
7598091Swpaul
7608091Swpaul	if ((req & VM_ALLOC_NOOBJ) == 0) {
7618091Swpaul		KASSERT(object != NULL,
7628755Swpaul		    ("vm_page_alloc: NULL object."));
7638755Swpaul		VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
7648755Swpaul		color = (pindex + object->pg_color) & PQ_L2_MASK;
7658091Swpaul	} else
7668091Swpaul		color = pindex & PQ_L2_MASK;
7678091Swpaul
7688091Swpaul	/*
7698091Swpaul	 * The pager is allowed to eat deeper into the free page list.
7708091Swpaul	 */
7718091Swpaul	if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT)) {
7728425Swpaul		page_req = VM_ALLOC_SYSTEM;
7738091Swpaul	};
7741927Swollman
7758091Swpaulloop:
7768091Swpaul	mtx_lock_spin(&vm_page_queue_free_mtx);
7778091Swpaul	if (cnt.v_free_count > cnt.v_free_reserved ||
7788091Swpaul	    (page_req == VM_ALLOC_SYSTEM &&
7798755Swpaul	     cnt.v_cache_count == 0 &&
7801927Swollman	     cnt.v_free_count > cnt.v_interrupt_free_min) ||
7818755Swpaul	    (page_req == VM_ALLOC_INTERRUPT && cnt.v_free_count > 0)) {
7828091Swpaul		/*
7838091Swpaul		 * Allocate from the free queue if the number of free pages
7848091Swpaul		 * exceeds the minimum for the request class.
7851927Swollman		 */
7868755Swpaul		m = vm_pageq_find(PQ_FREE, color, (req & VM_ALLOC_ZERO) != 0);
7878091Swpaul	} else if (page_req != VM_ALLOC_INTERRUPT) {
7888091Swpaul		mtx_unlock_spin(&vm_page_queue_free_mtx);
7898755Swpaul		/*
7908755Swpaul		 * Allocatable from cache (non-interrupt only).  On success,
7918755Swpaul		 * we must free the page and try again, thus ensuring that
7928755Swpaul		 * cnt.v_*_free_min counters are replenished.
7938755Swpaul		 */
7948755Swpaul		vm_page_lock_queues();
7958755Swpaul		if ((m = vm_page_select_cache(color)) == NULL) {
7968755Swpaul#if defined(DIAGNOSTIC)
7971927Swollman			if (cnt.v_cache_count > 0)
7981927Swollman				printf("vm_page_alloc(NORMAL): missing pages on cache queue: %d\n", cnt.v_cache_count);
79912862Swpaul#endif
80012862Swpaul			vm_page_unlock_queues();
80112862Swpaul			atomic_add_int(&vm_pageout_deficit, 1);
80212862Swpaul			pagedaemon_wakeup();
80312862Swpaul			return (NULL);
80412862Swpaul		}
80512862Swpaul		m_object = m->object;
80612862Swpaul		VM_OBJECT_LOCK_ASSERT(m_object, MA_OWNED);
80712862Swpaul		vm_page_busy(m);
80812862Swpaul		vm_page_free(m);
80912862Swpaul		vm_page_unlock_queues();
81012862Swpaul		if (m_object != object)
81112862Swpaul			VM_OBJECT_UNLOCK(m_object);
81212862Swpaul		goto loop;
8138091Swpaul	} else {
8141927Swollman		/*
8158755Swpaul		 * Not allocatable from cache from interrupt, give up.
8168091Swpaul		 */
8171927Swollman		mtx_unlock_spin(&vm_page_queue_free_mtx);
8181927Swollman		atomic_add_int(&vm_pageout_deficit, 1);
8198091Swpaul		pagedaemon_wakeup();
8201927Swollman		return (NULL);
8211927Swollman	}
8221927Swollman
8231927Swollman	/*
8248425Swpaul	 *  At this point we had better have found a good page.
8251927Swollman	 */
8261927Swollman
8271927Swollman	KASSERT(
8281927Swollman	    m != NULL,
8291927Swollman	    ("vm_page_alloc(): missing page on free queue")
8306732Swpaul	);
8316732Swpaul
8321927Swollman	/*
8331927Swollman	 * Remove from free queue
8341927Swollman	 */
8351927Swollman	vm_pageq_remove_nowakeup(m);
8368425Swpaul
8378091Swpaul	/*
8388091Swpaul	 * Initialize structure.  Only the PG_ZERO flag is inherited.
8398425Swpaul	 */
8408425Swpaul	flags = PG_BUSY;
8418091Swpaul	if (m->flags & PG_ZERO) {
8428755Swpaul		vm_page_zero_count--;
8438755Swpaul		if (req & VM_ALLOC_ZERO)
8448755Swpaul			flags = PG_ZERO | PG_BUSY;
8458755Swpaul	}
8468853Swpaul	if (req & VM_ALLOC_NOOBJ)
8478853Swpaul		flags &= ~PG_BUSY;
8488853Swpaul	m->flags = flags;
8498755Swpaul	if (req & VM_ALLOC_WIRED) {
8508755Swpaul		atomic_add_int(&cnt.v_wire_count, 1);
8518755Swpaul		m->wire_count = 1;
8529600Swpaul	} else
8539600Swpaul		m->wire_count = 0;
8546732Swpaul	m->hold_count = 0;
8556732Swpaul	m->act_count = 0;
8566732Swpaul	m->busy = 0;
8578246Swpaul	m->valid = 0;
8588755Swpaul	KASSERT(m->dirty == 0, ("vm_page_alloc: free/cache page %p was dirty", m));
8598246Swpaul	mtx_unlock_spin(&vm_page_queue_free_mtx);
8608246Swpaul
8616732Swpaul	if ((req & VM_ALLOC_NOOBJ) == 0)
8626732Swpaul		vm_page_insert(m, object, pindex);
8636732Swpaul	else
8648246Swpaul		m->pindex = pindex;
8658755Swpaul
8668755Swpaul	/*
8678755Swpaul	 * Don't wakeup too often - wakeup the pageout daemon when
8688755Swpaul	 * we would be nearly out of memory.
8698755Swpaul	 */
8708755Swpaul	if (vm_paging_needed())
8718755Swpaul		pagedaemon_wakeup();
8728755Swpaul
8738755Swpaul	return (m);
8748755Swpaul}
8758755Swpaul
8768755Swpaul/*
8778755Swpaul *	vm_wait:	(also see VM_WAIT macro)
8788755Swpaul *
8798755Swpaul *	Block until free pages are available for allocation
8808755Swpaul *	- Called in various places before memory allocations.
8818755Swpaul */
8828755Swpaulvoid
8838755Swpaulvm_wait(void)
8848755Swpaul{
8858755Swpaul
8868246Swpaul	vm_page_lock_queues();
8878246Swpaul	if (curproc == pageproc) {
8881927Swollman		vm_pageout_pages_needed = 1;
8898246Swpaul		msleep(&vm_pageout_pages_needed, &vm_page_queue_mtx,
8901927Swollman		    PDROP | PSWP, "VMWait", 0);
8911927Swollman	} else {
8928857Srgrimes		if (!vm_pages_needed) {
89321539Swpaul			vm_pages_needed = 1;
8948246Swpaul			wakeup(&vm_pages_needed);
8958246Swpaul		}
8961927Swollman		msleep(&cnt.v_free_count, &vm_page_queue_mtx, PDROP | PVM,
8971927Swollman		    "vmwait", 0);
8981927Swollman	}
8998091Swpaul}
9001927Swollman
9011927Swollman/*
9021927Swollman *	vm_waitpfault:	(also see VM_WAITPFAULT macro)
9031927Swollman *
9048091Swpaul *	Block until free pages are available for allocation
90530762Scharnier *	- Called only in vm_fault so that processes page faulting
9068755Swpaul *	  can be easily tracked.
9078755Swpaul *	- Sleeps at a lower priority than vm_wait() so that vm_wait()ing
9086478Swpaul *	  processes will be able to grab memory first.  Do not change
9091927Swollman *	  this balance without careful testing first.
9101927Swollman */
9116478Swpaulvoid
9121927Swollmanvm_waitpfault(void)
9131927Swollman{
9148755Swpaul
9151927Swollman	vm_page_lock_queues();
9161927Swollman	if (!vm_pages_needed) {
9171927Swollman		vm_pages_needed = 1;
9181927Swollman		wakeup(&vm_pages_needed);
9198091Swpaul	}
9201927Swollman	msleep(&cnt.v_free_count, &vm_page_queue_mtx, PDROP | PUSER,
9211927Swollman	    "pfault", 0);
9227864Swpaul}
9231927Swollman
9247864Swpaul/*
9251927Swollman *	vm_page_activate:
9261927Swollman *
9271927Swollman *	Put the specified page on the active list (if appropriate).
9287864Swpaul *	Ensure that act_count is at least ACT_INIT but do not otherwise
9291927Swollman *	mess with it.
9307864Swpaul *
9311927Swollman *	The page queues must be locked.
9321927Swollman *	This routine may not block.
9331927Swollman */
9341927Swollmanvoid
9351927Swollmanvm_page_activate(vm_page_t m)
9361927Swollman{
9371927Swollman
9381927Swollman	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
9391927Swollman	if (m->queue != PQ_ACTIVE) {
9401927Swollman		if ((m->queue - m->pc) == PQ_CACHE)
9411927Swollman			cnt.v_reactivated++;
9421927Swollman		vm_pageq_remove(m);
9431927Swollman		if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
9441927Swollman			if (m->act_count < ACT_INIT)
9451927Swollman				m->act_count = ACT_INIT;
9461927Swollman			vm_pageq_enqueue(PQ_ACTIVE, m);
9471927Swollman		}
9481927Swollman	} else {
94912862Swpaul		if (m->act_count < ACT_INIT)
95012862Swpaul			m->act_count = ACT_INIT;
9511927Swollman	}
9521927Swollman}
95321539Swpaul
9541927Swollman/*
9551927Swollman *	vm_page_free_wakeup:
9561927Swollman *
9571927Swollman *	Helper routine for vm_page_free_toq() and vm_page_cache().  This
9581927Swollman *	routine is called when a page has been added to the cache or free
9599600Swpaul *	queues.
9609600Swpaul *
9619600Swpaul *	The page queues must be locked.
9629600Swpaul *	This routine may not block.
9639600Swpaul */
9649600Swpaulstatic __inline void
9659600Swpaulvm_page_free_wakeup(void)
9669600Swpaul{
9679600Swpaul
9689600Swpaul	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
9699600Swpaul	/*
9709600Swpaul	 * if pageout daemon needs pages, then tell it that there are
9719600Swpaul	 * some free.
9729600Swpaul	 */
9739600Swpaul	if (vm_pageout_pages_needed &&
9749600Swpaul	    cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) {
9759600Swpaul		wakeup(&vm_pageout_pages_needed);
9769600Swpaul		vm_pageout_pages_needed = 0;
9779600Swpaul	}
9789600Swpaul	/*
9799600Swpaul	 * wakeup processes that are waiting on memory if we hit a
9809600Swpaul	 * high water mark. And wakeup scheduler process if we have
9819600Swpaul	 * lots of memory. this process will swapin processes.
9829600Swpaul	 */
9839600Swpaul	if (vm_pages_needed && !vm_page_count_min()) {
9849600Swpaul		vm_pages_needed = 0;
9859600Swpaul		wakeup(&cnt.v_free_count);
9869600Swpaul	}
9879600Swpaul}
9889600Swpaul
9899600Swpaul/*
9909600Swpaul *	vm_page_free_toq:
9919600Swpaul *
9929600Swpaul *	Returns the given page to the PQ_FREE list,
99312862Swpaul *	disassociating it with any VM object.
9949600Swpaul *
9959600Swpaul *	Object and page must be locked prior to entry.
9969600Swpaul *	This routine may not block.
9979600Swpaul */
9989600Swpaul
9999600Swpaulvoid
10009600Swpaulvm_page_free_toq(vm_page_t m)
10019600Swpaul{
10029600Swpaul	struct vpgqueues *pq;
10039600Swpaul	vm_object_t object = m->object;
10049600Swpaul
10059600Swpaul	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
10069600Swpaul	cnt.v_tfree++;
100726135Swpaul
10089600Swpaul	if (m->busy || ((m->queue - m->pc) == PQ_FREE)) {
10099600Swpaul		printf(
1010		"vm_page_free: pindex(%lu), busy(%d), PG_BUSY(%d), hold(%d)\n",
1011		    (u_long)m->pindex, m->busy, (m->flags & PG_BUSY) ? 1 : 0,
1012		    m->hold_count);
1013		if ((m->queue - m->pc) == PQ_FREE)
1014			panic("vm_page_free: freeing free page");
1015		else
1016			panic("vm_page_free: freeing busy page");
1017	}
1018
1019	/*
1020	 * unqueue, then remove page.  Note that we cannot destroy
1021	 * the page here because we do not want to call the pager's
1022	 * callback routine until after we've put the page on the
1023	 * appropriate free queue.
1024	 */
1025	vm_pageq_remove_nowakeup(m);
1026	vm_page_remove(m);
1027
1028	/*
1029	 * If fictitious remove object association and
1030	 * return, otherwise delay object association removal.
1031	 */
1032	if ((m->flags & PG_FICTITIOUS) != 0) {
1033		return;
1034	}
1035
1036	m->valid = 0;
1037	vm_page_undirty(m);
1038
1039	if (m->wire_count != 0) {
1040		if (m->wire_count > 1) {
1041			panic("vm_page_free: invalid wire count (%d), pindex: 0x%lx",
1042				m->wire_count, (long)m->pindex);
1043		}
1044		panic("vm_page_free: freeing wired page");
1045	}
1046
1047	/*
1048	 * If we've exhausted the object's resident pages we want to free
1049	 * it up.
1050	 */
1051	if (object &&
1052	    (object->type == OBJT_VNODE) &&
1053	    ((object->flags & OBJ_DEAD) == 0)
1054	) {
1055		struct vnode *vp = (struct vnode *)object->handle;
1056
1057		if (vp) {
1058			VI_LOCK(vp);
1059			if (VSHOULDFREE(vp))
1060				vfree(vp);
1061			VI_UNLOCK(vp);
1062		}
1063	}
1064
1065	/*
1066	 * Clear the UNMANAGED flag when freeing an unmanaged page.
1067	 */
1068	if (m->flags & PG_UNMANAGED) {
1069		m->flags &= ~PG_UNMANAGED;
1070	}
1071
1072	if (m->hold_count != 0) {
1073		m->flags &= ~PG_ZERO;
1074		m->queue = PQ_HOLD;
1075	} else
1076		m->queue = PQ_FREE + m->pc;
1077	pq = &vm_page_queues[m->queue];
1078	mtx_lock_spin(&vm_page_queue_free_mtx);
1079	pq->lcnt++;
1080	++(*pq->cnt);
1081
1082	/*
1083	 * Put zero'd pages on the end ( where we look for zero'd pages
1084	 * first ) and non-zerod pages at the head.
1085	 */
1086	if (m->flags & PG_ZERO) {
1087		TAILQ_INSERT_TAIL(&pq->pl, m, pageq);
1088		++vm_page_zero_count;
1089	} else {
1090		TAILQ_INSERT_HEAD(&pq->pl, m, pageq);
1091	}
1092	mtx_unlock_spin(&vm_page_queue_free_mtx);
1093	vm_page_free_wakeup();
1094}
1095
1096/*
1097 *	vm_page_unmanage:
1098 *
1099 * 	Prevent PV management from being done on the page.  The page is
1100 *	removed from the paging queues as if it were wired, and as a
1101 *	consequence of no longer being managed the pageout daemon will not
1102 *	touch it (since there is no way to locate the pte mappings for the
1103 *	page).  madvise() calls that mess with the pmap will also no longer
1104 *	operate on the page.
1105 *
1106 *	Beyond that the page is still reasonably 'normal'.  Freeing the page
1107 *	will clear the flag.
1108 *
1109 *	This routine is used by OBJT_PHYS objects - objects using unswappable
1110 *	physical memory as backing store rather then swap-backed memory and
1111 *	will eventually be extended to support 4MB unmanaged physical
1112 *	mappings.
1113 */
1114void
1115vm_page_unmanage(vm_page_t m)
1116{
1117
1118	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1119	if ((m->flags & PG_UNMANAGED) == 0) {
1120		if (m->wire_count == 0)
1121			vm_pageq_remove(m);
1122	}
1123	vm_page_flag_set(m, PG_UNMANAGED);
1124}
1125
1126/*
1127 *	vm_page_wire:
1128 *
1129 *	Mark this page as wired down by yet
1130 *	another map, removing it from paging queues
1131 *	as necessary.
1132 *
1133 *	The page queues must be locked.
1134 *	This routine may not block.
1135 */
1136void
1137vm_page_wire(vm_page_t m)
1138{
1139
1140	/*
1141	 * Only bump the wire statistics if the page is not already wired,
1142	 * and only unqueue the page if it is on some queue (if it is unmanaged
1143	 * it is already off the queues).
1144	 */
1145	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1146	if (m->flags & PG_FICTITIOUS)
1147		return;
1148	if (m->wire_count == 0) {
1149		if ((m->flags & PG_UNMANAGED) == 0)
1150			vm_pageq_remove(m);
1151		atomic_add_int(&cnt.v_wire_count, 1);
1152	}
1153	m->wire_count++;
1154	KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
1155}
1156
1157/*
1158 *	vm_page_unwire:
1159 *
1160 *	Release one wiring of this page, potentially
1161 *	enabling it to be paged again.
1162 *
1163 *	Many pages placed on the inactive queue should actually go
1164 *	into the cache, but it is difficult to figure out which.  What
1165 *	we do instead, if the inactive target is well met, is to put
1166 *	clean pages at the head of the inactive queue instead of the tail.
1167 *	This will cause them to be moved to the cache more quickly and
1168 *	if not actively re-referenced, freed more quickly.  If we just
1169 *	stick these pages at the end of the inactive queue, heavy filesystem
1170 *	meta-data accesses can cause an unnecessary paging load on memory bound
1171 *	processes.  This optimization causes one-time-use metadata to be
1172 *	reused more quickly.
1173 *
1174 *	BUT, if we are in a low-memory situation we have no choice but to
1175 *	put clean pages on the cache queue.
1176 *
1177 *	A number of routines use vm_page_unwire() to guarantee that the page
1178 *	will go into either the inactive or active queues, and will NEVER
1179 *	be placed in the cache - for example, just after dirtying a page.
1180 *	dirty pages in the cache are not allowed.
1181 *
1182 *	The page queues must be locked.
1183 *	This routine may not block.
1184 */
1185void
1186vm_page_unwire(vm_page_t m, int activate)
1187{
1188
1189	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1190	if (m->flags & PG_FICTITIOUS)
1191		return;
1192	if (m->wire_count > 0) {
1193		m->wire_count--;
1194		if (m->wire_count == 0) {
1195			atomic_subtract_int(&cnt.v_wire_count, 1);
1196			if (m->flags & PG_UNMANAGED) {
1197				;
1198			} else if (activate)
1199				vm_pageq_enqueue(PQ_ACTIVE, m);
1200			else {
1201				vm_page_flag_clear(m, PG_WINATCFLS);
1202				vm_pageq_enqueue(PQ_INACTIVE, m);
1203			}
1204		}
1205	} else {
1206		panic("vm_page_unwire: invalid wire count: %d", m->wire_count);
1207	}
1208}
1209
1210
1211/*
1212 * Move the specified page to the inactive queue.  If the page has
1213 * any associated swap, the swap is deallocated.
1214 *
1215 * Normally athead is 0 resulting in LRU operation.  athead is set
1216 * to 1 if we want this page to be 'as if it were placed in the cache',
1217 * except without unmapping it from the process address space.
1218 *
1219 * This routine may not block.
1220 */
1221static __inline void
1222_vm_page_deactivate(vm_page_t m, int athead)
1223{
1224
1225	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1226
1227	/*
1228	 * Ignore if already inactive.
1229	 */
1230	if (m->queue == PQ_INACTIVE)
1231		return;
1232	if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
1233		if ((m->queue - m->pc) == PQ_CACHE)
1234			cnt.v_reactivated++;
1235		vm_page_flag_clear(m, PG_WINATCFLS);
1236		vm_pageq_remove(m);
1237		if (athead)
1238			TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1239		else
1240			TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1241		m->queue = PQ_INACTIVE;
1242		vm_page_queues[PQ_INACTIVE].lcnt++;
1243		cnt.v_inactive_count++;
1244	}
1245}
1246
1247void
1248vm_page_deactivate(vm_page_t m)
1249{
1250    _vm_page_deactivate(m, 0);
1251}
1252
1253/*
1254 * vm_page_try_to_cache:
1255 *
1256 * Returns 0 on failure, 1 on success
1257 */
1258int
1259vm_page_try_to_cache(vm_page_t m)
1260{
1261
1262	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1263	if (m->dirty || m->hold_count || m->busy || m->wire_count ||
1264	    (m->flags & (PG_BUSY|PG_UNMANAGED))) {
1265		return (0);
1266	}
1267	pmap_remove_all(m);
1268	if (m->dirty)
1269		return (0);
1270	vm_page_cache(m);
1271	return (1);
1272}
1273
1274/*
1275 * vm_page_try_to_free()
1276 *
1277 *	Attempt to free the page.  If we cannot free it, we do nothing.
1278 *	1 is returned on success, 0 on failure.
1279 */
1280int
1281vm_page_try_to_free(vm_page_t m)
1282{
1283
1284	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1285	if (m->object != NULL)
1286		VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1287	if (m->dirty || m->hold_count || m->busy || m->wire_count ||
1288	    (m->flags & (PG_BUSY|PG_UNMANAGED))) {
1289		return (0);
1290	}
1291	pmap_remove_all(m);
1292	if (m->dirty)
1293		return (0);
1294	vm_page_busy(m);
1295	vm_page_free(m);
1296	return (1);
1297}
1298
1299/*
1300 * vm_page_cache
1301 *
1302 * Put the specified page onto the page cache queue (if appropriate).
1303 *
1304 * This routine may not block.
1305 */
1306void
1307vm_page_cache(vm_page_t m)
1308{
1309
1310	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1311	if ((m->flags & (PG_BUSY|PG_UNMANAGED)) || m->busy ||
1312	    m->hold_count || m->wire_count) {
1313		printf("vm_page_cache: attempting to cache busy page\n");
1314		return;
1315	}
1316	if ((m->queue - m->pc) == PQ_CACHE)
1317		return;
1318
1319	/*
1320	 * Remove all pmaps and indicate that the page is not
1321	 * writeable or mapped.
1322	 */
1323	pmap_remove_all(m);
1324	if (m->dirty != 0) {
1325		panic("vm_page_cache: caching a dirty page, pindex: %ld",
1326			(long)m->pindex);
1327	}
1328	vm_pageq_remove_nowakeup(m);
1329	vm_pageq_enqueue(PQ_CACHE + m->pc, m);
1330	vm_page_free_wakeup();
1331}
1332
1333/*
1334 * vm_page_dontneed
1335 *
1336 *	Cache, deactivate, or do nothing as appropriate.  This routine
1337 *	is typically used by madvise() MADV_DONTNEED.
1338 *
1339 *	Generally speaking we want to move the page into the cache so
1340 *	it gets reused quickly.  However, this can result in a silly syndrome
1341 *	due to the page recycling too quickly.  Small objects will not be
1342 *	fully cached.  On the otherhand, if we move the page to the inactive
1343 *	queue we wind up with a problem whereby very large objects
1344 *	unnecessarily blow away our inactive and cache queues.
1345 *
1346 *	The solution is to move the pages based on a fixed weighting.  We
1347 *	either leave them alone, deactivate them, or move them to the cache,
1348 *	where moving them to the cache has the highest weighting.
1349 *	By forcing some pages into other queues we eventually force the
1350 *	system to balance the queues, potentially recovering other unrelated
1351 *	space from active.  The idea is to not force this to happen too
1352 *	often.
1353 */
1354void
1355vm_page_dontneed(vm_page_t m)
1356{
1357	static int dnweight;
1358	int dnw;
1359	int head;
1360
1361	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1362	dnw = ++dnweight;
1363
1364	/*
1365	 * occassionally leave the page alone
1366	 */
1367	if ((dnw & 0x01F0) == 0 ||
1368	    m->queue == PQ_INACTIVE ||
1369	    m->queue - m->pc == PQ_CACHE
1370	) {
1371		if (m->act_count >= ACT_INIT)
1372			--m->act_count;
1373		return;
1374	}
1375
1376	if (m->dirty == 0 && pmap_is_modified(m))
1377		vm_page_dirty(m);
1378
1379	if (m->dirty || (dnw & 0x0070) == 0) {
1380		/*
1381		 * Deactivate the page 3 times out of 32.
1382		 */
1383		head = 0;
1384	} else {
1385		/*
1386		 * Cache the page 28 times out of every 32.  Note that
1387		 * the page is deactivated instead of cached, but placed
1388		 * at the head of the queue instead of the tail.
1389		 */
1390		head = 1;
1391	}
1392	_vm_page_deactivate(m, head);
1393}
1394
1395/*
1396 * Grab a page, waiting until we are waken up due to the page
1397 * changing state.  We keep on waiting, if the page continues
1398 * to be in the object.  If the page doesn't exist, first allocate it
1399 * and then conditionally zero it.
1400 *
1401 * This routine may block.
1402 */
1403vm_page_t
1404vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
1405{
1406	vm_page_t m;
1407
1408	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1409retrylookup:
1410	if ((m = vm_page_lookup(object, pindex)) != NULL) {
1411		vm_page_lock_queues();
1412		if (m->busy || (m->flags & PG_BUSY)) {
1413			vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
1414			VM_OBJECT_UNLOCK(object);
1415			msleep(m, &vm_page_queue_mtx, PDROP | PVM, "pgrbwt", 0);
1416			VM_OBJECT_LOCK(object);
1417			if ((allocflags & VM_ALLOC_RETRY) == 0)
1418				return (NULL);
1419			goto retrylookup;
1420		} else {
1421			if (allocflags & VM_ALLOC_WIRED)
1422				vm_page_wire(m);
1423			vm_page_busy(m);
1424			vm_page_unlock_queues();
1425			return (m);
1426		}
1427	}
1428	m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_RETRY);
1429	if (m == NULL) {
1430		VM_OBJECT_UNLOCK(object);
1431		VM_WAIT;
1432		VM_OBJECT_LOCK(object);
1433		if ((allocflags & VM_ALLOC_RETRY) == 0)
1434			return (NULL);
1435		goto retrylookup;
1436	}
1437	if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
1438		pmap_zero_page(m);
1439	return (m);
1440}
1441
1442/*
1443 * Mapping function for valid bits or for dirty bits in
1444 * a page.  May not block.
1445 *
1446 * Inputs are required to range within a page.
1447 */
1448__inline int
1449vm_page_bits(int base, int size)
1450{
1451	int first_bit;
1452	int last_bit;
1453
1454	KASSERT(
1455	    base + size <= PAGE_SIZE,
1456	    ("vm_page_bits: illegal base/size %d/%d", base, size)
1457	);
1458
1459	if (size == 0)		/* handle degenerate case */
1460		return (0);
1461
1462	first_bit = base >> DEV_BSHIFT;
1463	last_bit = (base + size - 1) >> DEV_BSHIFT;
1464
1465	return ((2 << last_bit) - (1 << first_bit));
1466}
1467
1468/*
1469 *	vm_page_set_validclean:
1470 *
1471 *	Sets portions of a page valid and clean.  The arguments are expected
1472 *	to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
1473 *	of any partial chunks touched by the range.  The invalid portion of
1474 *	such chunks will be zero'd.
1475 *
1476 *	This routine may not block.
1477 *
1478 *	(base + size) must be less then or equal to PAGE_SIZE.
1479 */
1480void
1481vm_page_set_validclean(vm_page_t m, int base, int size)
1482{
1483	int pagebits;
1484	int frag;
1485	int endoff;
1486
1487	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1488	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1489	if (size == 0)	/* handle degenerate case */
1490		return;
1491
1492	/*
1493	 * If the base is not DEV_BSIZE aligned and the valid
1494	 * bit is clear, we have to zero out a portion of the
1495	 * first block.
1496	 */
1497	if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
1498	    (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
1499		pmap_zero_page_area(m, frag, base - frag);
1500
1501	/*
1502	 * If the ending offset is not DEV_BSIZE aligned and the
1503	 * valid bit is clear, we have to zero out a portion of
1504	 * the last block.
1505	 */
1506	endoff = base + size;
1507	if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
1508	    (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
1509		pmap_zero_page_area(m, endoff,
1510		    DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
1511
1512	/*
1513	 * Set valid, clear dirty bits.  If validating the entire
1514	 * page we can safely clear the pmap modify bit.  We also
1515	 * use this opportunity to clear the PG_NOSYNC flag.  If a process
1516	 * takes a write fault on a MAP_NOSYNC memory area the flag will
1517	 * be set again.
1518	 *
1519	 * We set valid bits inclusive of any overlap, but we can only
1520	 * clear dirty bits for DEV_BSIZE chunks that are fully within
1521	 * the range.
1522	 */
1523	pagebits = vm_page_bits(base, size);
1524	m->valid |= pagebits;
1525#if 0	/* NOT YET */
1526	if ((frag = base & (DEV_BSIZE - 1)) != 0) {
1527		frag = DEV_BSIZE - frag;
1528		base += frag;
1529		size -= frag;
1530		if (size < 0)
1531			size = 0;
1532	}
1533	pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
1534#endif
1535	m->dirty &= ~pagebits;
1536	if (base == 0 && size == PAGE_SIZE) {
1537		pmap_clear_modify(m);
1538		vm_page_flag_clear(m, PG_NOSYNC);
1539	}
1540}
1541
1542void
1543vm_page_clear_dirty(vm_page_t m, int base, int size)
1544{
1545
1546	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1547	m->dirty &= ~vm_page_bits(base, size);
1548}
1549
1550/*
1551 *	vm_page_set_invalid:
1552 *
1553 *	Invalidates DEV_BSIZE'd chunks within a page.  Both the
1554 *	valid and dirty bits for the effected areas are cleared.
1555 *
1556 *	May not block.
1557 */
1558void
1559vm_page_set_invalid(vm_page_t m, int base, int size)
1560{
1561	int bits;
1562
1563	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1564	bits = vm_page_bits(base, size);
1565	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1566	m->valid &= ~bits;
1567	m->dirty &= ~bits;
1568	m->object->generation++;
1569}
1570
1571/*
1572 * vm_page_zero_invalid()
1573 *
1574 *	The kernel assumes that the invalid portions of a page contain
1575 *	garbage, but such pages can be mapped into memory by user code.
1576 *	When this occurs, we must zero out the non-valid portions of the
1577 *	page so user code sees what it expects.
1578 *
1579 *	Pages are most often semi-valid when the end of a file is mapped
1580 *	into memory and the file's size is not page aligned.
1581 */
1582void
1583vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
1584{
1585	int b;
1586	int i;
1587
1588	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1589	/*
1590	 * Scan the valid bits looking for invalid sections that
1591	 * must be zerod.  Invalid sub-DEV_BSIZE'd areas ( where the
1592	 * valid bit may be set ) have already been zerod by
1593	 * vm_page_set_validclean().
1594	 */
1595	for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
1596		if (i == (PAGE_SIZE / DEV_BSIZE) ||
1597		    (m->valid & (1 << i))
1598		) {
1599			if (i > b) {
1600				pmap_zero_page_area(m,
1601				    b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
1602			}
1603			b = i + 1;
1604		}
1605	}
1606
1607	/*
1608	 * setvalid is TRUE when we can safely set the zero'd areas
1609	 * as being valid.  We can do this if there are no cache consistancy
1610	 * issues.  e.g. it is ok to do with UFS, but not ok to do with NFS.
1611	 */
1612	if (setvalid)
1613		m->valid = VM_PAGE_BITS_ALL;
1614}
1615
1616/*
1617 *	vm_page_is_valid:
1618 *
1619 *	Is (partial) page valid?  Note that the case where size == 0
1620 *	will return FALSE in the degenerate case where the page is
1621 *	entirely invalid, and TRUE otherwise.
1622 *
1623 *	May not block.
1624 */
1625int
1626vm_page_is_valid(vm_page_t m, int base, int size)
1627{
1628	int bits = vm_page_bits(base, size);
1629
1630	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1631	if (m->valid && ((m->valid & bits) == bits))
1632		return 1;
1633	else
1634		return 0;
1635}
1636
1637/*
1638 * update dirty bits from pmap/mmu.  May not block.
1639 */
1640void
1641vm_page_test_dirty(vm_page_t m)
1642{
1643	if ((m->dirty != VM_PAGE_BITS_ALL) && pmap_is_modified(m)) {
1644		vm_page_dirty(m);
1645	}
1646}
1647
1648int so_zerocp_fullpage = 0;
1649
1650void
1651vm_page_cowfault(vm_page_t m)
1652{
1653	vm_page_t mnew;
1654	vm_object_t object;
1655	vm_pindex_t pindex;
1656
1657	object = m->object;
1658	pindex = m->pindex;
1659	vm_page_busy(m);
1660
1661 retry_alloc:
1662	vm_page_remove(m);
1663	mnew = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
1664	if (mnew == NULL) {
1665		vm_page_insert(m, object, pindex);
1666		vm_page_unlock_queues();
1667		VM_OBJECT_UNLOCK(object);
1668		VM_WAIT;
1669		VM_OBJECT_LOCK(object);
1670		vm_page_lock_queues();
1671		goto retry_alloc;
1672	}
1673
1674	if (m->cow == 0) {
1675		/*
1676		 * check to see if we raced with an xmit complete when
1677		 * waiting to allocate a page.  If so, put things back
1678		 * the way they were
1679		 */
1680		vm_page_busy(mnew);
1681		vm_page_free(mnew);
1682		vm_page_insert(m, object, pindex);
1683	} else { /* clear COW & copy page */
1684		if (!so_zerocp_fullpage)
1685			pmap_copy_page(m, mnew);
1686		mnew->valid = VM_PAGE_BITS_ALL;
1687		vm_page_dirty(mnew);
1688		vm_page_flag_clear(mnew, PG_BUSY);
1689	}
1690}
1691
1692void
1693vm_page_cowclear(vm_page_t m)
1694{
1695
1696	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1697	if (m->cow) {
1698		m->cow--;
1699		/*
1700		 * let vm_fault add back write permission  lazily
1701		 */
1702	}
1703	/*
1704	 *  sf_buf_free() will free the page, so we needn't do it here
1705	 */
1706}
1707
1708void
1709vm_page_cowsetup(vm_page_t m)
1710{
1711
1712	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1713	m->cow++;
1714	pmap_page_protect(m, VM_PROT_READ);
1715}
1716
1717#include "opt_ddb.h"
1718#ifdef DDB
1719#include <sys/kernel.h>
1720
1721#include <ddb/ddb.h>
1722
1723DB_SHOW_COMMAND(page, vm_page_print_page_info)
1724{
1725	db_printf("cnt.v_free_count: %d\n", cnt.v_free_count);
1726	db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count);
1727	db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count);
1728	db_printf("cnt.v_active_count: %d\n", cnt.v_active_count);
1729	db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count);
1730	db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved);
1731	db_printf("cnt.v_free_min: %d\n", cnt.v_free_min);
1732	db_printf("cnt.v_free_target: %d\n", cnt.v_free_target);
1733	db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min);
1734	db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target);
1735}
1736
1737DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
1738{
1739	int i;
1740	db_printf("PQ_FREE:");
1741	for (i = 0; i < PQ_L2_SIZE; i++) {
1742		db_printf(" %d", vm_page_queues[PQ_FREE + i].lcnt);
1743	}
1744	db_printf("\n");
1745
1746	db_printf("PQ_CACHE:");
1747	for (i = 0; i < PQ_L2_SIZE; i++) {
1748		db_printf(" %d", vm_page_queues[PQ_CACHE + i].lcnt);
1749	}
1750	db_printf("\n");
1751
1752	db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n",
1753		vm_page_queues[PQ_ACTIVE].lcnt,
1754		vm_page_queues[PQ_INACTIVE].lcnt);
1755}
1756#endif /* DDB */
1757