Deleted Added
full compact
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 * $FreeBSD: head/sys/vm/vm_pageout.c 92588 2002-03-18 15:08:09Z green $
68 * $FreeBSD: head/sys/vm/vm_pageout.c 92654 2002-03-19 09:11:49Z jeff $
69 */
70
71/*
72 * The proverbial page-out daemon.
73 */
74
75#include "opt_vm.h"
76#include <sys/param.h>
77#include <sys/systm.h>
78#include <sys/kernel.h>
79#include <sys/lock.h>
80#include <sys/mutex.h>
81#include <sys/proc.h>
82#include <sys/kthread.h>
83#include <sys/ktr.h>
84#include <sys/resourcevar.h>
85#include <sys/signalvar.h>
86#include <sys/vnode.h>
87#include <sys/vmmeter.h>
88#include <sys/sx.h>
89#include <sys/sysctl.h>
90
91#include <vm/vm.h>
92#include <vm/vm_param.h>
93#include <vm/vm_object.h>
94#include <vm/vm_page.h>
95#include <vm/vm_map.h>
96#include <vm/vm_pageout.h>
97#include <vm/vm_pager.h>
98#include <vm/vm_zone.h>
99#include <vm/swap_pager.h>
100#include <vm/vm_extern.h>
101
102#include <machine/mutex.h>
103
104/*
105 * System initialization
106 */
107
108/* the kernel process "vm_pageout"*/
109static void vm_pageout __P((void));
110static int vm_pageout_clean __P((vm_page_t));
111static void vm_pageout_scan __P((int pass));
112static int vm_pageout_free_page_calc __P((vm_size_t count));
113struct proc *pageproc;
114
115static struct kproc_desc page_kp = {
116 "pagedaemon",
117 vm_pageout,
118 &pageproc
119};
120SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp)
121
122#if !defined(NO_SWAPPING)
123/* the kernel process "vm_daemon"*/
124static void vm_daemon __P((void));
125static struct proc *vmproc;
126
127static struct kproc_desc vm_kp = {
128 "vmdaemon",
129 vm_daemon,
130 &vmproc
131};
132SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp)
133#endif
134
135
136int vm_pages_needed=0; /* Event on which pageout daemon sleeps */
137int vm_pageout_deficit=0; /* Estimated number of pages deficit */
138int vm_pageout_pages_needed=0; /* flag saying that the pageout daemon needs pages */
139
140#if !defined(NO_SWAPPING)
141static int vm_pageout_req_swapout; /* XXX */
142static int vm_daemon_needed;
143#endif
144extern int vm_swap_size;
145static int vm_max_launder = 32;
146static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
147static int vm_pageout_full_stats_interval = 0;
148static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0;
149static int defer_swap_pageouts=0;
150static int disable_swap_pageouts=0;
151
152#if defined(NO_SWAPPING)
153static int vm_swap_enabled=0;
154static int vm_swap_idle_enabled=0;
155#else
156static int vm_swap_enabled=1;
157static int vm_swap_idle_enabled=0;
158#endif
159
160SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
161 CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
162
163SYSCTL_INT(_vm, OID_AUTO, max_launder,
164 CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
165
166SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
167 CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
168
169SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
170 CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
171
172SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
173 CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
174
175SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max,
176 CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented");
177
178#if defined(NO_SWAPPING)
179SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
180 CTLFLAG_RD, &vm_swap_enabled, 0, "");
181SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
182 CTLFLAG_RD, &vm_swap_idle_enabled, 0, "");
183#else
184SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
185 CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
186SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
187 CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
188#endif
189
190SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
191 CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
192
193SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
194 CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
195
196static int pageout_lock_miss;
197SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
198 CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
199
200#define VM_PAGEOUT_PAGE_COUNT 16
201int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
202
203int vm_page_max_wired; /* XXX max # of wired pages system-wide */
204
205#if !defined(NO_SWAPPING)
206typedef void freeer_fcn_t __P((vm_map_t, vm_object_t, vm_pindex_t, int));
207static void vm_pageout_map_deactivate_pages __P((vm_map_t, vm_pindex_t));
208static freeer_fcn_t vm_pageout_object_deactivate_pages;
209static void vm_req_vmdaemon __P((void));
210#endif
211static void vm_pageout_page_stats(void);
212
213/*
214 * vm_pageout_clean:
215 *
216 * Clean the page and remove it from the laundry.
217 *
218 * We set the busy bit to cause potential page faults on this page to
219 * block. Note the careful timing, however, the busy bit isn't set till
220 * late and we cannot do anything that will mess with the page.
221 */
222static int
223vm_pageout_clean(m)
224 vm_page_t m;
225{
226 vm_object_t object;
227 vm_page_t mc[2*vm_pageout_page_count];
228 int pageout_count;
229 int ib, is, page_base;
230 vm_pindex_t pindex = m->pindex;
231
232 GIANT_REQUIRED;
233
234 object = m->object;
235
236 /*
237 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
238 * with the new swapper, but we could have serious problems paging
239 * out other object types if there is insufficient memory.
240 *
241 * Unfortunately, checking free memory here is far too late, so the
242 * check has been moved up a procedural level.
243 */
244
245 /*
246 * Don't mess with the page if it's busy, held, or special
247 */
248 if ((m->hold_count != 0) ||
249 ((m->busy != 0) || (m->flags & (PG_BUSY|PG_UNMANAGED)))) {
250 return 0;
251 }
252
253 mc[vm_pageout_page_count] = m;
254 pageout_count = 1;
255 page_base = vm_pageout_page_count;
256 ib = 1;
257 is = 1;
258
259 /*
260 * Scan object for clusterable pages.
261 *
262 * We can cluster ONLY if: ->> the page is NOT
263 * clean, wired, busy, held, or mapped into a
264 * buffer, and one of the following:
265 * 1) The page is inactive, or a seldom used
266 * active page.
267 * -or-
268 * 2) we force the issue.
269 *
270 * During heavy mmap/modification loads the pageout
271 * daemon can really fragment the underlying file
272 * due to flushing pages out of order and not trying
273 * align the clusters (which leave sporatic out-of-order
274 * holes). To solve this problem we do the reverse scan
275 * first and attempt to align our cluster, then do a
276 * forward scan if room remains.
277 */
278more:
279 while (ib && pageout_count < vm_pageout_page_count) {
280 vm_page_t p;
281
282 if (ib > pindex) {
283 ib = 0;
284 break;
285 }
286
287 if ((p = vm_page_lookup(object, pindex - ib)) == NULL) {
288 ib = 0;
289 break;
290 }
291 if (((p->queue - p->pc) == PQ_CACHE) ||
292 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
293 ib = 0;
294 break;
295 }
296 vm_page_test_dirty(p);
297 if ((p->dirty & p->valid) == 0 ||
298 p->queue != PQ_INACTIVE ||
299 p->wire_count != 0 || /* may be held by buf cache */
300 p->hold_count != 0) { /* may be undergoing I/O */
301 ib = 0;
302 break;
303 }
304 mc[--page_base] = p;
305 ++pageout_count;
306 ++ib;
307 /*
308 * alignment boundry, stop here and switch directions. Do
309 * not clear ib.
310 */
311 if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
312 break;
313 }
314
315 while (pageout_count < vm_pageout_page_count &&
316 pindex + is < object->size) {
317 vm_page_t p;
318
319 if ((p = vm_page_lookup(object, pindex + is)) == NULL)
320 break;
321 if (((p->queue - p->pc) == PQ_CACHE) ||
322 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
323 break;
324 }
325 vm_page_test_dirty(p);
326 if ((p->dirty & p->valid) == 0 ||
327 p->queue != PQ_INACTIVE ||
328 p->wire_count != 0 || /* may be held by buf cache */
329 p->hold_count != 0) { /* may be undergoing I/O */
330 break;
331 }
332 mc[page_base + pageout_count] = p;
333 ++pageout_count;
334 ++is;
335 }
336
337 /*
338 * If we exhausted our forward scan, continue with the reverse scan
339 * when possible, even past a page boundry. This catches boundry
340 * conditions.
341 */
342 if (ib && pageout_count < vm_pageout_page_count)
343 goto more;
344
345 /*
346 * we allow reads during pageouts...
347 */
348 return vm_pageout_flush(&mc[page_base], pageout_count, 0);
349}
350
351/*
352 * vm_pageout_flush() - launder the given pages
353 *
354 * The given pages are laundered. Note that we setup for the start of
355 * I/O ( i.e. busy the page ), mark it read-only, and bump the object
356 * reference count all in here rather then in the parent. If we want
357 * the parent to do more sophisticated things we may have to change
358 * the ordering.
359 */
360int
361vm_pageout_flush(mc, count, flags)
362 vm_page_t *mc;
363 int count;
364 int flags;
365{
366 vm_object_t object;
367 int pageout_status[count];
368 int numpagedout = 0;
369 int i;
370
371 GIANT_REQUIRED;
372 /*
373 * Initiate I/O. Bump the vm_page_t->busy counter and
374 * mark the pages read-only.
375 *
376 * We do not have to fixup the clean/dirty bits here... we can
377 * allow the pager to do it after the I/O completes.
378 *
379 * NOTE! mc[i]->dirty may be partial or fragmented due to an
380 * edge case with file fragments.
381 */
382 for (i = 0; i < count; i++) {
383 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, ("vm_pageout_flush page %p index %d/%d: partially invalid page", mc[i], i, count));
384 vm_page_io_start(mc[i]);
385 vm_page_protect(mc[i], VM_PROT_READ);
386 }
387
388 object = mc[0]->object;
389 vm_object_pip_add(object, count);
390
391 vm_pager_put_pages(object, mc, count,
392 (flags | ((object == kernel_object) ? OBJPC_SYNC : 0)),
393 pageout_status);
394
395 for (i = 0; i < count; i++) {
396 vm_page_t mt = mc[i];
397
398 switch (pageout_status[i]) {
399 case VM_PAGER_OK:
400 numpagedout++;
401 break;
402 case VM_PAGER_PEND:
403 numpagedout++;
404 break;
405 case VM_PAGER_BAD:
406 /*
407 * Page outside of range of object. Right now we
408 * essentially lose the changes by pretending it
409 * worked.
410 */
411 pmap_clear_modify(mt);
412 vm_page_undirty(mt);
413 break;
414 case VM_PAGER_ERROR:
415 case VM_PAGER_FAIL:
416 /*
417 * If page couldn't be paged out, then reactivate the
418 * page so it doesn't clog the inactive list. (We
419 * will try paging out it again later).
420 */
421 vm_page_activate(mt);
422 break;
423 case VM_PAGER_AGAIN:
424 break;
425 }
426
427 /*
428 * If the operation is still going, leave the page busy to
429 * block all other accesses. Also, leave the paging in
430 * progress indicator set so that we don't attempt an object
431 * collapse.
432 */
433 if (pageout_status[i] != VM_PAGER_PEND) {
434 vm_object_pip_wakeup(object);
435 vm_page_io_finish(mt);
436 if (!vm_page_count_severe() || !vm_page_try_to_cache(mt))
437 vm_page_protect(mt, VM_PROT_READ);
438 }
439 }
440 return numpagedout;
441}
442
443#if !defined(NO_SWAPPING)
444/*
445 * vm_pageout_object_deactivate_pages
446 *
447 * deactivate enough pages to satisfy the inactive target
448 * requirements or if vm_page_proc_limit is set, then
449 * deactivate all of the pages in the object and its
450 * backing_objects.
451 *
452 * The object and map must be locked.
453 */
454static void
455vm_pageout_object_deactivate_pages(map, object, desired, map_remove_only)
456 vm_map_t map;
457 vm_object_t object;
458 vm_pindex_t desired;
459 int map_remove_only;
460{
461 vm_page_t p, next;
462 int rcount;
463 int remove_mode;
464
465 GIANT_REQUIRED;
466 if (object->type == OBJT_DEVICE || object->type == OBJT_PHYS)
467 return;
468
469 while (object) {
470 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
471 return;
472 if (object->paging_in_progress)
473 return;
474
475 remove_mode = map_remove_only;
476 if (object->shadow_count > 1)
477 remove_mode = 1;
478 /*
479 * scan the objects entire memory queue
480 */
481 rcount = object->resident_page_count;
482 p = TAILQ_FIRST(&object->memq);
483 while (p && (rcount-- > 0)) {
484 int actcount;
485 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
486 return;
487 next = TAILQ_NEXT(p, listq);
488 cnt.v_pdpages++;
489 if (p->wire_count != 0 ||
490 p->hold_count != 0 ||
491 p->busy != 0 ||
492 (p->flags & (PG_BUSY|PG_UNMANAGED)) ||
493 !pmap_page_exists_quick(vm_map_pmap(map), p)) {
494 p = next;
495 continue;
496 }
497
498 actcount = pmap_ts_referenced(p);
499 if (actcount) {
500 vm_page_flag_set(p, PG_REFERENCED);
501 } else if (p->flags & PG_REFERENCED) {
502 actcount = 1;
503 }
504
505 if ((p->queue != PQ_ACTIVE) &&
506 (p->flags & PG_REFERENCED)) {
507 vm_page_activate(p);
508 p->act_count += actcount;
509 vm_page_flag_clear(p, PG_REFERENCED);
510 } else if (p->queue == PQ_ACTIVE) {
511 if ((p->flags & PG_REFERENCED) == 0) {
512 p->act_count -= min(p->act_count, ACT_DECLINE);
513 if (!remove_mode && (vm_pageout_algorithm || (p->act_count == 0))) {
514 vm_page_protect(p, VM_PROT_NONE);
515 vm_page_deactivate(p);
516 } else {
517 vm_pageq_requeue(p);
518 }
519 } else {
520 vm_page_activate(p);
521 vm_page_flag_clear(p, PG_REFERENCED);
522 if (p->act_count < (ACT_MAX - ACT_ADVANCE))
523 p->act_count += ACT_ADVANCE;
524 vm_pageq_requeue(p);
525 }
526 } else if (p->queue == PQ_INACTIVE) {
527 vm_page_protect(p, VM_PROT_NONE);
528 }
529 p = next;
530 }
531 object = object->backing_object;
532 }
533 return;
534}
535
536/*
537 * deactivate some number of pages in a map, try to do it fairly, but
538 * that is really hard to do.
539 */
540static void
541vm_pageout_map_deactivate_pages(map, desired)
542 vm_map_t map;
543 vm_pindex_t desired;
544{
545 vm_map_entry_t tmpe;
546 vm_object_t obj, bigobj;
547 int nothingwired;
548
549 GIANT_REQUIRED;
550 if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT, (void *)0, curthread)) {
551 return;
552 }
553
554 bigobj = NULL;
555 nothingwired = TRUE;
556
557 /*
558 * first, search out the biggest object, and try to free pages from
559 * that.
560 */
561 tmpe = map->header.next;
562 while (tmpe != &map->header) {
563 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
564 obj = tmpe->object.vm_object;
565 if ((obj != NULL) && (obj->shadow_count <= 1) &&
566 ((bigobj == NULL) ||
567 (bigobj->resident_page_count < obj->resident_page_count))) {
568 bigobj = obj;
569 }
570 }
571 if (tmpe->wired_count > 0)
572 nothingwired = FALSE;
573 tmpe = tmpe->next;
574 }
575
576 if (bigobj)
577 vm_pageout_object_deactivate_pages(map, bigobj, desired, 0);
578
579 /*
580 * Next, hunt around for other pages to deactivate. We actually
581 * do this search sort of wrong -- .text first is not the best idea.
582 */
583 tmpe = map->header.next;
584 while (tmpe != &map->header) {
585 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
586 break;
587 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
588 obj = tmpe->object.vm_object;
589 if (obj)
590 vm_pageout_object_deactivate_pages(map, obj, desired, 0);
591 }
592 tmpe = tmpe->next;
593 };
594
595 /*
596 * Remove all mappings if a process is swapped out, this will free page
597 * table pages.
598 */
599 if (desired == 0 && nothingwired)
600 pmap_remove(vm_map_pmap(map),
601 VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS);
602 vm_map_unlock(map);
603 return;
604}
605#endif /* !defined(NO_SWAPPING) */
606
607/*
608 * Don't try to be fancy - being fancy can lead to VOP_LOCK's and therefore
609 * to vnode deadlocks. We only do it for OBJT_DEFAULT and OBJT_SWAP objects
610 * which we know can be trivially freed.
611 */
612void
613vm_pageout_page_free(vm_page_t m) {
614 vm_object_t object = m->object;
615 int type = object->type;
616
617 GIANT_REQUIRED;
618 if (type == OBJT_SWAP || type == OBJT_DEFAULT)
619 vm_object_reference(object);
620 vm_page_busy(m);
621 vm_page_protect(m, VM_PROT_NONE);
622 vm_page_free(m);
623 if (type == OBJT_SWAP || type == OBJT_DEFAULT)
624 vm_object_deallocate(object);
625}
626
627/*
628 * vm_pageout_scan does the dirty work for the pageout daemon.
629 */
630static void
631vm_pageout_scan(int pass)
632{
633 vm_page_t m, next;
634 struct vm_page marker;
635 int save_page_shortage;
636 int save_inactive_count;
637 int page_shortage, maxscan, pcount;
638 int addl_page_shortage, addl_page_shortage_init;
639 struct proc *p, *bigproc;
640 vm_offset_t size, bigsize;
641 vm_object_t object;
642 int actcount;
643 int vnodes_skipped = 0;
644 int maxlaunder;
645 int s;
646
647 GIANT_REQUIRED;
648 /*
649 * Do whatever cleanup that the pmap code can.
650 */
651 pmap_collect();
652 uma_reclaim();
653
654 addl_page_shortage_init = vm_pageout_deficit;
655 vm_pageout_deficit = 0;
656
657 /*
658 * Calculate the number of pages we want to either free or move
659 * to the cache.
660 */
661 page_shortage = vm_paging_target() + addl_page_shortage_init;
662 save_page_shortage = page_shortage;
663 save_inactive_count = cnt.v_inactive_count;
664
665 /*
666 * Initialize our marker
667 */
668 bzero(&marker, sizeof(marker));
669 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
670 marker.queue = PQ_INACTIVE;
671 marker.wire_count = 1;
672
673 /*
674 * Start scanning the inactive queue for pages we can move to the
675 * cache or free. The scan will stop when the target is reached or
676 * we have scanned the entire inactive queue. Note that m->act_count
677 * is not used to form decisions for the inactive queue, only for the
678 * active queue.
679 *
680 * maxlaunder limits the number of dirty pages we flush per scan.
681 * For most systems a smaller value (16 or 32) is more robust under
682 * extreme memory and disk pressure because any unnecessary writes
683 * to disk can result in extreme performance degredation. However,
684 * systems with excessive dirty pages (especially when MAP_NOSYNC is
685 * used) will die horribly with limited laundering. If the pageout
686 * daemon cannot clean enough pages in the first pass, we let it go
687 * all out in succeeding passes.
688 */
689 if ((maxlaunder = vm_max_launder) <= 1)
690 maxlaunder = 1;
691 if (pass)
692 maxlaunder = 10000;
693rescan0:
694 addl_page_shortage = addl_page_shortage_init;
695 maxscan = cnt.v_inactive_count;
696
697 for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
698 m != NULL && maxscan-- > 0 && page_shortage > 0;
699 m = next) {
700
701 cnt.v_pdpages++;
702
703 if (m->queue != PQ_INACTIVE) {
704 goto rescan0;
705 }
706
707 next = TAILQ_NEXT(m, pageq);
708
709 /*
710 * skip marker pages
711 */
712 if (m->flags & PG_MARKER)
713 continue;
714
715 /*
716 * A held page may be undergoing I/O, so skip it.
717 */
718 if (m->hold_count) {
719 vm_pageq_requeue(m);
720 addl_page_shortage++;
721 continue;
722 }
723 /*
724 * Don't mess with busy pages, keep in the front of the
725 * queue, most likely are being paged out.
726 */
727 if (m->busy || (m->flags & PG_BUSY)) {
728 addl_page_shortage++;
729 continue;
730 }
731
732 /*
733 * If the object is not being used, we ignore previous
734 * references.
735 */
736 if (m->object->ref_count == 0) {
737 vm_page_flag_clear(m, PG_REFERENCED);
738 pmap_clear_reference(m);
739
740 /*
741 * Otherwise, if the page has been referenced while in the
742 * inactive queue, we bump the "activation count" upwards,
743 * making it less likely that the page will be added back to
744 * the inactive queue prematurely again. Here we check the
745 * page tables (or emulated bits, if any), given the upper
746 * level VM system not knowing anything about existing
747 * references.
748 */
749 } else if (((m->flags & PG_REFERENCED) == 0) &&
750 (actcount = pmap_ts_referenced(m))) {
751 vm_page_activate(m);
752 m->act_count += (actcount + ACT_ADVANCE);
753 continue;
754 }
755
756 /*
757 * If the upper level VM system knows about any page
758 * references, we activate the page. We also set the
759 * "activation count" higher than normal so that we will less
760 * likely place pages back onto the inactive queue again.
761 */
762 if ((m->flags & PG_REFERENCED) != 0) {
763 vm_page_flag_clear(m, PG_REFERENCED);
764 actcount = pmap_ts_referenced(m);
765 vm_page_activate(m);
766 m->act_count += (actcount + ACT_ADVANCE + 1);
767 continue;
768 }
769
770 /*
771 * If the upper level VM system doesn't know anything about
772 * the page being dirty, we have to check for it again. As
773 * far as the VM code knows, any partially dirty pages are
774 * fully dirty.
775 */
776 if (m->dirty == 0) {
777 vm_page_test_dirty(m);
778 } else {
779 vm_page_dirty(m);
780 }
781
782 /*
783 * Invalid pages can be easily freed
784 */
785 if (m->valid == 0) {
786 vm_pageout_page_free(m);
787 cnt.v_dfree++;
788 --page_shortage;
789
790 /*
791 * Clean pages can be placed onto the cache queue. This
792 * effectively frees them.
793 */
794 } else if (m->dirty == 0) {
795 vm_page_cache(m);
796 --page_shortage;
797 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
798 /*
799 * Dirty pages need to be paged out, but flushing
800 * a page is extremely expensive verses freeing
801 * a clean page. Rather then artificially limiting
802 * the number of pages we can flush, we instead give
803 * dirty pages extra priority on the inactive queue
804 * by forcing them to be cycled through the queue
805 * twice before being flushed, after which the
806 * (now clean) page will cycle through once more
807 * before being freed. This significantly extends
808 * the thrash point for a heavily loaded machine.
809 */
810 vm_page_flag_set(m, PG_WINATCFLS);
811 vm_pageq_requeue(m);
812 } else if (maxlaunder > 0) {
813 /*
814 * We always want to try to flush some dirty pages if
815 * we encounter them, to keep the system stable.
816 * Normally this number is small, but under extreme
817 * pressure where there are insufficient clean pages
818 * on the inactive queue, we may have to go all out.
819 */
820 int swap_pageouts_ok;
821 struct vnode *vp = NULL;
822 struct mount *mp;
823
824 object = m->object;
825
826 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
827 swap_pageouts_ok = 1;
828 } else {
829 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
830 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
831 vm_page_count_min());
832
833 }
834
835 /*
836 * We don't bother paging objects that are "dead".
837 * Those objects are in a "rundown" state.
838 */
839 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
840 vm_pageq_requeue(m);
841 continue;
842 }
843
844 /*
845 * The object is already known NOT to be dead. It
846 * is possible for the vget() to block the whole
847 * pageout daemon, but the new low-memory handling
848 * code should prevent it.
849 *
850 * The previous code skipped locked vnodes and, worse,
851 * reordered pages in the queue. This results in
852 * completely non-deterministic operation and, on a
853 * busy system, can lead to extremely non-optimal
854 * pageouts. For example, it can cause clean pages
855 * to be freed and dirty pages to be moved to the end
856 * of the queue. Since dirty pages are also moved to
857 * the end of the queue once-cleaned, this gives
858 * way too large a weighting to defering the freeing
859 * of dirty pages.
860 *
861 * We can't wait forever for the vnode lock, we might
862 * deadlock due to a vn_read() getting stuck in
863 * vm_wait while holding this vnode. We skip the
864 * vnode if we can't get it in a reasonable amount
865 * of time.
866 */
867 if (object->type == OBJT_VNODE) {
868 vp = object->handle;
869
870 mp = NULL;
871 if (vp->v_type == VREG)
872 vn_start_write(vp, &mp, V_NOWAIT);
873 if (vget(vp, LK_EXCLUSIVE|LK_NOOBJ|LK_TIMELOCK, curthread)) {
874 ++pageout_lock_miss;
875 vn_finished_write(mp);
876 if (object->flags & OBJ_MIGHTBEDIRTY)
877 vnodes_skipped++;
878 continue;
879 }
880
881 /*
882 * The page might have been moved to another
883 * queue during potential blocking in vget()
884 * above. The page might have been freed and
885 * reused for another vnode. The object might
886 * have been reused for another vnode.
887 */
888 if (m->queue != PQ_INACTIVE ||
889 m->object != object ||
890 object->handle != vp) {
891 if (object->flags & OBJ_MIGHTBEDIRTY)
892 vnodes_skipped++;
893 vput(vp);
894 vn_finished_write(mp);
895 continue;
896 }
897
898 /*
899 * The page may have been busied during the
900 * blocking in vput(); We don't move the
901 * page back onto the end of the queue so that
902 * statistics are more correct if we don't.
903 */
904 if (m->busy || (m->flags & PG_BUSY)) {
905 vput(vp);
906 vn_finished_write(mp);
907 continue;
908 }
909
910 /*
911 * If the page has become held it might
912 * be undergoing I/O, so skip it
913 */
914 if (m->hold_count) {
915 vm_pageq_requeue(m);
916 if (object->flags & OBJ_MIGHTBEDIRTY)
917 vnodes_skipped++;
918 vput(vp);
919 vn_finished_write(mp);
920 continue;
921 }
922 }
923
924 /*
925 * If a page is dirty, then it is either being washed
926 * (but not yet cleaned) or it is still in the
927 * laundry. If it is still in the laundry, then we
928 * start the cleaning operation.
929 *
930 * This operation may cluster, invalidating the 'next'
931 * pointer. To prevent an inordinate number of
932 * restarts we use our marker to remember our place.
933 *
934 * decrement page_shortage on success to account for
935 * the (future) cleaned page. Otherwise we could wind
936 * up laundering or cleaning too many pages.
937 */
938 s = splvm();
939 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m, &marker, pageq);
940 splx(s);
941 if (vm_pageout_clean(m) != 0) {
942 --page_shortage;
943 --maxlaunder;
944 }
945 s = splvm();
946 next = TAILQ_NEXT(&marker, pageq);
947 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq);
948 splx(s);
949 if (vp) {
950 vput(vp);
951 vn_finished_write(mp);
952 }
953 }
954 }
955
956 /*
957 * Compute the number of pages we want to try to move from the
958 * active queue to the inactive queue.
959 */
960 page_shortage = vm_paging_target() +
961 cnt.v_inactive_target - cnt.v_inactive_count;
962 page_shortage += addl_page_shortage;
963
964 /*
965 * Scan the active queue for things we can deactivate. We nominally
966 * track the per-page activity counter and use it to locate
967 * deactivation candidates.
968 */
969 pcount = cnt.v_active_count;
970 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
971
972 while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
973
974 /*
975 * This is a consistency check, and should likely be a panic
976 * or warning.
977 */
978 if (m->queue != PQ_ACTIVE) {
979 break;
980 }
981
982 next = TAILQ_NEXT(m, pageq);
983 /*
984 * Don't deactivate pages that are busy.
985 */
986 if ((m->busy != 0) ||
987 (m->flags & PG_BUSY) ||
988 (m->hold_count != 0)) {
989 vm_pageq_requeue(m);
990 m = next;
991 continue;
992 }
993
994 /*
995 * The count for pagedaemon pages is done after checking the
996 * page for eligibility...
997 */
998 cnt.v_pdpages++;
999
1000 /*
1001 * Check to see "how much" the page has been used.
1002 */
1003 actcount = 0;
1004 if (m->object->ref_count != 0) {
1005 if (m->flags & PG_REFERENCED) {
1006 actcount += 1;
1007 }
1008 actcount += pmap_ts_referenced(m);
1009 if (actcount) {
1010 m->act_count += ACT_ADVANCE + actcount;
1011 if (m->act_count > ACT_MAX)
1012 m->act_count = ACT_MAX;
1013 }
1014 }
1015
1016 /*
1017 * Since we have "tested" this bit, we need to clear it now.
1018 */
1019 vm_page_flag_clear(m, PG_REFERENCED);
1020
1021 /*
1022 * Only if an object is currently being used, do we use the
1023 * page activation count stats.
1024 */
1025 if (actcount && (m->object->ref_count != 0)) {
1026 vm_pageq_requeue(m);
1027 } else {
1028 m->act_count -= min(m->act_count, ACT_DECLINE);
1029 if (vm_pageout_algorithm ||
1030 m->object->ref_count == 0 ||
1031 m->act_count == 0) {
1032 page_shortage--;
1033 if (m->object->ref_count == 0) {
1034 vm_page_protect(m, VM_PROT_NONE);
1035 if (m->dirty == 0)
1036 vm_page_cache(m);
1037 else
1038 vm_page_deactivate(m);
1039 } else {
1040 vm_page_deactivate(m);
1041 }
1042 } else {
1043 vm_pageq_requeue(m);
1044 }
1045 }
1046 m = next;
1047 }
1048
1049 s = splvm();
1050
1051 /*
1052 * We try to maintain some *really* free pages, this allows interrupt
1053 * code to be guaranteed space. Since both cache and free queues
1054 * are considered basically 'free', moving pages from cache to free
1055 * does not effect other calculations.
1056 */
1057 while (cnt.v_free_count < cnt.v_free_reserved) {
1058 static int cache_rover = 0;
1059 m = vm_pageq_find(PQ_CACHE, cache_rover, FALSE);
1060 if (!m)
1061 break;
1062 if ((m->flags & (PG_BUSY|PG_UNMANAGED)) ||
1063 m->busy ||
1064 m->hold_count ||
1065 m->wire_count) {
1066#ifdef INVARIANTS
1067 printf("Warning: busy page %p found in cache\n", m);
1068#endif
1069 vm_page_deactivate(m);
1070 continue;
1071 }
1072 cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK;
1073 vm_pageout_page_free(m);
1074 cnt.v_dfree++;
1075 }
1076 splx(s);
1077
1078#if !defined(NO_SWAPPING)
1079 /*
1080 * Idle process swapout -- run once per second.
1081 */
1082 if (vm_swap_idle_enabled) {
1083 static long lsec;
1084 if (time_second != lsec) {
1085 vm_pageout_req_swapout |= VM_SWAP_IDLE;
1086 vm_req_vmdaemon();
1087 lsec = time_second;
1088 }
1089 }
1090#endif
1091
1092 /*
1093 * If we didn't get enough free pages, and we have skipped a vnode
1094 * in a writeable object, wakeup the sync daemon. And kick swapout
1095 * if we did not get enough free pages.
1096 */
1097 if (vm_paging_target() > 0) {
1098 if (vnodes_skipped && vm_page_count_min())
1099 (void) speedup_syncer();
1100#if !defined(NO_SWAPPING)
1101 if (vm_swap_enabled && vm_page_count_target()) {
1102 vm_req_vmdaemon();
1103 vm_pageout_req_swapout |= VM_SWAP_NORMAL;
1104 }
1105#endif
1106 }
1107
1108 /*
1109 * If we are out of swap and were not able to reach our paging
1110 * target, kill the largest process.
1111 *
1112 * We keep the process bigproc locked once we find it to keep anyone
1113 * from messing with it; however, there is a possibility of
1114 * deadlock if process B is bigproc and one of it's child processes
1115 * attempts to propagate a signal to B while we are waiting for A's
1116 * lock while walking this list. To avoid this, we don't block on
1117 * the process lock but just skip a process if it is already locked.
1118 */
1119 if ((vm_swap_size < 64 && vm_page_count_min()) ||
1120 (swap_pager_full && vm_paging_target() > 0)) {
1121#if 0
1122 if ((vm_swap_size < 64 || swap_pager_full) && vm_page_count_min()) {
1123#endif
1124 bigproc = NULL;
1125 bigsize = 0;
1126 sx_slock(&allproc_lock);
1127 LIST_FOREACH(p, &allproc, p_list) {
1128 /*
1129 * If this process is already locked, skip it.
1130 */
1131 if (PROC_TRYLOCK(p) == 0)
1132 continue;
1133 /*
1134 * if this is a system process, skip it
1135 */
1136 if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) ||
1137 ((p->p_pid < 48) && (vm_swap_size != 0))) {
1138 PROC_UNLOCK(p);
1139 continue;
1140 }
1141 /*
1142 * if the process is in a non-running type state,
1143 * don't touch it.
1144 */
1145 mtx_lock_spin(&sched_lock);
1146 if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
1147 mtx_unlock_spin(&sched_lock);
1148 PROC_UNLOCK(p);
1149 continue;
1150 }
1151 mtx_unlock_spin(&sched_lock);
1152 /*
1153 * get the process size
1154 */
1155 size = vmspace_resident_count(p->p_vmspace) +
1156 vmspace_swap_count(p->p_vmspace);
1157 /*
1158 * if the this process is bigger than the biggest one
1159 * remember it.
1160 */
1161 if (size > bigsize) {
1162 if (bigproc != NULL)
1163 PROC_UNLOCK(bigproc);
1164 bigproc = p;
1165 bigsize = size;
1166 } else
1167 PROC_UNLOCK(p);
1168 }
1169 sx_sunlock(&allproc_lock);
1170 if (bigproc != NULL) {
1171 struct ksegrp *kg;
1172 killproc(bigproc, "out of swap space");
1173 mtx_lock_spin(&sched_lock);
1174 FOREACH_KSEGRP_IN_PROC(bigproc, kg) {
1175 kg->kg_estcpu = 0;
1176 kg->kg_nice = PRIO_MIN; /* XXXKSE ??? */
1177 resetpriority(kg);
1178 }
1179 mtx_unlock_spin(&sched_lock);
1180 PROC_UNLOCK(bigproc);
1181 wakeup(&cnt.v_free_count);
1182 }
1183 }
1184}
1185
1186/*
1187 * This routine tries to maintain the pseudo LRU active queue,
1188 * so that during long periods of time where there is no paging,
1189 * that some statistic accumulation still occurs. This code
1190 * helps the situation where paging just starts to occur.
1191 */
1192static void
1193vm_pageout_page_stats()
1194{
1195 vm_page_t m,next;
1196 int pcount,tpcount; /* Number of pages to check */
1197 static int fullintervalcount = 0;
1198 int page_shortage;
1199 int s0;
1200
1201 page_shortage =
1202 (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) -
1203 (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
1204
1205 if (page_shortage <= 0)
1206 return;
1207
1208 s0 = splvm();
1209
1210 pcount = cnt.v_active_count;
1211 fullintervalcount += vm_pageout_stats_interval;
1212 if (fullintervalcount < vm_pageout_full_stats_interval) {
1213 tpcount = (vm_pageout_stats_max * cnt.v_active_count) / cnt.v_page_count;
1214 if (pcount > tpcount)
1215 pcount = tpcount;
1216 } else {
1217 fullintervalcount = 0;
1218 }
1219
1220 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1221 while ((m != NULL) && (pcount-- > 0)) {
1222 int actcount;
1223
1224 if (m->queue != PQ_ACTIVE) {
1225 break;
1226 }
1227
1228 next = TAILQ_NEXT(m, pageq);
1229 /*
1230 * Don't deactivate pages that are busy.
1231 */
1232 if ((m->busy != 0) ||
1233 (m->flags & PG_BUSY) ||
1234 (m->hold_count != 0)) {
1235 vm_pageq_requeue(m);
1236 m = next;
1237 continue;
1238 }
1239
1240 actcount = 0;
1241 if (m->flags & PG_REFERENCED) {
1242 vm_page_flag_clear(m, PG_REFERENCED);
1243 actcount += 1;
1244 }
1245
1246 actcount += pmap_ts_referenced(m);
1247 if (actcount) {
1248 m->act_count += ACT_ADVANCE + actcount;
1249 if (m->act_count > ACT_MAX)
1250 m->act_count = ACT_MAX;
1251 vm_pageq_requeue(m);
1252 } else {
1253 if (m->act_count == 0) {
1254 /*
1255 * We turn off page access, so that we have
1256 * more accurate RSS stats. We don't do this
1257 * in the normal page deactivation when the
1258 * system is loaded VM wise, because the
1259 * cost of the large number of page protect
1260 * operations would be higher than the value
1261 * of doing the operation.
1262 */
1263 vm_page_protect(m, VM_PROT_NONE);
1264 vm_page_deactivate(m);
1265 } else {
1266 m->act_count -= min(m->act_count, ACT_DECLINE);
1267 vm_pageq_requeue(m);
1268 }
1269 }
1270
1271 m = next;
1272 }
1273 splx(s0);
1274}
1275
1276static int
1277vm_pageout_free_page_calc(count)
1278vm_size_t count;
1279{
1280 if (count < cnt.v_page_count)
1281 return 0;
1282 /*
1283 * free_reserved needs to include enough for the largest swap pager
1284 * structures plus enough for any pv_entry structs when paging.
1285 */
1286 if (cnt.v_page_count > 1024)
1287 cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
1288 else
1289 cnt.v_free_min = 4;
1290 cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
1291 cnt.v_interrupt_free_min;
1292 cnt.v_free_reserved = vm_pageout_page_count +
1293 cnt.v_pageout_free_min + (count / 768) + PQ_L2_SIZE;
1294 cnt.v_free_severe = cnt.v_free_min / 2;
1295 cnt.v_free_min += cnt.v_free_reserved;
1296 cnt.v_free_severe += cnt.v_free_reserved;
1297 return 1;
1298}
1299
1300/*
1301 * vm_pageout is the high level pageout daemon.
1302 */
1303static void
1304vm_pageout()
1305{
1306 int pass;
1307
1308 mtx_lock(&Giant);
1309
1310 /*
1311 * Initialize some paging parameters.
1312 */
1313 cnt.v_interrupt_free_min = 2;
1314 if (cnt.v_page_count < 2000)
1315 vm_pageout_page_count = 8;
1316
1317 vm_pageout_free_page_calc(cnt.v_page_count);
1318 /*
1319 * v_free_target and v_cache_min control pageout hysteresis. Note
1320 * that these are more a measure of the VM cache queue hysteresis
1321 * then the VM free queue. Specifically, v_free_target is the
1322 * high water mark (free+cache pages).
1323 *
1324 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
1325 * low water mark, while v_free_min is the stop. v_cache_min must
1326 * be big enough to handle memory needs while the pageout daemon
1327 * is signalled and run to free more pages.
1328 */
1329 if (cnt.v_free_count > 6144)
1330 cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved;
1331 else
1332 cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved;
1333
1334 if (cnt.v_free_count > 2048) {
1335 cnt.v_cache_min = cnt.v_free_target;
1336 cnt.v_cache_max = 2 * cnt.v_cache_min;
1337 cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
1338 } else {
1339 cnt.v_cache_min = 0;
1340 cnt.v_cache_max = 0;
1341 cnt.v_inactive_target = cnt.v_free_count / 4;
1342 }
1343 if (cnt.v_inactive_target > cnt.v_free_count / 3)
1344 cnt.v_inactive_target = cnt.v_free_count / 3;
1345
1346 /* XXX does not really belong here */
1347 if (vm_page_max_wired == 0)
1348 vm_page_max_wired = cnt.v_free_count / 3;
1349
1350 if (vm_pageout_stats_max == 0)
1351 vm_pageout_stats_max = cnt.v_free_target;
1352
1353 /*
1354 * Set interval in seconds for stats scan.
1355 */
1356 if (vm_pageout_stats_interval == 0)
1357 vm_pageout_stats_interval = 5;
1358 if (vm_pageout_full_stats_interval == 0)
1359 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1360
1361 /*
1362 * Set maximum free per pass
1363 */
1364 if (vm_pageout_stats_free_max == 0)
1365 vm_pageout_stats_free_max = 5;
1366
1367 swap_pager_swap_init();
1368 pass = 0;
1369 /*
1370 * The pageout daemon is never done, so loop forever.
1371 */
1372 while (TRUE) {
1373 int error;
1374 int s = splvm();
1375
1376 /*
1377 * If we have enough free memory, wakeup waiters. Do
1378 * not clear vm_pages_needed until we reach our target,
1379 * otherwise we may be woken up over and over again and
1380 * waste a lot of cpu.
1381 */
1382 if (vm_pages_needed && !vm_page_count_min()) {
1383 if (vm_paging_needed() <= 0)
1384 vm_pages_needed = 0;
1385 wakeup(&cnt.v_free_count);
1386 }
1387 if (vm_pages_needed) {
1388 /*
1389 * Still not done, take a second pass without waiting
1390 * (unlimited dirty cleaning), otherwise sleep a bit
1391 * and try again.
1392 */
1393 ++pass;
1394 if (pass > 1)
1395 tsleep(&vm_pages_needed, PVM,
1396 "psleep", hz/2);
1397 } else {
1398 /*
1399 * Good enough, sleep & handle stats. Prime the pass
1400 * for the next run.
1401 */
1402 if (pass > 1)
1403 pass = 1;
1404 else
1405 pass = 0;
1406 error = tsleep(&vm_pages_needed, PVM,
1407 "psleep", vm_pageout_stats_interval * hz);
1408 if (error && !vm_pages_needed) {
1409 splx(s);
1410 pass = 0;
1411 vm_pageout_page_stats();
1412 continue;
1413 }
1414 }
1415
1416 if (vm_pages_needed)
1417 cnt.v_pdwakeups++;
1418 splx(s);
1419 vm_pageout_scan(pass);
1420 vm_pageout_deficit = 0;
1421 }
1422}
1423
1424void
1425pagedaemon_wakeup()
1426{
1427 if (!vm_pages_needed && curthread->td_proc != pageproc) {
1428 vm_pages_needed++;
1429 wakeup(&vm_pages_needed);
1430 }
1431}
1432
1433#if !defined(NO_SWAPPING)
1434static void
1435vm_req_vmdaemon()
1436{
1437 static int lastrun = 0;
1438
1439 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
1440 wakeup(&vm_daemon_needed);
1441 lastrun = ticks;
1442 }
1443}
1444
1445static void
1446vm_daemon()
1447{
1448 struct proc *p;
1449
1450 mtx_lock(&Giant);
1451 while (TRUE) {
1452 tsleep(&vm_daemon_needed, PPAUSE, "psleep", 0);
1453 if (vm_pageout_req_swapout) {
1454 swapout_procs(vm_pageout_req_swapout);
1455 vm_pageout_req_swapout = 0;
1456 }
1457 /*
1458 * scan the processes for exceeding their rlimits or if
1459 * process is swapped out -- deactivate pages
1460 */
1461 sx_slock(&allproc_lock);
1462 LIST_FOREACH(p, &allproc, p_list) {
1463 vm_pindex_t limit, size;
1464
1465 /*
1466 * if this is a system process or if we have already
1467 * looked at this process, skip it.
1468 */
1469 if (p->p_flag & (P_SYSTEM | P_WEXIT)) {
1470 continue;
1471 }
1472 /*
1473 * if the process is in a non-running type state,
1474 * don't touch it.
1475 */
1476 mtx_lock_spin(&sched_lock);
1477 if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
1478 mtx_unlock_spin(&sched_lock);
1479 continue;
1480 }
1481 /*
1482 * get a limit
1483 */
1484 limit = OFF_TO_IDX(
1485 qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
1486 p->p_rlimit[RLIMIT_RSS].rlim_max));
1487
1488 /*
1489 * let processes that are swapped out really be
1490 * swapped out set the limit to nothing (will force a
1491 * swap-out.)
1492 */
1493 if ((p->p_sflag & PS_INMEM) == 0)
1494 limit = 0; /* XXX */
1495 mtx_unlock_spin(&sched_lock);
1496
1497 size = vmspace_resident_count(p->p_vmspace);
1498 if (limit >= 0 && size >= limit) {
1499 vm_pageout_map_deactivate_pages(
1500 &p->p_vmspace->vm_map, limit);
1501 }
1502 }
1503 sx_sunlock(&allproc_lock);
1504 }
1505}
1506#endif /* !defined(NO_SWAPPING) */