xref: /freebsd/sys/vm/vm_pageout.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
1 /*
2  * Copyright (c) 1991 Regents of the University of California.
3  * All rights reserved.
4  * Copyright (c) 1994 John S. Dyson
5  * All rights reserved.
6  * Copyright (c) 1994 David Greenman
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * The Mach Operating System project at Carnegie-Mellon University.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	from: @(#)vm_pageout.c	7.4 (Berkeley) 5/7/91
41  *
42  *
43  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
44  * All rights reserved.
45  *
46  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
47  *
48  * Permission to use, copy, modify and distribute this software and
49  * its documentation is hereby granted, provided that both the copyright
50  * notice and this permission notice appear in all copies of the
51  * software, derivative works or modified versions, and any portions
52  * thereof, and that both notices appear in supporting documentation.
53  *
54  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
55  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
56  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
57  *
58  * Carnegie Mellon requests users of this software to return to
59  *
60  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
61  *  School of Computer Science
62  *  Carnegie Mellon University
63  *  Pittsburgh PA 15213-3890
64  *
65  * any improvements or extensions that they make and grant Carnegie the
66  * rights to redistribute these changes.
67  *
68  * $FreeBSD$
69  */
70 
71 /*
72  *	The proverbial page-out daemon.
73  */
74 
75 #include "opt_vm.h"
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/proc.h>
80 #include <sys/kthread.h>
81 #include <sys/resourcevar.h>
82 #include <sys/signalvar.h>
83 #include <sys/vnode.h>
84 #include <sys/vmmeter.h>
85 #include <sys/sysctl.h>
86 
87 #include <vm/vm.h>
88 #include <vm/vm_param.h>
89 #include <vm/vm_prot.h>
90 #include <sys/lock.h>
91 #include <vm/vm_object.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_pageout.h>
95 #include <vm/vm_pager.h>
96 #include <vm/swap_pager.h>
97 #include <vm/vm_extern.h>
98 
99 /*
100  * System initialization
101  */
102 
103 /* the kernel process "vm_pageout"*/
104 static void vm_pageout __P((void));
105 static int vm_pageout_clean __P((vm_page_t));
106 static int vm_pageout_scan __P((void));
107 static int vm_pageout_free_page_calc __P((vm_size_t count));
108 struct proc *pageproc;
109 
110 static struct kproc_desc page_kp = {
111 	"pagedaemon",
112 	vm_pageout,
113 	&pageproc
114 };
115 SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp)
116 
117 #if !defined(NO_SWAPPING)
118 /* the kernel process "vm_daemon"*/
119 static void vm_daemon __P((void));
120 static struct	proc *vmproc;
121 
122 static struct kproc_desc vm_kp = {
123 	"vmdaemon",
124 	vm_daemon,
125 	&vmproc
126 };
127 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp)
128 #endif
129 
130 
131 int vm_pages_needed=0;		/* Event on which pageout daemon sleeps */
132 int vm_pageout_deficit=0;	/* Estimated number of pages deficit */
133 int vm_pageout_pages_needed=0;	/* flag saying that the pageout daemon needs pages */
134 
135 #if !defined(NO_SWAPPING)
136 static int vm_pageout_req_swapout;	/* XXX */
137 static int vm_daemon_needed;
138 #endif
139 extern int vm_swap_size;
140 static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
141 static int vm_pageout_full_stats_interval = 0;
142 static int vm_pageout_stats_free_max=0, vm_pageout_algorithm_lru=0;
143 static int defer_swap_pageouts=0;
144 static int disable_swap_pageouts=0;
145 
146 static int max_page_launder=100;
147 #if defined(NO_SWAPPING)
148 static int vm_swap_enabled=0;
149 static int vm_swap_idle_enabled=0;
150 #else
151 static int vm_swap_enabled=1;
152 static int vm_swap_idle_enabled=0;
153 #endif
154 
155 SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
156 	CTLFLAG_RW, &vm_pageout_algorithm_lru, 0, "LRU page mgmt");
157 
158 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
159 	CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
160 
161 SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
162 	CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
163 
164 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
165 	CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
166 
167 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max,
168 	CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented");
169 
170 #if defined(NO_SWAPPING)
171 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
172 	CTLFLAG_RD, &vm_swap_enabled, 0, "");
173 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
174 	CTLFLAG_RD, &vm_swap_idle_enabled, 0, "");
175 #else
176 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
177 	CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
178 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
179 	CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
180 #endif
181 
182 SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
183 	CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
184 
185 SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
186 	CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
187 
188 SYSCTL_INT(_vm, OID_AUTO, max_page_launder,
189 	CTLFLAG_RW, &max_page_launder, 0, "Maximum number of pages to clean per pass");
190 
191 
192 #define VM_PAGEOUT_PAGE_COUNT 16
193 int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
194 
195 int vm_page_max_wired;		/* XXX max # of wired pages system-wide */
196 
197 #if !defined(NO_SWAPPING)
198 typedef void freeer_fcn_t __P((vm_map_t, vm_object_t, vm_pindex_t, int));
199 static void vm_pageout_map_deactivate_pages __P((vm_map_t, vm_pindex_t));
200 static freeer_fcn_t vm_pageout_object_deactivate_pages;
201 static void vm_req_vmdaemon __P((void));
202 #endif
203 static void vm_pageout_page_stats(void);
204 
205 /*
206  * vm_pageout_clean:
207  *
208  * Clean the page and remove it from the laundry.
209  *
210  * We set the busy bit to cause potential page faults on this page to
211  * block.  Note the careful timing, however, the busy bit isn't set till
212  * late and we cannot do anything that will mess with the page.
213  */
214 
215 static int
216 vm_pageout_clean(m)
217 	vm_page_t m;
218 {
219 	register vm_object_t object;
220 	vm_page_t mc[2*vm_pageout_page_count];
221 	int pageout_count;
222 	int ib, is, page_base;
223 	vm_pindex_t pindex = m->pindex;
224 
225 	object = m->object;
226 
227 	/*
228 	 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
229 	 * with the new swapper, but we could have serious problems paging
230 	 * out other object types if there is insufficient memory.
231 	 *
232 	 * Unfortunately, checking free memory here is far too late, so the
233 	 * check has been moved up a procedural level.
234 	 */
235 
236 	/*
237 	 * Don't mess with the page if it's busy.
238 	 */
239 	if ((m->hold_count != 0) ||
240 	    ((m->busy != 0) || (m->flags & PG_BUSY)))
241 		return 0;
242 
243 	mc[vm_pageout_page_count] = m;
244 	pageout_count = 1;
245 	page_base = vm_pageout_page_count;
246 	ib = 1;
247 	is = 1;
248 
249 	/*
250 	 * Scan object for clusterable pages.
251 	 *
252 	 * We can cluster ONLY if: ->> the page is NOT
253 	 * clean, wired, busy, held, or mapped into a
254 	 * buffer, and one of the following:
255 	 * 1) The page is inactive, or a seldom used
256 	 *    active page.
257 	 * -or-
258 	 * 2) we force the issue.
259 	 *
260 	 * During heavy mmap/modification loads the pageout
261 	 * daemon can really fragment the underlying file
262 	 * due to flushing pages out of order and not trying
263 	 * align the clusters (which leave sporatic out-of-order
264 	 * holes).  To solve this problem we do the reverse scan
265 	 * first and attempt to align our cluster, then do a
266 	 * forward scan if room remains.
267 	 */
268 
269 more:
270 	while (ib && pageout_count < vm_pageout_page_count) {
271 		vm_page_t p;
272 
273 		if (ib > pindex) {
274 			ib = 0;
275 			break;
276 		}
277 
278 		if ((p = vm_page_lookup(object, pindex - ib)) == NULL) {
279 			ib = 0;
280 			break;
281 		}
282 		if (((p->queue - p->pc) == PQ_CACHE) ||
283 		    (p->flags & PG_BUSY) || p->busy) {
284 			ib = 0;
285 			break;
286 		}
287 		vm_page_test_dirty(p);
288 		if ((p->dirty & p->valid) == 0 ||
289 		    p->queue != PQ_INACTIVE ||
290 		    p->wire_count != 0 ||
291 		    p->hold_count != 0) {
292 			ib = 0;
293 			break;
294 		}
295 		mc[--page_base] = p;
296 		++pageout_count;
297 		++ib;
298 		/*
299 		 * alignment boundry, stop here and switch directions.  Do
300 		 * not clear ib.
301 		 */
302 		if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
303 			break;
304 	}
305 
306 	while (pageout_count < vm_pageout_page_count &&
307 	    pindex + is < object->size) {
308 		vm_page_t p;
309 
310 		if ((p = vm_page_lookup(object, pindex + is)) == NULL)
311 			break;
312 		if (((p->queue - p->pc) == PQ_CACHE) ||
313 		    (p->flags & PG_BUSY) || p->busy) {
314 			break;
315 		}
316 		vm_page_test_dirty(p);
317 		if ((p->dirty & p->valid) == 0 ||
318 		    p->queue != PQ_INACTIVE ||
319 		    p->wire_count != 0 ||
320 		    p->hold_count != 0) {
321 			break;
322 		}
323 		mc[page_base + pageout_count] = p;
324 		++pageout_count;
325 		++is;
326 	}
327 
328 	/*
329 	 * If we exhausted our forward scan, continue with the reverse scan
330 	 * when possible, even past a page boundry.  This catches boundry
331 	 * conditions.
332 	 */
333 	if (ib && pageout_count < vm_pageout_page_count)
334 		goto more;
335 
336 	/*
337 	 * we allow reads during pageouts...
338 	 */
339 	return vm_pageout_flush(&mc[page_base], pageout_count, 0);
340 }
341 
342 /*
343  * vm_pageout_flush() - launder the given pages
344  *
345  *	The given pages are laundered.  Note that we setup for the start of
346  *	I/O ( i.e. busy the page ), mark it read-only, and bump the object
347  *	reference count all in here rather then in the parent.  If we want
348  *	the parent to do more sophisticated things we may have to change
349  *	the ordering.
350  */
351 
352 int
353 vm_pageout_flush(mc, count, flags)
354 	vm_page_t *mc;
355 	int count;
356 	int flags;
357 {
358 	register vm_object_t object;
359 	int pageout_status[count];
360 	int numpagedout = 0;
361 	int i;
362 
363 	/*
364 	 * Initiate I/O.  Bump the vm_page_t->busy counter and
365 	 * mark the pages read-only.
366 	 *
367 	 * We do not have to fixup the clean/dirty bits here... we can
368 	 * allow the pager to do it after the I/O completes.
369 	 */
370 
371 	for (i = 0; i < count; i++) {
372 		vm_page_io_start(mc[i]);
373 		vm_page_protect(mc[i], VM_PROT_READ);
374 	}
375 
376 	object = mc[0]->object;
377 	vm_object_pip_add(object, count);
378 
379 	vm_pager_put_pages(object, mc, count,
380 	    (flags | ((object == kernel_object) ? OBJPC_SYNC : 0)),
381 	    pageout_status);
382 
383 	for (i = 0; i < count; i++) {
384 		vm_page_t mt = mc[i];
385 
386 		switch (pageout_status[i]) {
387 		case VM_PAGER_OK:
388 			numpagedout++;
389 			break;
390 		case VM_PAGER_PEND:
391 			numpagedout++;
392 			break;
393 		case VM_PAGER_BAD:
394 			/*
395 			 * Page outside of range of object. Right now we
396 			 * essentially lose the changes by pretending it
397 			 * worked.
398 			 */
399 			pmap_clear_modify(VM_PAGE_TO_PHYS(mt));
400 			vm_page_undirty(mt);
401 			break;
402 		case VM_PAGER_ERROR:
403 		case VM_PAGER_FAIL:
404 			/*
405 			 * If page couldn't be paged out, then reactivate the
406 			 * page so it doesn't clog the inactive list.  (We
407 			 * will try paging out it again later).
408 			 */
409 			vm_page_activate(mt);
410 			break;
411 		case VM_PAGER_AGAIN:
412 			break;
413 		}
414 
415 		/*
416 		 * If the operation is still going, leave the page busy to
417 		 * block all other accesses. Also, leave the paging in
418 		 * progress indicator set so that we don't attempt an object
419 		 * collapse.
420 		 */
421 		if (pageout_status[i] != VM_PAGER_PEND) {
422 			vm_object_pip_wakeup(object);
423 			vm_page_io_finish(mt);
424 		}
425 	}
426 	return numpagedout;
427 }
428 
429 #if !defined(NO_SWAPPING)
430 /*
431  *	vm_pageout_object_deactivate_pages
432  *
433  *	deactivate enough pages to satisfy the inactive target
434  *	requirements or if vm_page_proc_limit is set, then
435  *	deactivate all of the pages in the object and its
436  *	backing_objects.
437  *
438  *	The object and map must be locked.
439  */
440 static void
441 vm_pageout_object_deactivate_pages(map, object, desired, map_remove_only)
442 	vm_map_t map;
443 	vm_object_t object;
444 	vm_pindex_t desired;
445 	int map_remove_only;
446 {
447 	register vm_page_t p, next;
448 	int rcount;
449 	int remove_mode;
450 	int s;
451 
452 	if (object->type == OBJT_DEVICE)
453 		return;
454 
455 	while (object) {
456 		if (pmap_resident_count(vm_map_pmap(map)) <= desired)
457 			return;
458 		if (object->paging_in_progress)
459 			return;
460 
461 		remove_mode = map_remove_only;
462 		if (object->shadow_count > 1)
463 			remove_mode = 1;
464 	/*
465 	 * scan the objects entire memory queue
466 	 */
467 		rcount = object->resident_page_count;
468 		p = TAILQ_FIRST(&object->memq);
469 		while (p && (rcount-- > 0)) {
470 			int actcount;
471 			if (pmap_resident_count(vm_map_pmap(map)) <= desired)
472 				return;
473 			next = TAILQ_NEXT(p, listq);
474 			cnt.v_pdpages++;
475 			if (p->wire_count != 0 ||
476 			    p->hold_count != 0 ||
477 			    p->busy != 0 ||
478 			    (p->flags & PG_BUSY) ||
479 			    !pmap_page_exists(vm_map_pmap(map), VM_PAGE_TO_PHYS(p))) {
480 				p = next;
481 				continue;
482 			}
483 
484 			actcount = pmap_ts_referenced(VM_PAGE_TO_PHYS(p));
485 			if (actcount) {
486 				vm_page_flag_set(p, PG_REFERENCED);
487 			} else if (p->flags & PG_REFERENCED) {
488 				actcount = 1;
489 			}
490 
491 			if ((p->queue != PQ_ACTIVE) &&
492 				(p->flags & PG_REFERENCED)) {
493 				vm_page_activate(p);
494 				p->act_count += actcount;
495 				vm_page_flag_clear(p, PG_REFERENCED);
496 			} else if (p->queue == PQ_ACTIVE) {
497 				if ((p->flags & PG_REFERENCED) == 0) {
498 					p->act_count -= min(p->act_count, ACT_DECLINE);
499 					if (!remove_mode && (vm_pageout_algorithm_lru || (p->act_count == 0))) {
500 						vm_page_protect(p, VM_PROT_NONE);
501 						vm_page_deactivate(p);
502 					} else {
503 						s = splvm();
504 						TAILQ_REMOVE(&vm_page_queue_active, p, pageq);
505 						TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq);
506 						splx(s);
507 					}
508 				} else {
509 					vm_page_activate(p);
510 					vm_page_flag_clear(p, PG_REFERENCED);
511 					if (p->act_count < (ACT_MAX - ACT_ADVANCE))
512 						p->act_count += ACT_ADVANCE;
513 					s = splvm();
514 					TAILQ_REMOVE(&vm_page_queue_active, p, pageq);
515 					TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq);
516 					splx(s);
517 				}
518 			} else if (p->queue == PQ_INACTIVE) {
519 				vm_page_protect(p, VM_PROT_NONE);
520 			}
521 			p = next;
522 		}
523 		object = object->backing_object;
524 	}
525 	return;
526 }
527 
528 /*
529  * deactivate some number of pages in a map, try to do it fairly, but
530  * that is really hard to do.
531  */
532 static void
533 vm_pageout_map_deactivate_pages(map, desired)
534 	vm_map_t map;
535 	vm_pindex_t desired;
536 {
537 	vm_map_entry_t tmpe;
538 	vm_object_t obj, bigobj;
539 
540 	if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT, (void *)0, curproc)) {
541 		return;
542 	}
543 
544 	bigobj = NULL;
545 
546 	/*
547 	 * first, search out the biggest object, and try to free pages from
548 	 * that.
549 	 */
550 	tmpe = map->header.next;
551 	while (tmpe != &map->header) {
552 		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
553 			obj = tmpe->object.vm_object;
554 			if ((obj != NULL) && (obj->shadow_count <= 1) &&
555 				((bigobj == NULL) ||
556 				 (bigobj->resident_page_count < obj->resident_page_count))) {
557 				bigobj = obj;
558 			}
559 		}
560 		tmpe = tmpe->next;
561 	}
562 
563 	if (bigobj)
564 		vm_pageout_object_deactivate_pages(map, bigobj, desired, 0);
565 
566 	/*
567 	 * Next, hunt around for other pages to deactivate.  We actually
568 	 * do this search sort of wrong -- .text first is not the best idea.
569 	 */
570 	tmpe = map->header.next;
571 	while (tmpe != &map->header) {
572 		if (pmap_resident_count(vm_map_pmap(map)) <= desired)
573 			break;
574 		if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
575 			obj = tmpe->object.vm_object;
576 			if (obj)
577 				vm_pageout_object_deactivate_pages(map, obj, desired, 0);
578 		}
579 		tmpe = tmpe->next;
580 	};
581 
582 	/*
583 	 * Remove all mappings if a process is swapped out, this will free page
584 	 * table pages.
585 	 */
586 	if (desired == 0)
587 		pmap_remove(vm_map_pmap(map),
588 			VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS);
589 	vm_map_unlock(map);
590 	return;
591 }
592 #endif
593 
594 /*
595  * Don't try to be fancy - being fancy can lead to VOP_LOCK's and therefore
596  * to vnode deadlocks.  We only do it for OBJT_DEFAULT and OBJT_SWAP objects
597  * which we know can be trivially freed.
598  */
599 
600 void
601 vm_pageout_page_free(vm_page_t m) {
602 	vm_object_t object = m->object;
603 	int type = object->type;
604 
605 	if (type == OBJT_SWAP || type == OBJT_DEFAULT)
606 		vm_object_reference(object);
607 	vm_page_busy(m);
608 	vm_page_protect(m, VM_PROT_NONE);
609 	vm_page_free(m);
610 	if (type == OBJT_SWAP || type == OBJT_DEFAULT)
611 		vm_object_deallocate(object);
612 }
613 
614 /*
615  *	vm_pageout_scan does the dirty work for the pageout daemon.
616  */
617 static int
618 vm_pageout_scan()
619 {
620 	vm_page_t m, next;
621 	int page_shortage, maxscan, pcount;
622 	int addl_page_shortage, addl_page_shortage_init;
623 	int maxlaunder;
624 	int launder_loop = 0;
625 	struct proc *p, *bigproc;
626 	vm_offset_t size, bigsize;
627 	vm_object_t object;
628 	int force_wakeup = 0;
629 	int actcount;
630 	int vnodes_skipped = 0;
631 	int s;
632 
633 	/*
634 	 * Do whatever cleanup that the pmap code can.
635 	 */
636 	pmap_collect();
637 
638 	addl_page_shortage_init = vm_pageout_deficit;
639 	vm_pageout_deficit = 0;
640 
641 	if (max_page_launder == 0)
642 		max_page_launder = 1;
643 
644 	/*
645 	 * Calculate the number of pages we want to either free or move
646 	 * to the cache.
647 	 */
648 
649 	page_shortage = vm_paging_target() + addl_page_shortage_init;
650 
651 	/*
652 	 * Figure out what to do with dirty pages when they are encountered.
653 	 * Assume that 1/3 of the pages on the inactive list are clean.  If
654 	 * we think we can reach our target, disable laundering (do not
655 	 * clean any dirty pages).  If we miss the target we will loop back
656 	 * up and do a laundering run.
657 	 */
658 
659 	if (cnt.v_inactive_count / 3 > page_shortage) {
660 		maxlaunder = 0;
661 		launder_loop = 0;
662 	} else {
663 		maxlaunder =
664 		    (cnt.v_inactive_target > max_page_launder) ?
665 		    max_page_launder : cnt.v_inactive_target;
666 		launder_loop = 1;
667 	}
668 
669 	/*
670 	 * Start scanning the inactive queue for pages we can move to the
671 	 * cache or free.  The scan will stop when the target is reached or
672 	 * we have scanned the entire inactive queue.
673 	 */
674 
675 rescan0:
676 	addl_page_shortage = addl_page_shortage_init;
677 	maxscan = cnt.v_inactive_count;
678 	for (m = TAILQ_FIRST(&vm_page_queue_inactive);
679 	     m != NULL && maxscan-- > 0 && page_shortage > 0;
680 	     m = next) {
681 
682 		cnt.v_pdpages++;
683 
684 		if (m->queue != PQ_INACTIVE) {
685 			goto rescan0;
686 		}
687 
688 		next = TAILQ_NEXT(m, pageq);
689 
690 		if (m->hold_count) {
691 			s = splvm();
692 			TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq);
693 			TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq);
694 			splx(s);
695 			addl_page_shortage++;
696 			continue;
697 		}
698 		/*
699 		 * Dont mess with busy pages, keep in the front of the
700 		 * queue, most likely are being paged out.
701 		 */
702 		if (m->busy || (m->flags & PG_BUSY)) {
703 			addl_page_shortage++;
704 			continue;
705 		}
706 
707 		/*
708 		 * If the object is not being used, we ignore previous
709 		 * references.
710 		 */
711 		if (m->object->ref_count == 0) {
712 			vm_page_flag_clear(m, PG_REFERENCED);
713 			pmap_clear_reference(VM_PAGE_TO_PHYS(m));
714 
715 		/*
716 		 * Otherwise, if the page has been referenced while in the
717 		 * inactive queue, we bump the "activation count" upwards,
718 		 * making it less likely that the page will be added back to
719 		 * the inactive queue prematurely again.  Here we check the
720 		 * page tables (or emulated bits, if any), given the upper
721 		 * level VM system not knowing anything about existing
722 		 * references.
723 		 */
724 		} else if (((m->flags & PG_REFERENCED) == 0) &&
725 			(actcount = pmap_ts_referenced(VM_PAGE_TO_PHYS(m)))) {
726 			vm_page_activate(m);
727 			m->act_count += (actcount + ACT_ADVANCE);
728 			continue;
729 		}
730 
731 		/*
732 		 * If the upper level VM system knows about any page
733 		 * references, we activate the page.  We also set the
734 		 * "activation count" higher than normal so that we will less
735 		 * likely place pages back onto the inactive queue again.
736 		 */
737 		if ((m->flags & PG_REFERENCED) != 0) {
738 			vm_page_flag_clear(m, PG_REFERENCED);
739 			actcount = pmap_ts_referenced(VM_PAGE_TO_PHYS(m));
740 			vm_page_activate(m);
741 			m->act_count += (actcount + ACT_ADVANCE + 1);
742 			continue;
743 		}
744 
745 		/*
746 		 * If the upper level VM system doesn't know anything about
747 		 * the page being dirty, we have to check for it again.  As
748 		 * far as the VM code knows, any partially dirty pages are
749 		 * fully dirty.
750 		 */
751 		if (m->dirty == 0) {
752 			vm_page_test_dirty(m);
753 		} else {
754 			vm_page_dirty(m);
755 		}
756 
757 		/*
758 		 * Invalid pages can be easily freed
759 		 */
760 		if (m->valid == 0) {
761 			vm_pageout_page_free(m);
762 			cnt.v_dfree++;
763 			--page_shortage;
764 
765 		/*
766 		 * Clean pages can be placed onto the cache queue.
767 		 */
768 		} else if (m->dirty == 0) {
769 			vm_page_cache(m);
770 			--page_shortage;
771 
772 		/*
773 		 * Dirty pages need to be paged out.  Note that we clean
774 		 * only a limited number of pages per pagedaemon pass.
775 		 */
776 		} else if (maxlaunder > 0) {
777 			int written;
778 			int swap_pageouts_ok;
779 			struct vnode *vp = NULL;
780 
781 			object = m->object;
782 
783 			if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
784 				swap_pageouts_ok = 1;
785 			} else {
786 				swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
787 				swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
788 				vm_page_count_min());
789 
790 			}
791 
792 			/*
793 			 * We don't bother paging objects that are "dead".
794 			 * Those objects are in a "rundown" state.
795 			 */
796 			if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
797 				s = splvm();
798 				TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq);
799 				TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq);
800 				splx(s);
801 				continue;
802 			}
803 
804 			/*
805 			 * For now we protect against potential memory
806 			 * deadlocks by requiring significant memory to be
807 			 * free if the object is not OBJT_DEFAULT or OBJT_SWAP.
808 			 * We do not 'trust' any other object type to operate
809 			 * with low memory, not even OBJT_DEVICE.  The VM
810 			 * allocator will special case allocations done by
811 			 * the pageout daemon so the check below actually
812 			 * does have some hysteresis in it.  It isn't the best
813 			 * solution, though.
814 			 */
815 
816 			if (object->type != OBJT_DEFAULT &&
817 			    object->type != OBJT_SWAP &&
818 			    cnt.v_free_count < cnt.v_free_reserved) {
819 				s = splvm();
820 				TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq);
821 				TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m,
822 				    pageq);
823 				splx(s);
824 				continue;
825 			}
826 
827 			/*
828 			 * Presumably we have sufficient free memory to do
829 			 * the more sophisticated checks and locking required
830 			 * for vnodes.
831 			 *
832 			 * The object is already known NOT to be dead.  The
833 			 * vget() may still block, though, because
834 			 * VOP_ISLOCKED() doesn't check to see if an inode
835 			 * (v_data) is associated with the vnode.  If it isn't,
836 			 * vget() will load in it from disk.  Worse, vget()
837 			 * may actually get stuck waiting on "inode" if another
838 			 * process is in the process of bringing the inode in.
839 			 * This is bad news for us either way.
840 			 *
841 			 * So for the moment we check v_data == NULL as a
842 			 * workaround.  This means that vnodes which do not
843 			 * use v_data in the way we expect probably will not
844 			 * wind up being paged out by the pager and it will be
845 			 * up to the syncer to get them.  That's better then
846 			 * us blocking here.
847 			 *
848 			 * This whole code section is bogus - we need to fix
849 			 * the vnode pager to handle vm_page_t's without us
850 			 * having to do any sophisticated VOP tests.
851 			 */
852 
853 			if (object->type == OBJT_VNODE) {
854 				vp = object->handle;
855 
856 				if (VOP_ISLOCKED(vp) ||
857 				    vp->v_data == NULL ||
858 				    vget(vp, LK_EXCLUSIVE|LK_NOOBJ, curproc)) {
859 					if ((m->queue == PQ_INACTIVE) &&
860 						(m->hold_count == 0) &&
861 						(m->busy == 0) &&
862 						(m->flags & PG_BUSY) == 0) {
863 						s = splvm();
864 						TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq);
865 						TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq);
866 						splx(s);
867 					}
868 					if (object->flags & OBJ_MIGHTBEDIRTY)
869 						vnodes_skipped++;
870 					continue;
871 				}
872 
873 				/*
874 				 * The page might have been moved to another queue
875 				 * during potential blocking in vget() above.
876 				 */
877 				if (m->queue != PQ_INACTIVE) {
878 					if (object->flags & OBJ_MIGHTBEDIRTY)
879 						vnodes_skipped++;
880 					vput(vp);
881 					continue;
882 				}
883 
884 				/*
885 				 * The page may have been busied during the blocking in
886 				 * vput();  We don't move the page back onto the end of
887 				 * the queue so that statistics are more correct if we don't.
888 				 */
889 				if (m->busy || (m->flags & PG_BUSY)) {
890 					vput(vp);
891 					continue;
892 				}
893 
894 				/*
895 				 * If the page has become held, then skip it
896 				 */
897 				if (m->hold_count) {
898 					s = splvm();
899 					TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq);
900 					TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq);
901 					splx(s);
902 					if (object->flags & OBJ_MIGHTBEDIRTY)
903 						vnodes_skipped++;
904 					vput(vp);
905 					continue;
906 				}
907 			}
908 
909 			/*
910 			 * If a page is dirty, then it is either being washed
911 			 * (but not yet cleaned) or it is still in the
912 			 * laundry.  If it is still in the laundry, then we
913 			 * start the cleaning operation.
914 			 */
915 			written = vm_pageout_clean(m);
916 			if (vp)
917 				vput(vp);
918 
919 			maxlaunder -= written;
920 		}
921 	}
922 
923 	/*
924 	 * If we still have a page shortage and we didn't launder anything,
925 	 * run the inactive scan again and launder something this time.
926 	 */
927 
928 	if (launder_loop == 0 && page_shortage > 0) {
929 		launder_loop = 1;
930 		maxlaunder =
931 		    (cnt.v_inactive_target > max_page_launder) ?
932 		    max_page_launder : cnt.v_inactive_target;
933 		goto rescan0;
934 	}
935 
936 	/*
937 	 * Compute the page shortage from the point of view of having to
938 	 * move pages from the active queue to the inactive queue.
939 	 */
940 
941 	page_shortage = (cnt.v_inactive_target + cnt.v_cache_min) -
942 	    (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
943 	page_shortage += addl_page_shortage;
944 
945 	/*
946 	 * Scan the active queue for things we can deactivate
947 	 */
948 
949 	pcount = cnt.v_active_count;
950 	m = TAILQ_FIRST(&vm_page_queue_active);
951 
952 	while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
953 
954 		/*
955 		 * This is a consistancy check, and should likely be a panic
956 		 * or warning.
957 		 */
958 		if (m->queue != PQ_ACTIVE) {
959 			break;
960 		}
961 
962 		next = TAILQ_NEXT(m, pageq);
963 		/*
964 		 * Don't deactivate pages that are busy.
965 		 */
966 		if ((m->busy != 0) ||
967 		    (m->flags & PG_BUSY) ||
968 		    (m->hold_count != 0)) {
969 			s = splvm();
970 			TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
971 			TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
972 			splx(s);
973 			m = next;
974 			continue;
975 		}
976 
977 		/*
978 		 * The count for pagedaemon pages is done after checking the
979 		 * page for eligbility...
980 		 */
981 		cnt.v_pdpages++;
982 
983 		/*
984 		 * Check to see "how much" the page has been used.
985 		 */
986 		actcount = 0;
987 		if (m->object->ref_count != 0) {
988 			if (m->flags & PG_REFERENCED) {
989 				actcount += 1;
990 			}
991 			actcount += pmap_ts_referenced(VM_PAGE_TO_PHYS(m));
992 			if (actcount) {
993 				m->act_count += ACT_ADVANCE + actcount;
994 				if (m->act_count > ACT_MAX)
995 					m->act_count = ACT_MAX;
996 			}
997 		}
998 
999 		/*
1000 		 * Since we have "tested" this bit, we need to clear it now.
1001 		 */
1002 		vm_page_flag_clear(m, PG_REFERENCED);
1003 
1004 		/*
1005 		 * Only if an object is currently being used, do we use the
1006 		 * page activation count stats.
1007 		 */
1008 		if (actcount && (m->object->ref_count != 0)) {
1009 			s = splvm();
1010 			TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
1011 			TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
1012 			splx(s);
1013 		} else {
1014 			m->act_count -= min(m->act_count, ACT_DECLINE);
1015 			if (vm_pageout_algorithm_lru ||
1016 				(m->object->ref_count == 0) || (m->act_count == 0)) {
1017 				page_shortage--;
1018 				if (m->object->ref_count == 0) {
1019 					vm_page_protect(m, VM_PROT_NONE);
1020 					if (m->dirty == 0)
1021 						vm_page_cache(m);
1022 					else
1023 						vm_page_deactivate(m);
1024 				} else {
1025 					vm_page_deactivate(m);
1026 				}
1027 			} else {
1028 				s = splvm();
1029 				TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
1030 				TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
1031 				splx(s);
1032 			}
1033 		}
1034 		m = next;
1035 	}
1036 
1037 	s = splvm();
1038 
1039 	/*
1040 	 * We try to maintain some *really* free pages, this allows interrupt
1041 	 * code to be guaranteed space.  Since both cache and free queues
1042 	 * are considered basically 'free', moving pages from cache to free
1043 	 * does not effect other calculations.
1044 	 */
1045 
1046 	while (cnt.v_free_count < cnt.v_free_reserved) {
1047 		static int cache_rover = 0;
1048 		m = vm_page_list_find(PQ_CACHE, cache_rover, FALSE);
1049 		if (!m)
1050 			break;
1051 		if ((m->flags & PG_BUSY) || m->busy || m->hold_count || m->wire_count) {
1052 #ifdef INVARIANTS
1053 			printf("Warning: busy page %p found in cache\n", m);
1054 #endif
1055 			vm_page_deactivate(m);
1056 			continue;
1057 		}
1058 		cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK;
1059 		vm_pageout_page_free(m);
1060 		cnt.v_dfree++;
1061 	}
1062 	splx(s);
1063 
1064 #if !defined(NO_SWAPPING)
1065 	/*
1066 	 * Idle process swapout -- run once per second.
1067 	 */
1068 	if (vm_swap_idle_enabled) {
1069 		static long lsec;
1070 		if (time_second != lsec) {
1071 			vm_pageout_req_swapout |= VM_SWAP_IDLE;
1072 			vm_req_vmdaemon();
1073 			lsec = time_second;
1074 		}
1075 	}
1076 #endif
1077 
1078 	/*
1079 	 * If we didn't get enough free pages, and we have skipped a vnode
1080 	 * in a writeable object, wakeup the sync daemon.  And kick swapout
1081 	 * if we did not get enough free pages.
1082 	 */
1083 	if (vm_paging_target() > 0) {
1084 		if (vnodes_skipped && vm_page_count_min())
1085 			(void) speedup_syncer();
1086 #if !defined(NO_SWAPPING)
1087 		if (vm_swap_enabled && vm_page_count_target()) {
1088 			vm_req_vmdaemon();
1089 			vm_pageout_req_swapout |= VM_SWAP_NORMAL;
1090 		}
1091 #endif
1092 	}
1093 
1094 	/*
1095 	 * make sure that we have swap space -- if we are low on memory and
1096 	 * swap -- then kill the biggest process.
1097 	 */
1098 	if ((vm_swap_size == 0 || swap_pager_full) && vm_page_count_min()) {
1099 		bigproc = NULL;
1100 		bigsize = 0;
1101 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1102 			/*
1103 			 * if this is a system process, skip it
1104 			 */
1105 			if ((p->p_flag & P_SYSTEM) || (p->p_lock > 0) ||
1106 			    (p->p_pid == 1) ||
1107 			    ((p->p_pid < 48) && (vm_swap_size != 0))) {
1108 				continue;
1109 			}
1110 			/*
1111 			 * if the process is in a non-running type state,
1112 			 * don't touch it.
1113 			 */
1114 			if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
1115 				continue;
1116 			}
1117 			/*
1118 			 * get the process size
1119 			 */
1120 			size = vmspace_resident_count(p->p_vmspace);
1121 			/*
1122 			 * if the this process is bigger than the biggest one
1123 			 * remember it.
1124 			 */
1125 			if (size > bigsize) {
1126 				bigproc = p;
1127 				bigsize = size;
1128 			}
1129 		}
1130 		if (bigproc != NULL) {
1131 			killproc(bigproc, "out of swap space");
1132 			bigproc->p_estcpu = 0;
1133 			bigproc->p_nice = PRIO_MIN;
1134 			resetpriority(bigproc);
1135 			wakeup(&cnt.v_free_count);
1136 		}
1137 	}
1138 	return force_wakeup;
1139 }
1140 
1141 /*
1142  * This routine tries to maintain the pseudo LRU active queue,
1143  * so that during long periods of time where there is no paging,
1144  * that some statistic accumlation still occurs.  This code
1145  * helps the situation where paging just starts to occur.
1146  */
1147 static void
1148 vm_pageout_page_stats()
1149 {
1150 	int s;
1151 	vm_page_t m,next;
1152 	int pcount,tpcount;		/* Number of pages to check */
1153 	static int fullintervalcount = 0;
1154 	int page_shortage;
1155 
1156 	page_shortage =
1157 	    (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) -
1158 	    (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
1159 
1160 	if (page_shortage <= 0)
1161 		return;
1162 
1163 	pcount = cnt.v_active_count;
1164 	fullintervalcount += vm_pageout_stats_interval;
1165 	if (fullintervalcount < vm_pageout_full_stats_interval) {
1166 		tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count;
1167 		if (pcount > tpcount)
1168 			pcount = tpcount;
1169 	}
1170 
1171 	m = TAILQ_FIRST(&vm_page_queue_active);
1172 	while ((m != NULL) && (pcount-- > 0)) {
1173 		int actcount;
1174 
1175 		if (m->queue != PQ_ACTIVE) {
1176 			break;
1177 		}
1178 
1179 		next = TAILQ_NEXT(m, pageq);
1180 		/*
1181 		 * Don't deactivate pages that are busy.
1182 		 */
1183 		if ((m->busy != 0) ||
1184 		    (m->flags & PG_BUSY) ||
1185 		    (m->hold_count != 0)) {
1186 			s = splvm();
1187 			TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
1188 			TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
1189 			splx(s);
1190 			m = next;
1191 			continue;
1192 		}
1193 
1194 		actcount = 0;
1195 		if (m->flags & PG_REFERENCED) {
1196 			vm_page_flag_clear(m, PG_REFERENCED);
1197 			actcount += 1;
1198 		}
1199 
1200 		actcount += pmap_ts_referenced(VM_PAGE_TO_PHYS(m));
1201 		if (actcount) {
1202 			m->act_count += ACT_ADVANCE + actcount;
1203 			if (m->act_count > ACT_MAX)
1204 				m->act_count = ACT_MAX;
1205 			s = splvm();
1206 			TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
1207 			TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
1208 			splx(s);
1209 		} else {
1210 			if (m->act_count == 0) {
1211 				/*
1212 				 * We turn off page access, so that we have more accurate
1213 				 * RSS stats.  We don't do this in the normal page deactivation
1214 				 * when the system is loaded VM wise, because the cost of
1215 				 * the large number of page protect operations would be higher
1216 				 * than the value of doing the operation.
1217 				 */
1218 				vm_page_protect(m, VM_PROT_NONE);
1219 				vm_page_deactivate(m);
1220 			} else {
1221 				m->act_count -= min(m->act_count, ACT_DECLINE);
1222 				s = splvm();
1223 				TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
1224 				TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
1225 				splx(s);
1226 			}
1227 		}
1228 
1229 		m = next;
1230 	}
1231 }
1232 
1233 static int
1234 vm_pageout_free_page_calc(count)
1235 vm_size_t count;
1236 {
1237 	if (count < cnt.v_page_count)
1238 		 return 0;
1239 	/*
1240 	 * free_reserved needs to include enough for the largest swap pager
1241 	 * structures plus enough for any pv_entry structs when paging.
1242 	 */
1243 	if (cnt.v_page_count > 1024)
1244 		cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
1245 	else
1246 		cnt.v_free_min = 4;
1247 	cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
1248 		cnt.v_interrupt_free_min;
1249 	cnt.v_free_reserved = vm_pageout_page_count +
1250 		cnt.v_pageout_free_min + (count / 768) + PQ_L2_SIZE;
1251 	cnt.v_free_severe = cnt.v_free_min / 2;
1252 	cnt.v_free_min += cnt.v_free_reserved;
1253 	cnt.v_free_severe += cnt.v_free_reserved;
1254 	return 1;
1255 }
1256 
1257 
1258 /*
1259  *	vm_pageout is the high level pageout daemon.
1260  */
1261 static void
1262 vm_pageout()
1263 {
1264 	/*
1265 	 * Initialize some paging parameters.
1266 	 */
1267 
1268 	cnt.v_interrupt_free_min = 2;
1269 	if (cnt.v_page_count < 2000)
1270 		vm_pageout_page_count = 8;
1271 
1272 	vm_pageout_free_page_calc(cnt.v_page_count);
1273 	/*
1274 	 * free_reserved needs to include enough for the largest swap pager
1275 	 * structures plus enough for any pv_entry structs when paging.
1276 	 */
1277 	if (cnt.v_free_count > 6144)
1278 		cnt.v_free_target = 3 * cnt.v_free_min + cnt.v_free_reserved;
1279 	else
1280 		cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved;
1281 
1282 	if (cnt.v_free_count > 2048) {
1283 		cnt.v_cache_min = cnt.v_free_target;
1284 		cnt.v_cache_max = 2 * cnt.v_cache_min;
1285 		cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
1286 	} else {
1287 		cnt.v_cache_min = 0;
1288 		cnt.v_cache_max = 0;
1289 		cnt.v_inactive_target = cnt.v_free_count / 4;
1290 	}
1291 	if (cnt.v_inactive_target > cnt.v_free_count / 3)
1292 		cnt.v_inactive_target = cnt.v_free_count / 3;
1293 
1294 	/* XXX does not really belong here */
1295 	if (vm_page_max_wired == 0)
1296 		vm_page_max_wired = cnt.v_free_count / 3;
1297 
1298 	if (vm_pageout_stats_max == 0)
1299 		vm_pageout_stats_max = cnt.v_free_target;
1300 
1301 	/*
1302 	 * Set interval in seconds for stats scan.
1303 	 */
1304 	if (vm_pageout_stats_interval == 0)
1305 		vm_pageout_stats_interval = 5;
1306 	if (vm_pageout_full_stats_interval == 0)
1307 		vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1308 
1309 
1310 	/*
1311 	 * Set maximum free per pass
1312 	 */
1313 	if (vm_pageout_stats_free_max == 0)
1314 		vm_pageout_stats_free_max = 5;
1315 
1316 	max_page_launder = (cnt.v_page_count > 1800 ? 32 : 16);
1317 
1318 	curproc->p_flag |= P_BUFEXHAUST;
1319 	swap_pager_swap_init();
1320 	/*
1321 	 * The pageout daemon is never done, so loop forever.
1322 	 */
1323 	while (TRUE) {
1324 		int error;
1325 		int s = splvm();
1326 
1327 		if (vm_pages_needed && vm_page_count_min()) {
1328 			/*
1329 			 * Still not done, sleep a bit and go again
1330 			 */
1331 			vm_pages_needed = 0;
1332 			tsleep(&vm_pages_needed, PVM, "psleep", hz/2);
1333 		} else {
1334 			/*
1335 			 * Good enough, sleep & handle stats
1336 			 */
1337 			vm_pages_needed = 0;
1338 			error = tsleep(&vm_pages_needed,
1339 				PVM, "psleep", vm_pageout_stats_interval * hz);
1340 			if (error && !vm_pages_needed) {
1341 				splx(s);
1342 				vm_pageout_page_stats();
1343 				continue;
1344 			}
1345 		}
1346 
1347 		if (vm_pages_needed)
1348 			cnt.v_pdwakeups++;
1349 		vm_pages_needed = 0;
1350 		splx(s);
1351 		vm_pageout_scan();
1352 		vm_pageout_deficit = 0;
1353 		wakeup(&cnt.v_free_count);
1354 	}
1355 }
1356 
1357 void
1358 pagedaemon_wakeup()
1359 {
1360 	if (!vm_pages_needed && curproc != pageproc) {
1361 		vm_pages_needed++;
1362 		wakeup(&vm_pages_needed);
1363 	}
1364 }
1365 
1366 #if !defined(NO_SWAPPING)
1367 static void
1368 vm_req_vmdaemon()
1369 {
1370 	static int lastrun = 0;
1371 
1372 	if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
1373 		wakeup(&vm_daemon_needed);
1374 		lastrun = ticks;
1375 	}
1376 }
1377 
1378 static void
1379 vm_daemon()
1380 {
1381 	struct proc *p;
1382 
1383 	while (TRUE) {
1384 		tsleep(&vm_daemon_needed, PPAUSE, "psleep", 0);
1385 		if (vm_pageout_req_swapout) {
1386 			swapout_procs(vm_pageout_req_swapout);
1387 			vm_pageout_req_swapout = 0;
1388 		}
1389 		/*
1390 		 * scan the processes for exceeding their rlimits or if
1391 		 * process is swapped out -- deactivate pages
1392 		 */
1393 
1394 		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1395 			vm_pindex_t limit, size;
1396 
1397 			/*
1398 			 * if this is a system process or if we have already
1399 			 * looked at this process, skip it.
1400 			 */
1401 			if (p->p_flag & (P_SYSTEM | P_WEXIT)) {
1402 				continue;
1403 			}
1404 			/*
1405 			 * if the process is in a non-running type state,
1406 			 * don't touch it.
1407 			 */
1408 			if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
1409 				continue;
1410 			}
1411 			/*
1412 			 * get a limit
1413 			 */
1414 			limit = OFF_TO_IDX(
1415 			    qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
1416 				p->p_rlimit[RLIMIT_RSS].rlim_max));
1417 
1418 			/*
1419 			 * let processes that are swapped out really be
1420 			 * swapped out set the limit to nothing (will force a
1421 			 * swap-out.)
1422 			 */
1423 			if ((p->p_flag & P_INMEM) == 0)
1424 				limit = 0;	/* XXX */
1425 
1426 			size = vmspace_resident_count(p->p_vmspace);
1427 			if (limit >= 0 && size >= limit) {
1428 				vm_pageout_map_deactivate_pages(
1429 				    &p->p_vmspace->vm_map, limit);
1430 			}
1431 		}
1432 	}
1433 }
1434 #endif
1435