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