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