2 * Copyright (c) 1991 Regents of the University of California.
4 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
9 * This code is derived from software contributed to Berkeley by
10 * The Mach Operating System project at Carnegie-Mellon University.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
40 * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91
43 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
44 * All rights reserved.
46 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
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.
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.
58 * Carnegie Mellon requests users of this software to return to
60 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
61 * School of Computer Science
62 * Carnegie Mellon University
63 * Pittsburgh PA 15213-3890
65 * any improvements or extensions that they make and grant Carnegie the
66 * rights to redistribute these changes.
68 * $FreeBSD: src/sys/vm/vm_pageout.c,v 1.151.2.15 2002/12/29 18:21:04 dillon Exp $
69 * $DragonFly: src/sys/vm/vm_pageout.c,v 1.11 2004/05/13 17:40:19 dillon Exp $
73 * The proverbial page-out daemon.
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
81 #include <sys/kthread.h>
82 #include <sys/resourcevar.h>
83 #include <sys/signalvar.h>
84 #include <sys/vnode.h>
85 #include <sys/vmmeter.h>
86 #include <sys/sysctl.h>
89 #include <vm/vm_param.h>
91 #include <vm/vm_object.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_pageout.h>
95 #include <vm/vm_pager.h>
96 #include <vm/swap_pager.h>
97 #include <vm/vm_extern.h>
98 #include <vm/vm_page2.h>
101 * System initialization
104 /* the kernel process "vm_pageout"*/
105 static void vm_pageout (void);
106 static int vm_pageout_clean (vm_page_t);
107 static void vm_pageout_scan (int pass);
108 static int vm_pageout_free_page_calc (vm_size_t count);
109 struct thread *pagethread;
111 static struct kproc_desc page_kp = {
116 SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp)
118 #if !defined(NO_SWAPPING)
119 /* the kernel process "vm_daemon"*/
120 static void vm_daemon (void);
121 static struct thread *vmthread;
123 static struct kproc_desc vm_kp = {
128 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp)
132 int vm_pages_needed=0; /* Event on which pageout daemon sleeps */
133 int vm_pageout_deficit=0; /* Estimated number of pages deficit */
134 int vm_pageout_pages_needed=0; /* flag saying that the pageout daemon needs pages */
136 #if !defined(NO_SWAPPING)
137 static int vm_pageout_req_swapout; /* XXX */
138 static int vm_daemon_needed;
140 extern int vm_swap_size;
141 static int vm_max_launder = 32;
142 static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
143 static int vm_pageout_full_stats_interval = 0;
144 static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0;
145 static int defer_swap_pageouts=0;
146 static int disable_swap_pageouts=0;
148 #if defined(NO_SWAPPING)
149 static int vm_swap_enabled=0;
150 static int vm_swap_idle_enabled=0;
152 static int vm_swap_enabled=1;
153 static int vm_swap_idle_enabled=0;
156 SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
157 CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
159 SYSCTL_INT(_vm, OID_AUTO, max_launder,
160 CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
162 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
163 CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
165 SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
166 CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
168 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
169 CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
171 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max,
172 CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented");
174 #if defined(NO_SWAPPING)
175 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
176 CTLFLAG_RD, &vm_swap_enabled, 0, "");
177 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
178 CTLFLAG_RD, &vm_swap_idle_enabled, 0, "");
180 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
181 CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
182 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
183 CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
186 SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
187 CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
189 SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
190 CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
192 static int pageout_lock_miss;
193 SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
194 CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
196 #define VM_PAGEOUT_PAGE_COUNT 16
197 int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
199 int vm_page_max_wired; /* XXX max # of wired pages system-wide */
201 #if !defined(NO_SWAPPING)
202 typedef void freeer_fcn_t (vm_map_t, vm_object_t, vm_pindex_t, int);
203 static void vm_pageout_map_deactivate_pages (vm_map_t, vm_pindex_t);
204 static freeer_fcn_t vm_pageout_object_deactivate_pages;
205 static void vm_req_vmdaemon (void);
207 static void vm_pageout_page_stats(void);
212 * Clean the page and remove it from the laundry. The page must not be
215 * We set the busy bit to cause potential page faults on this page to
216 * block. Note the careful timing, however, the busy bit isn't set till
217 * late and we cannot do anything that will mess with the page.
221 vm_pageout_clean(vm_page_t m)
224 vm_page_t mc[2*vm_pageout_page_count];
226 int ib, is, page_base;
227 vm_pindex_t pindex = m->pindex;
232 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
233 * with the new swapper, but we could have serious problems paging
234 * out other object types if there is insufficient memory.
236 * Unfortunately, checking free memory here is far too late, so the
237 * check has been moved up a procedural level.
241 * Don't mess with the page if it's busy, held, or special
243 if ((m->hold_count != 0) ||
244 ((m->busy != 0) || (m->flags & (PG_BUSY|PG_UNMANAGED)))) {
248 mc[vm_pageout_page_count] = m;
250 page_base = vm_pageout_page_count;
255 * Scan object for clusterable pages.
257 * We can cluster ONLY if: ->> the page is NOT
258 * clean, wired, busy, held, or mapped into a
259 * buffer, and one of the following:
260 * 1) The page is inactive, or a seldom used
263 * 2) we force the issue.
265 * During heavy mmap/modification loads the pageout
266 * daemon can really fragment the underlying file
267 * due to flushing pages out of order and not trying
268 * align the clusters (which leave sporatic out-of-order
269 * holes). To solve this problem we do the reverse scan
270 * first and attempt to align our cluster, then do a
271 * forward scan if room remains.
275 while (ib && pageout_count < vm_pageout_page_count) {
283 if ((p = vm_page_lookup(object, pindex - ib)) == NULL) {
287 if (((p->queue - p->pc) == PQ_CACHE) ||
288 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
292 vm_page_test_dirty(p);
293 if ((p->dirty & p->valid) == 0 ||
294 p->queue != PQ_INACTIVE ||
295 p->wire_count != 0 || /* may be held by buf cache */
296 p->hold_count != 0) { /* may be undergoing I/O */
304 * alignment boundry, stop here and switch directions. Do
307 if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
311 while (pageout_count < vm_pageout_page_count &&
312 pindex + is < object->size) {
315 if ((p = vm_page_lookup(object, pindex + is)) == NULL)
317 if (((p->queue - p->pc) == PQ_CACHE) ||
318 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
321 vm_page_test_dirty(p);
322 if ((p->dirty & p->valid) == 0 ||
323 p->queue != PQ_INACTIVE ||
324 p->wire_count != 0 || /* may be held by buf cache */
325 p->hold_count != 0) { /* may be undergoing I/O */
328 mc[page_base + pageout_count] = p;
334 * If we exhausted our forward scan, continue with the reverse scan
335 * when possible, even past a page boundry. This catches boundry
338 if (ib && pageout_count < vm_pageout_page_count)
342 * we allow reads during pageouts...
344 return vm_pageout_flush(&mc[page_base], pageout_count, 0);
348 * vm_pageout_flush() - launder the given pages
350 * The given pages are laundered. Note that we setup for the start of
351 * I/O ( i.e. busy the page ), mark it read-only, and bump the object
352 * reference count all in here rather then in the parent. If we want
353 * the parent to do more sophisticated things we may have to change
358 vm_pageout_flush(vm_page_t *mc, int count, int flags)
361 int pageout_status[count];
366 * Initiate I/O. Bump the vm_page_t->busy counter and
367 * mark the pages read-only.
369 * We do not have to fixup the clean/dirty bits here... we can
370 * allow the pager to do it after the I/O completes.
373 for (i = 0; i < count; i++) {
374 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, ("vm_pageout_flush page %p index %d/%d: partially invalid page", mc[i], i, count));
375 vm_page_io_start(mc[i]);
376 vm_page_protect(mc[i], VM_PROT_READ);
379 object = mc[0]->object;
380 vm_object_pip_add(object, count);
382 vm_pager_put_pages(object, mc, count,
383 (flags | ((object == kernel_object) ? VM_PAGER_PUT_SYNC : 0)),
386 for (i = 0; i < count; i++) {
387 vm_page_t mt = mc[i];
389 switch (pageout_status[i]) {
398 * Page outside of range of object. Right now we
399 * essentially lose the changes by pretending it
402 pmap_clear_modify(mt);
408 * If page couldn't be paged out, then reactivate the
409 * page so it doesn't clog the inactive list. (We
410 * will try paging out it again later).
412 vm_page_activate(mt);
419 * If the operation is still going, leave the page busy to
420 * block all other accesses. Also, leave the paging in
421 * progress indicator set so that we don't attempt an object
424 if (pageout_status[i] != VM_PAGER_PEND) {
425 vm_object_pip_wakeup(object);
426 vm_page_io_finish(mt);
427 if (!vm_page_count_severe() || !vm_page_try_to_cache(mt))
428 vm_page_protect(mt, VM_PROT_READ);
434 #if !defined(NO_SWAPPING)
436 * vm_pageout_object_deactivate_pages
438 * deactivate enough pages to satisfy the inactive target
439 * requirements or if vm_page_proc_limit is set, then
440 * deactivate all of the pages in the object and its
443 * The object and map must be locked.
446 vm_pageout_object_deactivate_pages(vm_map_t map, vm_object_t object,
447 vm_pindex_t desired, int map_remove_only)
454 if (object->type == OBJT_DEVICE || object->type == OBJT_PHYS)
458 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
460 if (object->paging_in_progress)
463 remove_mode = map_remove_only;
464 if (object->shadow_count > 1)
468 * scan the objects entire memory queue. spl protection is
469 * required to avoid an interrupt unbusy/free race against
473 rcount = object->resident_page_count;
474 p = TAILQ_FIRST(&object->memq);
476 while (p && (rcount-- > 0)) {
478 if (pmap_resident_count(vm_map_pmap(map)) <= desired) {
482 next = TAILQ_NEXT(p, listq);
483 mycpu->gd_cnt.v_pdpages++;
484 if (p->wire_count != 0 ||
485 p->hold_count != 0 ||
487 (p->flags & (PG_BUSY|PG_UNMANAGED)) ||
488 !pmap_page_exists_quick(vm_map_pmap(map), p)) {
493 actcount = pmap_ts_referenced(p);
495 vm_page_flag_set(p, PG_REFERENCED);
496 } else if (p->flags & PG_REFERENCED) {
500 if ((p->queue != PQ_ACTIVE) &&
501 (p->flags & PG_REFERENCED)) {
503 p->act_count += actcount;
504 vm_page_flag_clear(p, PG_REFERENCED);
505 } else if (p->queue == PQ_ACTIVE) {
506 if ((p->flags & PG_REFERENCED) == 0) {
507 p->act_count -= min(p->act_count, ACT_DECLINE);
508 if (!remove_mode && (vm_pageout_algorithm || (p->act_count == 0))) {
509 vm_page_protect(p, VM_PROT_NONE);
510 vm_page_deactivate(p);
512 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
513 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
517 vm_page_flag_clear(p, PG_REFERENCED);
518 if (p->act_count < (ACT_MAX - ACT_ADVANCE))
519 p->act_count += ACT_ADVANCE;
520 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
521 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
523 } else if (p->queue == PQ_INACTIVE) {
524 vm_page_protect(p, VM_PROT_NONE);
529 object = object->backing_object;
534 * deactivate some number of pages in a map, try to do it fairly, but
535 * that is really hard to do.
538 vm_pageout_map_deactivate_pages(vm_map_t map, vm_pindex_t desired)
541 vm_object_t obj, bigobj;
544 if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT, NULL, curthread)) {
552 * first, search out the biggest object, and try to free pages from
555 tmpe = map->header.next;
556 while (tmpe != &map->header) {
557 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
558 obj = tmpe->object.vm_object;
559 if ((obj != NULL) && (obj->shadow_count <= 1) &&
561 (bigobj->resident_page_count < obj->resident_page_count))) {
565 if (tmpe->wired_count > 0)
566 nothingwired = FALSE;
571 vm_pageout_object_deactivate_pages(map, bigobj, desired, 0);
574 * Next, hunt around for other pages to deactivate. We actually
575 * do this search sort of wrong -- .text first is not the best idea.
577 tmpe = map->header.next;
578 while (tmpe != &map->header) {
579 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
581 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
582 obj = tmpe->object.vm_object;
584 vm_pageout_object_deactivate_pages(map, obj, desired, 0);
590 * Remove all mappings if a process is swapped out, this will free page
593 if (desired == 0 && nothingwired)
594 pmap_remove(vm_map_pmap(map),
595 VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS);
602 * Don't try to be fancy - being fancy can lead to VOP_LOCK's and therefore
603 * to vnode deadlocks. We only do it for OBJT_DEFAULT and OBJT_SWAP objects
604 * which we know can be trivially freed.
608 vm_pageout_page_free(vm_page_t m) {
609 vm_object_t object = m->object;
610 int type = object->type;
612 if (type == OBJT_SWAP || type == OBJT_DEFAULT)
613 vm_object_reference(object);
615 vm_page_protect(m, VM_PROT_NONE);
617 if (type == OBJT_SWAP || type == OBJT_DEFAULT)
618 vm_object_deallocate(object);
622 * vm_pageout_scan does the dirty work for the pageout daemon.
625 vm_pageout_scan(int pass)
628 struct vm_page marker;
629 int page_shortage, maxscan, pcount;
630 int addl_page_shortage, addl_page_shortage_init;
631 struct proc *p, *bigproc;
632 vm_offset_t size, bigsize;
635 int vnodes_skipped = 0;
640 * Do whatever cleanup that the pmap code can.
644 addl_page_shortage_init = vm_pageout_deficit;
645 vm_pageout_deficit = 0;
648 * Calculate the number of pages we want to either free or move
651 page_shortage = vm_paging_target() + addl_page_shortage_init;
654 * Initialize our marker
656 bzero(&marker, sizeof(marker));
657 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
658 marker.queue = PQ_INACTIVE;
659 marker.wire_count = 1;
662 * Start scanning the inactive queue for pages we can move to the
663 * cache or free. The scan will stop when the target is reached or
664 * we have scanned the entire inactive queue. Note that m->act_count
665 * is not used to form decisions for the inactive queue, only for the
668 * maxlaunder limits the number of dirty pages we flush per scan.
669 * For most systems a smaller value (16 or 32) is more robust under
670 * extreme memory and disk pressure because any unnecessary writes
671 * to disk can result in extreme performance degredation. However,
672 * systems with excessive dirty pages (especially when MAP_NOSYNC is
673 * used) will die horribly with limited laundering. If the pageout
674 * daemon cannot clean enough pages in the first pass, we let it go
675 * all out in succeeding passes.
677 if ((maxlaunder = vm_max_launder) <= 1)
683 * We will generally be at splvm() throughout the scan, but we
684 * can release it temporarily when we are sitting on a non-busy
685 * page without fear. The spl protection is required because an
686 * an interrupt can come along and unbusy/free a busy page prior
687 * to our busy check, leaving us on the wrong queue or checking
692 addl_page_shortage = addl_page_shortage_init;
693 maxscan = vmstats.v_inactive_count;
694 for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
695 m != NULL && maxscan-- > 0 && page_shortage > 0;
698 mycpu->gd_cnt.v_pdpages++;
701 * Give interrupts a chance
707 * It's easier for some of the conditions below to just loop
708 * and catch queue changes here rather then check everywhere
711 if (m->queue != PQ_INACTIVE)
713 next = TAILQ_NEXT(m, pageq);
718 if (m->flags & PG_MARKER)
722 * A held page may be undergoing I/O, so skip it.
725 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
726 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
727 addl_page_shortage++;
732 * Dont mess with busy pages, keep in the front of the
733 * queue, most likely are being paged out.
735 if (m->busy || (m->flags & PG_BUSY)) {
736 addl_page_shortage++;
740 if (m->object->ref_count == 0) {
742 * If the object is not being used, we ignore previous
745 vm_page_flag_clear(m, PG_REFERENCED);
746 pmap_clear_reference(m);
748 } else if (((m->flags & PG_REFERENCED) == 0) &&
749 (actcount = pmap_ts_referenced(m))) {
751 * Otherwise, if the page has been referenced while
752 * in the inactive queue, we bump the "activation
753 * count" upwards, making it less likely that the
754 * page will be added back to the inactive queue
755 * prematurely again. Here we check the page tables
756 * (or emulated bits, if any), given the upper level
757 * VM system not knowing anything about existing
761 m->act_count += (actcount + ACT_ADVANCE);
766 * If the upper level VM system knows about any page
767 * references, we activate the page. We also set the
768 * "activation count" higher than normal so that we will less
769 * likely place pages back onto the inactive queue again.
771 if ((m->flags & PG_REFERENCED) != 0) {
772 vm_page_flag_clear(m, PG_REFERENCED);
773 actcount = pmap_ts_referenced(m);
775 m->act_count += (actcount + ACT_ADVANCE + 1);
780 * If the upper level VM system doesn't know anything about
781 * the page being dirty, we have to check for it again. As
782 * far as the VM code knows, any partially dirty pages are
785 * Pages marked PG_WRITEABLE may be mapped into the user
786 * address space of a process running on another cpu. A
787 * user process (without holding the MP lock) running on
788 * another cpu may be able to touch the page while we are
789 * trying to remove it. To prevent this from occuring we
790 * must call pmap_remove_all() or otherwise make the page
791 * read-only. If the race occured pmap_remove_all() is
792 * responsible for setting m->dirty.
795 vm_page_test_dirty(m);
797 if (m->dirty == 0 && (m->flags & PG_WRITEABLE) != 0)
806 * Invalid pages can be easily freed
808 vm_pageout_page_free(m);
809 mycpu->gd_cnt.v_dfree++;
811 } else if (m->dirty == 0) {
813 * Clean pages can be placed onto the cache queue.
814 * This effectively frees them.
818 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
820 * Dirty pages need to be paged out, but flushing
821 * a page is extremely expensive verses freeing
822 * a clean page. Rather then artificially limiting
823 * the number of pages we can flush, we instead give
824 * dirty pages extra priority on the inactive queue
825 * by forcing them to be cycled through the queue
826 * twice before being flushed, after which the
827 * (now clean) page will cycle through once more
828 * before being freed. This significantly extends
829 * the thrash point for a heavily loaded machine.
831 vm_page_flag_set(m, PG_WINATCFLS);
832 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
833 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
834 } else if (maxlaunder > 0) {
836 * We always want to try to flush some dirty pages if
837 * we encounter them, to keep the system stable.
838 * Normally this number is small, but under extreme
839 * pressure where there are insufficient clean pages
840 * on the inactive queue, we may have to go all out.
842 int swap_pageouts_ok;
843 struct vnode *vp = NULL;
847 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
848 swap_pageouts_ok = 1;
850 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
851 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
852 vm_page_count_min());
857 * We don't bother paging objects that are "dead".
858 * Those objects are in a "rundown" state.
860 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
861 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
862 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
867 * The object is already known NOT to be dead. It
868 * is possible for the vget() to block the whole
869 * pageout daemon, but the new low-memory handling
870 * code should prevent it.
872 * The previous code skipped locked vnodes and, worse,
873 * reordered pages in the queue. This results in
874 * completely non-deterministic operation because,
875 * quite often, a vm_fault has initiated an I/O and
876 * is holding a locked vnode at just the point where
877 * the pageout daemon is woken up.
879 * We can't wait forever for the vnode lock, we might
880 * deadlock due to a vn_read() getting stuck in
881 * vm_wait while holding this vnode. We skip the
882 * vnode if we can't get it in a reasonable amount
886 if (object->type == OBJT_VNODE) {
889 if (vget(vp, NULL, LK_EXCLUSIVE|LK_NOOBJ|LK_TIMELOCK, curthread)) {
891 if (object->flags & OBJ_MIGHTBEDIRTY)
897 * The page might have been moved to another
898 * queue during potential blocking in vget()
899 * above. The page might have been freed and
900 * reused for another vnode. The object might
901 * have been reused for another vnode.
903 if (m->queue != PQ_INACTIVE ||
904 m->object != object ||
905 object->handle != vp) {
906 if (object->flags & OBJ_MIGHTBEDIRTY)
913 * The page may have been busied during the
914 * blocking in vput(); We don't move the
915 * page back onto the end of the queue so that
916 * statistics are more correct if we don't.
918 if (m->busy || (m->flags & PG_BUSY)) {
924 * If the page has become held it might
925 * be undergoing I/O, so skip it
928 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
929 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
930 if (object->flags & OBJ_MIGHTBEDIRTY)
938 * If a page is dirty, then it is either being washed
939 * (but not yet cleaned) or it is still in the
940 * laundry. If it is still in the laundry, then we
941 * start the cleaning operation.
943 * This operation may cluster, invalidating the 'next'
944 * pointer. To prevent an inordinate number of
945 * restarts we use our marker to remember our place.
947 * decrement page_shortage on success to account for
948 * the (future) cleaned page. Otherwise we could wind
949 * up laundering or cleaning too many pages.
951 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m, &marker, pageq);
952 if (vm_pageout_clean(m) != 0) {
956 next = TAILQ_NEXT(&marker, pageq);
957 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq);
964 * Compute the number of pages we want to try to move from the
965 * active queue to the inactive queue.
967 page_shortage = vm_paging_target() +
968 vmstats.v_inactive_target - vmstats.v_inactive_count;
969 page_shortage += addl_page_shortage;
972 * Scan the active queue for things we can deactivate. We nominally
973 * track the per-page activity counter and use it to locate
974 * deactivation candidates.
976 * NOTE: we are still at splvm().
978 pcount = vmstats.v_active_count;
979 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
981 while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
983 * Give interrupts a chance.
989 * If the page was ripped out from under us, just stop.
991 if (m->queue != PQ_ACTIVE)
993 next = TAILQ_NEXT(m, pageq);
996 * Don't deactivate pages that are busy.
998 if ((m->busy != 0) ||
999 (m->flags & PG_BUSY) ||
1000 (m->hold_count != 0)) {
1001 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1002 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1008 * The count for pagedaemon pages is done after checking the
1009 * page for eligibility...
1011 mycpu->gd_cnt.v_pdpages++;
1014 * Check to see "how much" the page has been used.
1017 if (m->object->ref_count != 0) {
1018 if (m->flags & PG_REFERENCED) {
1021 actcount += pmap_ts_referenced(m);
1023 m->act_count += ACT_ADVANCE + actcount;
1024 if (m->act_count > ACT_MAX)
1025 m->act_count = ACT_MAX;
1030 * Since we have "tested" this bit, we need to clear it now.
1032 vm_page_flag_clear(m, PG_REFERENCED);
1035 * Only if an object is currently being used, do we use the
1036 * page activation count stats.
1038 if (actcount && (m->object->ref_count != 0)) {
1039 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1040 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1042 m->act_count -= min(m->act_count, ACT_DECLINE);
1043 if (vm_pageout_algorithm ||
1044 m->object->ref_count == 0 ||
1045 m->act_count == 0) {
1047 if (m->object->ref_count == 0) {
1048 vm_page_protect(m, VM_PROT_NONE);
1052 vm_page_deactivate(m);
1054 vm_page_deactivate(m);
1057 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1058 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1065 * We try to maintain some *really* free pages, this allows interrupt
1066 * code to be guaranteed space. Since both cache and free queues
1067 * are considered basically 'free', moving pages from cache to free
1068 * does not effect other calculations.
1070 * NOTE: we are still at splvm().
1073 while (vmstats.v_free_count < vmstats.v_free_reserved) {
1074 static int cache_rover = 0;
1075 m = vm_page_list_find(PQ_CACHE, cache_rover, FALSE);
1078 if ((m->flags & (PG_BUSY|PG_UNMANAGED)) ||
1083 printf("Warning: busy page %p found in cache\n", m);
1085 vm_page_deactivate(m);
1088 cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK;
1089 vm_pageout_page_free(m);
1090 mycpu->gd_cnt.v_dfree++;
1095 #if !defined(NO_SWAPPING)
1097 * Idle process swapout -- run once per second.
1099 if (vm_swap_idle_enabled) {
1101 if (time_second != lsec) {
1102 vm_pageout_req_swapout |= VM_SWAP_IDLE;
1110 * If we didn't get enough free pages, and we have skipped a vnode
1111 * in a writeable object, wakeup the sync daemon. And kick swapout
1112 * if we did not get enough free pages.
1114 if (vm_paging_target() > 0) {
1115 if (vnodes_skipped && vm_page_count_min())
1116 (void) speedup_syncer();
1117 #if !defined(NO_SWAPPING)
1118 if (vm_swap_enabled && vm_page_count_target()) {
1120 vm_pageout_req_swapout |= VM_SWAP_NORMAL;
1126 * If we are out of swap and were not able to reach our paging
1127 * target, kill the largest process.
1129 if ((vm_swap_size < 64 && vm_page_count_min()) ||
1130 (swap_pager_full && vm_paging_target() > 0)) {
1132 if ((vm_swap_size < 64 || swap_pager_full) && vm_page_count_min()) {
1136 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1138 * if this is a system process, skip it
1140 if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) ||
1141 ((p->p_pid < 48) && (vm_swap_size != 0))) {
1145 * if the process is in a non-running type state,
1148 if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
1152 * get the process size
1154 size = vmspace_resident_count(p->p_vmspace) +
1155 vmspace_swap_count(p->p_vmspace);
1157 * if the this process is bigger than the biggest one
1160 if (size > bigsize) {
1165 if (bigproc != NULL) {
1166 killproc(bigproc, "out of swap space");
1167 bigproc->p_estcpu = 0;
1168 bigproc->p_nice = PRIO_MIN;
1169 resetpriority(bigproc);
1170 wakeup(&vmstats.v_free_count);
1176 * This routine tries to maintain the pseudo LRU active queue,
1177 * so that during long periods of time where there is no paging,
1178 * that some statistic accumulation still occurs. This code
1179 * helps the situation where paging just starts to occur.
1182 vm_pageout_page_stats(void)
1186 int pcount,tpcount; /* Number of pages to check */
1187 static int fullintervalcount = 0;
1192 (vmstats.v_inactive_target + vmstats.v_cache_max + vmstats.v_free_min) -
1193 (vmstats.v_free_count + vmstats.v_inactive_count + vmstats.v_cache_count);
1195 if (page_shortage <= 0)
1200 pcount = vmstats.v_active_count;
1201 fullintervalcount += vm_pageout_stats_interval;
1202 if (fullintervalcount < vm_pageout_full_stats_interval) {
1203 tpcount = (vm_pageout_stats_max * vmstats.v_active_count) / vmstats.v_page_count;
1204 if (pcount > tpcount)
1207 fullintervalcount = 0;
1210 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1211 while ((m != NULL) && (pcount-- > 0)) {
1214 if (m->queue != PQ_ACTIVE) {
1218 next = TAILQ_NEXT(m, pageq);
1220 * Don't deactivate pages that are busy.
1222 if ((m->busy != 0) ||
1223 (m->flags & PG_BUSY) ||
1224 (m->hold_count != 0)) {
1226 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1227 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1234 if (m->flags & PG_REFERENCED) {
1235 vm_page_flag_clear(m, PG_REFERENCED);
1239 actcount += pmap_ts_referenced(m);
1241 m->act_count += ACT_ADVANCE + actcount;
1242 if (m->act_count > ACT_MAX)
1243 m->act_count = ACT_MAX;
1245 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1246 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1249 if (m->act_count == 0) {
1251 * We turn off page access, so that we have
1252 * more accurate RSS stats. We don't do this
1253 * in the normal page deactivation when the
1254 * system is loaded VM wise, because the
1255 * cost of the large number of page protect
1256 * operations would be higher than the value
1257 * of doing the operation.
1259 vm_page_protect(m, VM_PROT_NONE);
1260 vm_page_deactivate(m);
1262 m->act_count -= min(m->act_count, ACT_DECLINE);
1264 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1265 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1276 vm_pageout_free_page_calc(vm_size_t count)
1278 if (count < vmstats.v_page_count)
1281 * free_reserved needs to include enough for the largest swap pager
1282 * structures plus enough for any pv_entry structs when paging.
1284 if (vmstats.v_page_count > 1024)
1285 vmstats.v_free_min = 4 + (vmstats.v_page_count - 1024) / 200;
1287 vmstats.v_free_min = 4;
1288 vmstats.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
1289 vmstats.v_interrupt_free_min;
1290 vmstats.v_free_reserved = vm_pageout_page_count +
1291 vmstats.v_pageout_free_min + (count / 768) + PQ_L2_SIZE;
1292 vmstats.v_free_severe = vmstats.v_free_min / 2;
1293 vmstats.v_free_min += vmstats.v_free_reserved;
1294 vmstats.v_free_severe += vmstats.v_free_reserved;
1300 * vm_pageout is the high level pageout daemon.
1308 * Initialize some paging parameters.
1311 vmstats.v_interrupt_free_min = 2;
1312 if (vmstats.v_page_count < 2000)
1313 vm_pageout_page_count = 8;
1315 vm_pageout_free_page_calc(vmstats.v_page_count);
1317 * v_free_target and v_cache_min control pageout hysteresis. Note
1318 * that these are more a measure of the VM cache queue hysteresis
1319 * then the VM free queue. Specifically, v_free_target is the
1320 * high water mark (free+cache pages).
1322 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
1323 * low water mark, while v_free_min is the stop. v_cache_min must
1324 * be big enough to handle memory needs while the pageout daemon
1325 * is signalled and run to free more pages.
1327 if (vmstats.v_free_count > 6144)
1328 vmstats.v_free_target = 4 * vmstats.v_free_min + vmstats.v_free_reserved;
1330 vmstats.v_free_target = 2 * vmstats.v_free_min + vmstats.v_free_reserved;
1332 if (vmstats.v_free_count > 2048) {
1333 vmstats.v_cache_min = vmstats.v_free_target;
1334 vmstats.v_cache_max = 2 * vmstats.v_cache_min;
1335 vmstats.v_inactive_target = (3 * vmstats.v_free_target) / 2;
1337 vmstats.v_cache_min = 0;
1338 vmstats.v_cache_max = 0;
1339 vmstats.v_inactive_target = vmstats.v_free_count / 4;
1341 if (vmstats.v_inactive_target > vmstats.v_free_count / 3)
1342 vmstats.v_inactive_target = vmstats.v_free_count / 3;
1344 /* XXX does not really belong here */
1345 if (vm_page_max_wired == 0)
1346 vm_page_max_wired = vmstats.v_free_count / 3;
1348 if (vm_pageout_stats_max == 0)
1349 vm_pageout_stats_max = vmstats.v_free_target;
1352 * Set interval in seconds for stats scan.
1354 if (vm_pageout_stats_interval == 0)
1355 vm_pageout_stats_interval = 5;
1356 if (vm_pageout_full_stats_interval == 0)
1357 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1361 * Set maximum free per pass
1363 if (vm_pageout_stats_free_max == 0)
1364 vm_pageout_stats_free_max = 5;
1366 swap_pager_swap_init();
1369 * The pageout daemon is never done, so loop forever.
1376 * If we have enough free memory, wakeup waiters. Do
1377 * not clear vm_pages_needed until we reach our target,
1378 * otherwise we may be woken up over and over again and
1379 * waste a lot of cpu.
1381 if (vm_pages_needed && !vm_page_count_min()) {
1382 if (vm_paging_needed() <= 0)
1383 vm_pages_needed = 0;
1384 wakeup(&vmstats.v_free_count);
1386 if (vm_pages_needed) {
1388 * Still not done, take a second pass without waiting
1389 * (unlimited dirty cleaning), otherwise sleep a bit
1394 tsleep(&vm_pages_needed, 0, "psleep", hz/2);
1397 * Good enough, sleep & handle stats. Prime the pass
1404 error = tsleep(&vm_pages_needed,
1405 0, "psleep", vm_pageout_stats_interval * hz);
1406 if (error && !vm_pages_needed) {
1409 vm_pageout_page_stats();
1414 if (vm_pages_needed)
1415 mycpu->gd_cnt.v_pdwakeups++;
1417 vm_pageout_scan(pass);
1418 vm_pageout_deficit = 0;
1423 pagedaemon_wakeup(void)
1425 if (!vm_pages_needed && curthread != pagethread) {
1427 wakeup(&vm_pages_needed);
1431 #if !defined(NO_SWAPPING)
1433 vm_req_vmdaemon(void)
1435 static int lastrun = 0;
1437 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
1438 wakeup(&vm_daemon_needed);
1449 tsleep(&vm_daemon_needed, 0, "psleep", 0);
1450 if (vm_pageout_req_swapout) {
1451 swapout_procs(vm_pageout_req_swapout);
1452 vm_pageout_req_swapout = 0;
1455 * scan the processes for exceeding their rlimits or if
1456 * process is swapped out -- deactivate pages
1459 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1460 vm_pindex_t limit, size;
1463 * if this is a system process or if we have already
1464 * looked at this process, skip it.
1466 if (p->p_flag & (P_SYSTEM | P_WEXIT)) {
1470 * if the process is in a non-running type state,
1473 if (p->p_stat != SRUN && p->p_stat != SSLEEP) {
1480 qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
1481 p->p_rlimit[RLIMIT_RSS].rlim_max));
1484 * let processes that are swapped out really be
1485 * swapped out set the limit to nothing (will force a
1488 if ((p->p_flag & P_INMEM) == 0)
1489 limit = 0; /* XXX */
1491 size = vmspace_resident_count(p->p_vmspace);
1492 if (limit >= 0 && size >= limit) {
1493 vm_pageout_map_deactivate_pages(
1494 &p->p_vmspace->vm_map, limit);