xref: /freebsd/sys/vm/vm_pageout.c (revision afe61c15161c324a7af299a9b8457aba5afc92db)
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  *	@(#)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.20 1994/04/20 07:07: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 
81 #include <vm/vm.h>
82 #include <vm/vm_page.h>
83 #include <vm/vm_pageout.h>
84 
85 extern vm_map_t kmem_map;
86 int	vm_pages_needed;		/* Event on which pageout daemon sleeps */
87 int	vm_pagescanner;			/* Event on which pagescanner sleeps */
88 int	vm_pageout_free_min = 0;	/* Stop pageout to wait for pagers at this free level */
89 
90 int	vm_pageout_pages_needed = 0;	/* flag saying that the pageout daemon needs pages */
91 int	vm_page_pagesfreed;
92 
93 extern int npendingio;
94 extern int hz;
95 int	vm_pageout_proc_limit;
96 extern int nswiodone;
97 extern int swap_pager_full;
98 extern 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	6
106 #define ACT_MAX		300
107 
108 #define LOWATER ((2048*1024)/NBPG)
109 
110 #define VM_PAGEOUT_PAGE_COUNT 8
111 static vm_offset_t vm_space_needed;
112 int vm_pageout_req_do_stats;
113 
114 int	vm_page_max_wired = 0;	/* XXX max # of wired pages system-wide */
115 
116 
117 /*
118  * vm_pageout_clean:
119  * 	cleans a vm_page
120  */
121 int
122 vm_pageout_clean(m, sync)
123 	register vm_page_t m;
124 	int sync;
125 {
126 	/*
127 	 *	Clean the page and remove it from the
128 	 *	laundry.
129 	 *
130 	 *	We set the busy bit to cause
131 	 *	potential page faults on this page to
132 	 *	block.
133 	 *
134 	 *	And we set pageout-in-progress to keep
135 	 *	the object from disappearing during
136 	 *	pageout.  This guarantees that the
137 	 *	page won't move from the inactive
138 	 *	queue.  (However, any other page on
139 	 *	the inactive queue may move!)
140 	 */
141 
142 	register vm_object_t	object;
143 	register vm_pager_t	pager;
144 	int			pageout_status[VM_PAGEOUT_PAGE_COUNT];
145 	vm_page_t		ms[VM_PAGEOUT_PAGE_COUNT];
146 	int			pageout_count;
147 	int			anyok=0;
148 	int			i;
149 	vm_offset_t offset = m->offset;
150 
151 	object = m->object;
152 	if (!object) {
153 		printf("pager: object missing\n");
154 		return 0;
155 	}
156 
157 	/*
158 	 *	Try to collapse the object before
159 	 *	making a pager for it.  We must
160 	 *	unlock the page queues first.
161 	 *	We try to defer the creation of a pager
162 	 *	until all shadows are not paging.  This
163 	 *	allows vm_object_collapse to work better and
164 	 *	helps control swap space size.
165 	 *	(J. Dyson 11 Nov 93)
166 	 */
167 
168 	if (!object->pager &&
169 		cnt.v_free_count < vm_pageout_free_min)
170 		return 0;
171 
172 	if (!object->pager &&
173 		object->shadow &&
174 		object->shadow->paging_in_progress)
175 		return 0;
176 
177 	if( !sync) {
178 		if (object->shadow) {
179 			vm_object_collapse(object);
180 			if (!vm_page_lookup(object, offset))
181 				return 0;
182 		}
183 
184 		if ((m->flags & PG_BUSY) || (m->hold_count != 0)) {
185 			return 0;
186 		}
187 	}
188 
189 	pageout_count = 1;
190 	ms[0] = m;
191 
192 	if( pager = object->pager) {
193 		for(i=1;i<VM_PAGEOUT_PAGE_COUNT;i++) {
194 			if( ms[i] = vm_page_lookup( object, offset+i*NBPG)) {
195 				if((((ms[i]->flags & (PG_CLEAN|PG_INACTIVE|PG_BUSY)) == PG_INACTIVE)
196 					|| (( ms[i]->flags & PG_CLEAN) == 0 && sync == VM_PAGEOUT_FORCE))
197 					&& (ms[i]->wire_count == 0)
198 					&& (ms[i]->hold_count == 0))
199 					pageout_count++;
200 				else
201 					break;
202 			} else
203 				break;
204 		}
205 		for(i=0;i<pageout_count;i++) {
206 			ms[i]->flags |= PG_BUSY;
207 			pmap_page_protect(VM_PAGE_TO_PHYS(ms[i]), VM_PROT_READ);
208 		}
209 		object->paging_in_progress += pageout_count;
210 		cnt.v_pageouts += pageout_count;
211 	} else {
212 
213 		m->flags |= PG_BUSY;
214 
215 		pmap_page_protect(VM_PAGE_TO_PHYS(m), VM_PROT_READ);
216 
217 		cnt.v_pageouts++;
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,
230 	 *	use the default pager.  If there's
231 	 *	no place to put the page at the
232 	 *	moment, leave it in the laundry and
233 	 *	hope that there will be paging space
234 	 *	later.
235 	 */
236 
237 	if ((pager && pager->pg_type == PG_SWAP) ||
238 		cnt.v_free_count >= vm_pageout_free_min) {
239 		if( pageout_count == 1) {
240 			pageout_status[0] = pager ?
241 				vm_pager_put(pager, m,
242 				    ((sync || (object == kernel_object)) ? TRUE: FALSE)) :
243 				VM_PAGER_FAIL;
244 		} else {
245 			if( !pager) {
246 				for(i=0;i<pageout_count;i++)
247 					pageout_status[i] = VM_PAGER_FAIL;
248 			} else {
249 				vm_pager_put_pages(pager, ms, pageout_count,
250 				    ((sync || (object == kernel_object)) ? TRUE : FALSE),
251 				    pageout_status);
252 			}
253 		}
254 
255 	} else {
256 		for(i=0;i<pageout_count;i++)
257 			pageout_status[i] = VM_PAGER_FAIL;
258 	}
259 
260 	for(i=0;i<pageout_count;i++) {
261 		switch (pageout_status[i]) {
262 		case VM_PAGER_OK:
263 			ms[i]->flags &= ~PG_LAUNDRY;
264 			++anyok;
265 			break;
266 		case VM_PAGER_PEND:
267 			ms[i]->flags &= ~PG_LAUNDRY;
268 			++anyok;
269 			break;
270 		case VM_PAGER_BAD:
271 			/*
272 			 * Page outside of range of object.
273 			 * Right now we essentially lose the
274 			 * changes by pretending it worked.
275 			 */
276 			ms[i]->flags &= ~PG_LAUNDRY;
277 			ms[i]->flags |= PG_CLEAN;
278 			pmap_clear_modify(VM_PAGE_TO_PHYS(ms[i]));
279 			break;
280 		case VM_PAGER_ERROR:
281 		case VM_PAGER_FAIL:
282 			/*
283 			 * If page couldn't be paged out, then
284 			 * reactivate the page so it doesn't
285 			 * clog the inactive list.  (We will
286 			 * try paging out it again later).
287 			 */
288 			if (ms[i]->flags & PG_INACTIVE)
289 				vm_page_activate(ms[i]);
290 			break;
291 		case VM_PAGER_AGAIN:
292 			break;
293 		}
294 
295 
296 		/*
297 		 * If the operation is still going, leave
298 		 * the page busy to block all other accesses.
299 		 * Also, leave the paging in progress
300 		 * indicator set so that we don't attempt an
301 		 * object collapse.
302 		 */
303 		if (pageout_status[i] != VM_PAGER_PEND) {
304 			PAGE_WAKEUP(ms[i]);
305 			if (--object->paging_in_progress == 0)
306 				wakeup((caddr_t) object);
307 			if (pmap_is_referenced(VM_PAGE_TO_PHYS(ms[i]))) {
308 				pmap_clear_reference(VM_PAGE_TO_PHYS(ms[i]));
309 				if( ms[i]->flags & PG_INACTIVE)
310 					vm_page_activate(ms[i]);
311 			}
312 		}
313 	}
314 	return anyok;
315 }
316 
317 /*
318  *	vm_pageout_object_deactivate_pages
319  *
320  *	deactivate enough pages to satisfy the inactive target
321  *	requirements or if vm_page_proc_limit is set, then
322  *	deactivate all of the pages in the object and its
323  *	shadows.
324  *
325  *	The object and map must be locked.
326  */
327 int
328 vm_pageout_object_deactivate_pages(map, object, count)
329 	vm_map_t map;
330 	vm_object_t object;
331 	int count;
332 {
333 	register vm_page_t	p, next;
334 	int rcount;
335 	int s;
336 	int dcount;
337 
338 	dcount = 0;
339 	if (count == 0)
340 		count = 1;
341 
342 	if (object->shadow) {
343 		int scount = count;
344 		if( object->shadow->ref_count > 1)
345 			scount /= object->shadow->ref_count;
346 		if( scount)
347 			dcount += vm_pageout_object_deactivate_pages(map, object->shadow, scount);
348 	}
349 
350 	if (object->paging_in_progress)
351 		return dcount;
352 
353 	/*
354 	 * scan the objects entire memory queue
355 	 */
356 	rcount = object->resident_page_count;
357 	p = object->memq.tqh_first;
358 	while (p && (rcount-- > 0)) {
359 		next = p->listq.tqe_next;
360 		vm_page_lock_queues();
361 		/*
362 		 * if a page is active, not wired and is in the processes pmap,
363 		 * then deactivate the page.
364 		 */
365 		if ((p->flags & (PG_ACTIVE|PG_BUSY)) == PG_ACTIVE &&
366 			p->wire_count == 0 &&
367 			p->hold_count == 0 &&
368 			pmap_page_exists(vm_map_pmap(map), VM_PAGE_TO_PHYS(p))) {
369 			if (!pmap_is_referenced(VM_PAGE_TO_PHYS(p))) {
370 				p->act_count -= min(p->act_count, ACT_DECLINE);
371 				/*
372 				 * if the page act_count is zero -- then we deactivate
373 				 */
374 				if (!p->act_count) {
375 					vm_page_deactivate(p);
376 					pmap_page_protect(VM_PAGE_TO_PHYS(p),
377 						VM_PROT_NONE);
378 				/*
379 				 * else if on the next go-around we will deactivate the page
380 				 * we need to place the page on the end of the queue to age
381 				 * the other pages in memory.
382 				 */
383 				} else {
384 					TAILQ_REMOVE(&vm_page_queue_active, p, pageq);
385 					TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq);
386 					TAILQ_REMOVE(&object->memq, p, listq);
387 					TAILQ_INSERT_TAIL(&object->memq, p, listq);
388 				}
389 				/*
390 				 * see if we are done yet
391 				 */
392 				if (p->flags & PG_INACTIVE) {
393 					--count;
394 					++dcount;
395 					if (count <= 0 &&
396 						cnt.v_inactive_count > cnt.v_inactive_target) {
397 							vm_page_unlock_queues();
398 							return dcount;
399 					}
400 				}
401 
402 			} else {
403 				/*
404 				 * Move the page to the bottom of the queue.
405 				 */
406 				pmap_clear_reference(VM_PAGE_TO_PHYS(p));
407 				if (p->act_count < ACT_MAX)
408 					p->act_count += ACT_ADVANCE;
409 
410 				TAILQ_REMOVE(&vm_page_queue_active, p, pageq);
411 				TAILQ_INSERT_TAIL(&vm_page_queue_active, p, pageq);
412 				TAILQ_REMOVE(&object->memq, p, listq);
413 				TAILQ_INSERT_TAIL(&object->memq, p, listq);
414 			}
415 		}
416 
417 		vm_page_unlock_queues();
418 		p = next;
419 	}
420 	return dcount;
421 }
422 
423 
424 /*
425  * deactivate some number of pages in a map, try to do it fairly, but
426  * that is really hard to do.
427  */
428 
429 void
430 vm_pageout_map_deactivate_pages(map, entry, count, freeer)
431 	vm_map_t map;
432 	vm_map_entry_t entry;
433 	int *count;
434 	int (*freeer)(vm_map_t, vm_object_t, int);
435 {
436 	vm_map_t tmpm;
437 	vm_map_entry_t tmpe;
438 	vm_object_t obj;
439 	if (*count <= 0)
440 		return;
441 	vm_map_reference(map);
442 	if (!lock_try_read(&map->lock)) {
443 		vm_map_deallocate(map);
444 		return;
445 	}
446 	if (entry == 0) {
447 		tmpe = map->header.next;
448 		while (tmpe != &map->header && *count > 0) {
449 			vm_pageout_map_deactivate_pages(map, tmpe, count, freeer);
450 			tmpe = tmpe->next;
451 		};
452 	} else if (entry->is_sub_map || entry->is_a_map) {
453 		tmpm = entry->object.share_map;
454 		tmpe = tmpm->header.next;
455 		while (tmpe != &tmpm->header && *count > 0) {
456 			vm_pageout_map_deactivate_pages(tmpm, tmpe, count, freeer);
457 			tmpe = tmpe->next;
458 		};
459 	} else if (obj = entry->object.vm_object) {
460 		*count -= (*freeer)(map, obj, *count);
461 	}
462 	lock_read_done(&map->lock);
463 	vm_map_deallocate(map);
464 	return;
465 }
466 
467 /*
468  *	vm_pageout_scan does the dirty work for the pageout daemon.
469  */
470 int
471 vm_pageout_scan()
472 {
473 	vm_page_t	m;
474 	int		page_shortage, maxscan, maxlaunder;
475 	int		pages_freed, free, nproc;
476 	int		desired_free;
477 	vm_page_t	next;
478 	struct proc	*p;
479 	vm_object_t	object;
480 	int		s;
481 	int		force_wakeup = 0;
482 
483 morefree:
484 	/*
485 	 * scan the processes for exceeding their rlimits or if process
486 	 * is swapped out -- deactivate pages
487 	 */
488 
489 rescanproc1:
490 	for (p = (struct proc *)allproc; p != NULL; p = p->p_next) {
491 		vm_offset_t size;
492 		int overage;
493 		vm_offset_t limit;
494 
495 		/*
496 		 * if this is a system process or if we have already
497 		 * looked at this process, skip it.
498 		 */
499 		if (p->p_flag & (P_SYSTEM|P_WEXIT)) {
500 			continue;
501 		}
502 
503 		/*
504 		 * if the process is in a non-running type state,
505 		 * don't touch it.
506 		 */
507 		if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
508 			continue;
509 		}
510 
511 		/*
512 		 * get a limit
513 		 */
514 		limit = min(p->p_rlimit[RLIMIT_RSS].rlim_cur,
515 			    p->p_rlimit[RLIMIT_RSS].rlim_max);
516 
517 		/*
518 		 * let processes that are swapped out really be swapped out
519 		 * set the limit to nothing (will force a swap-out.)
520 		 */
521 		if ((p->p_flag & P_INMEM) == 0)
522 			limit = 0;
523 
524 		size = p->p_vmspace->vm_pmap.pm_stats.resident_count * NBPG;
525 		if (size >= limit) {
526 			overage = (size - limit) / NBPG;
527 			vm_pageout_map_deactivate_pages(&p->p_vmspace->vm_map,
528 				(vm_map_entry_t) 0, &overage, vm_pageout_object_deactivate_pages);
529 		}
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;
550 rescan1:
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 			/*
620 			 * if the next page has been re-activated, start scanning again
621 			 */
622 			if (next && (next->flags & PG_INACTIVE) == 0)
623 				goto rescan1;
624 		} else if (pmap_is_referenced(VM_PAGE_TO_PHYS(m))) {
625 			pmap_clear_reference(VM_PAGE_TO_PHYS(m));
626 			vm_page_activate(m);
627 		}
628 		m = next;
629 	}
630 
631 	/*
632 	 * now check malloc area or swap processes out if we are in low
633 	 * memory conditions
634 	 */
635 	if (cnt.v_free_count <= cnt.v_free_min) {
636 		/*
637 		 * swap out inactive processes
638 		 */
639 		swapout_threads();
640 	}
641 
642 	/*
643 	 *	Compute the page shortage.  If we are still very low on memory
644 	 *	be sure that we will move a minimal amount of pages from active
645 	 *	to inactive.
646 	 */
647 
648 	page_shortage = cnt.v_inactive_target -
649 	    (cnt.v_free_count + cnt.v_inactive_count);
650 
651 	if (page_shortage <= 0) {
652 		if (pages_freed == 0) {
653 			if( cnt.v_free_count < cnt.v_free_min) {
654 				page_shortage = cnt.v_free_min - cnt.v_free_count;
655 			} else if(((cnt.v_free_count + cnt.v_inactive_count) <
656 				(cnt.v_free_min + cnt.v_inactive_target))) {
657 				page_shortage = 1;
658 			} else {
659 				page_shortage = 0;
660 			}
661 		}
662 
663 	}
664 
665 	maxscan = cnt.v_active_count;
666 	m = vm_page_queue_active.tqh_first;
667 	while (m && maxscan-- && (page_shortage > 0)) {
668 
669 		next = m->pageq.tqe_next;
670 
671 		/*
672  		 * Don't deactivate pages that are busy.
673 		 */
674 		if ((m->flags & PG_BUSY) || (m->hold_count != 0)) {
675 			m = next;
676 			continue;
677 		}
678 
679 		if (pmap_is_referenced(VM_PAGE_TO_PHYS(m))) {
680 			pmap_clear_reference(VM_PAGE_TO_PHYS(m));
681 			if (m->act_count < ACT_MAX)
682 				m->act_count += ACT_ADVANCE;
683 			TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
684 			TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
685 			TAILQ_REMOVE(&m->object->memq, m, listq);
686 			TAILQ_INSERT_TAIL(&m->object->memq, m, listq);
687 		} else {
688 			m->act_count -= min(m->act_count, ACT_DECLINE);
689 
690 			/*
691 			 * if the page act_count is zero -- then we deactivate
692 			 */
693 			if (!m->act_count) {
694 				vm_page_deactivate(m);
695 				--page_shortage;
696 			/*
697 			 * else if on the next go-around we will deactivate the page
698 			 * we need to place the page on the end of the queue to age
699 			 * the other pages in memory.
700 			 */
701 			} else {
702 				TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
703 				TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
704 				TAILQ_REMOVE(&m->object->memq, m, listq);
705 				TAILQ_INSERT_TAIL(&m->object->memq, m, listq);
706 			}
707 		}
708 
709 		m = next;
710 	}
711 
712 	/*
713 	 * if we have not freed any pages and we are desparate for memory
714 	 * then we keep trying until we get some (any) memory.
715 	 */
716 
717 	if( !force_wakeup && (swap_pager_full || !force_wakeup ||
718 		(pages_freed == 0 && (cnt.v_free_count < cnt.v_free_min)))){
719 		vm_pager_sync();
720 		force_wakeup = 1;
721 		goto morefree;
722 	}
723 	vm_page_pagesfreed += pages_freed;
724 	return force_wakeup;
725 }
726 
727 void
728 vm_pagescan()
729 {
730 	int maxscan, pages_scanned, pages_referenced, nextscan, scantick = hz/20;
731 	int m_ref, next_ref;
732 	vm_page_t m, next;
733 
734 	(void) spl0();
735 
736 	nextscan = scantick;
737 
738 scanloop:
739 
740 	pages_scanned = 0;
741 	pages_referenced = 0;
742 	maxscan = min(cnt.v_active_count, MAXSCAN);
743 
744 	/*
745 	 * Gather statistics on page usage.
746 	 */
747 	m = vm_page_queue_active.tqh_first;
748 	while (m && (maxscan-- > 0)) {
749 
750 		++pages_scanned;
751 
752 		next = m->pageq.tqe_next;
753 
754 		/*
755  		 * Dont mess with pages that are busy.
756 		 */
757 		if ((m->flags & PG_BUSY) || (m->hold_count != 0)) {
758 			TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
759 			TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
760 			m = next;
761 			continue;
762 		}
763 
764 		/*
765 		 * Advance pages that have been referenced, decline pages that
766 		 * have not.
767 		 */
768 		if (pmap_is_referenced(VM_PAGE_TO_PHYS(m))) {
769 			pmap_clear_reference(VM_PAGE_TO_PHYS(m));
770 			pages_referenced++;
771 			if (m->act_count < ACT_MAX)
772 				m->act_count += ACT_ADVANCE;
773 			TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
774 			TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
775 			TAILQ_REMOVE(&m->object->memq, m, listq);
776 			TAILQ_INSERT_TAIL(&m->object->memq, m, listq);
777 		} else {
778 			m->act_count -= min(m->act_count, ACT_DECLINE);
779 			/*
780 			 * if the page act_count is zero, and we are low on mem -- then we deactivate
781 			 */
782 			if (!m->act_count &&
783 			    (cnt.v_free_count+cnt.v_inactive_count < cnt.v_free_target+cnt.v_inactive_target )) {
784 				vm_page_deactivate(m);
785 			/*
786 			 * else if on the next go-around we will deactivate the page
787 			 * we need to place the page on the end of the queue to age
788 			 * the other pages in memory.
789 			 */
790 			} else {
791 				TAILQ_REMOVE(&vm_page_queue_active, m, pageq);
792 				TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
793 				TAILQ_REMOVE(&m->object->memq, m, listq);
794 				TAILQ_INSERT_TAIL(&m->object->memq, m, listq);
795 			}
796 		}
797 		m = next;
798 	}
799 
800 	if (pages_referenced) {
801 		nextscan = (pages_scanned / pages_referenced) * scantick;
802 		nextscan = max(nextscan, scantick);
803 		nextscan = min(nextscan, hz);
804 	} else
805 		nextscan = hz;
806 	tsleep((caddr_t) &vm_pagescanner, PVM, "scanw", nextscan);
807 
808 	goto scanloop;
809 }
810 
811 /*
812  *	vm_pageout is the high level pageout daemon.
813  */
814 void
815 vm_pageout()
816 {
817 	extern npendingio, swiopend;
818 	static nowakeup;
819 	(void) spl0();
820 
821 	/*
822 	 *	Initialize some paging parameters.
823 	 */
824 
825 vmretry:
826 	cnt.v_free_min = 12;
827 	cnt.v_free_reserved = 8;
828 	if (cnt.v_free_min < 8)
829 		cnt.v_free_min = 8;
830 	if (cnt.v_free_min > 32)
831 		cnt.v_free_min = 32;
832 	vm_pageout_free_min = 4;
833 	cnt.v_free_target = 2*cnt.v_free_min + cnt.v_free_reserved;
834 	cnt.v_inactive_target = cnt.v_free_count / 12;
835 	cnt.v_free_min += cnt.v_free_reserved;
836 
837         /* XXX does not really belong here */
838 	if (vm_page_max_wired == 0)
839 		vm_page_max_wired = cnt.v_free_count / 3;
840 
841 
842 	(void) swap_pager_alloc(0, 0, 0, 0);
843 
844 	/*
845 	 *	The pageout daemon is never done, so loop
846 	 *	forever.
847 	 */
848 	while (TRUE) {
849 		int force_wakeup;
850 		extern struct loadavg averunnable;
851 /*
852 		cnt.v_free_min = 12 + averunnable.ldavg[0] / 1024;
853 		cnt.v_free_target = 2*cnt.v_free_min + cnt.v_free_reserved;
854 		cnt.v_inactive_target = cnt.v_free_target*2;
855 */
856 
857 		tsleep((caddr_t) &vm_pages_needed, PVM, "psleep", 0);
858 
859 		vm_pager_sync();
860 		/*
861 		 * The force wakeup hack added to eliminate delays and potiential
862 		 * deadlock.  It was possible for the page daemon to indefintely
863 		 * postpone waking up a process that it might be waiting for memory
864 		 * on.  The putmulti stuff seems to have aggravated the situation.
865 		 */
866 		force_wakeup = vm_pageout_scan();
867 		vm_pager_sync();
868 		if( force_wakeup)
869 			wakeup( (caddr_t) &cnt.v_free_count);
870 		cnt.v_scan++;
871 		wakeup((caddr_t) kmem_map);
872 	}
873 }
874 
875