vm_page.c revision 208764
1/*-
2 * Copyright (c) 1991 Regents of the University of California.
3 * All rights reserved.
4 * Copyright (c) 1998 Matthew Dillon.  All Rights Reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * The Mach Operating System project at Carnegie-Mellon University.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	from: @(#)vm_page.c	7.4 (Berkeley) 5/7/91
34 */
35
36/*-
37 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38 * All rights reserved.
39 *
40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
41 *
42 * Permission to use, copy, modify and distribute this software and
43 * its documentation is hereby granted, provided that both the copyright
44 * notice and this permission notice appear in all copies of the
45 * software, derivative works or modified versions, and any portions
46 * thereof, and that both notices appear in supporting documentation.
47 *
48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51 *
52 * Carnegie Mellon requests users of this software to return to
53 *
54 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55 *  School of Computer Science
56 *  Carnegie Mellon University
57 *  Pittsburgh PA 15213-3890
58 *
59 * any improvements or extensions that they make and grant Carnegie the
60 * rights to redistribute these changes.
61 */
62
63/*
64 *			GENERAL RULES ON VM_PAGE MANIPULATION
65 *
66 *	- a pageq mutex is required when adding or removing a page from a
67 *	  page queue (vm_page_queue[]), regardless of other mutexes or the
68 *	  busy state of a page.
69 *
70 *	- a hash chain mutex is required when associating or disassociating
71 *	  a page from the VM PAGE CACHE hash table (vm_page_buckets),
72 *	  regardless of other mutexes or the busy state of a page.
73 *
74 *	- either a hash chain mutex OR a busied page is required in order
75 *	  to modify the page flags.  A hash chain mutex must be obtained in
76 *	  order to busy a page.  A page's flags cannot be modified by a
77 *	  hash chain mutex if the page is marked busy.
78 *
79 *	- The object memq mutex is held when inserting or removing
80 *	  pages from an object (vm_page_insert() or vm_page_remove()).  This
81 *	  is different from the object's main mutex.
82 *
83 *	Generally speaking, you have to be aware of side effects when running
84 *	vm_page ops.  A vm_page_lookup() will return with the hash chain
85 *	locked, whether it was able to lookup the page or not.  vm_page_free(),
86 *	vm_page_cache(), vm_page_activate(), and a number of other routines
87 *	will release the hash chain mutex for you.  Intermediate manipulation
88 *	routines such as vm_page_flag_set() expect the hash chain to be held
89 *	on entry and the hash chain will remain held on return.
90 *
91 *	pageq scanning can only occur with the pageq in question locked.
92 *	We have a known bottleneck with the active queue, but the cache
93 *	and free queues are actually arrays already.
94 */
95
96/*
97 *	Resident memory management module.
98 */
99
100#include <sys/cdefs.h>
101__FBSDID("$FreeBSD: head/sys/vm/vm_page.c 208764 2010-06-03 02:24:53Z alc $");
102
103#include "opt_vm.h"
104
105#include <sys/param.h>
106#include <sys/systm.h>
107#include <sys/lock.h>
108#include <sys/kernel.h>
109#include <sys/limits.h>
110#include <sys/malloc.h>
111#include <sys/msgbuf.h>
112#include <sys/mutex.h>
113#include <sys/proc.h>
114#include <sys/sysctl.h>
115#include <sys/vmmeter.h>
116#include <sys/vnode.h>
117
118#include <vm/vm.h>
119#include <vm/pmap.h>
120#include <vm/vm_param.h>
121#include <vm/vm_kern.h>
122#include <vm/vm_object.h>
123#include <vm/vm_page.h>
124#include <vm/vm_pageout.h>
125#include <vm/vm_pager.h>
126#include <vm/vm_phys.h>
127#include <vm/vm_reserv.h>
128#include <vm/vm_extern.h>
129#include <vm/uma.h>
130#include <vm/uma_int.h>
131
132#include <machine/md_var.h>
133
134#if defined(__amd64__) || defined (__i386__)
135extern struct sysctl_oid_list sysctl__vm_pmap_children;
136#else
137SYSCTL_NODE(_vm, OID_AUTO, pmap, CTLFLAG_RD, 0, "VM/pmap parameters");
138#endif
139
140static uint64_t pmap_tryrelock_calls;
141SYSCTL_QUAD(_vm_pmap, OID_AUTO, tryrelock_calls, CTLFLAG_RD,
142    &pmap_tryrelock_calls, 0, "Number of tryrelock calls");
143
144static int pmap_tryrelock_restart;
145SYSCTL_INT(_vm_pmap, OID_AUTO, tryrelock_restart, CTLFLAG_RD,
146    &pmap_tryrelock_restart, 0, "Number of tryrelock restarts");
147
148static int pmap_tryrelock_race;
149SYSCTL_INT(_vm_pmap, OID_AUTO, tryrelock_race, CTLFLAG_RD,
150    &pmap_tryrelock_race, 0, "Number of tryrelock pmap race cases");
151
152/*
153 *	Associated with page of user-allocatable memory is a
154 *	page structure.
155 */
156
157struct vpgqueues vm_page_queues[PQ_COUNT];
158struct vpglocks vm_page_queue_lock;
159struct vpglocks vm_page_queue_free_lock;
160
161struct vpglocks	pa_lock[PA_LOCK_COUNT] __aligned(CACHE_LINE_SIZE);
162
163vm_page_t vm_page_array = 0;
164int vm_page_array_size = 0;
165long first_page = 0;
166int vm_page_zero_count = 0;
167
168static int boot_pages = UMA_BOOT_PAGES;
169TUNABLE_INT("vm.boot_pages", &boot_pages);
170SYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RD, &boot_pages, 0,
171	"number of pages allocated for bootstrapping the VM system");
172
173static void vm_page_clear_dirty_mask(vm_page_t m, int pagebits);
174static void vm_page_queue_remove(int queue, vm_page_t m);
175static void vm_page_enqueue(int queue, vm_page_t m);
176
177/* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */
178#if PAGE_SIZE == 32768
179#ifdef CTASSERT
180CTASSERT(sizeof(u_long) >= 8);
181#endif
182#endif
183
184/*
185 * Try to acquire a physical address lock while a pmap is locked.  If we
186 * fail to trylock we unlock and lock the pmap directly and cache the
187 * locked pa in *locked.  The caller should then restart their loop in case
188 * the virtual to physical mapping has changed.
189 */
190int
191vm_page_pa_tryrelock(pmap_t pmap, vm_paddr_t pa, vm_paddr_t *locked)
192{
193	vm_paddr_t lockpa;
194	uint32_t gen_count;
195
196	gen_count = pmap->pm_gen_count;
197	atomic_add_long((volatile long *)&pmap_tryrelock_calls, 1);
198	lockpa = *locked;
199	*locked = pa;
200	if (lockpa) {
201		PA_LOCK_ASSERT(lockpa, MA_OWNED);
202		if (PA_LOCKPTR(pa) == PA_LOCKPTR(lockpa))
203			return (0);
204		PA_UNLOCK(lockpa);
205	}
206	if (PA_TRYLOCK(pa))
207		return (0);
208	PMAP_UNLOCK(pmap);
209	atomic_add_int((volatile int *)&pmap_tryrelock_restart, 1);
210	PA_LOCK(pa);
211	PMAP_LOCK(pmap);
212
213	if (pmap->pm_gen_count != gen_count + 1) {
214		pmap->pm_retries++;
215		atomic_add_int((volatile int *)&pmap_tryrelock_race, 1);
216		return (EAGAIN);
217	}
218	return (0);
219}
220
221/*
222 *	vm_set_page_size:
223 *
224 *	Sets the page size, perhaps based upon the memory
225 *	size.  Must be called before any use of page-size
226 *	dependent functions.
227 */
228void
229vm_set_page_size(void)
230{
231	if (cnt.v_page_size == 0)
232		cnt.v_page_size = PAGE_SIZE;
233	if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0)
234		panic("vm_set_page_size: page size not a power of two");
235}
236
237/*
238 *	vm_page_blacklist_lookup:
239 *
240 *	See if a physical address in this page has been listed
241 *	in the blacklist tunable.  Entries in the tunable are
242 *	separated by spaces or commas.  If an invalid integer is
243 *	encountered then the rest of the string is skipped.
244 */
245static int
246vm_page_blacklist_lookup(char *list, vm_paddr_t pa)
247{
248	vm_paddr_t bad;
249	char *cp, *pos;
250
251	for (pos = list; *pos != '\0'; pos = cp) {
252		bad = strtoq(pos, &cp, 0);
253		if (*cp != '\0') {
254			if (*cp == ' ' || *cp == ',') {
255				cp++;
256				if (cp == pos)
257					continue;
258			} else
259				break;
260		}
261		if (pa == trunc_page(bad))
262			return (1);
263	}
264	return (0);
265}
266
267/*
268 *	vm_page_startup:
269 *
270 *	Initializes the resident memory module.
271 *
272 *	Allocates memory for the page cells, and
273 *	for the object/offset-to-page hash table headers.
274 *	Each page cell is initialized and placed on the free list.
275 */
276vm_offset_t
277vm_page_startup(vm_offset_t vaddr)
278{
279	vm_offset_t mapped;
280	vm_paddr_t page_range;
281	vm_paddr_t new_end;
282	int i;
283	vm_paddr_t pa;
284	int nblocks;
285	vm_paddr_t last_pa;
286	char *list;
287
288	/* the biggest memory array is the second group of pages */
289	vm_paddr_t end;
290	vm_paddr_t biggestsize;
291	vm_paddr_t low_water, high_water;
292	int biggestone;
293
294	biggestsize = 0;
295	biggestone = 0;
296	nblocks = 0;
297	vaddr = round_page(vaddr);
298
299	for (i = 0; phys_avail[i + 1]; i += 2) {
300		phys_avail[i] = round_page(phys_avail[i]);
301		phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
302	}
303
304	low_water = phys_avail[0];
305	high_water = phys_avail[1];
306
307	for (i = 0; phys_avail[i + 1]; i += 2) {
308		vm_paddr_t size = phys_avail[i + 1] - phys_avail[i];
309
310		if (size > biggestsize) {
311			biggestone = i;
312			biggestsize = size;
313		}
314		if (phys_avail[i] < low_water)
315			low_water = phys_avail[i];
316		if (phys_avail[i + 1] > high_water)
317			high_water = phys_avail[i + 1];
318		++nblocks;
319	}
320
321#ifdef XEN
322	low_water = 0;
323#endif
324
325	end = phys_avail[biggestone+1];
326
327	/*
328	 * Initialize the locks.
329	 */
330	mtx_init(&vm_page_queue_mtx, "vm page queue mutex", NULL, MTX_DEF |
331	    MTX_RECURSE);
332	mtx_init(&vm_page_queue_free_mtx, "vm page queue free mutex", NULL,
333	    MTX_DEF);
334
335	/* Setup page locks. */
336	for (i = 0; i < PA_LOCK_COUNT; i++)
337		mtx_init(&pa_lock[i].data, "page lock", NULL,
338		    MTX_DEF | MTX_RECURSE | MTX_DUPOK);
339
340	/*
341	 * Initialize the queue headers for the hold queue, the active queue,
342	 * and the inactive queue.
343	 */
344	for (i = 0; i < PQ_COUNT; i++)
345		TAILQ_INIT(&vm_page_queues[i].pl);
346	vm_page_queues[PQ_INACTIVE].cnt = &cnt.v_inactive_count;
347	vm_page_queues[PQ_ACTIVE].cnt = &cnt.v_active_count;
348	vm_page_queues[PQ_HOLD].cnt = &cnt.v_active_count;
349
350	/*
351	 * Allocate memory for use when boot strapping the kernel memory
352	 * allocator.
353	 */
354	new_end = end - (boot_pages * UMA_SLAB_SIZE);
355	new_end = trunc_page(new_end);
356	mapped = pmap_map(&vaddr, new_end, end,
357	    VM_PROT_READ | VM_PROT_WRITE);
358	bzero((void *)mapped, end - new_end);
359	uma_startup((void *)mapped, boot_pages);
360
361#if defined(__amd64__) || defined(__i386__) || defined(__arm__)
362	/*
363	 * Allocate a bitmap to indicate that a random physical page
364	 * needs to be included in a minidump.
365	 *
366	 * The amd64 port needs this to indicate which direct map pages
367	 * need to be dumped, via calls to dump_add_page()/dump_drop_page().
368	 *
369	 * However, i386 still needs this workspace internally within the
370	 * minidump code.  In theory, they are not needed on i386, but are
371	 * included should the sf_buf code decide to use them.
372	 */
373	page_range = phys_avail[(nblocks - 1) * 2 + 1] / PAGE_SIZE;
374	vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY);
375	new_end -= vm_page_dump_size;
376	vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
377	    new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
378	bzero((void *)vm_page_dump, vm_page_dump_size);
379#endif
380#ifdef __amd64__
381	/*
382	 * Request that the physical pages underlying the message buffer be
383	 * included in a crash dump.  Since the message buffer is accessed
384	 * through the direct map, they are not automatically included.
385	 */
386	pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr);
387	last_pa = pa + round_page(MSGBUF_SIZE);
388	while (pa < last_pa) {
389		dump_add_page(pa);
390		pa += PAGE_SIZE;
391	}
392#endif
393	/*
394	 * Compute the number of pages of memory that will be available for
395	 * use (taking into account the overhead of a page structure per
396	 * page).
397	 */
398	first_page = low_water / PAGE_SIZE;
399#ifdef VM_PHYSSEG_SPARSE
400	page_range = 0;
401	for (i = 0; phys_avail[i + 1] != 0; i += 2)
402		page_range += atop(phys_avail[i + 1] - phys_avail[i]);
403#elif defined(VM_PHYSSEG_DENSE)
404	page_range = high_water / PAGE_SIZE - first_page;
405#else
406#error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
407#endif
408	end = new_end;
409
410	/*
411	 * Reserve an unmapped guard page to trap access to vm_page_array[-1].
412	 */
413	vaddr += PAGE_SIZE;
414
415	/*
416	 * Initialize the mem entry structures now, and put them in the free
417	 * queue.
418	 */
419	new_end = trunc_page(end - page_range * sizeof(struct vm_page));
420	mapped = pmap_map(&vaddr, new_end, end,
421	    VM_PROT_READ | VM_PROT_WRITE);
422	vm_page_array = (vm_page_t) mapped;
423#if VM_NRESERVLEVEL > 0
424	/*
425	 * Allocate memory for the reservation management system's data
426	 * structures.
427	 */
428	new_end = vm_reserv_startup(&vaddr, new_end, high_water);
429#endif
430#ifdef __amd64__
431	/*
432	 * pmap_map on amd64 comes out of the direct-map, not kvm like i386,
433	 * so the pages must be tracked for a crashdump to include this data.
434	 * This includes the vm_page_array and the early UMA bootstrap pages.
435	 */
436	for (pa = new_end; pa < phys_avail[biggestone + 1]; pa += PAGE_SIZE)
437		dump_add_page(pa);
438#endif
439	phys_avail[biggestone + 1] = new_end;
440
441	/*
442	 * Clear all of the page structures
443	 */
444	bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
445	for (i = 0; i < page_range; i++)
446		vm_page_array[i].order = VM_NFREEORDER;
447	vm_page_array_size = page_range;
448
449	/*
450	 * Initialize the physical memory allocator.
451	 */
452	vm_phys_init();
453
454	/*
455	 * Add every available physical page that is not blacklisted to
456	 * the free lists.
457	 */
458	cnt.v_page_count = 0;
459	cnt.v_free_count = 0;
460	list = getenv("vm.blacklist");
461	for (i = 0; phys_avail[i + 1] != 0; i += 2) {
462		pa = phys_avail[i];
463		last_pa = phys_avail[i + 1];
464		while (pa < last_pa) {
465			if (list != NULL &&
466			    vm_page_blacklist_lookup(list, pa))
467				printf("Skipping page with pa 0x%jx\n",
468				    (uintmax_t)pa);
469			else
470				vm_phys_add_page(pa);
471			pa += PAGE_SIZE;
472		}
473	}
474	freeenv(list);
475#if VM_NRESERVLEVEL > 0
476	/*
477	 * Initialize the reservation management system.
478	 */
479	vm_reserv_init();
480#endif
481	return (vaddr);
482}
483
484void
485vm_page_flag_set(vm_page_t m, unsigned short bits)
486{
487
488	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
489	m->flags |= bits;
490}
491
492void
493vm_page_flag_clear(vm_page_t m, unsigned short bits)
494{
495
496	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
497	m->flags &= ~bits;
498}
499
500void
501vm_page_busy(vm_page_t m)
502{
503
504	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
505	KASSERT((m->oflags & VPO_BUSY) == 0,
506	    ("vm_page_busy: page already busy!!!"));
507	m->oflags |= VPO_BUSY;
508}
509
510/*
511 *      vm_page_flash:
512 *
513 *      wakeup anyone waiting for the page.
514 */
515void
516vm_page_flash(vm_page_t m)
517{
518
519	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
520	if (m->oflags & VPO_WANTED) {
521		m->oflags &= ~VPO_WANTED;
522		wakeup(m);
523	}
524}
525
526/*
527 *      vm_page_wakeup:
528 *
529 *      clear the VPO_BUSY flag and wakeup anyone waiting for the
530 *      page.
531 *
532 */
533void
534vm_page_wakeup(vm_page_t m)
535{
536
537	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
538	KASSERT(m->oflags & VPO_BUSY, ("vm_page_wakeup: page not busy!!!"));
539	m->oflags &= ~VPO_BUSY;
540	vm_page_flash(m);
541}
542
543void
544vm_page_io_start(vm_page_t m)
545{
546
547	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
548	m->busy++;
549}
550
551void
552vm_page_io_finish(vm_page_t m)
553{
554
555	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
556	m->busy--;
557	if (m->busy == 0)
558		vm_page_flash(m);
559}
560
561/*
562 * Keep page from being freed by the page daemon
563 * much of the same effect as wiring, except much lower
564 * overhead and should be used only for *very* temporary
565 * holding ("wiring").
566 */
567void
568vm_page_hold(vm_page_t mem)
569{
570
571	vm_page_lock_assert(mem, MA_OWNED);
572        mem->hold_count++;
573}
574
575void
576vm_page_unhold(vm_page_t mem)
577{
578
579	vm_page_lock_assert(mem, MA_OWNED);
580	--mem->hold_count;
581	KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!"));
582	if (mem->hold_count == 0 && VM_PAGE_INQUEUE2(mem, PQ_HOLD))
583		vm_page_free_toq(mem);
584}
585
586/*
587 *	vm_page_free:
588 *
589 *	Free a page.
590 */
591void
592vm_page_free(vm_page_t m)
593{
594
595	m->flags &= ~PG_ZERO;
596	vm_page_free_toq(m);
597}
598
599/*
600 *	vm_page_free_zero:
601 *
602 *	Free a page to the zerod-pages queue
603 */
604void
605vm_page_free_zero(vm_page_t m)
606{
607
608	m->flags |= PG_ZERO;
609	vm_page_free_toq(m);
610}
611
612/*
613 *	vm_page_sleep:
614 *
615 *	Sleep and release the page and page queues locks.
616 *
617 *	The object containing the given page must be locked.
618 */
619void
620vm_page_sleep(vm_page_t m, const char *msg)
621{
622
623	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
624	if (mtx_owned(&vm_page_queue_mtx))
625		vm_page_unlock_queues();
626	if (mtx_owned(vm_page_lockptr(m)))
627		vm_page_unlock(m);
628
629	/*
630	 * It's possible that while we sleep, the page will get
631	 * unbusied and freed.  If we are holding the object
632	 * lock, we will assume we hold a reference to the object
633	 * such that even if m->object changes, we can re-lock
634	 * it.
635	 */
636	m->oflags |= VPO_WANTED;
637	msleep(m, VM_OBJECT_MTX(m->object), PVM, msg, 0);
638}
639
640/*
641 *	vm_page_dirty:
642 *
643 *	make page all dirty
644 */
645void
646vm_page_dirty(vm_page_t m)
647{
648
649	KASSERT((m->flags & PG_CACHED) == 0,
650	    ("vm_page_dirty: page in cache!"));
651	KASSERT(!VM_PAGE_IS_FREE(m),
652	    ("vm_page_dirty: page is free!"));
653	KASSERT(m->valid == VM_PAGE_BITS_ALL,
654	    ("vm_page_dirty: page is invalid!"));
655	m->dirty = VM_PAGE_BITS_ALL;
656}
657
658/*
659 *	vm_page_splay:
660 *
661 *	Implements Sleator and Tarjan's top-down splay algorithm.  Returns
662 *	the vm_page containing the given pindex.  If, however, that
663 *	pindex is not found in the vm_object, returns a vm_page that is
664 *	adjacent to the pindex, coming before or after it.
665 */
666vm_page_t
667vm_page_splay(vm_pindex_t pindex, vm_page_t root)
668{
669	struct vm_page dummy;
670	vm_page_t lefttreemax, righttreemin, y;
671
672	if (root == NULL)
673		return (root);
674	lefttreemax = righttreemin = &dummy;
675	for (;; root = y) {
676		if (pindex < root->pindex) {
677			if ((y = root->left) == NULL)
678				break;
679			if (pindex < y->pindex) {
680				/* Rotate right. */
681				root->left = y->right;
682				y->right = root;
683				root = y;
684				if ((y = root->left) == NULL)
685					break;
686			}
687			/* Link into the new root's right tree. */
688			righttreemin->left = root;
689			righttreemin = root;
690		} else if (pindex > root->pindex) {
691			if ((y = root->right) == NULL)
692				break;
693			if (pindex > y->pindex) {
694				/* Rotate left. */
695				root->right = y->left;
696				y->left = root;
697				root = y;
698				if ((y = root->right) == NULL)
699					break;
700			}
701			/* Link into the new root's left tree. */
702			lefttreemax->right = root;
703			lefttreemax = root;
704		} else
705			break;
706	}
707	/* Assemble the new root. */
708	lefttreemax->right = root->left;
709	righttreemin->left = root->right;
710	root->left = dummy.right;
711	root->right = dummy.left;
712	return (root);
713}
714
715/*
716 *	vm_page_insert:		[ internal use only ]
717 *
718 *	Inserts the given mem entry into the object and object list.
719 *
720 *	The pagetables are not updated but will presumably fault the page
721 *	in if necessary, or if a kernel page the caller will at some point
722 *	enter the page into the kernel's pmap.  We are not allowed to block
723 *	here so we *can't* do this anyway.
724 *
725 *	The object and page must be locked.
726 *	This routine may not block.
727 */
728void
729vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
730{
731	vm_page_t root;
732
733	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
734	if (m->object != NULL)
735		panic("vm_page_insert: page already inserted");
736
737	/*
738	 * Record the object/offset pair in this page
739	 */
740	m->object = object;
741	m->pindex = pindex;
742
743	/*
744	 * Now link into the object's ordered list of backed pages.
745	 */
746	root = object->root;
747	if (root == NULL) {
748		m->left = NULL;
749		m->right = NULL;
750		TAILQ_INSERT_TAIL(&object->memq, m, listq);
751	} else {
752		root = vm_page_splay(pindex, root);
753		if (pindex < root->pindex) {
754			m->left = root->left;
755			m->right = root;
756			root->left = NULL;
757			TAILQ_INSERT_BEFORE(root, m, listq);
758		} else if (pindex == root->pindex)
759			panic("vm_page_insert: offset already allocated");
760		else {
761			m->right = root->right;
762			m->left = root;
763			root->right = NULL;
764			TAILQ_INSERT_AFTER(&object->memq, root, m, listq);
765		}
766	}
767	object->root = m;
768	object->generation++;
769
770	/*
771	 * show that the object has one more resident page.
772	 */
773	object->resident_page_count++;
774	/*
775	 * Hold the vnode until the last page is released.
776	 */
777	if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
778		vhold((struct vnode *)object->handle);
779
780	/*
781	 * Since we are inserting a new and possibly dirty page,
782	 * update the object's OBJ_MIGHTBEDIRTY flag.
783	 */
784	if (m->flags & PG_WRITEABLE)
785		vm_object_set_writeable_dirty(object);
786}
787
788/*
789 *	vm_page_remove:
790 *				NOTE: used by device pager as well -wfj
791 *
792 *	Removes the given mem entry from the object/offset-page
793 *	table and the object page list, but do not invalidate/terminate
794 *	the backing store.
795 *
796 *	The object and page must be locked.
797 *	The underlying pmap entry (if any) is NOT removed here.
798 *	This routine may not block.
799 */
800void
801vm_page_remove(vm_page_t m)
802{
803	vm_object_t object;
804	vm_page_t root;
805
806	if ((m->flags & PG_UNMANAGED) == 0)
807		vm_page_lock_assert(m, MA_OWNED);
808	if ((object = m->object) == NULL)
809		return;
810	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
811	if (m->oflags & VPO_BUSY) {
812		m->oflags &= ~VPO_BUSY;
813		vm_page_flash(m);
814	}
815
816	/*
817	 * Now remove from the object's list of backed pages.
818	 */
819	if (m != object->root)
820		vm_page_splay(m->pindex, object->root);
821	if (m->left == NULL)
822		root = m->right;
823	else {
824		root = vm_page_splay(m->pindex, m->left);
825		root->right = m->right;
826	}
827	object->root = root;
828	TAILQ_REMOVE(&object->memq, m, listq);
829
830	/*
831	 * And show that the object has one fewer resident page.
832	 */
833	object->resident_page_count--;
834	object->generation++;
835	/*
836	 * The vnode may now be recycled.
837	 */
838	if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
839		vdrop((struct vnode *)object->handle);
840
841	m->object = NULL;
842}
843
844/*
845 *	vm_page_lookup:
846 *
847 *	Returns the page associated with the object/offset
848 *	pair specified; if none is found, NULL is returned.
849 *
850 *	The object must be locked.
851 *	This routine may not block.
852 *	This is a critical path routine
853 */
854vm_page_t
855vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
856{
857	vm_page_t m;
858
859	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
860	if ((m = object->root) != NULL && m->pindex != pindex) {
861		m = vm_page_splay(pindex, m);
862		if ((object->root = m)->pindex != pindex)
863			m = NULL;
864	}
865	return (m);
866}
867
868/*
869 *	vm_page_rename:
870 *
871 *	Move the given memory entry from its
872 *	current object to the specified target object/offset.
873 *
874 *	The object must be locked.
875 *	This routine may not block.
876 *
877 *	Note: swap associated with the page must be invalidated by the move.  We
878 *	      have to do this for several reasons:  (1) we aren't freeing the
879 *	      page, (2) we are dirtying the page, (3) the VM system is probably
880 *	      moving the page from object A to B, and will then later move
881 *	      the backing store from A to B and we can't have a conflict.
882 *
883 *	Note: we *always* dirty the page.  It is necessary both for the
884 *	      fact that we moved it, and because we may be invalidating
885 *	      swap.  If the page is on the cache, we have to deactivate it
886 *	      or vm_page_dirty() will panic.  Dirty pages are not allowed
887 *	      on the cache.
888 */
889void
890vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
891{
892
893	vm_page_remove(m);
894	vm_page_insert(m, new_object, new_pindex);
895	vm_page_dirty(m);
896}
897
898/*
899 *	Convert all of the given object's cached pages that have a
900 *	pindex within the given range into free pages.  If the value
901 *	zero is given for "end", then the range's upper bound is
902 *	infinity.  If the given object is backed by a vnode and it
903 *	transitions from having one or more cached pages to none, the
904 *	vnode's hold count is reduced.
905 */
906void
907vm_page_cache_free(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
908{
909	vm_page_t m, m_next;
910	boolean_t empty;
911
912	mtx_lock(&vm_page_queue_free_mtx);
913	if (__predict_false(object->cache == NULL)) {
914		mtx_unlock(&vm_page_queue_free_mtx);
915		return;
916	}
917	m = object->cache = vm_page_splay(start, object->cache);
918	if (m->pindex < start) {
919		if (m->right == NULL)
920			m = NULL;
921		else {
922			m_next = vm_page_splay(start, m->right);
923			m_next->left = m;
924			m->right = NULL;
925			m = object->cache = m_next;
926		}
927	}
928
929	/*
930	 * At this point, "m" is either (1) a reference to the page
931	 * with the least pindex that is greater than or equal to
932	 * "start" or (2) NULL.
933	 */
934	for (; m != NULL && (m->pindex < end || end == 0); m = m_next) {
935		/*
936		 * Find "m"'s successor and remove "m" from the
937		 * object's cache.
938		 */
939		if (m->right == NULL) {
940			object->cache = m->left;
941			m_next = NULL;
942		} else {
943			m_next = vm_page_splay(start, m->right);
944			m_next->left = m->left;
945			object->cache = m_next;
946		}
947		/* Convert "m" to a free page. */
948		m->object = NULL;
949		m->valid = 0;
950		/* Clear PG_CACHED and set PG_FREE. */
951		m->flags ^= PG_CACHED | PG_FREE;
952		KASSERT((m->flags & (PG_CACHED | PG_FREE)) == PG_FREE,
953		    ("vm_page_cache_free: page %p has inconsistent flags", m));
954		cnt.v_cache_count--;
955		cnt.v_free_count++;
956	}
957	empty = object->cache == NULL;
958	mtx_unlock(&vm_page_queue_free_mtx);
959	if (object->type == OBJT_VNODE && empty)
960		vdrop(object->handle);
961}
962
963/*
964 *	Returns the cached page that is associated with the given
965 *	object and offset.  If, however, none exists, returns NULL.
966 *
967 *	The free page queue must be locked.
968 */
969static inline vm_page_t
970vm_page_cache_lookup(vm_object_t object, vm_pindex_t pindex)
971{
972	vm_page_t m;
973
974	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
975	if ((m = object->cache) != NULL && m->pindex != pindex) {
976		m = vm_page_splay(pindex, m);
977		if ((object->cache = m)->pindex != pindex)
978			m = NULL;
979	}
980	return (m);
981}
982
983/*
984 *	Remove the given cached page from its containing object's
985 *	collection of cached pages.
986 *
987 *	The free page queue must be locked.
988 */
989void
990vm_page_cache_remove(vm_page_t m)
991{
992	vm_object_t object;
993	vm_page_t root;
994
995	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
996	KASSERT((m->flags & PG_CACHED) != 0,
997	    ("vm_page_cache_remove: page %p is not cached", m));
998	object = m->object;
999	if (m != object->cache) {
1000		root = vm_page_splay(m->pindex, object->cache);
1001		KASSERT(root == m,
1002		    ("vm_page_cache_remove: page %p is not cached in object %p",
1003		    m, object));
1004	}
1005	if (m->left == NULL)
1006		root = m->right;
1007	else if (m->right == NULL)
1008		root = m->left;
1009	else {
1010		root = vm_page_splay(m->pindex, m->left);
1011		root->right = m->right;
1012	}
1013	object->cache = root;
1014	m->object = NULL;
1015	cnt.v_cache_count--;
1016}
1017
1018/*
1019 *	Transfer all of the cached pages with offset greater than or
1020 *	equal to 'offidxstart' from the original object's cache to the
1021 *	new object's cache.  However, any cached pages with offset
1022 *	greater than or equal to the new object's size are kept in the
1023 *	original object.  Initially, the new object's cache must be
1024 *	empty.  Offset 'offidxstart' in the original object must
1025 *	correspond to offset zero in the new object.
1026 *
1027 *	The new object must be locked.
1028 */
1029void
1030vm_page_cache_transfer(vm_object_t orig_object, vm_pindex_t offidxstart,
1031    vm_object_t new_object)
1032{
1033	vm_page_t m, m_next;
1034
1035	/*
1036	 * Insertion into an object's collection of cached pages
1037	 * requires the object to be locked.  In contrast, removal does
1038	 * not.
1039	 */
1040	VM_OBJECT_LOCK_ASSERT(new_object, MA_OWNED);
1041	KASSERT(new_object->cache == NULL,
1042	    ("vm_page_cache_transfer: object %p has cached pages",
1043	    new_object));
1044	mtx_lock(&vm_page_queue_free_mtx);
1045	if ((m = orig_object->cache) != NULL) {
1046		/*
1047		 * Transfer all of the pages with offset greater than or
1048		 * equal to 'offidxstart' from the original object's
1049		 * cache to the new object's cache.
1050		 */
1051		m = vm_page_splay(offidxstart, m);
1052		if (m->pindex < offidxstart) {
1053			orig_object->cache = m;
1054			new_object->cache = m->right;
1055			m->right = NULL;
1056		} else {
1057			orig_object->cache = m->left;
1058			new_object->cache = m;
1059			m->left = NULL;
1060		}
1061		while ((m = new_object->cache) != NULL) {
1062			if ((m->pindex - offidxstart) >= new_object->size) {
1063				/*
1064				 * Return all of the cached pages with
1065				 * offset greater than or equal to the
1066				 * new object's size to the original
1067				 * object's cache.
1068				 */
1069				new_object->cache = m->left;
1070				m->left = orig_object->cache;
1071				orig_object->cache = m;
1072				break;
1073			}
1074			m_next = vm_page_splay(m->pindex, m->right);
1075			/* Update the page's object and offset. */
1076			m->object = new_object;
1077			m->pindex -= offidxstart;
1078			if (m_next == NULL)
1079				break;
1080			m->right = NULL;
1081			m_next->left = m;
1082			new_object->cache = m_next;
1083		}
1084		KASSERT(new_object->cache == NULL ||
1085		    new_object->type == OBJT_SWAP,
1086		    ("vm_page_cache_transfer: object %p's type is incompatible"
1087		    " with cached pages", new_object));
1088	}
1089	mtx_unlock(&vm_page_queue_free_mtx);
1090}
1091
1092/*
1093 *	vm_page_alloc:
1094 *
1095 *	Allocate and return a memory cell associated
1096 *	with this VM object/offset pair.
1097 *
1098 *	page_req classes:
1099 *	VM_ALLOC_NORMAL		normal process request
1100 *	VM_ALLOC_SYSTEM		system *really* needs a page
1101 *	VM_ALLOC_INTERRUPT	interrupt time request
1102 *	VM_ALLOC_ZERO		zero page
1103 *	VM_ALLOC_WIRED		wire the allocated page
1104 *	VM_ALLOC_NOOBJ		page is not associated with a vm object
1105 *	VM_ALLOC_NOBUSY		do not set the page busy
1106 *	VM_ALLOC_IFNOTCACHED	return NULL, do not reactivate if the page
1107 *				is cached
1108 *
1109 *	This routine may not sleep.
1110 */
1111vm_page_t
1112vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
1113{
1114	struct vnode *vp = NULL;
1115	vm_object_t m_object;
1116	vm_page_t m;
1117	int flags, page_req;
1118
1119	page_req = req & VM_ALLOC_CLASS_MASK;
1120	KASSERT(curthread->td_intr_nesting_level == 0 ||
1121	    page_req == VM_ALLOC_INTERRUPT,
1122	    ("vm_page_alloc(NORMAL|SYSTEM) in interrupt context"));
1123
1124	if ((req & VM_ALLOC_NOOBJ) == 0) {
1125		KASSERT(object != NULL,
1126		    ("vm_page_alloc: NULL object."));
1127		VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1128	}
1129
1130	/*
1131	 * The pager is allowed to eat deeper into the free page list.
1132	 */
1133	if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT)) {
1134		page_req = VM_ALLOC_SYSTEM;
1135	};
1136
1137	mtx_lock(&vm_page_queue_free_mtx);
1138	if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved ||
1139	    (page_req == VM_ALLOC_SYSTEM &&
1140	    cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) ||
1141	    (page_req == VM_ALLOC_INTERRUPT &&
1142	    cnt.v_free_count + cnt.v_cache_count > 0)) {
1143		/*
1144		 * Allocate from the free queue if the number of free pages
1145		 * exceeds the minimum for the request class.
1146		 */
1147		if (object != NULL &&
1148		    (m = vm_page_cache_lookup(object, pindex)) != NULL) {
1149			if ((req & VM_ALLOC_IFNOTCACHED) != 0) {
1150				mtx_unlock(&vm_page_queue_free_mtx);
1151				return (NULL);
1152			}
1153			if (vm_phys_unfree_page(m))
1154				vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, 0);
1155#if VM_NRESERVLEVEL > 0
1156			else if (!vm_reserv_reactivate_page(m))
1157#else
1158			else
1159#endif
1160				panic("vm_page_alloc: cache page %p is missing"
1161				    " from the free queue", m);
1162		} else if ((req & VM_ALLOC_IFCACHED) != 0) {
1163			mtx_unlock(&vm_page_queue_free_mtx);
1164			return (NULL);
1165#if VM_NRESERVLEVEL > 0
1166		} else if (object == NULL || object->type == OBJT_DEVICE ||
1167		    object->type == OBJT_SG ||
1168		    (object->flags & OBJ_COLORED) == 0 ||
1169		    (m = vm_reserv_alloc_page(object, pindex)) == NULL) {
1170#else
1171		} else {
1172#endif
1173			m = vm_phys_alloc_pages(object != NULL ?
1174			    VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0);
1175#if VM_NRESERVLEVEL > 0
1176			if (m == NULL && vm_reserv_reclaim_inactive()) {
1177				m = vm_phys_alloc_pages(object != NULL ?
1178				    VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT,
1179				    0);
1180			}
1181#endif
1182		}
1183	} else {
1184		/*
1185		 * Not allocatable, give up.
1186		 */
1187		mtx_unlock(&vm_page_queue_free_mtx);
1188		atomic_add_int(&vm_pageout_deficit, 1);
1189		pagedaemon_wakeup();
1190		return (NULL);
1191	}
1192
1193	/*
1194	 *  At this point we had better have found a good page.
1195	 */
1196
1197	KASSERT(m != NULL, ("vm_page_alloc: missing page"));
1198	KASSERT(m->queue == PQ_NONE,
1199	    ("vm_page_alloc: page %p has unexpected queue %d", m, m->queue));
1200	KASSERT(m->wire_count == 0, ("vm_page_alloc: page %p is wired", m));
1201	KASSERT(m->hold_count == 0, ("vm_page_alloc: page %p is held", m));
1202	KASSERT(m->busy == 0, ("vm_page_alloc: page %p is busy", m));
1203	KASSERT(m->dirty == 0, ("vm_page_alloc: page %p is dirty", m));
1204	KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1205	    ("vm_page_alloc: page %p has unexpected memattr %d", m,
1206	    pmap_page_get_memattr(m)));
1207	if ((m->flags & PG_CACHED) != 0) {
1208		KASSERT(m->valid != 0,
1209		    ("vm_page_alloc: cached page %p is invalid", m));
1210		if (m->object == object && m->pindex == pindex)
1211	  		cnt.v_reactivated++;
1212		else
1213			m->valid = 0;
1214		m_object = m->object;
1215		vm_page_cache_remove(m);
1216		if (m_object->type == OBJT_VNODE && m_object->cache == NULL)
1217			vp = m_object->handle;
1218	} else {
1219		KASSERT(VM_PAGE_IS_FREE(m),
1220		    ("vm_page_alloc: page %p is not free", m));
1221		KASSERT(m->valid == 0,
1222		    ("vm_page_alloc: free page %p is valid", m));
1223		cnt.v_free_count--;
1224	}
1225
1226	/*
1227	 * Initialize structure.  Only the PG_ZERO flag is inherited.
1228	 */
1229	flags = 0;
1230	if (m->flags & PG_ZERO) {
1231		vm_page_zero_count--;
1232		if (req & VM_ALLOC_ZERO)
1233			flags = PG_ZERO;
1234	}
1235	if (object == NULL || object->type == OBJT_PHYS)
1236		flags |= PG_UNMANAGED;
1237	m->flags = flags;
1238	if (req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ))
1239		m->oflags = 0;
1240	else
1241		m->oflags = VPO_BUSY;
1242	if (req & VM_ALLOC_WIRED) {
1243		atomic_add_int(&cnt.v_wire_count, 1);
1244		m->wire_count = 1;
1245	}
1246	m->act_count = 0;
1247	mtx_unlock(&vm_page_queue_free_mtx);
1248
1249	if (object != NULL) {
1250		/* Ignore device objects; the pager sets "memattr" for them. */
1251		if (object->memattr != VM_MEMATTR_DEFAULT &&
1252		    object->type != OBJT_DEVICE && object->type != OBJT_SG)
1253			pmap_page_set_memattr(m, object->memattr);
1254		vm_page_insert(m, object, pindex);
1255	} else
1256		m->pindex = pindex;
1257
1258	/*
1259	 * The following call to vdrop() must come after the above call
1260	 * to vm_page_insert() in case both affect the same object and
1261	 * vnode.  Otherwise, the affected vnode's hold count could
1262	 * temporarily become zero.
1263	 */
1264	if (vp != NULL)
1265		vdrop(vp);
1266
1267	/*
1268	 * Don't wakeup too often - wakeup the pageout daemon when
1269	 * we would be nearly out of memory.
1270	 */
1271	if (vm_paging_needed())
1272		pagedaemon_wakeup();
1273
1274	return (m);
1275}
1276
1277/*
1278 *	vm_wait:	(also see VM_WAIT macro)
1279 *
1280 *	Block until free pages are available for allocation
1281 *	- Called in various places before memory allocations.
1282 */
1283void
1284vm_wait(void)
1285{
1286
1287	mtx_lock(&vm_page_queue_free_mtx);
1288	if (curproc == pageproc) {
1289		vm_pageout_pages_needed = 1;
1290		msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx,
1291		    PDROP | PSWP, "VMWait", 0);
1292	} else {
1293		if (!vm_pages_needed) {
1294			vm_pages_needed = 1;
1295			wakeup(&vm_pages_needed);
1296		}
1297		msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM,
1298		    "vmwait", 0);
1299	}
1300}
1301
1302/*
1303 *	vm_waitpfault:	(also see VM_WAITPFAULT macro)
1304 *
1305 *	Block until free pages are available for allocation
1306 *	- Called only in vm_fault so that processes page faulting
1307 *	  can be easily tracked.
1308 *	- Sleeps at a lower priority than vm_wait() so that vm_wait()ing
1309 *	  processes will be able to grab memory first.  Do not change
1310 *	  this balance without careful testing first.
1311 */
1312void
1313vm_waitpfault(void)
1314{
1315
1316	mtx_lock(&vm_page_queue_free_mtx);
1317	if (!vm_pages_needed) {
1318		vm_pages_needed = 1;
1319		wakeup(&vm_pages_needed);
1320	}
1321	msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER,
1322	    "pfault", 0);
1323}
1324
1325/*
1326 *	vm_page_requeue:
1327 *
1328 *	If the given page is contained within a page queue, move it to the tail
1329 *	of that queue.
1330 *
1331 *	The page queues must be locked.
1332 */
1333void
1334vm_page_requeue(vm_page_t m)
1335{
1336	int queue = VM_PAGE_GETQUEUE(m);
1337	struct vpgqueues *vpq;
1338
1339	if (queue != PQ_NONE) {
1340		vpq = &vm_page_queues[queue];
1341		TAILQ_REMOVE(&vpq->pl, m, pageq);
1342		TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
1343	}
1344}
1345
1346/*
1347 *	vm_page_queue_remove:
1348 *
1349 *	Remove the given page from the specified queue.
1350 *
1351 *	The page and page queues must be locked.
1352 */
1353static __inline void
1354vm_page_queue_remove(int queue, vm_page_t m)
1355{
1356	struct vpgqueues *pq;
1357
1358	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1359	vm_page_lock_assert(m, MA_OWNED);
1360	pq = &vm_page_queues[queue];
1361	TAILQ_REMOVE(&pq->pl, m, pageq);
1362	(*pq->cnt)--;
1363}
1364
1365/*
1366 *	vm_pageq_remove:
1367 *
1368 *	Remove a page from its queue.
1369 *
1370 *	The given page must be locked.
1371 *	This routine may not block.
1372 */
1373void
1374vm_pageq_remove(vm_page_t m)
1375{
1376	int queue = VM_PAGE_GETQUEUE(m);
1377
1378	vm_page_lock_assert(m, MA_OWNED);
1379	if (queue != PQ_NONE) {
1380		vm_page_lock_queues();
1381		VM_PAGE_SETQUEUE2(m, PQ_NONE);
1382		vm_page_queue_remove(queue, m);
1383		vm_page_unlock_queues();
1384	}
1385}
1386
1387/*
1388 *	vm_page_enqueue:
1389 *
1390 *	Add the given page to the specified queue.
1391 *
1392 *	The page queues must be locked.
1393 */
1394static void
1395vm_page_enqueue(int queue, vm_page_t m)
1396{
1397	struct vpgqueues *vpq;
1398
1399	vpq = &vm_page_queues[queue];
1400	VM_PAGE_SETQUEUE2(m, queue);
1401	TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
1402	++*vpq->cnt;
1403}
1404
1405/*
1406 *	vm_page_activate:
1407 *
1408 *	Put the specified page on the active list (if appropriate).
1409 *	Ensure that act_count is at least ACT_INIT but do not otherwise
1410 *	mess with it.
1411 *
1412 *	The page must be locked.
1413 *	This routine may not block.
1414 */
1415void
1416vm_page_activate(vm_page_t m)
1417{
1418	int queue;
1419
1420	vm_page_lock_assert(m, MA_OWNED);
1421	if ((queue = VM_PAGE_GETKNOWNQUEUE2(m)) != PQ_ACTIVE) {
1422		if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
1423			if (m->act_count < ACT_INIT)
1424				m->act_count = ACT_INIT;
1425			vm_page_lock_queues();
1426			if (queue != PQ_NONE)
1427				vm_page_queue_remove(queue, m);
1428			vm_page_enqueue(PQ_ACTIVE, m);
1429			vm_page_unlock_queues();
1430		} else
1431			KASSERT(queue == PQ_NONE,
1432			    ("vm_page_activate: wired page %p is queued", m));
1433	} else {
1434		if (m->act_count < ACT_INIT)
1435			m->act_count = ACT_INIT;
1436	}
1437}
1438
1439/*
1440 *	vm_page_free_wakeup:
1441 *
1442 *	Helper routine for vm_page_free_toq() and vm_page_cache().  This
1443 *	routine is called when a page has been added to the cache or free
1444 *	queues.
1445 *
1446 *	The page queues must be locked.
1447 *	This routine may not block.
1448 */
1449static inline void
1450vm_page_free_wakeup(void)
1451{
1452
1453	mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1454	/*
1455	 * if pageout daemon needs pages, then tell it that there are
1456	 * some free.
1457	 */
1458	if (vm_pageout_pages_needed &&
1459	    cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) {
1460		wakeup(&vm_pageout_pages_needed);
1461		vm_pageout_pages_needed = 0;
1462	}
1463	/*
1464	 * wakeup processes that are waiting on memory if we hit a
1465	 * high water mark. And wakeup scheduler process if we have
1466	 * lots of memory. this process will swapin processes.
1467	 */
1468	if (vm_pages_needed && !vm_page_count_min()) {
1469		vm_pages_needed = 0;
1470		wakeup(&cnt.v_free_count);
1471	}
1472}
1473
1474/*
1475 *	vm_page_free_toq:
1476 *
1477 *	Returns the given page to the free list,
1478 *	disassociating it with any VM object.
1479 *
1480 *	Object and page must be locked prior to entry.
1481 *	This routine may not block.
1482 */
1483
1484void
1485vm_page_free_toq(vm_page_t m)
1486{
1487
1488	if ((m->flags & PG_UNMANAGED) == 0) {
1489		vm_page_lock_assert(m, MA_OWNED);
1490		KASSERT(!pmap_page_is_mapped(m),
1491		    ("vm_page_free_toq: freeing mapped page %p", m));
1492	}
1493	PCPU_INC(cnt.v_tfree);
1494
1495	if (m->busy || VM_PAGE_IS_FREE(m)) {
1496		printf(
1497		"vm_page_free: pindex(%lu), busy(%d), VPO_BUSY(%d), hold(%d)\n",
1498		    (u_long)m->pindex, m->busy, (m->oflags & VPO_BUSY) ? 1 : 0,
1499		    m->hold_count);
1500		if (VM_PAGE_IS_FREE(m))
1501			panic("vm_page_free: freeing free page");
1502		else
1503			panic("vm_page_free: freeing busy page");
1504	}
1505
1506	/*
1507	 * unqueue, then remove page.  Note that we cannot destroy
1508	 * the page here because we do not want to call the pager's
1509	 * callback routine until after we've put the page on the
1510	 * appropriate free queue.
1511	 */
1512	if ((m->flags & PG_UNMANAGED) == 0)
1513		vm_pageq_remove(m);
1514	vm_page_remove(m);
1515
1516	/*
1517	 * If fictitious remove object association and
1518	 * return, otherwise delay object association removal.
1519	 */
1520	if ((m->flags & PG_FICTITIOUS) != 0) {
1521		return;
1522	}
1523
1524	m->valid = 0;
1525	vm_page_undirty(m);
1526
1527	if (m->wire_count != 0) {
1528		if (m->wire_count > 1) {
1529			panic("vm_page_free: invalid wire count (%d), pindex: 0x%lx",
1530				m->wire_count, (long)m->pindex);
1531		}
1532		panic("vm_page_free: freeing wired page");
1533	}
1534	if (m->hold_count != 0) {
1535		m->flags &= ~PG_ZERO;
1536		vm_page_lock_queues();
1537		vm_page_enqueue(PQ_HOLD, m);
1538		vm_page_unlock_queues();
1539	} else {
1540		/*
1541		 * Restore the default memory attribute to the page.
1542		 */
1543		if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
1544			pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
1545
1546		/*
1547		 * Insert the page into the physical memory allocator's
1548		 * cache/free page queues.
1549		 */
1550		mtx_lock(&vm_page_queue_free_mtx);
1551		m->flags |= PG_FREE;
1552		cnt.v_free_count++;
1553#if VM_NRESERVLEVEL > 0
1554		if (!vm_reserv_free_page(m))
1555#else
1556		if (TRUE)
1557#endif
1558			vm_phys_free_pages(m, 0);
1559		if ((m->flags & PG_ZERO) != 0)
1560			++vm_page_zero_count;
1561		else
1562			vm_page_zero_idle_wakeup();
1563		vm_page_free_wakeup();
1564		mtx_unlock(&vm_page_queue_free_mtx);
1565	}
1566}
1567
1568/*
1569 *	vm_page_wire:
1570 *
1571 *	Mark this page as wired down by yet
1572 *	another map, removing it from paging queues
1573 *	as necessary.
1574 *
1575 *	The page must be locked.
1576 *	This routine may not block.
1577 */
1578void
1579vm_page_wire(vm_page_t m)
1580{
1581
1582	/*
1583	 * Only bump the wire statistics if the page is not already wired,
1584	 * and only unqueue the page if it is on some queue (if it is unmanaged
1585	 * it is already off the queues).
1586	 */
1587	vm_page_lock_assert(m, MA_OWNED);
1588	if (m->flags & PG_FICTITIOUS)
1589		return;
1590	if (m->wire_count == 0) {
1591		if ((m->flags & PG_UNMANAGED) == 0)
1592			vm_pageq_remove(m);
1593		atomic_add_int(&cnt.v_wire_count, 1);
1594	}
1595	m->wire_count++;
1596	KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
1597}
1598
1599/*
1600 *	vm_page_unwire:
1601 *
1602 *	Release one wiring of this page, potentially
1603 *	enabling it to be paged again.
1604 *
1605 *	Many pages placed on the inactive queue should actually go
1606 *	into the cache, but it is difficult to figure out which.  What
1607 *	we do instead, if the inactive target is well met, is to put
1608 *	clean pages at the head of the inactive queue instead of the tail.
1609 *	This will cause them to be moved to the cache more quickly and
1610 *	if not actively re-referenced, freed more quickly.  If we just
1611 *	stick these pages at the end of the inactive queue, heavy filesystem
1612 *	meta-data accesses can cause an unnecessary paging load on memory bound
1613 *	processes.  This optimization causes one-time-use metadata to be
1614 *	reused more quickly.
1615 *
1616 *	BUT, if we are in a low-memory situation we have no choice but to
1617 *	put clean pages on the cache queue.
1618 *
1619 *	A number of routines use vm_page_unwire() to guarantee that the page
1620 *	will go into either the inactive or active queues, and will NEVER
1621 *	be placed in the cache - for example, just after dirtying a page.
1622 *	dirty pages in the cache are not allowed.
1623 *
1624 *	The page must be locked.
1625 *	This routine may not block.
1626 */
1627void
1628vm_page_unwire(vm_page_t m, int activate)
1629{
1630
1631	if ((m->flags & PG_UNMANAGED) == 0)
1632		vm_page_lock_assert(m, MA_OWNED);
1633	if (m->flags & PG_FICTITIOUS)
1634		return;
1635	if (m->wire_count > 0) {
1636		m->wire_count--;
1637		if (m->wire_count == 0) {
1638			atomic_subtract_int(&cnt.v_wire_count, 1);
1639			if ((m->flags & PG_UNMANAGED) != 0)
1640				return;
1641			vm_page_lock_queues();
1642			if (activate)
1643				vm_page_enqueue(PQ_ACTIVE, m);
1644			else {
1645				vm_page_flag_clear(m, PG_WINATCFLS);
1646				vm_page_enqueue(PQ_INACTIVE, m);
1647			}
1648			vm_page_unlock_queues();
1649		}
1650	} else {
1651		panic("vm_page_unwire: invalid wire count: %d", m->wire_count);
1652	}
1653}
1654
1655/*
1656 * Move the specified page to the inactive queue.
1657 *
1658 * Normally athead is 0 resulting in LRU operation.  athead is set
1659 * to 1 if we want this page to be 'as if it were placed in the cache',
1660 * except without unmapping it from the process address space.
1661 *
1662 * This routine may not block.
1663 */
1664static inline void
1665_vm_page_deactivate(vm_page_t m, int athead)
1666{
1667	int queue;
1668
1669	vm_page_lock_assert(m, MA_OWNED);
1670
1671	/*
1672	 * Ignore if already inactive.
1673	 */
1674	if ((queue = VM_PAGE_GETKNOWNQUEUE2(m)) == PQ_INACTIVE)
1675		return;
1676	if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
1677		vm_page_lock_queues();
1678		vm_page_flag_clear(m, PG_WINATCFLS);
1679		if (queue != PQ_NONE)
1680			vm_page_queue_remove(queue, m);
1681		if (athead)
1682			TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m,
1683			    pageq);
1684		else
1685			TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m,
1686			    pageq);
1687		VM_PAGE_SETQUEUE2(m, PQ_INACTIVE);
1688		cnt.v_inactive_count++;
1689		vm_page_unlock_queues();
1690	}
1691}
1692
1693/*
1694 * Move the specified page to the inactive queue.
1695 *
1696 * The page must be locked.
1697 */
1698void
1699vm_page_deactivate(vm_page_t m)
1700{
1701
1702	_vm_page_deactivate(m, 0);
1703}
1704
1705/*
1706 * vm_page_try_to_cache:
1707 *
1708 * Returns 0 on failure, 1 on success
1709 */
1710int
1711vm_page_try_to_cache(vm_page_t m)
1712{
1713
1714	vm_page_lock_assert(m, MA_OWNED);
1715	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1716	if (m->dirty || m->hold_count || m->busy || m->wire_count ||
1717	    (m->oflags & VPO_BUSY) || (m->flags & PG_UNMANAGED))
1718		return (0);
1719	pmap_remove_all(m);
1720	if (m->dirty)
1721		return (0);
1722	vm_page_cache(m);
1723	return (1);
1724}
1725
1726/*
1727 * vm_page_try_to_free()
1728 *
1729 *	Attempt to free the page.  If we cannot free it, we do nothing.
1730 *	1 is returned on success, 0 on failure.
1731 */
1732int
1733vm_page_try_to_free(vm_page_t m)
1734{
1735
1736	vm_page_lock_assert(m, MA_OWNED);
1737	if (m->object != NULL)
1738		VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1739	if (m->dirty || m->hold_count || m->busy || m->wire_count ||
1740	    (m->oflags & VPO_BUSY) || (m->flags & PG_UNMANAGED))
1741		return (0);
1742	pmap_remove_all(m);
1743	if (m->dirty)
1744		return (0);
1745	vm_page_free(m);
1746	return (1);
1747}
1748
1749/*
1750 * vm_page_cache
1751 *
1752 * Put the specified page onto the page cache queue (if appropriate).
1753 *
1754 * This routine may not block.
1755 */
1756void
1757vm_page_cache(vm_page_t m)
1758{
1759	vm_object_t object;
1760	vm_page_t root;
1761
1762	vm_page_lock_assert(m, MA_OWNED);
1763	object = m->object;
1764	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1765	if ((m->flags & PG_UNMANAGED) || (m->oflags & VPO_BUSY) || m->busy ||
1766	    m->hold_count || m->wire_count)
1767		panic("vm_page_cache: attempting to cache busy page");
1768	pmap_remove_all(m);
1769	if (m->dirty != 0)
1770		panic("vm_page_cache: page %p is dirty", m);
1771	if (m->valid == 0 || object->type == OBJT_DEFAULT ||
1772	    (object->type == OBJT_SWAP &&
1773	    !vm_pager_has_page(object, m->pindex, NULL, NULL))) {
1774		/*
1775		 * Hypothesis: A cache-elgible page belonging to a
1776		 * default object or swap object but without a backing
1777		 * store must be zero filled.
1778		 */
1779		vm_page_free(m);
1780		return;
1781	}
1782	KASSERT((m->flags & PG_CACHED) == 0,
1783	    ("vm_page_cache: page %p is already cached", m));
1784	PCPU_INC(cnt.v_tcached);
1785
1786	/*
1787	 * Remove the page from the paging queues.
1788	 */
1789	vm_pageq_remove(m);
1790
1791	/*
1792	 * Remove the page from the object's collection of resident
1793	 * pages.
1794	 */
1795	if (m != object->root)
1796		vm_page_splay(m->pindex, object->root);
1797	if (m->left == NULL)
1798		root = m->right;
1799	else {
1800		root = vm_page_splay(m->pindex, m->left);
1801		root->right = m->right;
1802	}
1803	object->root = root;
1804	TAILQ_REMOVE(&object->memq, m, listq);
1805	object->resident_page_count--;
1806	object->generation++;
1807
1808	/*
1809	 * Restore the default memory attribute to the page.
1810	 */
1811	if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
1812		pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
1813
1814	/*
1815	 * Insert the page into the object's collection of cached pages
1816	 * and the physical memory allocator's cache/free page queues.
1817	 */
1818	m->flags &= ~PG_ZERO;
1819	mtx_lock(&vm_page_queue_free_mtx);
1820	m->flags |= PG_CACHED;
1821	cnt.v_cache_count++;
1822	root = object->cache;
1823	if (root == NULL) {
1824		m->left = NULL;
1825		m->right = NULL;
1826	} else {
1827		root = vm_page_splay(m->pindex, root);
1828		if (m->pindex < root->pindex) {
1829			m->left = root->left;
1830			m->right = root;
1831			root->left = NULL;
1832		} else if (__predict_false(m->pindex == root->pindex))
1833			panic("vm_page_cache: offset already cached");
1834		else {
1835			m->right = root->right;
1836			m->left = root;
1837			root->right = NULL;
1838		}
1839	}
1840	object->cache = m;
1841#if VM_NRESERVLEVEL > 0
1842	if (!vm_reserv_free_page(m)) {
1843#else
1844	if (TRUE) {
1845#endif
1846		vm_phys_set_pool(VM_FREEPOOL_CACHE, m, 0);
1847		vm_phys_free_pages(m, 0);
1848	}
1849	vm_page_free_wakeup();
1850	mtx_unlock(&vm_page_queue_free_mtx);
1851
1852	/*
1853	 * Increment the vnode's hold count if this is the object's only
1854	 * cached page.  Decrement the vnode's hold count if this was
1855	 * the object's only resident page.
1856	 */
1857	if (object->type == OBJT_VNODE) {
1858		if (root == NULL && object->resident_page_count != 0)
1859			vhold(object->handle);
1860		else if (root != NULL && object->resident_page_count == 0)
1861			vdrop(object->handle);
1862	}
1863}
1864
1865/*
1866 * vm_page_dontneed
1867 *
1868 *	Cache, deactivate, or do nothing as appropriate.  This routine
1869 *	is typically used by madvise() MADV_DONTNEED.
1870 *
1871 *	Generally speaking we want to move the page into the cache so
1872 *	it gets reused quickly.  However, this can result in a silly syndrome
1873 *	due to the page recycling too quickly.  Small objects will not be
1874 *	fully cached.  On the otherhand, if we move the page to the inactive
1875 *	queue we wind up with a problem whereby very large objects
1876 *	unnecessarily blow away our inactive and cache queues.
1877 *
1878 *	The solution is to move the pages based on a fixed weighting.  We
1879 *	either leave them alone, deactivate them, or move them to the cache,
1880 *	where moving them to the cache has the highest weighting.
1881 *	By forcing some pages into other queues we eventually force the
1882 *	system to balance the queues, potentially recovering other unrelated
1883 *	space from active.  The idea is to not force this to happen too
1884 *	often.
1885 */
1886void
1887vm_page_dontneed(vm_page_t m)
1888{
1889	int dnw;
1890	int head;
1891
1892	vm_page_lock_assert(m, MA_OWNED);
1893	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1894	dnw = PCPU_GET(dnweight);
1895	PCPU_INC(dnweight);
1896
1897	/*
1898	 * Occasionally leave the page alone.
1899	 */
1900	if ((dnw & 0x01F0) == 0 ||
1901	    VM_PAGE_INQUEUE2(m, PQ_INACTIVE)) {
1902		if (m->act_count >= ACT_INIT)
1903			--m->act_count;
1904		return;
1905	}
1906
1907	/*
1908	 * Clear any references to the page.  Otherwise, the page daemon will
1909	 * immediately reactivate the page.
1910	 *
1911	 * Perform the pmap_clear_reference() first.  Otherwise, a concurrent
1912	 * pmap operation, such as pmap_remove(), could clear a reference in
1913	 * the pmap and set PG_REFERENCED on the page before the
1914	 * pmap_clear_reference() had completed.  Consequently, the page would
1915	 * appear referenced based upon an old reference that occurred before
1916	 * this function ran.
1917	 */
1918	pmap_clear_reference(m);
1919	vm_page_lock_queues();
1920	vm_page_flag_clear(m, PG_REFERENCED);
1921	vm_page_unlock_queues();
1922
1923	if (m->dirty == 0 && pmap_is_modified(m))
1924		vm_page_dirty(m);
1925
1926	if (m->dirty || (dnw & 0x0070) == 0) {
1927		/*
1928		 * Deactivate the page 3 times out of 32.
1929		 */
1930		head = 0;
1931	} else {
1932		/*
1933		 * Cache the page 28 times out of every 32.  Note that
1934		 * the page is deactivated instead of cached, but placed
1935		 * at the head of the queue instead of the tail.
1936		 */
1937		head = 1;
1938	}
1939	_vm_page_deactivate(m, head);
1940}
1941
1942/*
1943 * Grab a page, waiting until we are waken up due to the page
1944 * changing state.  We keep on waiting, if the page continues
1945 * to be in the object.  If the page doesn't exist, first allocate it
1946 * and then conditionally zero it.
1947 *
1948 * This routine may block.
1949 */
1950vm_page_t
1951vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
1952{
1953	vm_page_t m;
1954
1955	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1956retrylookup:
1957	if ((m = vm_page_lookup(object, pindex)) != NULL) {
1958		if ((m->oflags & VPO_BUSY) != 0 || m->busy != 0) {
1959			if ((allocflags & VM_ALLOC_RETRY) != 0) {
1960				/*
1961				 * Reference the page before unlocking and
1962				 * sleeping so that the page daemon is less
1963				 * likely to reclaim it.
1964				 */
1965				vm_page_lock_queues();
1966				vm_page_flag_set(m, PG_REFERENCED);
1967			}
1968			vm_page_sleep(m, "pgrbwt");
1969			if ((allocflags & VM_ALLOC_RETRY) == 0)
1970				return (NULL);
1971			goto retrylookup;
1972		} else {
1973			if ((allocflags & VM_ALLOC_WIRED) != 0) {
1974				vm_page_lock(m);
1975				vm_page_wire(m);
1976				vm_page_unlock(m);
1977			}
1978			if ((allocflags & VM_ALLOC_NOBUSY) == 0)
1979				vm_page_busy(m);
1980			return (m);
1981		}
1982	}
1983	m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_RETRY);
1984	if (m == NULL) {
1985		VM_OBJECT_UNLOCK(object);
1986		VM_WAIT;
1987		VM_OBJECT_LOCK(object);
1988		if ((allocflags & VM_ALLOC_RETRY) == 0)
1989			return (NULL);
1990		goto retrylookup;
1991	} else if (m->valid != 0)
1992		return (m);
1993	if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
1994		pmap_zero_page(m);
1995	return (m);
1996}
1997
1998/*
1999 * Mapping function for valid bits or for dirty bits in
2000 * a page.  May not block.
2001 *
2002 * Inputs are required to range within a page.
2003 */
2004int
2005vm_page_bits(int base, int size)
2006{
2007	int first_bit;
2008	int last_bit;
2009
2010	KASSERT(
2011	    base + size <= PAGE_SIZE,
2012	    ("vm_page_bits: illegal base/size %d/%d", base, size)
2013	);
2014
2015	if (size == 0)		/* handle degenerate case */
2016		return (0);
2017
2018	first_bit = base >> DEV_BSHIFT;
2019	last_bit = (base + size - 1) >> DEV_BSHIFT;
2020
2021	return ((2 << last_bit) - (1 << first_bit));
2022}
2023
2024/*
2025 *	vm_page_set_valid:
2026 *
2027 *	Sets portions of a page valid.  The arguments are expected
2028 *	to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
2029 *	of any partial chunks touched by the range.  The invalid portion of
2030 *	such chunks will be zeroed.
2031 *
2032 *	(base + size) must be less then or equal to PAGE_SIZE.
2033 */
2034void
2035vm_page_set_valid(vm_page_t m, int base, int size)
2036{
2037	int endoff, frag;
2038
2039	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2040	if (size == 0)	/* handle degenerate case */
2041		return;
2042
2043	/*
2044	 * If the base is not DEV_BSIZE aligned and the valid
2045	 * bit is clear, we have to zero out a portion of the
2046	 * first block.
2047	 */
2048	if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
2049	    (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
2050		pmap_zero_page_area(m, frag, base - frag);
2051
2052	/*
2053	 * If the ending offset is not DEV_BSIZE aligned and the
2054	 * valid bit is clear, we have to zero out a portion of
2055	 * the last block.
2056	 */
2057	endoff = base + size;
2058	if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
2059	    (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
2060		pmap_zero_page_area(m, endoff,
2061		    DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
2062
2063	/*
2064	 * Assert that no previously invalid block that is now being validated
2065	 * is already dirty.
2066	 */
2067	KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0,
2068	    ("vm_page_set_valid: page %p is dirty", m));
2069
2070	/*
2071	 * Set valid bits inclusive of any overlap.
2072	 */
2073	m->valid |= vm_page_bits(base, size);
2074}
2075
2076/*
2077 * Clear the given bits from the specified page's dirty field.
2078 */
2079static __inline void
2080vm_page_clear_dirty_mask(vm_page_t m, int pagebits)
2081{
2082
2083	/*
2084	 * If the object is locked and the page is neither VPO_BUSY nor
2085	 * PG_WRITEABLE, then the page's dirty field cannot possibly be
2086	 * modified by a concurrent pmap operation.
2087	 */
2088	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2089	if ((m->oflags & VPO_BUSY) == 0 && (m->flags & PG_WRITEABLE) == 0)
2090		m->dirty &= ~pagebits;
2091	else {
2092		vm_page_lock_queues();
2093		m->dirty &= ~pagebits;
2094		vm_page_unlock_queues();
2095	}
2096}
2097
2098/*
2099 *	vm_page_set_validclean:
2100 *
2101 *	Sets portions of a page valid and clean.  The arguments are expected
2102 *	to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
2103 *	of any partial chunks touched by the range.  The invalid portion of
2104 *	such chunks will be zero'd.
2105 *
2106 *	This routine may not block.
2107 *
2108 *	(base + size) must be less then or equal to PAGE_SIZE.
2109 */
2110void
2111vm_page_set_validclean(vm_page_t m, int base, int size)
2112{
2113	u_long oldvalid;
2114	int endoff, frag, pagebits;
2115
2116	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2117	if (size == 0)	/* handle degenerate case */
2118		return;
2119
2120	/*
2121	 * If the base is not DEV_BSIZE aligned and the valid
2122	 * bit is clear, we have to zero out a portion of the
2123	 * first block.
2124	 */
2125	if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
2126	    (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
2127		pmap_zero_page_area(m, frag, base - frag);
2128
2129	/*
2130	 * If the ending offset is not DEV_BSIZE aligned and the
2131	 * valid bit is clear, we have to zero out a portion of
2132	 * the last block.
2133	 */
2134	endoff = base + size;
2135	if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
2136	    (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
2137		pmap_zero_page_area(m, endoff,
2138		    DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
2139
2140	/*
2141	 * Set valid, clear dirty bits.  If validating the entire
2142	 * page we can safely clear the pmap modify bit.  We also
2143	 * use this opportunity to clear the VPO_NOSYNC flag.  If a process
2144	 * takes a write fault on a MAP_NOSYNC memory area the flag will
2145	 * be set again.
2146	 *
2147	 * We set valid bits inclusive of any overlap, but we can only
2148	 * clear dirty bits for DEV_BSIZE chunks that are fully within
2149	 * the range.
2150	 */
2151	oldvalid = m->valid;
2152	pagebits = vm_page_bits(base, size);
2153	m->valid |= pagebits;
2154#if 0	/* NOT YET */
2155	if ((frag = base & (DEV_BSIZE - 1)) != 0) {
2156		frag = DEV_BSIZE - frag;
2157		base += frag;
2158		size -= frag;
2159		if (size < 0)
2160			size = 0;
2161	}
2162	pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
2163#endif
2164	if (base == 0 && size == PAGE_SIZE) {
2165		/*
2166		 * The page can only be modified within the pmap if it is
2167		 * mapped, and it can only be mapped if it was previously
2168		 * fully valid.
2169		 */
2170		if (oldvalid == VM_PAGE_BITS_ALL)
2171			/*
2172			 * Perform the pmap_clear_modify() first.  Otherwise,
2173			 * a concurrent pmap operation, such as
2174			 * pmap_protect(), could clear a modification in the
2175			 * pmap and set the dirty field on the page before
2176			 * pmap_clear_modify() had begun and after the dirty
2177			 * field was cleared here.
2178			 */
2179			pmap_clear_modify(m);
2180		m->dirty = 0;
2181		m->oflags &= ~VPO_NOSYNC;
2182	} else if (oldvalid != VM_PAGE_BITS_ALL)
2183		m->dirty &= ~pagebits;
2184	else
2185		vm_page_clear_dirty_mask(m, pagebits);
2186}
2187
2188void
2189vm_page_clear_dirty(vm_page_t m, int base, int size)
2190{
2191
2192	vm_page_clear_dirty_mask(m, vm_page_bits(base, size));
2193}
2194
2195/*
2196 *	vm_page_set_invalid:
2197 *
2198 *	Invalidates DEV_BSIZE'd chunks within a page.  Both the
2199 *	valid and dirty bits for the effected areas are cleared.
2200 *
2201 *	May not block.
2202 */
2203void
2204vm_page_set_invalid(vm_page_t m, int base, int size)
2205{
2206	int bits;
2207
2208	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2209	KASSERT((m->oflags & VPO_BUSY) == 0,
2210	    ("vm_page_set_invalid: page %p is busy", m));
2211	bits = vm_page_bits(base, size);
2212	if (m->valid == VM_PAGE_BITS_ALL && bits != 0)
2213		pmap_remove_all(m);
2214	KASSERT(!pmap_page_is_mapped(m),
2215	    ("vm_page_set_invalid: page %p is mapped", m));
2216	m->valid &= ~bits;
2217	m->dirty &= ~bits;
2218	m->object->generation++;
2219}
2220
2221/*
2222 * vm_page_zero_invalid()
2223 *
2224 *	The kernel assumes that the invalid portions of a page contain
2225 *	garbage, but such pages can be mapped into memory by user code.
2226 *	When this occurs, we must zero out the non-valid portions of the
2227 *	page so user code sees what it expects.
2228 *
2229 *	Pages are most often semi-valid when the end of a file is mapped
2230 *	into memory and the file's size is not page aligned.
2231 */
2232void
2233vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
2234{
2235	int b;
2236	int i;
2237
2238	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2239	/*
2240	 * Scan the valid bits looking for invalid sections that
2241	 * must be zerod.  Invalid sub-DEV_BSIZE'd areas ( where the
2242	 * valid bit may be set ) have already been zerod by
2243	 * vm_page_set_validclean().
2244	 */
2245	for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
2246		if (i == (PAGE_SIZE / DEV_BSIZE) ||
2247		    (m->valid & (1 << i))
2248		) {
2249			if (i > b) {
2250				pmap_zero_page_area(m,
2251				    b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
2252			}
2253			b = i + 1;
2254		}
2255	}
2256
2257	/*
2258	 * setvalid is TRUE when we can safely set the zero'd areas
2259	 * as being valid.  We can do this if there are no cache consistancy
2260	 * issues.  e.g. it is ok to do with UFS, but not ok to do with NFS.
2261	 */
2262	if (setvalid)
2263		m->valid = VM_PAGE_BITS_ALL;
2264}
2265
2266/*
2267 *	vm_page_is_valid:
2268 *
2269 *	Is (partial) page valid?  Note that the case where size == 0
2270 *	will return FALSE in the degenerate case where the page is
2271 *	entirely invalid, and TRUE otherwise.
2272 *
2273 *	May not block.
2274 */
2275int
2276vm_page_is_valid(vm_page_t m, int base, int size)
2277{
2278	int bits = vm_page_bits(base, size);
2279
2280	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2281	if (m->valid && ((m->valid & bits) == bits))
2282		return 1;
2283	else
2284		return 0;
2285}
2286
2287/*
2288 * update dirty bits from pmap/mmu.  May not block.
2289 */
2290void
2291vm_page_test_dirty(vm_page_t m)
2292{
2293
2294	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2295	if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m))
2296		vm_page_dirty(m);
2297}
2298
2299int so_zerocp_fullpage = 0;
2300
2301/*
2302 *	Replace the given page with a copy.  The copied page assumes
2303 *	the portion of the given page's "wire_count" that is not the
2304 *	responsibility of this copy-on-write mechanism.
2305 *
2306 *	The object containing the given page must have a non-zero
2307 *	paging-in-progress count and be locked.
2308 */
2309void
2310vm_page_cowfault(vm_page_t m)
2311{
2312	vm_page_t mnew;
2313	vm_object_t object;
2314	vm_pindex_t pindex;
2315
2316	mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED);
2317	vm_page_lock_assert(m, MA_OWNED);
2318	object = m->object;
2319	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2320	KASSERT(object->paging_in_progress != 0,
2321	    ("vm_page_cowfault: object %p's paging-in-progress count is zero.",
2322	    object));
2323	pindex = m->pindex;
2324
2325 retry_alloc:
2326	pmap_remove_all(m);
2327	vm_page_remove(m);
2328	mnew = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY);
2329	if (mnew == NULL) {
2330		vm_page_insert(m, object, pindex);
2331		vm_page_unlock(m);
2332		VM_OBJECT_UNLOCK(object);
2333		VM_WAIT;
2334		VM_OBJECT_LOCK(object);
2335		if (m == vm_page_lookup(object, pindex)) {
2336			vm_page_lock(m);
2337			goto retry_alloc;
2338		} else {
2339			/*
2340			 * Page disappeared during the wait.
2341			 */
2342			return;
2343		}
2344	}
2345
2346	if (m->cow == 0) {
2347		/*
2348		 * check to see if we raced with an xmit complete when
2349		 * waiting to allocate a page.  If so, put things back
2350		 * the way they were
2351		 */
2352		vm_page_unlock(m);
2353		vm_page_lock(mnew);
2354		vm_page_free(mnew);
2355		vm_page_unlock(mnew);
2356		vm_page_insert(m, object, pindex);
2357	} else { /* clear COW & copy page */
2358		if (!so_zerocp_fullpage)
2359			pmap_copy_page(m, mnew);
2360		mnew->valid = VM_PAGE_BITS_ALL;
2361		vm_page_dirty(mnew);
2362		mnew->wire_count = m->wire_count - m->cow;
2363		m->wire_count = m->cow;
2364		vm_page_unlock(m);
2365	}
2366}
2367
2368void
2369vm_page_cowclear(vm_page_t m)
2370{
2371
2372	vm_page_lock_assert(m, MA_OWNED);
2373	if (m->cow) {
2374		m->cow--;
2375		/*
2376		 * let vm_fault add back write permission  lazily
2377		 */
2378	}
2379	/*
2380	 *  sf_buf_free() will free the page, so we needn't do it here
2381	 */
2382}
2383
2384int
2385vm_page_cowsetup(vm_page_t m)
2386{
2387
2388	vm_page_lock_assert(m, MA_OWNED);
2389	if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0 ||
2390	    m->cow == USHRT_MAX - 1 || !VM_OBJECT_TRYLOCK(m->object))
2391		return (EBUSY);
2392	m->cow++;
2393	pmap_remove_write(m);
2394	VM_OBJECT_UNLOCK(m->object);
2395	return (0);
2396}
2397
2398#include "opt_ddb.h"
2399#ifdef DDB
2400#include <sys/kernel.h>
2401
2402#include <ddb/ddb.h>
2403
2404DB_SHOW_COMMAND(page, vm_page_print_page_info)
2405{
2406	db_printf("cnt.v_free_count: %d\n", cnt.v_free_count);
2407	db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count);
2408	db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count);
2409	db_printf("cnt.v_active_count: %d\n", cnt.v_active_count);
2410	db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count);
2411	db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved);
2412	db_printf("cnt.v_free_min: %d\n", cnt.v_free_min);
2413	db_printf("cnt.v_free_target: %d\n", cnt.v_free_target);
2414	db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min);
2415	db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target);
2416}
2417
2418DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
2419{
2420
2421	db_printf("PQ_FREE:");
2422	db_printf(" %d", cnt.v_free_count);
2423	db_printf("\n");
2424
2425	db_printf("PQ_CACHE:");
2426	db_printf(" %d", cnt.v_cache_count);
2427	db_printf("\n");
2428
2429	db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n",
2430		*vm_page_queues[PQ_ACTIVE].cnt,
2431		*vm_page_queues[PQ_INACTIVE].cnt);
2432}
2433#endif /* DDB */
2434