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