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