vm_pageout.c revision 12767
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 * $Id: vm_pageout.c,v 1.61 1995/12/07 12:48:24 davidg Exp $
69 */
70
71/*
72 *	The proverbial page-out daemon.
73 */
74
75#include <sys/param.h>
76#include <sys/systm.h>
77#include <sys/kernel.h>
78#include <sys/proc.h>
79#include <sys/resourcevar.h>
80#include <sys/malloc.h>
81#include <sys/kernel.h>
82#include <sys/signalvar.h>
83#include <sys/vnode.h>
84#include <sys/vmmeter.h>
85
86#include <vm/vm.h>
87#include <vm/vm_param.h>
88#include <vm/vm_prot.h>
89#include <vm/lock.h>
90#include <vm/vm_object.h>
91#include <vm/vm_page.h>
92#include <vm/vm_map.h>
93#include <vm/vm_pageout.h>
94#include <vm/vm_kern.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"*/
104static void vm_pageout __P((void));
105static int vm_pageout_clean __P((vm_page_t, int));
106static int vm_pageout_scan __P((void));
107struct proc *pageproc;
108
109static struct kproc_desc page_kp = {
110	"pagedaemon",
111	vm_pageout,
112	&pageproc
113};
114SYSINIT_KT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp)
115
116/* the kernel process "vm_daemon"*/
117static void vm_daemon __P((void));
118struct	proc *vmproc;
119
120static struct kproc_desc vm_kp = {
121	"vmdaemon",
122	vm_daemon,
123	&vmproc
124};
125SYSINIT_KT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp)
126
127
128int vm_pages_needed;		/* Event on which pageout daemon sleeps */
129
130int vm_pageout_pages_needed;	/* flag saying that the pageout daemon needs pages */
131
132extern int npendingio;
133int vm_pageout_req_swapout;	/* XXX */
134int vm_daemon_needed;
135extern int nswiodone;
136extern int vm_swap_size;
137extern int vfs_update_wakeup;
138
139#define MAXSCAN 1024		/* maximum number of pages to scan in queues */
140
141#define MAXLAUNDER (cnt.v_page_count > 1800 ? 32 : 16)
142
143#define VM_PAGEOUT_PAGE_COUNT 16
144int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
145
146int vm_page_max_wired;		/* XXX max # of wired pages system-wide */
147
148typedef int freeer_fcn_t __P((vm_map_t, vm_object_t, int, int));
149static void vm_pageout_map_deactivate_pages __P((vm_map_t, vm_map_entry_t,
150						 int *, freeer_fcn_t *));
151static freeer_fcn_t vm_pageout_object_deactivate_pages;
152static void vm_req_vmdaemon __P((void));
153
154/*
155 * vm_pageout_clean:
156 *
157 * Clean the page and remove it from the laundry.
158 *
159 * We set the busy bit to cause potential page faults on this page to
160 * block.
161 *
162 * And we set pageout-in-progress to keep the object from disappearing
163 * during pageout.  This guarantees that the page won't move from the
164 * inactive queue.  (However, any other page on the inactive queue may
165 * move!)
166 */
167static int
168vm_pageout_clean(m, sync)
169	vm_page_t m;
170	int sync;
171{
172	register vm_object_t object;
173	vm_page_t mc[2*VM_PAGEOUT_PAGE_COUNT];
174	int pageout_count;
175	int i, forward_okay, backward_okay, page_base;
176	vm_pindex_t pindex = m->pindex;
177
178	object = m->object;
179
180	/*
181	 * If not OBJT_SWAP, additional memory may be needed to do the pageout.
182	 * Try to avoid the deadlock.
183	 */
184	if ((sync != VM_PAGEOUT_FORCE) &&
185	    (object->type != OBJT_SWAP) &&
186	    ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min))
187		return 0;
188
189	/*
190	 * Don't mess with the page if it's busy.
191	 */
192	if ((!sync && m->hold_count != 0) ||
193	    ((m->busy != 0) || (m->flags & PG_BUSY)))
194		return 0;
195
196	/*
197	 * Try collapsing before it's too late.
198	 */
199	if (!sync && object->backing_object) {
200		vm_object_collapse(object);
201	}
202	mc[VM_PAGEOUT_PAGE_COUNT] = m;
203	pageout_count = 1;
204	page_base = VM_PAGEOUT_PAGE_COUNT;
205	forward_okay = TRUE;
206	if (pindex != 0)
207		backward_okay = TRUE;
208	else
209		backward_okay = FALSE;
210	/*
211	 * Scan object for clusterable pages.
212	 *
213	 * We can cluster ONLY if: ->> the page is NOT
214	 * clean, wired, busy, held, or mapped into a
215	 * buffer, and one of the following:
216	 * 1) The page is inactive, or a seldom used
217	 *    active page.
218	 * -or-
219	 * 2) we force the issue.
220	 */
221	for (i = 1; (i < vm_pageout_page_count) && (forward_okay || backward_okay); i++) {
222		vm_page_t p;
223
224		/*
225		 * See if forward page is clusterable.
226		 */
227		if (forward_okay) {
228			/*
229			 * Stop forward scan at end of object.
230			 */
231			if ((pindex + i) > object->size) {
232				forward_okay = FALSE;
233				goto do_backward;
234			}
235			p = vm_page_lookup(object, pindex + i);
236			if (p) {
237				if ((p->flags & (PG_BUSY|PG_CACHE)) || p->busy) {
238					forward_okay = FALSE;
239					goto do_backward;
240				}
241				vm_page_test_dirty(p);
242				if ((p->dirty & p->valid) != 0 &&
243				    ((p->flags & PG_INACTIVE) ||
244				     (sync == VM_PAGEOUT_FORCE)) &&
245				    (p->wire_count == 0) &&
246				    (p->hold_count == 0)) {
247					mc[VM_PAGEOUT_PAGE_COUNT + i] = p;
248					pageout_count++;
249					if (pageout_count == vm_pageout_page_count)
250						break;
251				} else {
252					forward_okay = FALSE;
253				}
254			} else {
255				forward_okay = FALSE;
256			}
257		}
258do_backward:
259		/*
260		 * See if backward page is clusterable.
261		 */
262		if (backward_okay) {
263			/*
264			 * Stop backward scan at beginning of object.
265			 */
266			if ((pindex - i) == 0) {
267				backward_okay = FALSE;
268			}
269			p = vm_page_lookup(object, pindex - i);
270			if (p) {
271				if ((p->flags & (PG_BUSY|PG_CACHE)) || p->busy) {
272					backward_okay = FALSE;
273					continue;
274				}
275				vm_page_test_dirty(p);
276				if ((p->dirty & p->valid) != 0 &&
277				    ((p->flags & PG_INACTIVE) ||
278				     (sync == VM_PAGEOUT_FORCE)) &&
279				    (p->wire_count == 0) &&
280				    (p->hold_count == 0)) {
281					mc[VM_PAGEOUT_PAGE_COUNT - i] = p;
282					pageout_count++;
283					page_base--;
284					if (pageout_count == vm_pageout_page_count)
285						break;
286				} else {
287					backward_okay = FALSE;
288				}
289			} else {
290				backward_okay = FALSE;
291			}
292		}
293	}
294
295	/*
296	 * we allow reads during pageouts...
297	 */
298	for (i = page_base; i < (page_base + pageout_count); i++) {
299		mc[i]->flags |= PG_BUSY;
300		vm_page_protect(mc[i], VM_PROT_READ);
301	}
302
303	return vm_pageout_flush(&mc[page_base], pageout_count, sync);
304}
305
306int
307vm_pageout_flush(mc, count, sync)
308	vm_page_t *mc;
309	int count;
310	int sync;
311{
312	register vm_object_t object;
313	int pageout_status[count];
314	int anyok = 0;
315	int i;
316
317	object = mc[0]->object;
318	object->paging_in_progress += count;
319
320	vm_pager_put_pages(object, mc, count,
321	    ((sync || (object == kernel_object)) ? TRUE : FALSE),
322	    pageout_status);
323
324
325	for (i = 0; i < count; i++) {
326		vm_page_t mt = mc[i];
327
328		switch (pageout_status[i]) {
329		case VM_PAGER_OK:
330			++anyok;
331			break;
332		case VM_PAGER_PEND:
333			++anyok;
334			break;
335		case VM_PAGER_BAD:
336			/*
337			 * Page outside of range of object. Right now we
338			 * essentially lose the changes by pretending it
339			 * worked.
340			 */
341			pmap_clear_modify(VM_PAGE_TO_PHYS(mt));
342			mt->dirty = 0;
343			break;
344		case VM_PAGER_ERROR:
345		case VM_PAGER_FAIL:
346			/*
347			 * If page couldn't be paged out, then reactivate the
348			 * page so it doesn't clog the inactive list.  (We
349			 * will try paging out it again later).
350			 */
351			if (mt->flags & PG_INACTIVE)
352				vm_page_activate(mt);
353			break;
354		case VM_PAGER_AGAIN:
355			break;
356		}
357
358
359		/*
360		 * If the operation is still going, leave the page busy to
361		 * block all other accesses. Also, leave the paging in
362		 * progress indicator set so that we don't attempt an object
363		 * collapse.
364		 */
365		if (pageout_status[i] != VM_PAGER_PEND) {
366			vm_object_pip_wakeup(object);
367			if ((mt->flags & (PG_REFERENCED|PG_WANTED)) ||
368			    pmap_is_referenced(VM_PAGE_TO_PHYS(mt))) {
369				pmap_clear_reference(VM_PAGE_TO_PHYS(mt));
370				mt->flags &= ~PG_REFERENCED;
371				if (mt->flags & PG_INACTIVE)
372					vm_page_activate(mt);
373			}
374			PAGE_WAKEUP(mt);
375		}
376	}
377	return anyok;
378}
379
380/*
381 *	vm_pageout_object_deactivate_pages
382 *
383 *	deactivate enough pages to satisfy the inactive target
384 *	requirements or if vm_page_proc_limit is set, then
385 *	deactivate all of the pages in the object and its
386 *	backing_objects.
387 *
388 *	The object and map must be locked.
389 */
390static int
391vm_pageout_object_deactivate_pages(map, object, count, map_remove_only)
392	vm_map_t map;
393	vm_object_t object;
394	int count;
395	int map_remove_only;
396{
397	register vm_page_t p, next;
398	int rcount;
399	int dcount;
400
401	dcount = 0;
402	if (count == 0)
403		count = 1;
404
405	if (object->type == OBJT_DEVICE)
406		return 0;
407
408	if (object->backing_object) {
409		if (object->backing_object->ref_count == 1)
410			dcount += vm_pageout_object_deactivate_pages(map,
411			    object->backing_object, count / 2 + 1, map_remove_only);
412		else
413			vm_pageout_object_deactivate_pages(map,
414			    object->backing_object, count, 1);
415	}
416	if (object->paging_in_progress)
417		return dcount;
418
419	/*
420	 * scan the objects entire memory queue
421	 */
422	rcount = object->resident_page_count;
423	p = object->memq.tqh_first;
424	while (p && (rcount-- > 0)) {
425		next = p->listq.tqe_next;
426		cnt.v_pdpages++;
427		if (p->wire_count != 0 ||
428		    p->hold_count != 0 ||
429		    p->busy != 0 ||
430		    !pmap_page_exists(vm_map_pmap(map), VM_PAGE_TO_PHYS(p))) {
431			p = next;
432			continue;
433		}
434		/*
435		 * if a page is active, not wired and is in the processes
436		 * pmap, then deactivate the page.
437		 */
438		if ((p->flags & (PG_ACTIVE | PG_BUSY)) == PG_ACTIVE) {
439			if (!pmap_is_referenced(VM_PAGE_TO_PHYS(p)) &&
440			    (p->flags & (PG_REFERENCED|PG_WANTED)) == 0) {
441				p->act_count -= min(p->act_count, ACT_DECLINE);
442				/*
443				 * if the page act_count is zero -- then we
444				 * deactivate
445				 */
446				if (!p->act_count) {
447					if (!map_remove_only)
448						vm_page_deactivate(p);
449					vm_page_protect(p, VM_PROT_NONE);
450					/*
451					 * else if on the next go-around we
452					 * will deactivate the page we need to
453					 * place the page on the end of the
454					 * queue to age the other pages in
455					 * memory.
456					 */
457				} else {
458					TAILQ_REMOVE(&vm_page_queue_active, p, pageq);
459					TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq);
460				}
461				/*
462				 * see if we are done yet
463				 */
464				if (p->flags & PG_INACTIVE) {
465					--count;
466					++dcount;
467					if (count <= 0 &&
468					    cnt.v_inactive_count > cnt.v_inactive_target) {
469						return dcount;
470					}
471				}
472			} else {
473				/*
474				 * Move the page to the bottom of the queue.
475				 */
476				pmap_clear_reference(VM_PAGE_TO_PHYS(p));
477				p->flags &= ~PG_REFERENCED;
478				if (p->act_count < ACT_MAX)
479					p->act_count += ACT_ADVANCE;
480
481				TAILQ_REMOVE(&vm_page_queue_active, p, pageq);
482				TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq);
483			}
484		} else if ((p->flags & (PG_INACTIVE | PG_BUSY)) == PG_INACTIVE) {
485			vm_page_protect(p, VM_PROT_NONE);
486		}
487		p = next;
488	}
489	return dcount;
490}
491
492
493/*
494 * deactivate some number of pages in a map, try to do it fairly, but
495 * that is really hard to do.
496 */
497
498static void
499vm_pageout_map_deactivate_pages(map, entry, count, freeer)
500	vm_map_t map;
501	vm_map_entry_t entry;
502	int *count;
503	freeer_fcn_t *freeer;
504{
505	vm_map_t tmpm;
506	vm_map_entry_t tmpe;
507	vm_object_t obj;
508
509	if (*count <= 0)
510		return;
511	vm_map_reference(map);
512	if (!lock_try_read(&map->lock)) {
513		vm_map_deallocate(map);
514		return;
515	}
516	if (entry == 0) {
517		tmpe = map->header.next;
518		while (tmpe != &map->header && *count > 0) {
519			vm_pageout_map_deactivate_pages(map, tmpe, count, freeer);
520			tmpe = tmpe->next;
521		};
522	} else if (entry->is_sub_map || entry->is_a_map) {
523		tmpm = entry->object.share_map;
524		tmpe = tmpm->header.next;
525		while (tmpe != &tmpm->header && *count > 0) {
526			vm_pageout_map_deactivate_pages(tmpm, tmpe, count, freeer);
527			tmpe = tmpe->next;
528		};
529	} else if ((obj = entry->object.vm_object) != 0) {
530		*count -= (*freeer) (map, obj, *count, TRUE);
531	}
532	lock_read_done(&map->lock);
533	vm_map_deallocate(map);
534	return;
535}
536
537static void
538vm_req_vmdaemon()
539{
540	static int lastrun = 0;
541
542	if ((ticks > (lastrun + hz / 10)) || (ticks < lastrun)) {
543		wakeup(&vm_daemon_needed);
544		lastrun = ticks;
545	}
546}
547
548/*
549 *	vm_pageout_scan does the dirty work for the pageout daemon.
550 */
551static int
552vm_pageout_scan()
553{
554	vm_page_t m;
555	int page_shortage, maxscan, maxlaunder, pcount;
556	int pages_freed;
557	vm_page_t next;
558	struct proc *p, *bigproc;
559	vm_offset_t size, bigsize;
560	vm_object_t object;
561	int force_wakeup = 0;
562	int vnodes_skipped = 0;
563
564	pages_freed = 0;
565
566	/*
567	 * Start scanning the inactive queue for pages we can free. We keep
568	 * scanning until we have enough free pages or we have scanned through
569	 * the entire queue.  If we encounter dirty pages, we start cleaning
570	 * them.
571	 */
572
573	maxlaunder = (cnt.v_inactive_target > MAXLAUNDER) ?
574	    MAXLAUNDER : cnt.v_inactive_target;
575
576rescan1:
577	maxscan = cnt.v_inactive_count;
578	m = vm_page_queue_inactive.tqh_first;
579	while ((m != NULL) && (maxscan-- > 0) &&
580	    ((cnt.v_cache_count + cnt.v_free_count) < (cnt.v_cache_min + cnt.v_free_target))) {
581		vm_page_t next;
582
583		cnt.v_pdpages++;
584		next = m->pageq.tqe_next;
585
586#if defined(VM_DIAGNOSE)
587		if ((m->flags & PG_INACTIVE) == 0) {
588			printf("vm_pageout_scan: page not inactive?\n");
589			break;
590		}
591#endif
592
593		/*
594		 * dont mess with busy pages
595		 */
596		if (m->hold_count || m->busy || (m->flags & PG_BUSY)) {
597			TAILQ_REMOVE(&vm_page_queue_inactive, m, pageq);
598			TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq);
599			m = next;
600			continue;
601		}
602		if (((m->flags & PG_REFERENCED) == 0) &&
603		    pmap_is_referenced(VM_PAGE_TO_PHYS(m))) {
604			m->flags |= PG_REFERENCED;
605		}
606		if (m->object->ref_count == 0) {
607			m->flags &= ~PG_REFERENCED;
608			pmap_clear_reference(VM_PAGE_TO_PHYS(m));
609		}
610		if ((m->flags & (PG_REFERENCED|PG_WANTED)) != 0) {
611			m->flags &= ~PG_REFERENCED;
612			pmap_clear_reference(VM_PAGE_TO_PHYS(m));
613			vm_page_activate(m);
614			if (m->act_count < ACT_MAX)
615				m->act_count += ACT_ADVANCE;
616			m = next;
617			continue;
618		}
619
620		vm_page_test_dirty(m);
621		if (m->dirty == 0) {
622			if (m->bmapped == 0) {
623				if (m->valid == 0) {
624					pmap_page_protect(VM_PAGE_TO_PHYS(m), VM_PROT_NONE);
625					vm_page_free(m);
626					cnt.v_dfree++;
627				} else {
628					vm_page_cache(m);
629				}
630				++pages_freed;
631			} else {
632				m = next;
633				continue;
634			}
635		} else if (maxlaunder > 0) {
636			int written;
637			struct vnode *vp = NULL;
638
639			object = m->object;
640			if (object->flags & OBJ_DEAD) {
641				m = next;
642				continue;
643			}
644
645			if (object->type == OBJT_VNODE) {
646				vp = object->handle;
647				if (VOP_ISLOCKED(vp) || vget(vp, 1)) {
648					if (object->flags & OBJ_MIGHTBEDIRTY)
649						++vnodes_skipped;
650					m = next;
651					continue;
652				}
653			}
654
655			/*
656			 * If a page is dirty, then it is either being washed
657			 * (but not yet cleaned) or it is still in the
658			 * laundry.  If it is still in the laundry, then we
659			 * start the cleaning operation.
660			 */
661			written = vm_pageout_clean(m, 0);
662
663			if (vp)
664				vput(vp);
665
666			if (!next) {
667				break;
668			}
669			maxlaunder -= written;
670			/*
671			 * if the next page has been re-activated, start
672			 * scanning again
673			 */
674			if ((next->flags & PG_INACTIVE) == 0) {
675				vm_pager_sync();
676				goto rescan1;
677			}
678		}
679		m = next;
680	}
681
682	/*
683	 * Compute the page shortage.  If we are still very low on memory be
684	 * sure that we will move a minimal amount of pages from active to
685	 * inactive.
686	 */
687
688	page_shortage = cnt.v_inactive_target -
689	    (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
690	if (page_shortage <= 0) {
691		if (pages_freed == 0) {
692			page_shortage = cnt.v_free_min - cnt.v_free_count;
693		} else {
694			page_shortage = 1;
695		}
696	}
697	maxscan = MAXSCAN;
698	pcount = cnt.v_active_count;
699	m = vm_page_queue_active.tqh_first;
700	while ((m != NULL) && (maxscan > 0) && (pcount-- > 0) && (page_shortage > 0)) {
701
702		cnt.v_pdpages++;
703		next = m->pageq.tqe_next;
704
705		/*
706		 * Don't deactivate pages that are busy.
707		 */
708		if ((m->busy != 0) ||
709		    (m->flags & PG_BUSY) ||
710		    (m->hold_count != 0)) {
711			TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
712			TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
713			m = next;
714			/* printf("busy: s: %d, f: 0x%x, h: %d\n",
715				m->busy, m->flags, m->hold_count); */
716			continue;
717		}
718		if (m->object->ref_count &&
719			((m->flags & (PG_REFERENCED|PG_WANTED)) ||
720			pmap_is_referenced(VM_PAGE_TO_PHYS(m)))) {
721			pmap_clear_reference(VM_PAGE_TO_PHYS(m));
722			m->flags &= ~PG_REFERENCED;
723			if (m->act_count < ACT_MAX) {
724				m->act_count += ACT_ADVANCE;
725			}
726			TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
727			TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
728		} else {
729			m->flags &= ~PG_REFERENCED;
730			pmap_clear_reference(VM_PAGE_TO_PHYS(m));
731			m->act_count -= min(m->act_count, ACT_DECLINE);
732
733			/*
734			 * if the page act_count is zero -- then we deactivate
735			 */
736			if (!m->act_count && (page_shortage > 0)) {
737				if (m->object->ref_count == 0) {
738					--page_shortage;
739					vm_page_test_dirty(m);
740					if ((m->bmapped == 0) && (m->dirty == 0) ) {
741						m->act_count = 0;
742						vm_page_cache(m);
743					} else {
744						vm_page_deactivate(m);
745					}
746				} else {
747					vm_page_deactivate(m);
748					--page_shortage;
749				}
750			} else if (m->act_count) {
751				TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
752				TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
753			}
754		}
755		maxscan--;
756		m = next;
757	}
758
759	/*
760	 * We try to maintain some *really* free pages, this allows interrupt
761	 * code to be guaranteed space.
762	 */
763	while (cnt.v_free_count < cnt.v_free_reserved) {
764		m = vm_page_queue_cache.tqh_first;
765		if (!m)
766			break;
767		vm_page_free(m);
768		cnt.v_dfree++;
769	}
770
771	/*
772	 * If we didn't get enough free pages, and we have skipped a vnode
773	 * in a writeable object, wakeup the sync daemon.  And kick swapout
774	 * if we did not get enough free pages.
775	 */
776	if ((cnt.v_cache_count + cnt.v_free_count) < cnt.v_free_target) {
777		if (vnodes_skipped &&
778		    (cnt.v_cache_count + cnt.v_free_count) < cnt.v_free_min) {
779			if (!vfs_update_wakeup) {
780				vfs_update_wakeup = 1;
781				wakeup(&vfs_update_wakeup);
782			}
783		}
784		/*
785		 * now swap processes out if we are in low memory conditions
786		 */
787		if (!swap_pager_full && vm_swap_size &&
788			vm_pageout_req_swapout == 0) {
789			vm_pageout_req_swapout = 1;
790			vm_req_vmdaemon();
791		}
792	}
793
794	if ((cnt.v_inactive_count + cnt.v_free_count + cnt.v_cache_count) <
795	    (cnt.v_inactive_target + cnt.v_free_min)) {
796		vm_req_vmdaemon();
797	}
798
799	/*
800	 * make sure that we have swap space -- if we are low on memory and
801	 * swap -- then kill the biggest process.
802	 */
803	if ((vm_swap_size == 0 || swap_pager_full) &&
804	    ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min)) {
805		bigproc = NULL;
806		bigsize = 0;
807		for (p = (struct proc *) allproc; p != NULL; p = p->p_next) {
808			/*
809			 * if this is a system process, skip it
810			 */
811			if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) ||
812			    ((p->p_pid < 48) && (vm_swap_size != 0))) {
813				continue;
814			}
815			/*
816			 * if the process is in a non-running type state,
817			 * don't touch it.
818			 */
819			if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
820				continue;
821			}
822			/*
823			 * get the process size
824			 */
825			size = p->p_vmspace->vm_pmap.pm_stats.resident_count;
826			/*
827			 * if the this process is bigger than the biggest one
828			 * remember it.
829			 */
830			if (size > bigsize) {
831				bigproc = p;
832				bigsize = size;
833			}
834		}
835		if (bigproc != NULL) {
836			printf("Process %lu killed by vm_pageout -- out of swap\n", (u_long) bigproc->p_pid);
837			psignal(bigproc, SIGKILL);
838			bigproc->p_estcpu = 0;
839			bigproc->p_nice = PRIO_MIN;
840			resetpriority(bigproc);
841			wakeup(&cnt.v_free_count);
842		}
843	}
844	return force_wakeup;
845}
846
847/*
848 *	vm_pageout is the high level pageout daemon.
849 */
850static void
851vm_pageout()
852{
853	(void) spl0();
854
855	/*
856	 * Initialize some paging parameters.
857	 */
858
859	cnt.v_interrupt_free_min = 2;
860
861	if (cnt.v_page_count > 1024)
862		cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
863	else
864		cnt.v_free_min = 4;
865	/*
866	 * free_reserved needs to include enough for the largest swap pager
867	 * structures plus enough for any pv_entry structs when paging.
868	 */
869	cnt.v_pageout_free_min = 6 + cnt.v_page_count / 1024 +
870				cnt.v_interrupt_free_min;
871	cnt.v_free_reserved = cnt.v_pageout_free_min + 6;
872	cnt.v_free_target = 3 * cnt.v_free_min + cnt.v_free_reserved;
873	cnt.v_free_min += cnt.v_free_reserved;
874
875	if (cnt.v_page_count > 1024) {
876		cnt.v_cache_max = (cnt.v_free_count - 1024) / 2;
877		cnt.v_cache_min = (cnt.v_free_count - 1024) / 8;
878		cnt.v_inactive_target = 2*cnt.v_cache_min + 192;
879	} else {
880		cnt.v_cache_min = 0;
881		cnt.v_cache_max = 0;
882		cnt.v_inactive_target = cnt.v_free_count / 4;
883	}
884
885	/* XXX does not really belong here */
886	if (vm_page_max_wired == 0)
887		vm_page_max_wired = cnt.v_free_count / 3;
888
889
890	swap_pager_swap_init();
891	/*
892	 * The pageout daemon is never done, so loop forever.
893	 */
894	while (TRUE) {
895		int s = splhigh();
896
897		if (!vm_pages_needed ||
898			((cnt.v_free_count >= cnt.v_free_reserved) &&
899			 (cnt.v_free_count + cnt.v_cache_count >= cnt.v_free_min))) {
900			vm_pages_needed = 0;
901			tsleep(&vm_pages_needed, PVM, "psleep", 0);
902		}
903		vm_pages_needed = 0;
904		splx(s);
905		cnt.v_pdwakeups++;
906		vm_pager_sync();
907		vm_pageout_scan();
908		vm_pager_sync();
909		wakeup(&cnt.v_free_count);
910		wakeup(kmem_map);
911	}
912}
913
914static void
915vm_daemon()
916{
917	vm_object_t object;
918	struct proc *p;
919
920	while (TRUE) {
921		tsleep(&vm_daemon_needed, PUSER, "psleep", 0);
922		if (vm_pageout_req_swapout) {
923			swapout_procs();
924			vm_pageout_req_swapout = 0;
925		}
926		/*
927		 * scan the processes for exceeding their rlimits or if
928		 * process is swapped out -- deactivate pages
929		 */
930
931		for (p = (struct proc *) allproc; p != NULL; p = p->p_next) {
932			int overage;
933			quad_t limit;
934			vm_offset_t size;
935
936			/*
937			 * if this is a system process or if we have already
938			 * looked at this process, skip it.
939			 */
940			if (p->p_flag & (P_SYSTEM | P_WEXIT)) {
941				continue;
942			}
943			/*
944			 * if the process is in a non-running type state,
945			 * don't touch it.
946			 */
947			if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
948				continue;
949			}
950			/*
951			 * get a limit
952			 */
953			limit = qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
954			    p->p_rlimit[RLIMIT_RSS].rlim_max);
955
956			/*
957			 * let processes that are swapped out really be
958			 * swapped out set the limit to nothing (will force a
959			 * swap-out.)
960			 */
961			if ((p->p_flag & P_INMEM) == 0)
962				limit = 0;	/* XXX */
963
964			size = p->p_vmspace->vm_pmap.pm_stats.resident_count * PAGE_SIZE;
965			if (limit >= 0 && size >= limit) {
966				overage = (size - limit) >> PAGE_SHIFT;
967				vm_pageout_map_deactivate_pages(&p->p_vmspace->vm_map,
968				    (vm_map_entry_t) 0, &overage, vm_pageout_object_deactivate_pages);
969			}
970		}
971
972		/*
973		 * we remove cached objects that have no RSS...
974		 */
975restart:
976		object = vm_object_cached_list.tqh_first;
977		while (object) {
978			/*
979			 * if there are no resident pages -- get rid of the object
980			 */
981			if (object->resident_page_count == 0) {
982				vm_object_reference(object);
983				pager_cache(object, FALSE);
984				goto restart;
985			}
986			object = object->cached_list.tqe_next;
987		}
988	}
989}
990