vm_page.c revision 166637
1275970Scy/*-
2275970Scy * Copyright (c) 1991 Regents of the University of California.
3275970Scy * All rights reserved.
4275970Scy *
5275970Scy * This code is derived from software contributed to Berkeley by
6275970Scy * The Mach Operating System project at Carnegie-Mellon University.
7275970Scy *
8275970Scy * Redistribution and use in source and binary forms, with or without
9275970Scy * modification, are permitted provided that the following conditions
10275970Scy * are met:
11275970Scy * 1. Redistributions of source code must retain the above copyright
12275970Scy *    notice, this list of conditions and the following disclaimer.
13285612Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
14275970Scy *    notice, this list of conditions and the following disclaimer in the
15285612Sdelphij *    documentation and/or other materials provided with the distribution.
16275970Scy * 4. Neither the name of the University nor the names of its contributors
17285612Sdelphij *    may be used to endorse or promote products derived from this software
18275970Scy *    without specific prior written permission.
19275970Scy *
20275970Scy * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21275970Scy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22275970Scy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23275970Scy * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24275970Scy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25275970Scy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26275970Scy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27275970Scy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28275970Scy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29275970Scy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30275970Scy * SUCH DAMAGE.
31275970Scy *
32275970Scy *	from: @(#)vm_page.c	7.4 (Berkeley) 5/7/91
33275970Scy */
34275970Scy
35275970Scy/*-
36275970Scy * Copyright (c) 1987, 1990 Carnegie-Mellon University.
37275970Scy * All rights reserved.
38275970Scy *
39275970Scy * Authors: Avadis Tevanian, Jr., Michael Wayne Young
40275970Scy *
41275970Scy * Permission to use, copy, modify and distribute this software and
42275970Scy * its documentation is hereby granted, provided that both the copyright
43275970Scy * notice and this permission notice appear in all copies of the
44275970Scy * software, derivative works or modified versions, and any portions
45275970Scy * thereof, and that both notices appear in supporting documentation.
46275970Scy *
47275970Scy * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
48275970Scy * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
49275970Scy * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
50275970Scy *
51275970Scy * Carnegie Mellon requests users of this software to return to
52275970Scy *
53275970Scy *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
54275970Scy *  School of Computer Science
55275970Scy *  Carnegie Mellon University
56275970Scy *  Pittsburgh PA 15213-3890
57275970Scy *
58275970Scy * any improvements or extensions that they make and grant Carnegie the
59275970Scy * rights to redistribute these changes.
60275970Scy */
61275970Scy
62285612Sdelphij/*
63275970Scy *			GENERAL RULES ON VM_PAGE MANIPULATION
64275970Scy *
65275970Scy *	- a pageq mutex is required when adding or removing a page from a
66275970Scy *	  page queue (vm_page_queue[]), regardless of other mutexes or the
67275970Scy *	  busy state of a page.
68275970Scy *
69275970Scy *	- a hash chain mutex is required when associating or disassociating
70275970Scy *	  a page from the VM PAGE CACHE hash table (vm_page_buckets),
71275970Scy *	  regardless of other mutexes or the busy state of a page.
72275970Scy *
73275970Scy *	- either a hash chain mutex OR a busied page is required in order
74275970Scy *	  to modify the page flags.  A hash chain mutex must be obtained in
75275970Scy *	  order to busy a page.  A page's flags cannot be modified by a
76275970Scy *	  hash chain mutex if the page is marked busy.
77275970Scy *
78275970Scy *	- The object memq mutex is held when inserting or removing
79275970Scy *	  pages from an object (vm_page_insert() or vm_page_remove()).  This
80275970Scy *	  is different from the object's main mutex.
81285612Sdelphij *
82275970Scy *	Generally speaking, you have to be aware of side effects when running
83275970Scy *	vm_page ops.  A vm_page_lookup() will return with the hash chain
84275970Scy *	locked, whether it was able to lookup the page or not.  vm_page_free(),
85275970Scy *	vm_page_cache(), vm_page_activate(), and a number of other routines
86275970Scy *	will release the hash chain mutex for you.  Intermediate manipulation
87275970Scy *	routines such as vm_page_flag_set() expect the hash chain to be held
88275970Scy *	on entry and the hash chain will remain held on return.
89275970Scy *
90275970Scy *	pageq scanning can only occur with the pageq in question locked.
91275970Scy *	We have a known bottleneck with the active queue, but the cache
92275970Scy *	and free queues are actually arrays already.
93275970Scy */
94275970Scy
95275970Scy/*
96285612Sdelphij *	Resident memory management module.
97275970Scy */
98285612Sdelphij
99275970Scy#include <sys/cdefs.h>
100275970Scy__FBSDID("$FreeBSD: head/sys/vm/vm_page.c 166637 2007-02-11 05:18:40Z alc $");
101275970Scy
102275970Scy#include <sys/param.h>
103275970Scy#include <sys/systm.h>
104275970Scy#include <sys/lock.h>
105275970Scy#include <sys/kernel.h>
106285612Sdelphij#include <sys/malloc.h>
107275970Scy#include <sys/mutex.h>
108275970Scy#include <sys/proc.h>
109275970Scy#include <sys/sysctl.h>
110285612Sdelphij#include <sys/vmmeter.h>
111275970Scy#include <sys/vnode.h>
112275970Scy
113275970Scy#include <vm/vm.h>
114275970Scy#include <vm/vm_param.h>
115275970Scy#include <vm/vm_kern.h>
116275970Scy#include <vm/vm_object.h>
117275970Scy#include <vm/vm_page.h>
118275970Scy#include <vm/vm_pageout.h>
119275970Scy#include <vm/vm_pager.h>
120275970Scy#include <vm/vm_extern.h>
121275970Scy#include <vm/uma.h>
122275970Scy#include <vm/uma_int.h>
123275970Scy
124275970Scy#include <machine/md_var.h>
125275970Scy
126275970Scy/*
127275970Scy *	Associated with page of user-allocatable memory is a
128275970Scy *	page structure.
129275970Scy */
130275970Scy
131275970Scystruct mtx vm_page_queue_mtx;
132275970Scystruct mtx vm_page_queue_free_mtx;
133275970Scy
134275970Scyvm_page_t vm_page_array = 0;
135275970Scyint vm_page_array_size = 0;
136275970Scylong first_page = 0;
137275970Scyint vm_page_zero_count = 0;
138275970Scy
139275970Scystatic int boot_pages = UMA_BOOT_PAGES;
140275970ScyTUNABLE_INT("vm.boot_pages", &boot_pages);
141275970ScySYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RD, &boot_pages, 0,
142275970Scy	"number of pages allocated for bootstrapping the VM system");
143275970Scy
144275970Scy/*
145275970Scy *	vm_set_page_size:
146275970Scy *
147275970Scy *	Sets the page size, perhaps based upon the memory
148275970Scy *	size.  Must be called before any use of page-size
149275970Scy *	dependent functions.
150275970Scy */
151275970Scyvoid
152275970Scyvm_set_page_size(void)
153275970Scy{
154275970Scy	if (cnt.v_page_size == 0)
155275970Scy		cnt.v_page_size = PAGE_SIZE;
156275970Scy	if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0)
157275970Scy		panic("vm_set_page_size: page size not a power of two");
158275970Scy}
159275970Scy
160275970Scy/*
161275970Scy *	vm_page_blacklist_lookup:
162275970Scy *
163275970Scy *	See if a physical address in this page has been listed
164275970Scy *	in the blacklist tunable.  Entries in the tunable are
165275970Scy *	separated by spaces or commas.  If an invalid integer is
166275970Scy *	encountered then the rest of the string is skipped.
167275970Scy */
168275970Scystatic int
169275970Scyvm_page_blacklist_lookup(char *list, vm_paddr_t pa)
170275970Scy{
171275970Scy	vm_paddr_t bad;
172275970Scy	char *cp, *pos;
173275970Scy
174275970Scy	for (pos = list; *pos != '\0'; pos = cp) {
175275970Scy		bad = strtoq(pos, &cp, 0);
176275970Scy		if (*cp != '\0') {
177275970Scy			if (*cp == ' ' || *cp == ',') {
178285612Sdelphij				cp++;
179275970Scy				if (cp == pos)
180275970Scy					continue;
181275970Scy			} else
182275970Scy				break;
183275970Scy		}
184275970Scy		if (pa == trunc_page(bad))
185275970Scy			return (1);
186275970Scy	}
187275970Scy	return (0);
188275970Scy}
189275970Scy
190275970Scy/*
191275970Scy *	vm_page_startup:
192275970Scy *
193275970Scy *	Initializes the resident memory module.
194275970Scy *
195275970Scy *	Allocates memory for the page cells, and
196275970Scy *	for the object/offset-to-page hash table headers.
197275970Scy *	Each page cell is initialized and placed on the free list.
198275970Scy */
199285612Sdelphijvm_offset_t
200285612Sdelphijvm_page_startup(vm_offset_t vaddr)
201285612Sdelphij{
202285612Sdelphij	vm_offset_t mapped;
203285612Sdelphij	vm_size_t npages;
204285612Sdelphij	vm_paddr_t page_range;
205285612Sdelphij	vm_paddr_t new_end;
206285612Sdelphij	int i;
207285612Sdelphij	vm_paddr_t pa;
208285612Sdelphij	int nblocks;
209285612Sdelphij	vm_paddr_t last_pa;
210285612Sdelphij	char *list;
211285612Sdelphij
212285612Sdelphij	/* the biggest memory array is the second group of pages */
213275970Scy	vm_paddr_t end;
214275970Scy	vm_paddr_t biggestsize;
215275970Scy	vm_paddr_t low_water, high_water;
216275970Scy	int biggestone;
217275970Scy
218275970Scy	vm_paddr_t total;
219275970Scy
220275970Scy	total = 0;
221275970Scy	biggestsize = 0;
222275970Scy	biggestone = 0;
223275970Scy	nblocks = 0;
224275970Scy	vaddr = round_page(vaddr);
225275970Scy
226275970Scy	for (i = 0; phys_avail[i + 1]; i += 2) {
227275970Scy		phys_avail[i] = round_page(phys_avail[i]);
228275970Scy		phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
229275970Scy	}
230275970Scy
231275970Scy	low_water = phys_avail[0];
232275970Scy	high_water = phys_avail[1];
233275970Scy
234275970Scy	for (i = 0; phys_avail[i + 1]; i += 2) {
235275970Scy		vm_paddr_t size = phys_avail[i + 1] - phys_avail[i];
236275970Scy
237285612Sdelphij		if (size > biggestsize) {
238275970Scy			biggestone = i;
239275970Scy			biggestsize = size;
240275970Scy		}
241275970Scy		if (phys_avail[i] < low_water)
242275970Scy			low_water = phys_avail[i];
243275970Scy		if (phys_avail[i + 1] > high_water)
244275970Scy			high_water = phys_avail[i + 1];
245275970Scy		++nblocks;
246275970Scy		total += size;
247275970Scy	}
248275970Scy
249275970Scy	end = phys_avail[biggestone+1];
250275970Scy
251275970Scy	/*
252275970Scy	 * Initialize the locks.
253275970Scy	 */
254275970Scy	mtx_init(&vm_page_queue_mtx, "vm page queue mutex", NULL, MTX_DEF |
255275970Scy	    MTX_RECURSE);
256275970Scy	mtx_init(&vm_page_queue_free_mtx, "vm page queue free mutex", NULL,
257275970Scy	    MTX_DEF);
258275970Scy
259275970Scy	/*
260275970Scy	 * Initialize the queue headers for the free queue, the active queue
261275970Scy	 * and the inactive queue.
262275970Scy	 */
263275970Scy	vm_pageq_init();
264275970Scy
265275970Scy	/*
266275970Scy	 * Allocate memory for use when boot strapping the kernel memory
267275970Scy	 * allocator.
268275970Scy	 */
269275970Scy	new_end = end - (boot_pages * UMA_SLAB_SIZE);
270275970Scy	new_end = trunc_page(new_end);
271275970Scy	mapped = pmap_map(&vaddr, new_end, end,
272275970Scy	    VM_PROT_READ | VM_PROT_WRITE);
273275970Scy	bzero((void *)mapped, end - new_end);
274275970Scy	uma_startup((void *)mapped, boot_pages);
275275970Scy
276275970Scy#if defined(__amd64__) || defined(__i386__)
277275970Scy	/*
278275970Scy	 * Allocate a bitmap to indicate that a random physical page
279275970Scy	 * needs to be included in a minidump.
280275970Scy	 *
281275970Scy	 * The amd64 port needs this to indicate which direct map pages
282275970Scy	 * need to be dumped, via calls to dump_add_page()/dump_drop_page().
283275970Scy	 *
284275970Scy	 * However, i386 still needs this workspace internally within the
285275970Scy	 * minidump code.  In theory, they are not needed on i386, but are
286275970Scy	 * included should the sf_buf code decide to use them.
287275970Scy	 */
288275970Scy	page_range = phys_avail[(nblocks - 1) * 2 + 1] / PAGE_SIZE;
289275970Scy	vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY);
290275970Scy	new_end -= vm_page_dump_size;
291275970Scy	vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
292275970Scy	    new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
293275970Scy	bzero((void *)vm_page_dump, vm_page_dump_size);
294275970Scy#endif
295275970Scy	/*
296275970Scy	 * Compute the number of pages of memory that will be available for
297275970Scy	 * use (taking into account the overhead of a page structure per
298275970Scy	 * page).
299275970Scy	 */
300275970Scy	first_page = low_water / PAGE_SIZE;
301275970Scy	page_range = high_water / PAGE_SIZE - first_page;
302275970Scy	npages = (total - (page_range * sizeof(struct vm_page)) -
303275970Scy	    (end - new_end)) / PAGE_SIZE;
304275970Scy	end = new_end;
305275970Scy
306275970Scy	/*
307275970Scy	 * Reserve an unmapped guard page to trap access to vm_page_array[-1].
308275970Scy	 */
309275970Scy	vaddr += PAGE_SIZE;
310275970Scy
311275970Scy	/*
312275970Scy	 * Initialize the mem entry structures now, and put them in the free
313275970Scy	 * queue.
314275970Scy	 */
315275970Scy	new_end = trunc_page(end - page_range * sizeof(struct vm_page));
316275970Scy	mapped = pmap_map(&vaddr, new_end, end,
317275970Scy	    VM_PROT_READ | VM_PROT_WRITE);
318275970Scy	vm_page_array = (vm_page_t) mapped;
319275970Scy#ifdef __amd64__
320275970Scy	/*
321275970Scy	 * pmap_map on amd64 comes out of the direct-map, not kvm like i386,
322275970Scy	 * so the pages must be tracked for a crashdump to include this data.
323275970Scy	 * This includes the vm_page_array and the early UMA bootstrap pages.
324275970Scy	 */
325275970Scy	for (pa = new_end; pa < phys_avail[biggestone + 1]; pa += PAGE_SIZE)
326275970Scy		dump_add_page(pa);
327275970Scy#endif
328275970Scy	phys_avail[biggestone + 1] = new_end;
329275970Scy
330275970Scy	/*
331275970Scy	 * Clear all of the page structures
332275970Scy	 */
333275970Scy	bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
334275970Scy	vm_page_array_size = page_range;
335275970Scy
336275970Scy	/*
337275970Scy	 * This assertion tests the hypothesis that npages and total are
338275970Scy	 * redundant.  XXX
339275970Scy	 */
340275970Scy	page_range = 0;
341275970Scy	for (i = 0; phys_avail[i + 1] != 0; i += 2)
342275970Scy		page_range += atop(phys_avail[i + 1] - phys_avail[i]);
343275970Scy	KASSERT(page_range == npages,
344275970Scy	    ("vm_page_startup: inconsistent page counts"));
345275970Scy
346275970Scy	/*
347275970Scy	 * Construct the free queue(s) in descending order (by physical
348275970Scy	 * address) so that the first 16MB of physical memory is allocated
349275970Scy	 * last rather than first.  On large-memory machines, this avoids
350275970Scy	 * the exhaustion of low physical memory before isa_dma_init has run.
351275970Scy	 */
352275970Scy	cnt.v_page_count = 0;
353275970Scy	cnt.v_free_count = 0;
354275970Scy	list = getenv("vm.blacklist");
355275970Scy	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
356275970Scy		pa = phys_avail[i];
357275970Scy		last_pa = phys_avail[i + 1];
358275970Scy		while (pa < last_pa) {
359275970Scy			if (list != NULL &&
360275970Scy			    vm_page_blacklist_lookup(list, pa))
361275970Scy				printf("Skipping page with pa 0x%jx\n",
362275970Scy				    (uintmax_t)pa);
363275970Scy			else
364275970Scy				vm_pageq_add_new_page(pa);
365275970Scy			pa += PAGE_SIZE;
366275970Scy		}
367275970Scy	}
368275970Scy	freeenv(list);
369275970Scy	return (vaddr);
370275970Scy}
371275970Scy
372275970Scyvoid
373275970Scyvm_page_flag_set(vm_page_t m, unsigned short bits)
374275970Scy{
375275970Scy
376275970Scy	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
377275970Scy	m->flags |= bits;
378275970Scy}
379275970Scy
380275970Scyvoid
381275970Scyvm_page_flag_clear(vm_page_t m, unsigned short bits)
382275970Scy{
383275970Scy
384275970Scy	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
385275970Scy	m->flags &= ~bits;
386275970Scy}
387275970Scy
388275970Scyvoid
389275970Scyvm_page_busy(vm_page_t m)
390275970Scy{
391275970Scy
392275970Scy	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
393275970Scy	KASSERT((m->oflags & VPO_BUSY) == 0,
394275970Scy	    ("vm_page_busy: page already busy!!!"));
395275970Scy	m->oflags |= VPO_BUSY;
396275970Scy}
397275970Scy
398275970Scy/*
399275970Scy *      vm_page_flash:
400275970Scy *
401275970Scy *      wakeup anyone waiting for the page.
402275970Scy */
403275970Scyvoid
404275970Scyvm_page_flash(vm_page_t m)
405275970Scy{
406275970Scy
407275970Scy	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
408275970Scy	if (m->oflags & VPO_WANTED) {
409275970Scy		m->oflags &= ~VPO_WANTED;
410275970Scy		wakeup(m);
411275970Scy	}
412275970Scy}
413275970Scy
414275970Scy/*
415275970Scy *      vm_page_wakeup:
416275970Scy *
417275970Scy *      clear the VPO_BUSY flag and wakeup anyone waiting for the
418275970Scy *      page.
419275970Scy *
420275970Scy */
421275970Scyvoid
422275970Scyvm_page_wakeup(vm_page_t m)
423275970Scy{
424275970Scy
425275970Scy	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
426275970Scy	KASSERT(m->oflags & VPO_BUSY, ("vm_page_wakeup: page not busy!!!"));
427275970Scy	m->oflags &= ~VPO_BUSY;
428275970Scy	vm_page_flash(m);
429275970Scy}
430275970Scy
431275970Scyvoid
432275970Scyvm_page_io_start(vm_page_t m)
433275970Scy{
434275970Scy
435275970Scy	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
436275970Scy	m->busy++;
437275970Scy}
438275970Scy
439275970Scyvoid
440275970Scyvm_page_io_finish(vm_page_t m)
441275970Scy{
442275970Scy
443275970Scy	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
444275970Scy	m->busy--;
445275970Scy	if (m->busy == 0)
446275970Scy		vm_page_flash(m);
447275970Scy}
448275970Scy
449275970Scy/*
450275970Scy * Keep page from being freed by the page daemon
451275970Scy * much of the same effect as wiring, except much lower
452275970Scy * overhead and should be used only for *very* temporary
453275970Scy * holding ("wiring").
454275970Scy */
455275970Scyvoid
456275970Scyvm_page_hold(vm_page_t mem)
457275970Scy{
458275970Scy
459275970Scy	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
460275970Scy        mem->hold_count++;
461275970Scy}
462275970Scy
463275970Scyvoid
464275970Scyvm_page_unhold(vm_page_t mem)
465275970Scy{
466275970Scy
467275970Scy	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
468275970Scy	--mem->hold_count;
469275970Scy	KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!"));
470275970Scy	if (mem->hold_count == 0 && VM_PAGE_INQUEUE2(mem, PQ_HOLD))
471275970Scy		vm_page_free_toq(mem);
472275970Scy}
473275970Scy
474275970Scy/*
475285612Sdelphij *	vm_page_free:
476275970Scy *
477275970Scy *	Free a page
478275970Scy *
479275970Scy *	The clearing of PG_ZERO is a temporary safety until the code can be
480275970Scy *	reviewed to determine that PG_ZERO is being properly cleared on
481275970Scy *	write faults or maps.  PG_ZERO was previously cleared in
482275970Scy *	vm_page_alloc().
483275970Scy */
484275970Scyvoid
485275970Scyvm_page_free(vm_page_t m)
486275970Scy{
487275970Scy	vm_page_flag_clear(m, PG_ZERO);
488275970Scy	vm_page_free_toq(m);
489275970Scy}
490275970Scy
491275970Scy/*
492275970Scy *	vm_page_free_zero:
493275970Scy *
494275970Scy *	Free a page to the zerod-pages queue
495275970Scy */
496275970Scyvoid
497275970Scyvm_page_free_zero(vm_page_t m)
498275970Scy{
499275970Scy	vm_page_flag_set(m, PG_ZERO);
500275970Scy	vm_page_free_toq(m);
501275970Scy}
502275970Scy
503275970Scy/*
504275970Scy *	vm_page_sleep:
505275970Scy *
506275970Scy *	Sleep and release the page queues lock.
507275970Scy *
508275970Scy *	The object containing the given page must be locked.
509275970Scy */
510275970Scyvoid
511275970Scyvm_page_sleep(vm_page_t m, const char *msg)
512275970Scy{
513275970Scy
514275970Scy	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
515275970Scy	if (!mtx_owned(&vm_page_queue_mtx))
516275970Scy		vm_page_lock_queues();
517275970Scy	vm_page_flag_set(m, PG_REFERENCED);
518275970Scy	vm_page_unlock_queues();
519275970Scy
520275970Scy	/*
521275970Scy	 * It's possible that while we sleep, the page will get
522275970Scy	 * unbusied and freed.  If we are holding the object
523275970Scy	 * lock, we will assume we hold a reference to the object
524275970Scy	 * such that even if m->object changes, we can re-lock
525275970Scy	 * it.
526275970Scy	 */
527275970Scy	m->oflags |= VPO_WANTED;
528275970Scy	msleep(m, VM_OBJECT_MTX(m->object), PVM, msg, 0);
529275970Scy}
530275970Scy
531275970Scy/*
532275970Scy *	vm_page_dirty:
533275970Scy *
534275970Scy *	make page all dirty
535275970Scy */
536275970Scyvoid
537275970Scyvm_page_dirty(vm_page_t m)
538275970Scy{
539275970Scy	KASSERT(VM_PAGE_GETKNOWNQUEUE1(m) != PQ_CACHE,
540275970Scy	    ("vm_page_dirty: page in cache!"));
541275970Scy	KASSERT(VM_PAGE_GETKNOWNQUEUE1(m) != PQ_FREE,
542275970Scy	    ("vm_page_dirty: page is free!"));
543275970Scy	m->dirty = VM_PAGE_BITS_ALL;
544275970Scy}
545275970Scy
546275970Scy/*
547275970Scy *	vm_page_splay:
548275970Scy *
549275970Scy *	Implements Sleator and Tarjan's top-down splay algorithm.  Returns
550275970Scy *	the vm_page containing the given pindex.  If, however, that
551275970Scy *	pindex is not found in the vm_object, returns a vm_page that is
552275970Scy *	adjacent to the pindex, coming before or after it.
553275970Scy */
554275970Scyvm_page_t
555275970Scyvm_page_splay(vm_pindex_t pindex, vm_page_t root)
556275970Scy{
557275970Scy	struct vm_page dummy;
558275970Scy	vm_page_t lefttreemax, righttreemin, y;
559275970Scy
560275970Scy	if (root == NULL)
561275970Scy		return (root);
562275970Scy	lefttreemax = righttreemin = &dummy;
563275970Scy	for (;; root = y) {
564275970Scy		if (pindex < root->pindex) {
565275970Scy			if ((y = root->left) == NULL)
566275970Scy				break;
567275970Scy			if (pindex < y->pindex) {
568275970Scy				/* Rotate right. */
569275970Scy				root->left = y->right;
570275970Scy				y->right = root;
571275970Scy				root = y;
572275970Scy				if ((y = root->left) == NULL)
573275970Scy					break;
574275970Scy			}
575275970Scy			/* Link into the new root's right tree. */
576275970Scy			righttreemin->left = root;
577275970Scy			righttreemin = root;
578275970Scy		} else if (pindex > root->pindex) {
579275970Scy			if ((y = root->right) == NULL)
580275970Scy				break;
581275970Scy			if (pindex > y->pindex) {
582275970Scy				/* Rotate left. */
583275970Scy				root->right = y->left;
584275970Scy				y->left = root;
585275970Scy				root = y;
586275970Scy				if ((y = root->right) == NULL)
587275970Scy					break;
588275970Scy			}
589275970Scy			/* Link into the new root's left tree. */
590275970Scy			lefttreemax->right = root;
591275970Scy			lefttreemax = root;
592275970Scy		} else
593275970Scy			break;
594275970Scy	}
595275970Scy	/* Assemble the new root. */
596275970Scy	lefttreemax->right = root->left;
597275970Scy	righttreemin->left = root->right;
598275970Scy	root->left = dummy.right;
599275970Scy	root->right = dummy.left;
600275970Scy	return (root);
601275970Scy}
602275970Scy
603275970Scy/*
604285612Sdelphij *	vm_page_insert:		[ internal use only ]
605275970Scy *
606285612Sdelphij *	Inserts the given mem entry into the object and object list.
607275970Scy *
608275970Scy *	The pagetables are not updated but will presumably fault the page
609275970Scy *	in if necessary, or if a kernel page the caller will at some point
610275970Scy *	enter the page into the kernel's pmap.  We are not allowed to block
611275970Scy *	here so we *can't* do this anyway.
612275970Scy *
613275970Scy *	The object and page must be locked.
614285612Sdelphij *	This routine may not block.
615275970Scy */
616275970Scyvoid
617275970Scyvm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
618275970Scy{
619285612Sdelphij	vm_page_t root;
620275970Scy
621275970Scy	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
622275970Scy	if (m->object != NULL)
623275970Scy		panic("vm_page_insert: page already inserted");
624275970Scy
625275970Scy	/*
626275970Scy	 * Record the object/offset pair in this page
627275970Scy	 */
628275970Scy	m->object = object;
629275970Scy	m->pindex = pindex;
630275970Scy
631275970Scy	/*
632275970Scy	 * Now link into the object's ordered list of backed pages.
633285612Sdelphij	 */
634275970Scy	root = object->root;
635275970Scy	if (root == NULL) {
636275970Scy		m->left = NULL;
637275970Scy		m->right = NULL;
638275970Scy		TAILQ_INSERT_TAIL(&object->memq, m, listq);
639275970Scy	} else {
640285612Sdelphij		root = vm_page_splay(pindex, root);
641275970Scy		if (pindex < root->pindex) {
642275970Scy			m->left = root->left;
643275970Scy			m->right = root;
644275970Scy			root->left = NULL;
645275970Scy			TAILQ_INSERT_BEFORE(root, m, listq);
646275970Scy		} else if (pindex == root->pindex)
647275970Scy			panic("vm_page_insert: offset already allocated");
648275970Scy		else {
649285612Sdelphij			m->right = root->right;
650275970Scy			m->left = root;
651275970Scy			root->right = NULL;
652275970Scy			TAILQ_INSERT_AFTER(&object->memq, root, m, listq);
653275970Scy		}
654275970Scy	}
655275970Scy	object->root = m;
656275970Scy	object->generation++;
657275970Scy
658275970Scy	/*
659275970Scy	 * show that the object has one more resident page.
660275970Scy	 */
661285612Sdelphij	object->resident_page_count++;
662275970Scy	/*
663275970Scy	 * Hold the vnode until the last page is released.
664275970Scy	 */
665275970Scy	if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
666275970Scy		vhold((struct vnode *)object->handle);
667285612Sdelphij
668275970Scy	/*
669275970Scy	 * Since we are inserting a new and possibly dirty page,
670275970Scy	 * update the object's OBJ_MIGHTBEDIRTY flag.
671275970Scy	 */
672275970Scy	if (m->flags & PG_WRITEABLE)
673275970Scy		vm_object_set_writeable_dirty(object);
674275970Scy}
675275970Scy
676275970Scy/*
677275970Scy *	vm_page_remove:
678275970Scy *				NOTE: used by device pager as well -wfj
679275970Scy *
680275970Scy *	Removes the given mem entry from the object/offset-page
681275970Scy *	table and the object page list, but do not invalidate/terminate
682275970Scy *	the backing store.
683275970Scy *
684275970Scy *	The object and page must be locked.
685275970Scy *	The underlying pmap entry (if any) is NOT removed here.
686275970Scy *	This routine may not block.
687275970Scy */
688275970Scyvoid
689275970Scyvm_page_remove(vm_page_t m)
690275970Scy{
691275970Scy	vm_object_t object;
692275970Scy	vm_page_t root;
693275970Scy
694275970Scy	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
695275970Scy	if ((object = m->object) == NULL)
696275970Scy		return;
697275970Scy	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
698275970Scy	if (m->oflags & VPO_BUSY) {
699275970Scy		m->oflags &= ~VPO_BUSY;
700275970Scy		vm_page_flash(m);
701275970Scy	}
702275970Scy
703275970Scy	/*
704275970Scy	 * Now remove from the object's list of backed pages.
705275970Scy	 */
706275970Scy	if (m != object->root)
707275970Scy		vm_page_splay(m->pindex, object->root);
708275970Scy	if (m->left == NULL)
709275970Scy		root = m->right;
710275970Scy	else {
711275970Scy		root = vm_page_splay(m->pindex, m->left);
712275970Scy		root->right = m->right;
713275970Scy	}
714275970Scy	object->root = root;
715275970Scy	TAILQ_REMOVE(&object->memq, m, listq);
716275970Scy
717275970Scy	/*
718275970Scy	 * And show that the object has one fewer resident page.
719275970Scy	 */
720275970Scy	object->resident_page_count--;
721275970Scy	object->generation++;
722275970Scy	/*
723275970Scy	 * The vnode may now be recycled.
724275970Scy	 */
725275970Scy	if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
726275970Scy		vdrop((struct vnode *)object->handle);
727275970Scy
728275970Scy	m->object = NULL;
729275970Scy}
730275970Scy
731275970Scy/*
732275970Scy *	vm_page_lookup:
733275970Scy *
734275970Scy *	Returns the page associated with the object/offset
735275970Scy *	pair specified; if none is found, NULL is returned.
736275970Scy *
737275970Scy *	The object must be locked.
738275970Scy *	This routine may not block.
739275970Scy *	This is a critical path routine
740275970Scy */
741275970Scyvm_page_t
742275970Scyvm_page_lookup(vm_object_t object, vm_pindex_t pindex)
743275970Scy{
744275970Scy	vm_page_t m;
745275970Scy
746275970Scy	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
747275970Scy	if ((m = object->root) != NULL && m->pindex != pindex) {
748275970Scy		m = vm_page_splay(pindex, m);
749275970Scy		if ((object->root = m)->pindex != pindex)
750275970Scy			m = NULL;
751275970Scy	}
752285612Sdelphij	return (m);
753275970Scy}
754285612Sdelphij
755275970Scy/*
756285612Sdelphij *	vm_page_rename:
757275970Scy *
758275970Scy *	Move the given memory entry from its
759275970Scy *	current object to the specified target object/offset.
760275970Scy *
761275970Scy *	The object must be locked.
762275970Scy *	This routine may not block.
763275970Scy *
764275970Scy *	Note: swap associated with the page must be invalidated by the move.  We
765275970Scy *	      have to do this for several reasons:  (1) we aren't freeing the
766275970Scy *	      page, (2) we are dirtying the page, (3) the VM system is probably
767275970Scy *	      moving the page from object A to B, and will then later move
768275970Scy *	      the backing store from A to B and we can't have a conflict.
769275970Scy *
770275970Scy *	Note: we *always* dirty the page.  It is necessary both for the
771275970Scy *	      fact that we moved it, and because we may be invalidating
772275970Scy *	      swap.  If the page is on the cache, we have to deactivate it
773275970Scy *	      or vm_page_dirty() will panic.  Dirty pages are not allowed
774275970Scy *	      on the cache.
775275970Scy */
776275970Scyvoid
777275970Scyvm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
778275970Scy{
779275970Scy
780275970Scy	vm_page_remove(m);
781275970Scy	vm_page_insert(m, new_object, new_pindex);
782275970Scy	if (VM_PAGE_INQUEUE1(m, PQ_CACHE))
783285612Sdelphij		vm_page_deactivate(m);
784275970Scy	vm_page_dirty(m);
785275970Scy}
786275970Scy
787275970Scy/*
788275970Scy *	vm_page_select_cache:
789275970Scy *
790275970Scy *	Move a page of the given color from the cache queue to the free
791275970Scy *	queue.  As pages might be found, but are not applicable, they are
792275970Scy *	deactivated.
793275970Scy *
794275970Scy *	This routine may not block.
795275970Scy */
796275970Scyvm_page_t
797275970Scyvm_page_select_cache(int color)
798275970Scy{
799275970Scy	vm_object_t object;
800275970Scy	vm_page_t m;
801275970Scy	boolean_t was_trylocked;
802275970Scy
803275970Scy	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
804275970Scy	while ((m = vm_pageq_find(PQ_CACHE, color, FALSE)) != NULL) {
805275970Scy		KASSERT(m->dirty == 0, ("Found dirty cache page %p", m));
806275970Scy		KASSERT(!pmap_page_is_mapped(m),
807275970Scy		    ("Found mapped cache page %p", m));
808275970Scy		KASSERT((m->flags & PG_UNMANAGED) == 0,
809275970Scy		    ("Found unmanaged cache page %p", m));
810275970Scy		KASSERT(m->wire_count == 0, ("Found wired cache page %p", m));
811275970Scy		if (m->hold_count == 0 && (object = m->object,
812275970Scy		    (was_trylocked = VM_OBJECT_TRYLOCK(object)) ||
813275970Scy		    VM_OBJECT_LOCKED(object))) {
814275970Scy			KASSERT((m->oflags & VPO_BUSY) == 0 && m->busy == 0,
815275970Scy			    ("Found busy cache page %p", m));
816275970Scy			vm_page_free(m);
817275970Scy			if (was_trylocked)
818275970Scy				VM_OBJECT_UNLOCK(object);
819275970Scy			break;
820275970Scy		}
821275970Scy		vm_page_deactivate(m);
822275970Scy	}
823275970Scy	return (m);
824275970Scy}
825275970Scy
826275970Scy/*
827275970Scy *	vm_page_alloc:
828275970Scy *
829275970Scy *	Allocate and return a memory cell associated
830275970Scy *	with this VM object/offset pair.
831275970Scy *
832275970Scy *	page_req classes:
833275970Scy *	VM_ALLOC_NORMAL		normal process request
834275970Scy *	VM_ALLOC_SYSTEM		system *really* needs a page
835275970Scy *	VM_ALLOC_INTERRUPT	interrupt time request
836275970Scy *	VM_ALLOC_ZERO		zero page
837275970Scy *
838275970Scy *	This routine may not block.
839275970Scy *
840275970Scy *	Additional special handling is required when called from an
841275970Scy *	interrupt (VM_ALLOC_INTERRUPT).  We are not allowed to mess with
842275970Scy *	the page cache in this case.
843275970Scy */
844275970Scyvm_page_t
845275970Scyvm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
846275970Scy{
847275970Scy	vm_page_t m = NULL;
848275970Scy	int color, flags, page_req;
849275970Scy
850275970Scy	page_req = req & VM_ALLOC_CLASS_MASK;
851275970Scy	KASSERT(curthread->td_intr_nesting_level == 0 ||
852275970Scy	    page_req == VM_ALLOC_INTERRUPT,
853275970Scy	    ("vm_page_alloc(NORMAL|SYSTEM) in interrupt context"));
854275970Scy
855275970Scy	if ((req & VM_ALLOC_NOOBJ) == 0) {
856275970Scy		KASSERT(object != NULL,
857275970Scy		    ("vm_page_alloc: NULL object."));
858275970Scy		VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
859275970Scy		color = (pindex + object->pg_color) & PQ_COLORMASK;
860275970Scy	} else
861275970Scy		color = pindex & PQ_COLORMASK;
862275970Scy
863275970Scy	/*
864275970Scy	 * The pager is allowed to eat deeper into the free page list.
865275970Scy	 */
866275970Scy	if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT)) {
867275970Scy		page_req = VM_ALLOC_SYSTEM;
868275970Scy	};
869275970Scy
870275970Scyloop:
871275970Scy	mtx_lock(&vm_page_queue_free_mtx);
872275970Scy	if (cnt.v_free_count > cnt.v_free_reserved ||
873275970Scy	    (page_req == VM_ALLOC_SYSTEM &&
874275970Scy	     cnt.v_cache_count == 0 &&
875275970Scy	     cnt.v_free_count > cnt.v_interrupt_free_min) ||
876275970Scy	    (page_req == VM_ALLOC_INTERRUPT && cnt.v_free_count > 0)) {
877275970Scy		/*
878275970Scy		 * Allocate from the free queue if the number of free pages
879275970Scy		 * exceeds the minimum for the request class.
880275970Scy		 */
881275970Scy		m = vm_pageq_find(PQ_FREE, color, (req & VM_ALLOC_ZERO) != 0);
882275970Scy	} else if (page_req != VM_ALLOC_INTERRUPT) {
883275970Scy		mtx_unlock(&vm_page_queue_free_mtx);
884275970Scy		/*
885275970Scy		 * Allocatable from cache (non-interrupt only).  On success,
886275970Scy		 * we must free the page and try again, thus ensuring that
887275970Scy		 * cnt.v_*_free_min counters are replenished.
888275970Scy		 */
889275970Scy		vm_page_lock_queues();
890275970Scy		if ((m = vm_page_select_cache(color)) == NULL) {
891275970Scy			KASSERT(cnt.v_cache_count == 0,
892275970Scy			    ("vm_page_alloc: cache queue is missing %d pages",
893275970Scy			    cnt.v_cache_count));
894285612Sdelphij			vm_page_unlock_queues();
895275970Scy			atomic_add_int(&vm_pageout_deficit, 1);
896275970Scy			pagedaemon_wakeup();
897275970Scy
898275970Scy			if (page_req != VM_ALLOC_SYSTEM)
899275970Scy				return (NULL);
900275970Scy
901275970Scy			mtx_lock(&vm_page_queue_free_mtx);
902275970Scy			if (cnt.v_free_count <= cnt.v_interrupt_free_min) {
903275970Scy				mtx_unlock(&vm_page_queue_free_mtx);
904275970Scy				return (NULL);
905275970Scy			}
906275970Scy			m = vm_pageq_find(PQ_FREE, color, (req & VM_ALLOC_ZERO) != 0);
907275970Scy		} else {
908275970Scy			vm_page_unlock_queues();
909275970Scy			goto loop;
910275970Scy		}
911275970Scy	} else {
912275970Scy		/*
913275970Scy		 * Not allocatable from cache from interrupt, give up.
914275970Scy		 */
915275970Scy		mtx_unlock(&vm_page_queue_free_mtx);
916275970Scy		atomic_add_int(&vm_pageout_deficit, 1);
917275970Scy		pagedaemon_wakeup();
918275970Scy		return (NULL);
919275970Scy	}
920275970Scy
921275970Scy	/*
922275970Scy	 *  At this point we had better have found a good page.
923275970Scy	 */
924275970Scy
925275970Scy	KASSERT(
926285612Sdelphij	    m != NULL,
927285612Sdelphij	    ("vm_page_alloc(): missing page on free queue")
928285612Sdelphij	);
929285612Sdelphij
930285612Sdelphij	/*
931275970Scy	 * Remove from free queue
932275970Scy	 */
933275970Scy	vm_pageq_remove_nowakeup(m);
934275970Scy
935275970Scy	/*
936275970Scy	 * Initialize structure.  Only the PG_ZERO flag is inherited.
937275970Scy	 */
938275970Scy	flags = 0;
939275970Scy	if (m->flags & PG_ZERO) {
940275970Scy		vm_page_zero_count--;
941275970Scy		if (req & VM_ALLOC_ZERO)
942275970Scy			flags = PG_ZERO;
943275970Scy	}
944275970Scy	m->flags = flags;
945275970Scy	if (req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ))
946275970Scy		m->oflags = 0;
947275970Scy	else
948275970Scy		m->oflags = VPO_BUSY;
949275970Scy	if (req & VM_ALLOC_WIRED) {
950275970Scy		atomic_add_int(&cnt.v_wire_count, 1);
951275970Scy		m->wire_count = 1;
952275970Scy	} else
953275970Scy		m->wire_count = 0;
954275970Scy	m->hold_count = 0;
955275970Scy	m->act_count = 0;
956275970Scy	m->busy = 0;
957275970Scy	m->valid = 0;
958275970Scy	KASSERT(m->dirty == 0, ("vm_page_alloc: free/cache page %p was dirty", m));
959275970Scy	mtx_unlock(&vm_page_queue_free_mtx);
960275970Scy
961275970Scy	if ((req & VM_ALLOC_NOOBJ) == 0)
962275970Scy		vm_page_insert(m, object, pindex);
963275970Scy	else
964275970Scy		m->pindex = pindex;
965275970Scy
966275970Scy	/*
967275970Scy	 * Don't wakeup too often - wakeup the pageout daemon when
968275970Scy	 * we would be nearly out of memory.
969275970Scy	 */
970275970Scy	if (vm_paging_needed())
971275970Scy		pagedaemon_wakeup();
972275970Scy
973275970Scy	return (m);
974275970Scy}
975275970Scy
976280849Scy/*
977275970Scy *	vm_wait:	(also see VM_WAIT macro)
978280849Scy *
979275970Scy *	Block until free pages are available for allocation
980275970Scy *	- Called in various places before memory allocations.
981275970Scy */
982275970Scyvoid
983275970Scyvm_wait(void)
984275970Scy{
985275970Scy
986275970Scy	mtx_lock(&vm_page_queue_free_mtx);
987275970Scy	if (curproc == pageproc) {
988275970Scy		vm_pageout_pages_needed = 1;
989275970Scy		msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx,
990275970Scy		    PDROP | PSWP, "VMWait", 0);
991275970Scy	} else {
992275970Scy		if (!vm_pages_needed) {
993275970Scy			vm_pages_needed = 1;
994275970Scy			wakeup(&vm_pages_needed);
995275970Scy		}
996275970Scy		msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM,
997275970Scy		    "vmwait", 0);
998275970Scy	}
999275970Scy}
1000275970Scy
1001275970Scy/*
1002 *	vm_waitpfault:	(also see VM_WAITPFAULT macro)
1003 *
1004 *	Block until free pages are available for allocation
1005 *	- Called only in vm_fault so that processes page faulting
1006 *	  can be easily tracked.
1007 *	- Sleeps at a lower priority than vm_wait() so that vm_wait()ing
1008 *	  processes will be able to grab memory first.  Do not change
1009 *	  this balance without careful testing first.
1010 */
1011void
1012vm_waitpfault(void)
1013{
1014
1015	mtx_lock(&vm_page_queue_free_mtx);
1016	if (!vm_pages_needed) {
1017		vm_pages_needed = 1;
1018		wakeup(&vm_pages_needed);
1019	}
1020	msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER,
1021	    "pfault", 0);
1022}
1023
1024/*
1025 *	vm_page_activate:
1026 *
1027 *	Put the specified page on the active list (if appropriate).
1028 *	Ensure that act_count is at least ACT_INIT but do not otherwise
1029 *	mess with it.
1030 *
1031 *	The page queues must be locked.
1032 *	This routine may not block.
1033 */
1034void
1035vm_page_activate(vm_page_t m)
1036{
1037
1038	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1039	if (VM_PAGE_GETKNOWNQUEUE2(m) != PQ_ACTIVE) {
1040		if (VM_PAGE_INQUEUE1(m, PQ_CACHE))
1041			cnt.v_reactivated++;
1042		vm_pageq_remove(m);
1043		if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
1044			if (m->act_count < ACT_INIT)
1045				m->act_count = ACT_INIT;
1046			vm_pageq_enqueue(PQ_ACTIVE, m);
1047		}
1048	} else {
1049		if (m->act_count < ACT_INIT)
1050			m->act_count = ACT_INIT;
1051	}
1052}
1053
1054/*
1055 *	vm_page_free_wakeup:
1056 *
1057 *	Helper routine for vm_page_free_toq() and vm_page_cache().  This
1058 *	routine is called when a page has been added to the cache or free
1059 *	queues.
1060 *
1061 *	The page queues must be locked.
1062 *	This routine may not block.
1063 */
1064static inline void
1065vm_page_free_wakeup(void)
1066{
1067
1068	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1069	/*
1070	 * if pageout daemon needs pages, then tell it that there are
1071	 * some free.
1072	 */
1073	if (vm_pageout_pages_needed &&
1074	    cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) {
1075		wakeup(&vm_pageout_pages_needed);
1076		vm_pageout_pages_needed = 0;
1077	}
1078	/*
1079	 * wakeup processes that are waiting on memory if we hit a
1080	 * high water mark. And wakeup scheduler process if we have
1081	 * lots of memory. this process will swapin processes.
1082	 */
1083	if (vm_pages_needed && !vm_page_count_min()) {
1084		vm_pages_needed = 0;
1085		wakeup(&cnt.v_free_count);
1086	}
1087}
1088
1089/*
1090 *	vm_page_free_toq:
1091 *
1092 *	Returns the given page to the PQ_FREE list,
1093 *	disassociating it with any VM object.
1094 *
1095 *	Object and page must be locked prior to entry.
1096 *	This routine may not block.
1097 */
1098
1099void
1100vm_page_free_toq(vm_page_t m)
1101{
1102	struct vpgqueues *pq;
1103
1104	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1105	KASSERT(!pmap_page_is_mapped(m),
1106	    ("vm_page_free_toq: freeing mapped page %p", m));
1107	cnt.v_tfree++;
1108
1109	if (m->busy || VM_PAGE_INQUEUE1(m, PQ_FREE)) {
1110		printf(
1111		"vm_page_free: pindex(%lu), busy(%d), VPO_BUSY(%d), hold(%d)\n",
1112		    (u_long)m->pindex, m->busy, (m->oflags & VPO_BUSY) ? 1 : 0,
1113		    m->hold_count);
1114		if (VM_PAGE_INQUEUE1(m, PQ_FREE))
1115			panic("vm_page_free: freeing free page");
1116		else
1117			panic("vm_page_free: freeing busy page");
1118	}
1119
1120	/*
1121	 * unqueue, then remove page.  Note that we cannot destroy
1122	 * the page here because we do not want to call the pager's
1123	 * callback routine until after we've put the page on the
1124	 * appropriate free queue.
1125	 */
1126	vm_pageq_remove_nowakeup(m);
1127	vm_page_remove(m);
1128
1129	/*
1130	 * If fictitious remove object association and
1131	 * return, otherwise delay object association removal.
1132	 */
1133	if ((m->flags & PG_FICTITIOUS) != 0) {
1134		return;
1135	}
1136
1137	m->valid = 0;
1138	vm_page_undirty(m);
1139
1140	if (m->wire_count != 0) {
1141		if (m->wire_count > 1) {
1142			panic("vm_page_free: invalid wire count (%d), pindex: 0x%lx",
1143				m->wire_count, (long)m->pindex);
1144		}
1145		panic("vm_page_free: freeing wired page");
1146	}
1147	if (m->hold_count != 0) {
1148		m->flags &= ~PG_ZERO;
1149		VM_PAGE_SETQUEUE2(m, PQ_HOLD);
1150	} else
1151		VM_PAGE_SETQUEUE1(m, PQ_FREE);
1152	pq = &vm_page_queues[VM_PAGE_GETQUEUE(m)];
1153	mtx_lock(&vm_page_queue_free_mtx);
1154	pq->lcnt++;
1155	++(*pq->cnt);
1156
1157	/*
1158	 * Put zero'd pages on the end ( where we look for zero'd pages
1159	 * first ) and non-zerod pages at the head.
1160	 */
1161	if (m->flags & PG_ZERO) {
1162		TAILQ_INSERT_TAIL(&pq->pl, m, pageq);
1163		++vm_page_zero_count;
1164	} else {
1165		TAILQ_INSERT_HEAD(&pq->pl, m, pageq);
1166		vm_page_zero_idle_wakeup();
1167	}
1168	vm_page_free_wakeup();
1169	mtx_unlock(&vm_page_queue_free_mtx);
1170}
1171
1172/*
1173 *	vm_page_unmanage:
1174 *
1175 * 	Prevent PV management from being done on the page.  The page is
1176 *	removed from the paging queues as if it were wired, and as a
1177 *	consequence of no longer being managed the pageout daemon will not
1178 *	touch it (since there is no way to locate the pte mappings for the
1179 *	page).  madvise() calls that mess with the pmap will also no longer
1180 *	operate on the page.
1181 *
1182 *	Beyond that the page is still reasonably 'normal'.  Freeing the page
1183 *	will clear the flag.
1184 *
1185 *	This routine is used by OBJT_PHYS objects - objects using unswappable
1186 *	physical memory as backing store rather then swap-backed memory and
1187 *	will eventually be extended to support 4MB unmanaged physical
1188 *	mappings.
1189 */
1190void
1191vm_page_unmanage(vm_page_t m)
1192{
1193
1194	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1195	if ((m->flags & PG_UNMANAGED) == 0) {
1196		if (m->wire_count == 0)
1197			vm_pageq_remove(m);
1198	}
1199	vm_page_flag_set(m, PG_UNMANAGED);
1200}
1201
1202/*
1203 *	vm_page_wire:
1204 *
1205 *	Mark this page as wired down by yet
1206 *	another map, removing it from paging queues
1207 *	as necessary.
1208 *
1209 *	The page queues must be locked.
1210 *	This routine may not block.
1211 */
1212void
1213vm_page_wire(vm_page_t m)
1214{
1215
1216	/*
1217	 * Only bump the wire statistics if the page is not already wired,
1218	 * and only unqueue the page if it is on some queue (if it is unmanaged
1219	 * it is already off the queues).
1220	 */
1221	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1222	if (m->flags & PG_FICTITIOUS)
1223		return;
1224	if (m->wire_count == 0) {
1225		if ((m->flags & PG_UNMANAGED) == 0)
1226			vm_pageq_remove(m);
1227		atomic_add_int(&cnt.v_wire_count, 1);
1228	}
1229	m->wire_count++;
1230	KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
1231}
1232
1233/*
1234 *	vm_page_unwire:
1235 *
1236 *	Release one wiring of this page, potentially
1237 *	enabling it to be paged again.
1238 *
1239 *	Many pages placed on the inactive queue should actually go
1240 *	into the cache, but it is difficult to figure out which.  What
1241 *	we do instead, if the inactive target is well met, is to put
1242 *	clean pages at the head of the inactive queue instead of the tail.
1243 *	This will cause them to be moved to the cache more quickly and
1244 *	if not actively re-referenced, freed more quickly.  If we just
1245 *	stick these pages at the end of the inactive queue, heavy filesystem
1246 *	meta-data accesses can cause an unnecessary paging load on memory bound
1247 *	processes.  This optimization causes one-time-use metadata to be
1248 *	reused more quickly.
1249 *
1250 *	BUT, if we are in a low-memory situation we have no choice but to
1251 *	put clean pages on the cache queue.
1252 *
1253 *	A number of routines use vm_page_unwire() to guarantee that the page
1254 *	will go into either the inactive or active queues, and will NEVER
1255 *	be placed in the cache - for example, just after dirtying a page.
1256 *	dirty pages in the cache are not allowed.
1257 *
1258 *	The page queues must be locked.
1259 *	This routine may not block.
1260 */
1261void
1262vm_page_unwire(vm_page_t m, int activate)
1263{
1264
1265	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1266	if (m->flags & PG_FICTITIOUS)
1267		return;
1268	if (m->wire_count > 0) {
1269		m->wire_count--;
1270		if (m->wire_count == 0) {
1271			atomic_subtract_int(&cnt.v_wire_count, 1);
1272			if (m->flags & PG_UNMANAGED) {
1273				;
1274			} else if (activate)
1275				vm_pageq_enqueue(PQ_ACTIVE, m);
1276			else {
1277				vm_page_flag_clear(m, PG_WINATCFLS);
1278				vm_pageq_enqueue(PQ_INACTIVE, m);
1279			}
1280		}
1281	} else {
1282		panic("vm_page_unwire: invalid wire count: %d", m->wire_count);
1283	}
1284}
1285
1286
1287/*
1288 * Move the specified page to the inactive queue.  If the page has
1289 * any associated swap, the swap is deallocated.
1290 *
1291 * Normally athead is 0 resulting in LRU operation.  athead is set
1292 * to 1 if we want this page to be 'as if it were placed in the cache',
1293 * except without unmapping it from the process address space.
1294 *
1295 * This routine may not block.
1296 */
1297static inline void
1298_vm_page_deactivate(vm_page_t m, int athead)
1299{
1300
1301	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1302
1303	/*
1304	 * Ignore if already inactive.
1305	 */
1306	if (VM_PAGE_INQUEUE2(m, PQ_INACTIVE))
1307		return;
1308	if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
1309		if (VM_PAGE_INQUEUE1(m, PQ_CACHE))
1310			cnt.v_reactivated++;
1311		vm_page_flag_clear(m, PG_WINATCFLS);
1312		vm_pageq_remove(m);
1313		if (athead)
1314			TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1315		else
1316			TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1317		VM_PAGE_SETQUEUE2(m, PQ_INACTIVE);
1318		vm_page_queues[PQ_INACTIVE].lcnt++;
1319		cnt.v_inactive_count++;
1320	}
1321}
1322
1323void
1324vm_page_deactivate(vm_page_t m)
1325{
1326    _vm_page_deactivate(m, 0);
1327}
1328
1329/*
1330 * vm_page_try_to_cache:
1331 *
1332 * Returns 0 on failure, 1 on success
1333 */
1334int
1335vm_page_try_to_cache(vm_page_t m)
1336{
1337
1338	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1339	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1340	if (m->dirty || m->hold_count || m->busy || m->wire_count ||
1341	    (m->oflags & VPO_BUSY) || (m->flags & PG_UNMANAGED)) {
1342		return (0);
1343	}
1344	pmap_remove_all(m);
1345	if (m->dirty)
1346		return (0);
1347	vm_page_cache(m);
1348	return (1);
1349}
1350
1351/*
1352 * vm_page_try_to_free()
1353 *
1354 *	Attempt to free the page.  If we cannot free it, we do nothing.
1355 *	1 is returned on success, 0 on failure.
1356 */
1357int
1358vm_page_try_to_free(vm_page_t m)
1359{
1360
1361	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1362	if (m->object != NULL)
1363		VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1364	if (m->dirty || m->hold_count || m->busy || m->wire_count ||
1365	    (m->oflags & VPO_BUSY) || (m->flags & PG_UNMANAGED)) {
1366		return (0);
1367	}
1368	pmap_remove_all(m);
1369	if (m->dirty)
1370		return (0);
1371	vm_page_free(m);
1372	return (1);
1373}
1374
1375/*
1376 * vm_page_cache
1377 *
1378 * Put the specified page onto the page cache queue (if appropriate).
1379 *
1380 * This routine may not block.
1381 */
1382void
1383vm_page_cache(vm_page_t m)
1384{
1385
1386	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1387	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1388	if ((m->flags & PG_UNMANAGED) || (m->oflags & VPO_BUSY) || m->busy ||
1389	    m->hold_count || m->wire_count) {
1390		printf("vm_page_cache: attempting to cache busy page\n");
1391		return;
1392	}
1393	if (VM_PAGE_INQUEUE1(m, PQ_CACHE))
1394		return;
1395
1396	/*
1397	 * Remove all pmaps and indicate that the page is not
1398	 * writeable or mapped.
1399	 */
1400	pmap_remove_all(m);
1401	if (m->dirty != 0) {
1402		panic("vm_page_cache: caching a dirty page, pindex: %ld",
1403			(long)m->pindex);
1404	}
1405	vm_pageq_remove_nowakeup(m);
1406	vm_pageq_enqueue(PQ_CACHE + m->pc, m);
1407	mtx_lock(&vm_page_queue_free_mtx);
1408	vm_page_free_wakeup();
1409	mtx_unlock(&vm_page_queue_free_mtx);
1410}
1411
1412/*
1413 * vm_page_dontneed
1414 *
1415 *	Cache, deactivate, or do nothing as appropriate.  This routine
1416 *	is typically used by madvise() MADV_DONTNEED.
1417 *
1418 *	Generally speaking we want to move the page into the cache so
1419 *	it gets reused quickly.  However, this can result in a silly syndrome
1420 *	due to the page recycling too quickly.  Small objects will not be
1421 *	fully cached.  On the otherhand, if we move the page to the inactive
1422 *	queue we wind up with a problem whereby very large objects
1423 *	unnecessarily blow away our inactive and cache queues.
1424 *
1425 *	The solution is to move the pages based on a fixed weighting.  We
1426 *	either leave them alone, deactivate them, or move them to the cache,
1427 *	where moving them to the cache has the highest weighting.
1428 *	By forcing some pages into other queues we eventually force the
1429 *	system to balance the queues, potentially recovering other unrelated
1430 *	space from active.  The idea is to not force this to happen too
1431 *	often.
1432 */
1433void
1434vm_page_dontneed(vm_page_t m)
1435{
1436	static int dnweight;
1437	int dnw;
1438	int head;
1439
1440	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1441	dnw = ++dnweight;
1442
1443	/*
1444	 * occassionally leave the page alone
1445	 */
1446	if ((dnw & 0x01F0) == 0 ||
1447	    VM_PAGE_INQUEUE2(m, PQ_INACTIVE) ||
1448	    VM_PAGE_INQUEUE1(m, PQ_CACHE)
1449	) {
1450		if (m->act_count >= ACT_INIT)
1451			--m->act_count;
1452		return;
1453	}
1454
1455	if (m->dirty == 0 && pmap_is_modified(m))
1456		vm_page_dirty(m);
1457
1458	if (m->dirty || (dnw & 0x0070) == 0) {
1459		/*
1460		 * Deactivate the page 3 times out of 32.
1461		 */
1462		head = 0;
1463	} else {
1464		/*
1465		 * Cache the page 28 times out of every 32.  Note that
1466		 * the page is deactivated instead of cached, but placed
1467		 * at the head of the queue instead of the tail.
1468		 */
1469		head = 1;
1470	}
1471	_vm_page_deactivate(m, head);
1472}
1473
1474/*
1475 * Grab a page, waiting until we are waken up due to the page
1476 * changing state.  We keep on waiting, if the page continues
1477 * to be in the object.  If the page doesn't exist, first allocate it
1478 * and then conditionally zero it.
1479 *
1480 * This routine may block.
1481 */
1482vm_page_t
1483vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
1484{
1485	vm_page_t m;
1486
1487	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1488retrylookup:
1489	if ((m = vm_page_lookup(object, pindex)) != NULL) {
1490		if (vm_page_sleep_if_busy(m, TRUE, "pgrbwt")) {
1491			if ((allocflags & VM_ALLOC_RETRY) == 0)
1492				return (NULL);
1493			goto retrylookup;
1494		} else {
1495			if ((allocflags & VM_ALLOC_WIRED) != 0) {
1496				vm_page_lock_queues();
1497				vm_page_wire(m);
1498				vm_page_unlock_queues();
1499			}
1500			if ((allocflags & VM_ALLOC_NOBUSY) == 0)
1501				vm_page_busy(m);
1502			return (m);
1503		}
1504	}
1505	m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_RETRY);
1506	if (m == NULL) {
1507		VM_OBJECT_UNLOCK(object);
1508		VM_WAIT;
1509		VM_OBJECT_LOCK(object);
1510		if ((allocflags & VM_ALLOC_RETRY) == 0)
1511			return (NULL);
1512		goto retrylookup;
1513	}
1514	if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
1515		pmap_zero_page(m);
1516	return (m);
1517}
1518
1519/*
1520 * Mapping function for valid bits or for dirty bits in
1521 * a page.  May not block.
1522 *
1523 * Inputs are required to range within a page.
1524 */
1525inline int
1526vm_page_bits(int base, int size)
1527{
1528	int first_bit;
1529	int last_bit;
1530
1531	KASSERT(
1532	    base + size <= PAGE_SIZE,
1533	    ("vm_page_bits: illegal base/size %d/%d", base, size)
1534	);
1535
1536	if (size == 0)		/* handle degenerate case */
1537		return (0);
1538
1539	first_bit = base >> DEV_BSHIFT;
1540	last_bit = (base + size - 1) >> DEV_BSHIFT;
1541
1542	return ((2 << last_bit) - (1 << first_bit));
1543}
1544
1545/*
1546 *	vm_page_set_validclean:
1547 *
1548 *	Sets portions of a page valid and clean.  The arguments are expected
1549 *	to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
1550 *	of any partial chunks touched by the range.  The invalid portion of
1551 *	such chunks will be zero'd.
1552 *
1553 *	This routine may not block.
1554 *
1555 *	(base + size) must be less then or equal to PAGE_SIZE.
1556 */
1557void
1558vm_page_set_validclean(vm_page_t m, int base, int size)
1559{
1560	int pagebits;
1561	int frag;
1562	int endoff;
1563
1564	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1565	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1566	if (size == 0)	/* handle degenerate case */
1567		return;
1568
1569	/*
1570	 * If the base is not DEV_BSIZE aligned and the valid
1571	 * bit is clear, we have to zero out a portion of the
1572	 * first block.
1573	 */
1574	if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
1575	    (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
1576		pmap_zero_page_area(m, frag, base - frag);
1577
1578	/*
1579	 * If the ending offset is not DEV_BSIZE aligned and the
1580	 * valid bit is clear, we have to zero out a portion of
1581	 * the last block.
1582	 */
1583	endoff = base + size;
1584	if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
1585	    (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
1586		pmap_zero_page_area(m, endoff,
1587		    DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
1588
1589	/*
1590	 * Set valid, clear dirty bits.  If validating the entire
1591	 * page we can safely clear the pmap modify bit.  We also
1592	 * use this opportunity to clear the VPO_NOSYNC flag.  If a process
1593	 * takes a write fault on a MAP_NOSYNC memory area the flag will
1594	 * be set again.
1595	 *
1596	 * We set valid bits inclusive of any overlap, but we can only
1597	 * clear dirty bits for DEV_BSIZE chunks that are fully within
1598	 * the range.
1599	 */
1600	pagebits = vm_page_bits(base, size);
1601	m->valid |= pagebits;
1602#if 0	/* NOT YET */
1603	if ((frag = base & (DEV_BSIZE - 1)) != 0) {
1604		frag = DEV_BSIZE - frag;
1605		base += frag;
1606		size -= frag;
1607		if (size < 0)
1608			size = 0;
1609	}
1610	pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
1611#endif
1612	m->dirty &= ~pagebits;
1613	if (base == 0 && size == PAGE_SIZE) {
1614		pmap_clear_modify(m);
1615		m->oflags &= ~VPO_NOSYNC;
1616	}
1617}
1618
1619void
1620vm_page_clear_dirty(vm_page_t m, int base, int size)
1621{
1622
1623	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1624	m->dirty &= ~vm_page_bits(base, size);
1625}
1626
1627/*
1628 *	vm_page_set_invalid:
1629 *
1630 *	Invalidates DEV_BSIZE'd chunks within a page.  Both the
1631 *	valid and dirty bits for the effected areas are cleared.
1632 *
1633 *	May not block.
1634 */
1635void
1636vm_page_set_invalid(vm_page_t m, int base, int size)
1637{
1638	int bits;
1639
1640	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1641	bits = vm_page_bits(base, size);
1642	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1643	if (m->valid == VM_PAGE_BITS_ALL && bits != 0)
1644		pmap_remove_all(m);
1645	m->valid &= ~bits;
1646	m->dirty &= ~bits;
1647	m->object->generation++;
1648}
1649
1650/*
1651 * vm_page_zero_invalid()
1652 *
1653 *	The kernel assumes that the invalid portions of a page contain
1654 *	garbage, but such pages can be mapped into memory by user code.
1655 *	When this occurs, we must zero out the non-valid portions of the
1656 *	page so user code sees what it expects.
1657 *
1658 *	Pages are most often semi-valid when the end of a file is mapped
1659 *	into memory and the file's size is not page aligned.
1660 */
1661void
1662vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
1663{
1664	int b;
1665	int i;
1666
1667	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1668	/*
1669	 * Scan the valid bits looking for invalid sections that
1670	 * must be zerod.  Invalid sub-DEV_BSIZE'd areas ( where the
1671	 * valid bit may be set ) have already been zerod by
1672	 * vm_page_set_validclean().
1673	 */
1674	for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
1675		if (i == (PAGE_SIZE / DEV_BSIZE) ||
1676		    (m->valid & (1 << i))
1677		) {
1678			if (i > b) {
1679				pmap_zero_page_area(m,
1680				    b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
1681			}
1682			b = i + 1;
1683		}
1684	}
1685
1686	/*
1687	 * setvalid is TRUE when we can safely set the zero'd areas
1688	 * as being valid.  We can do this if there are no cache consistancy
1689	 * issues.  e.g. it is ok to do with UFS, but not ok to do with NFS.
1690	 */
1691	if (setvalid)
1692		m->valid = VM_PAGE_BITS_ALL;
1693}
1694
1695/*
1696 *	vm_page_is_valid:
1697 *
1698 *	Is (partial) page valid?  Note that the case where size == 0
1699 *	will return FALSE in the degenerate case where the page is
1700 *	entirely invalid, and TRUE otherwise.
1701 *
1702 *	May not block.
1703 */
1704int
1705vm_page_is_valid(vm_page_t m, int base, int size)
1706{
1707	int bits = vm_page_bits(base, size);
1708
1709	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1710	if (m->valid && ((m->valid & bits) == bits))
1711		return 1;
1712	else
1713		return 0;
1714}
1715
1716/*
1717 * update dirty bits from pmap/mmu.  May not block.
1718 */
1719void
1720vm_page_test_dirty(vm_page_t m)
1721{
1722	if ((m->dirty != VM_PAGE_BITS_ALL) && pmap_is_modified(m)) {
1723		vm_page_dirty(m);
1724	}
1725}
1726
1727int so_zerocp_fullpage = 0;
1728
1729void
1730vm_page_cowfault(vm_page_t m)
1731{
1732	vm_page_t mnew;
1733	vm_object_t object;
1734	vm_pindex_t pindex;
1735
1736	object = m->object;
1737	pindex = m->pindex;
1738
1739 retry_alloc:
1740	pmap_remove_all(m);
1741	vm_page_remove(m);
1742	mnew = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY);
1743	if (mnew == NULL) {
1744		vm_page_insert(m, object, pindex);
1745		vm_page_unlock_queues();
1746		VM_OBJECT_UNLOCK(object);
1747		VM_WAIT;
1748		VM_OBJECT_LOCK(object);
1749		vm_page_lock_queues();
1750		goto retry_alloc;
1751	}
1752
1753	if (m->cow == 0) {
1754		/*
1755		 * check to see if we raced with an xmit complete when
1756		 * waiting to allocate a page.  If so, put things back
1757		 * the way they were
1758		 */
1759		vm_page_free(mnew);
1760		vm_page_insert(m, object, pindex);
1761	} else { /* clear COW & copy page */
1762		if (!so_zerocp_fullpage)
1763			pmap_copy_page(m, mnew);
1764		mnew->valid = VM_PAGE_BITS_ALL;
1765		vm_page_dirty(mnew);
1766		mnew->wire_count = m->wire_count - m->cow;
1767		m->wire_count = m->cow;
1768	}
1769}
1770
1771void
1772vm_page_cowclear(vm_page_t m)
1773{
1774
1775	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1776	if (m->cow) {
1777		m->cow--;
1778		/*
1779		 * let vm_fault add back write permission  lazily
1780		 */
1781	}
1782	/*
1783	 *  sf_buf_free() will free the page, so we needn't do it here
1784	 */
1785}
1786
1787void
1788vm_page_cowsetup(vm_page_t m)
1789{
1790
1791	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1792	m->cow++;
1793	pmap_remove_write(m);
1794}
1795
1796#include "opt_ddb.h"
1797#ifdef DDB
1798#include <sys/kernel.h>
1799
1800#include <ddb/ddb.h>
1801
1802DB_SHOW_COMMAND(page, vm_page_print_page_info)
1803{
1804	db_printf("cnt.v_free_count: %d\n", cnt.v_free_count);
1805	db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count);
1806	db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count);
1807	db_printf("cnt.v_active_count: %d\n", cnt.v_active_count);
1808	db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count);
1809	db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved);
1810	db_printf("cnt.v_free_min: %d\n", cnt.v_free_min);
1811	db_printf("cnt.v_free_target: %d\n", cnt.v_free_target);
1812	db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min);
1813	db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target);
1814}
1815
1816DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
1817{
1818	int i;
1819	db_printf("PQ_FREE:");
1820	for (i = 0; i < PQ_NUMCOLORS; i++) {
1821		db_printf(" %d", vm_page_queues[PQ_FREE + i].lcnt);
1822	}
1823	db_printf("\n");
1824
1825	db_printf("PQ_CACHE:");
1826	for (i = 0; i < PQ_NUMCOLORS; i++) {
1827		db_printf(" %d", vm_page_queues[PQ_CACHE + i].lcnt);
1828	}
1829	db_printf("\n");
1830
1831	db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n",
1832		vm_page_queues[PQ_ACTIVE].lcnt,
1833		vm_page_queues[PQ_INACTIVE].lcnt);
1834}
1835#endif /* DDB */
1836