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