4 * Copyright (c) 1991 Regents of the University of California.
6 * Copyright (c) 1994 John S. Dyson
8 * Copyright (c) 1994 David Greenman
11 * This code is derived from software contributed to Berkeley by
12 * The Mach Operating System project at Carnegie-Mellon University.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91
41 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
42 * All rights reserved.
44 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
46 * Permission to use, copy, modify and distribute this software and
47 * its documentation is hereby granted, provided that both the copyright
48 * notice and this permission notice appear in all copies of the
49 * software, derivative works or modified versions, and any portions
50 * thereof, and that both notices appear in supporting documentation.
52 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
54 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
56 * Carnegie Mellon requests users of this software to return to
58 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
59 * School of Computer Science
60 * Carnegie Mellon University
61 * Pittsburgh PA 15213-3890
63 * any improvements or extensions that they make and grant Carnegie the
64 * rights to redistribute these changes.
66 * $FreeBSD: src/sys/vm/vm_pageout.c,v 1.151.2.15 2002/12/29 18:21:04 dillon Exp $
70 * The proverbial page-out daemon.
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
78 #include <sys/kthread.h>
79 #include <sys/resourcevar.h>
80 #include <sys/signalvar.h>
81 #include <sys/vnode.h>
82 #include <sys/vmmeter.h>
83 #include <sys/sysctl.h>
86 #include <vm/vm_param.h>
88 #include <vm/vm_object.h>
89 #include <vm/vm_page.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_pageout.h>
92 #include <vm/vm_pager.h>
93 #include <vm/swap_pager.h>
94 #include <vm/vm_extern.h>
96 #include <sys/thread2.h>
97 #include <sys/spinlock2.h>
98 #include <vm/vm_page2.h>
101 * System initialization
104 /* the kernel process "vm_pageout"*/
105 static int vm_pageout_clean (vm_page_t);
106 static int vm_pageout_scan (int pass);
107 static int vm_pageout_free_page_calc (vm_size_t count);
108 struct thread *pagethread;
110 #if !defined(NO_SWAPPING)
111 /* the kernel process "vm_daemon"*/
112 static void vm_daemon (void);
113 static struct thread *vmthread;
115 static struct kproc_desc vm_kp = {
120 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp)
124 int vm_pages_needed=0; /* Event on which pageout daemon sleeps */
125 int vm_pageout_deficit=0; /* Estimated number of pages deficit */
126 int vm_pageout_pages_needed=0; /* flag saying that the pageout daemon needs pages */
128 #if !defined(NO_SWAPPING)
129 static int vm_pageout_req_swapout; /* XXX */
130 static int vm_daemon_needed;
132 static int vm_max_launder = 32;
133 static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
134 static int vm_pageout_full_stats_interval = 0;
135 static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0;
136 static int defer_swap_pageouts=0;
137 static int disable_swap_pageouts=0;
139 #if defined(NO_SWAPPING)
140 static int vm_swap_enabled=0;
141 static int vm_swap_idle_enabled=0;
143 static int vm_swap_enabled=1;
144 static int vm_swap_idle_enabled=0;
147 SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
148 CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
150 SYSCTL_INT(_vm, OID_AUTO, max_launder,
151 CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
153 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
154 CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
156 SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
157 CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
159 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
160 CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
162 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max,
163 CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented");
165 #if defined(NO_SWAPPING)
166 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
167 CTLFLAG_RD, &vm_swap_enabled, 0, "");
168 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
169 CTLFLAG_RD, &vm_swap_idle_enabled, 0, "");
171 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
172 CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
173 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
174 CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
177 SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
178 CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
180 SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
181 CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
183 static int pageout_lock_miss;
184 SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
185 CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
188 SYSCTL_INT(_vm, OID_AUTO, vm_load,
189 CTLFLAG_RD, &vm_load, 0, "load on the VM system");
190 int vm_load_enable = 1;
191 SYSCTL_INT(_vm, OID_AUTO, vm_load_enable,
192 CTLFLAG_RW, &vm_load_enable, 0, "enable vm_load rate limiting");
195 SYSCTL_INT(_vm, OID_AUTO, vm_load_debug,
196 CTLFLAG_RW, &vm_load_debug, 0, "debug vm_load");
199 #define VM_PAGEOUT_PAGE_COUNT 16
200 int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
202 int vm_page_max_wired; /* XXX max # of wired pages system-wide */
204 #if !defined(NO_SWAPPING)
205 typedef void freeer_fcn_t (vm_map_t, vm_object_t, vm_pindex_t, int);
206 static void vm_pageout_map_deactivate_pages (vm_map_t, vm_pindex_t);
207 static freeer_fcn_t vm_pageout_object_deactivate_pages;
208 static void vm_req_vmdaemon (void);
210 static void vm_pageout_page_stats(void);
213 * Update vm_load to slow down faulting processes.
219 vm_fault_ratecheck(void)
221 if (vm_pages_needed) {
233 * Clean the page and remove it from the laundry. The page must not be
236 * We set the busy bit to cause potential page faults on this page to
237 * block. Note the careful timing, however, the busy bit isn't set till
238 * late and we cannot do anything that will mess with the page.
241 vm_pageout_clean(vm_page_t m)
244 vm_page_t mc[2*vm_pageout_page_count];
247 int ib, is, page_base;
248 vm_pindex_t pindex = m->pindex;
253 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
254 * with the new swapper, but we could have serious problems paging
255 * out other object types if there is insufficient memory.
257 * Unfortunately, checking free memory here is far too late, so the
258 * check has been moved up a procedural level.
262 * Don't mess with the page if it's busy, held, or special
264 * XXX do we really need to check hold_count here? hold_count
265 * isn't supposed to mess with vm_page ops except prevent the
266 * page from being reused.
268 if (m->hold_count != 0 || (m->flags & PG_UNMANAGED)) {
273 mc[vm_pageout_page_count] = m;
275 page_base = vm_pageout_page_count;
280 * Scan object for clusterable pages.
282 * We can cluster ONLY if: ->> the page is NOT
283 * clean, wired, busy, held, or mapped into a
284 * buffer, and one of the following:
285 * 1) The page is inactive, or a seldom used
288 * 2) we force the issue.
290 * During heavy mmap/modification loads the pageout
291 * daemon can really fragment the underlying file
292 * due to flushing pages out of order and not trying
293 * align the clusters (which leave sporatic out-of-order
294 * holes). To solve this problem we do the reverse scan
295 * first and attempt to align our cluster, then do a
296 * forward scan if room remains.
299 vm_object_hold(object);
301 while (ib && pageout_count < vm_pageout_page_count) {
309 p = vm_page_lookup_busy_try(object, pindex - ib, TRUE, &error);
310 if (error || p == NULL) {
314 if ((p->queue - p->pc) == PQ_CACHE ||
315 (p->flags & PG_UNMANAGED)) {
320 vm_page_test_dirty(p);
321 if ((p->dirty & p->valid) == 0 ||
322 p->queue != PQ_INACTIVE ||
323 p->wire_count != 0 || /* may be held by buf cache */
324 p->hold_count != 0) { /* may be undergoing I/O */
333 * alignment boundry, stop here and switch directions. Do
336 if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
340 while (pageout_count < vm_pageout_page_count &&
341 pindex + is < object->size) {
344 p = vm_page_lookup_busy_try(object, pindex + is, TRUE, &error);
345 if (error || p == NULL)
347 if (((p->queue - p->pc) == PQ_CACHE) ||
348 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
352 vm_page_test_dirty(p);
353 if ((p->dirty & p->valid) == 0 ||
354 p->queue != PQ_INACTIVE ||
355 p->wire_count != 0 || /* may be held by buf cache */
356 p->hold_count != 0) { /* may be undergoing I/O */
360 mc[page_base + pageout_count] = p;
366 * If we exhausted our forward scan, continue with the reverse scan
367 * when possible, even past a page boundry. This catches boundry
370 if (ib && pageout_count < vm_pageout_page_count)
373 vm_object_drop(object);
376 * we allow reads during pageouts...
378 return vm_pageout_flush(&mc[page_base], pageout_count, 0);
382 * vm_pageout_flush() - launder the given pages
384 * The given pages are laundered. Note that we setup for the start of
385 * I/O ( i.e. busy the page ), mark it read-only, and bump the object
386 * reference count all in here rather then in the parent. If we want
387 * the parent to do more sophisticated things we may have to change
390 * The pages in the array must be busied by the caller and will be
391 * unbusied by this function.
394 vm_pageout_flush(vm_page_t *mc, int count, int flags)
397 int pageout_status[count];
402 * Initiate I/O. Bump the vm_page_t->busy counter.
404 for (i = 0; i < count; i++) {
405 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL,
406 ("vm_pageout_flush page %p index %d/%d: partially "
407 "invalid page", mc[i], i, count));
408 vm_page_io_start(mc[i]);
412 * We must make the pages read-only. This will also force the
413 * modified bit in the related pmaps to be cleared. The pager
414 * cannot clear the bit for us since the I/O completion code
415 * typically runs from an interrupt. The act of making the page
416 * read-only handles the case for us.
418 * Then we can unbusy the pages, we still hold a reference by virtue
421 for (i = 0; i < count; i++) {
422 vm_page_protect(mc[i], VM_PROT_READ);
423 vm_page_wakeup(mc[i]);
426 object = mc[0]->object;
427 vm_object_pip_add(object, count);
429 vm_pager_put_pages(object, mc, count,
430 (flags | ((object == &kernel_object) ? VM_PAGER_PUT_SYNC : 0)),
433 for (i = 0; i < count; i++) {
434 vm_page_t mt = mc[i];
436 switch (pageout_status[i]) {
445 * Page outside of range of object. Right now we
446 * essentially lose the changes by pretending it
449 vm_page_busy_wait(mt, FALSE, "pgbad");
450 pmap_clear_modify(mt);
457 * A page typically cannot be paged out when we
458 * have run out of swap. We leave the page
459 * marked inactive and will try to page it out
462 * Starvation of the active page list is used to
463 * determine when the system is massively memory
472 * If the operation is still going, leave the page busy to
473 * block all other accesses. Also, leave the paging in
474 * progress indicator set so that we don't attempt an object
477 * For any pages which have completed synchronously,
478 * deactivate the page if we are under a severe deficit.
479 * Do not try to enter them into the cache, though, they
480 * might still be read-heavy.
482 if (pageout_status[i] != VM_PAGER_PEND) {
483 vm_page_busy_wait(mt, FALSE, "pgouw");
484 if (vm_page_count_severe())
485 vm_page_deactivate(mt);
487 if (!vm_page_count_severe() || !vm_page_try_to_cache(mt))
488 vm_page_protect(mt, VM_PROT_READ);
490 vm_page_io_finish(mt);
492 vm_object_pip_wakeup(object);
498 #if !defined(NO_SWAPPING)
500 * deactivate enough pages to satisfy the inactive target
501 * requirements or if vm_page_proc_limit is set, then
502 * deactivate all of the pages in the object and its
505 * The map must be locked.
506 * The caller must hold the vm_object.
508 static int vm_pageout_object_deactivate_pages_callback(vm_page_t, void *);
511 vm_pageout_object_deactivate_pages(vm_map_t map, vm_object_t object,
512 vm_pindex_t desired, int map_remove_only)
514 struct rb_vm_page_scan_info info;
522 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
524 if (lobject->type == OBJT_DEVICE || lobject->type == OBJT_PHYS)
526 if (lobject->paging_in_progress)
529 remove_mode = map_remove_only;
530 if (lobject->shadow_count > 1)
534 * scan the objects entire memory queue. We hold the
535 * object's token so the scan should not race anything.
537 info.limit = remove_mode;
539 info.desired = desired;
540 vm_page_rb_tree_RB_SCAN(&lobject->rb_memq, NULL,
541 vm_pageout_object_deactivate_pages_callback,
544 while ((tobject = lobject->backing_object) != NULL) {
545 KKASSERT(tobject != object);
546 vm_object_hold(tobject);
547 if (tobject == lobject->backing_object)
549 vm_object_drop(tobject);
551 if (lobject != object)
552 vm_object_drop(lobject);
555 if (lobject != object)
556 vm_object_drop(lobject);
560 * The caller must hold the vm_object.
563 vm_pageout_object_deactivate_pages_callback(vm_page_t p, void *data)
565 struct rb_vm_page_scan_info *info = data;
568 if (pmap_resident_count(vm_map_pmap(info->map)) <= info->desired) {
571 mycpu->gd_cnt.v_pdpages++;
573 if (vm_page_busy_try(p, TRUE))
575 if (p->wire_count || p->hold_count || (p->flags & PG_UNMANAGED)) {
579 if (!pmap_page_exists_quick(vm_map_pmap(info->map), p)) {
584 actcount = pmap_ts_referenced(p);
586 vm_page_flag_set(p, PG_REFERENCED);
587 } else if (p->flags & PG_REFERENCED) {
591 vm_page_and_queue_spin_lock(p);
592 if (p->queue != PQ_ACTIVE && (p->flags & PG_REFERENCED)) {
593 vm_page_and_queue_spin_unlock(p);
595 p->act_count += actcount;
596 vm_page_flag_clear(p, PG_REFERENCED);
597 } else if (p->queue == PQ_ACTIVE) {
598 if ((p->flags & PG_REFERENCED) == 0) {
599 p->act_count -= min(p->act_count, ACT_DECLINE);
601 (vm_pageout_algorithm || (p->act_count == 0))) {
602 vm_page_and_queue_spin_unlock(p);
603 vm_page_protect(p, VM_PROT_NONE);
604 vm_page_deactivate(p);
606 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
607 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
608 vm_page_and_queue_spin_unlock(p);
611 vm_page_and_queue_spin_unlock(p);
613 vm_page_flag_clear(p, PG_REFERENCED);
615 vm_page_and_queue_spin_lock(p);
616 if (p->queue == PQ_ACTIVE) {
617 if (p->act_count < (ACT_MAX - ACT_ADVANCE))
618 p->act_count += ACT_ADVANCE;
619 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
620 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
622 vm_page_and_queue_spin_unlock(p);
624 } else if (p->queue == PQ_INACTIVE) {
625 vm_page_and_queue_spin_unlock(p);
626 vm_page_protect(p, VM_PROT_NONE);
628 vm_page_and_queue_spin_unlock(p);
635 * Deactivate some number of pages in a map, try to do it fairly, but
636 * that is really hard to do.
639 vm_pageout_map_deactivate_pages(vm_map_t map, vm_pindex_t desired)
642 vm_object_t obj, bigobj;
645 if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT)) {
653 * first, search out the biggest object, and try to free pages from
656 tmpe = map->header.next;
657 while (tmpe != &map->header) {
658 switch(tmpe->maptype) {
659 case VM_MAPTYPE_NORMAL:
660 case VM_MAPTYPE_VPAGETABLE:
661 obj = tmpe->object.vm_object;
662 if ((obj != NULL) && (obj->shadow_count <= 1) &&
664 (bigobj->resident_page_count < obj->resident_page_count))) {
671 if (tmpe->wired_count > 0)
672 nothingwired = FALSE;
677 vm_pageout_object_deactivate_pages(map, bigobj, desired, 0);
680 * Next, hunt around for other pages to deactivate. We actually
681 * do this search sort of wrong -- .text first is not the best idea.
683 tmpe = map->header.next;
684 while (tmpe != &map->header) {
685 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
687 switch(tmpe->maptype) {
688 case VM_MAPTYPE_NORMAL:
689 case VM_MAPTYPE_VPAGETABLE:
690 obj = tmpe->object.vm_object;
692 vm_pageout_object_deactivate_pages(map, obj, desired, 0);
701 * Remove all mappings if a process is swapped out, this will free page
704 if (desired == 0 && nothingwired)
705 pmap_remove(vm_map_pmap(map),
706 VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
712 * Called when the pageout scan wants to free a page. We no longer
713 * try to cycle the vm_object here with a reference & dealloc, which can
714 * cause a non-trivial object collapse in a critical path.
716 * It is unclear why we cycled the ref_count in the past, perhaps to try
717 * to optimize shadow chain collapses but I don't quite see why it would
718 * be necessary. An OBJ_DEAD object should terminate any and all vm_pages
719 * synchronously and not have to be kicked-start.
722 vm_pageout_page_free(vm_page_t m)
724 vm_page_protect(m, VM_PROT_NONE);
729 * vm_pageout_scan does the dirty work for the pageout daemon.
731 struct vm_pageout_scan_info {
732 struct proc *bigproc;
736 static int vm_pageout_scan_callback(struct proc *p, void *data);
739 vm_pageout_scan(int pass)
741 struct vm_pageout_scan_info info;
743 struct vm_page marker;
744 struct vnode *vpfailed; /* warning, allowed to be stale */
747 int inactive_shortage, active_shortage;
748 int inactive_original_shortage;
751 int vnodes_skipped = 0;
755 * Do whatever cleanup that the pmap code can.
760 * Calculate our target for the number of free+cache pages we
761 * want to get to. This is higher then the number that causes
762 * allocations to stall (severe) in order to provide hysteresis,
763 * and if we don't make it all the way but get to the minimum
766 inactive_shortage = vm_paging_target() + vm_pageout_deficit;
767 inactive_original_shortage = inactive_shortage;
768 vm_pageout_deficit = 0;
771 * Start scanning the inactive queue for pages we can move to the
772 * cache or free. The scan will stop when the target is reached or
773 * we have scanned the entire inactive queue. Note that m->act_count
774 * is not used to form decisions for the inactive queue, only for the
777 * maxlaunder limits the number of dirty pages we flush per scan.
778 * For most systems a smaller value (16 or 32) is more robust under
779 * extreme memory and disk pressure because any unnecessary writes
780 * to disk can result in extreme performance degredation. However,
781 * systems with excessive dirty pages (especially when MAP_NOSYNC is
782 * used) will die horribly with limited laundering. If the pageout
783 * daemon cannot clean enough pages in the first pass, we let it go
784 * all out in succeeding passes.
786 if ((maxlaunder = vm_max_launder) <= 1)
792 * Initialize our marker
794 bzero(&marker, sizeof(marker));
795 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
796 marker.queue = PQ_INACTIVE;
797 marker.wire_count = 1;
800 * Inactive queue scan.
802 * NOTE: The vm_page must be spinlocked before the queue to avoid
803 * deadlocks, so it is easiest to simply iterate the loop
804 * with the queue unlocked at the top.
808 vm_page_queues_spin_lock(PQ_INACTIVE);
809 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq);
810 maxscan = vmstats.v_inactive_count;
811 vm_page_queues_spin_unlock(PQ_INACTIVE);
813 while ((m = TAILQ_NEXT(&marker, pageq)) != NULL &&
814 maxscan-- > 0 && inactive_shortage > 0)
816 vm_page_and_queue_spin_lock(m);
817 if (m != TAILQ_NEXT(&marker, pageq)) {
818 vm_page_and_queue_spin_unlock(m);
822 KKASSERT(m->queue == PQ_INACTIVE);
823 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl,
825 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m,
827 mycpu->gd_cnt.v_pdpages++;
832 if (m->flags & PG_MARKER) {
833 vm_page_and_queue_spin_unlock(m);
838 * Try to busy the page. Don't mess with pages which are
839 * already busy or reorder them in the queue.
841 if (vm_page_busy_try(m, TRUE)) {
842 vm_page_and_queue_spin_unlock(m);
845 vm_page_and_queue_spin_unlock(m);
846 KKASSERT(m->queue == PQ_INACTIVE);
849 * The page has been successfully busied and is now no
850 * longer spinlocked. The queue is no longer spinlocked
855 * A held page may be undergoing I/O, so skip it.
858 vm_page_and_queue_spin_lock(m);
859 if (m->queue == PQ_INACTIVE) {
860 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl,
862 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl,
865 vm_page_and_queue_spin_unlock(m);
866 ++vm_swapcache_inactive_heuristic;
871 if (m->object->ref_count == 0) {
873 * If the object is not being used, we ignore previous
876 vm_page_flag_clear(m, PG_REFERENCED);
877 pmap_clear_reference(m);
878 /* fall through to end */
879 } else if (((m->flags & PG_REFERENCED) == 0) &&
880 (actcount = pmap_ts_referenced(m))) {
882 * Otherwise, if the page has been referenced while
883 * in the inactive queue, we bump the "activation
884 * count" upwards, making it less likely that the
885 * page will be added back to the inactive queue
886 * prematurely again. Here we check the page tables
887 * (or emulated bits, if any), given the upper level
888 * VM system not knowing anything about existing
892 m->act_count += (actcount + ACT_ADVANCE);
898 * (m) is still busied.
900 * If the upper level VM system knows about any page
901 * references, we activate the page. We also set the
902 * "activation count" higher than normal so that we will less
903 * likely place pages back onto the inactive queue again.
905 if ((m->flags & PG_REFERENCED) != 0) {
906 vm_page_flag_clear(m, PG_REFERENCED);
907 actcount = pmap_ts_referenced(m);
909 m->act_count += (actcount + ACT_ADVANCE + 1);
915 * If the upper level VM system doesn't know anything about
916 * the page being dirty, we have to check for it again. As
917 * far as the VM code knows, any partially dirty pages are
920 * Pages marked PG_WRITEABLE may be mapped into the user
921 * address space of a process running on another cpu. A
922 * user process (without holding the MP lock) running on
923 * another cpu may be able to touch the page while we are
924 * trying to remove it. vm_page_cache() will handle this
928 vm_page_test_dirty(m);
935 * Invalid pages can be easily freed
937 vm_pageout_page_free(m);
938 mycpu->gd_cnt.v_dfree++;
940 } else if (m->dirty == 0) {
942 * Clean pages can be placed onto the cache queue.
943 * This effectively frees them.
947 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
949 * Dirty pages need to be paged out, but flushing
950 * a page is extremely expensive verses freeing
951 * a clean page. Rather then artificially limiting
952 * the number of pages we can flush, we instead give
953 * dirty pages extra priority on the inactive queue
954 * by forcing them to be cycled through the queue
955 * twice before being flushed, after which the
956 * (now clean) page will cycle through once more
957 * before being freed. This significantly extends
958 * the thrash point for a heavily loaded machine.
960 vm_page_flag_set(m, PG_WINATCFLS);
961 vm_page_and_queue_spin_lock(m);
962 if (m->queue == PQ_INACTIVE) {
963 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
964 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
966 vm_page_and_queue_spin_unlock(m);
967 ++vm_swapcache_inactive_heuristic;
969 } else if (maxlaunder > 0) {
971 * We always want to try to flush some dirty pages if
972 * we encounter them, to keep the system stable.
973 * Normally this number is small, but under extreme
974 * pressure where there are insufficient clean pages
975 * on the inactive queue, we may have to go all out.
977 int swap_pageouts_ok;
978 struct vnode *vp = NULL;
982 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
983 swap_pageouts_ok = 1;
985 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
986 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
987 vm_page_count_min(0));
992 * We don't bother paging objects that are "dead".
993 * Those objects are in a "rundown" state.
995 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
996 vm_page_and_queue_spin_lock(m);
997 if (m->queue == PQ_INACTIVE) {
998 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
999 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1001 vm_page_and_queue_spin_unlock(m);
1002 ++vm_swapcache_inactive_heuristic;
1008 * (m) is still busied.
1010 * The object is already known NOT to be dead. It
1011 * is possible for the vget() to block the whole
1012 * pageout daemon, but the new low-memory handling
1013 * code should prevent it.
1015 * The previous code skipped locked vnodes and, worse,
1016 * reordered pages in the queue. This results in
1017 * completely non-deterministic operation because,
1018 * quite often, a vm_fault has initiated an I/O and
1019 * is holding a locked vnode at just the point where
1020 * the pageout daemon is woken up.
1022 * We can't wait forever for the vnode lock, we might
1023 * deadlock due to a vn_read() getting stuck in
1024 * vm_wait while holding this vnode. We skip the
1025 * vnode if we can't get it in a reasonable amount
1028 * vpfailed is used to (try to) avoid the case where
1029 * a large number of pages are associated with a
1030 * locked vnode, which could cause the pageout daemon
1031 * to stall for an excessive amount of time.
1033 if (object->type == OBJT_VNODE) {
1036 vp = object->handle;
1037 flags = LK_EXCLUSIVE | LK_NOOBJ;
1041 flags |= LK_TIMELOCK;
1046 * We have unbusied (m) temporarily so we can
1047 * acquire the vp lock without deadlocking.
1048 * (m) is held to prevent destruction.
1050 if (vget(vp, flags) != 0) {
1052 ++pageout_lock_miss;
1053 if (object->flags & OBJ_MIGHTBEDIRTY)
1060 * The page might have been moved to another
1061 * queue during potential blocking in vget()
1062 * above. The page might have been freed and
1063 * reused for another vnode. The object might
1064 * have been reused for another vnode.
1066 if (m->queue != PQ_INACTIVE ||
1067 m->object != object ||
1068 object->handle != vp) {
1069 if (object->flags & OBJ_MIGHTBEDIRTY)
1077 * The page may have been busied during the
1078 * blocking in vput(); We don't move the
1079 * page back onto the end of the queue so that
1080 * statistics are more correct if we don't.
1082 if (vm_page_busy_try(m, TRUE)) {
1090 * (m) is busied again
1092 * We own the busy bit and remove our hold
1093 * bit. If the page is still held it
1094 * might be undergoing I/O, so skip it.
1096 if (m->hold_count) {
1097 vm_page_and_queue_spin_lock(m);
1098 if (m->queue == PQ_INACTIVE) {
1099 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1100 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1102 vm_page_and_queue_spin_unlock(m);
1103 ++vm_swapcache_inactive_heuristic;
1104 if (object->flags & OBJ_MIGHTBEDIRTY)
1110 /* (m) is left busied as we fall through */
1114 * page is busy and not held here.
1116 * If a page is dirty, then it is either being washed
1117 * (but not yet cleaned) or it is still in the
1118 * laundry. If it is still in the laundry, then we
1119 * start the cleaning operation.
1121 * decrement inactive_shortage on success to account
1122 * for the (future) cleaned page. Otherwise we
1123 * could wind up laundering or cleaning too many
1126 if (vm_pageout_clean(m) != 0) {
1127 --inactive_shortage;
1130 /* clean ate busy, page no longer accessible */
1137 vm_page_queues_spin_lock(PQ_INACTIVE);
1138 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq);
1139 vm_page_queues_spin_unlock(PQ_INACTIVE);
1142 * We want to move pages from the active queue to the inactive
1143 * queue to get the inactive queue to the inactive target. If
1144 * we still have a page shortage from above we try to directly free
1145 * clean pages instead of moving them.
1147 * If we do still have a shortage we keep track of the number of
1148 * pages we free or cache (recycle_count) as a measure of thrashing
1149 * between the active and inactive queues.
1151 * If we were able to completely satisfy the free+cache targets
1152 * from the inactive pool we limit the number of pages we move
1153 * from the active pool to the inactive pool to 2x the pages we
1154 * had removed from the inactive pool (with a minimum of 1/5 the
1155 * inactive target). If we were not able to completely satisfy
1156 * the free+cache targets we go for the whole target aggressively.
1158 * NOTE: Both variables can end up negative.
1159 * NOTE: We are still in a critical section.
1161 active_shortage = vmstats.v_inactive_target - vmstats.v_inactive_count;
1162 if (inactive_original_shortage < vmstats.v_inactive_target / 10)
1163 inactive_original_shortage = vmstats.v_inactive_target / 10;
1164 if (inactive_shortage <= 0 &&
1165 active_shortage > inactive_original_shortage * 2) {
1166 active_shortage = inactive_original_shortage * 2;
1170 marker.queue = PQ_ACTIVE;
1172 vm_page_queues_spin_lock(PQ_ACTIVE);
1173 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_ACTIVE].pl, &marker, pageq);
1174 vm_page_queues_spin_unlock(PQ_ACTIVE);
1175 pcount = vmstats.v_active_count;
1177 while ((m = TAILQ_NEXT(&marker, pageq)) != NULL &&
1178 pcount-- > 0 && (inactive_shortage > 0 || active_shortage > 0))
1180 vm_page_and_queue_spin_lock(m);
1181 if (m != TAILQ_NEXT(&marker, pageq)) {
1182 vm_page_and_queue_spin_unlock(m);
1186 KKASSERT(m->queue == PQ_ACTIVE);
1187 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl,
1189 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_ACTIVE].pl, m,
1195 if (m->flags & PG_MARKER) {
1196 vm_page_and_queue_spin_unlock(m);
1201 * Try to busy the page. Don't mess with pages which are
1202 * already busy or reorder them in the queue.
1204 if (vm_page_busy_try(m, TRUE)) {
1205 vm_page_and_queue_spin_unlock(m);
1210 * Don't deactivate pages that are held, even if we can
1211 * busy them. (XXX why not?)
1213 if (m->hold_count != 0) {
1214 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl,
1216 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl,
1218 vm_page_and_queue_spin_unlock(m);
1222 vm_page_and_queue_spin_unlock(m);
1225 * The page has been successfully busied and the page and
1226 * queue are no longer locked.
1230 * The count for pagedaemon pages is done after checking the
1231 * page for eligibility...
1233 mycpu->gd_cnt.v_pdpages++;
1236 * Check to see "how much" the page has been used and clear
1237 * the tracking access bits. If the object has no references
1238 * don't bother paying the expense.
1241 if (m->object->ref_count != 0) {
1242 if (m->flags & PG_REFERENCED)
1244 actcount += pmap_ts_referenced(m);
1246 m->act_count += ACT_ADVANCE + actcount;
1247 if (m->act_count > ACT_MAX)
1248 m->act_count = ACT_MAX;
1251 vm_page_flag_clear(m, PG_REFERENCED);
1254 * actcount is only valid if the object ref_count is non-zero.
1256 if (actcount && m->object->ref_count != 0) {
1257 vm_page_and_queue_spin_lock(m);
1258 if (m->queue == PQ_ACTIVE) {
1259 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl,
1261 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl,
1264 vm_page_and_queue_spin_unlock(m);
1267 m->act_count -= min(m->act_count, ACT_DECLINE);
1268 if (vm_pageout_algorithm ||
1269 m->object->ref_count == 0 ||
1270 m->act_count < pass + 1
1273 * Deactivate the page. If we had a
1274 * shortage from our inactive scan try to
1275 * free (cache) the page instead.
1277 * Don't just blindly cache the page if
1278 * we do not have a shortage from the
1279 * inactive scan, that could lead to
1280 * gigabytes being moved.
1283 if (inactive_shortage > 0 ||
1284 m->object->ref_count == 0) {
1285 if (inactive_shortage > 0)
1287 vm_page_protect(m, VM_PROT_NONE);
1288 if (m->dirty == 0 &&
1289 inactive_shortage > 0) {
1290 --inactive_shortage;
1293 vm_page_deactivate(m);
1297 vm_page_deactivate(m);
1301 vm_page_and_queue_spin_lock(m);
1302 if (m->queue == PQ_ACTIVE) {
1304 &vm_page_queues[PQ_ACTIVE].pl,
1307 &vm_page_queues[PQ_ACTIVE].pl,
1310 vm_page_and_queue_spin_unlock(m);
1317 * Clean out our local marker.
1319 vm_page_queues_spin_lock(PQ_ACTIVE);
1320 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, &marker, pageq);
1321 vm_page_queues_spin_unlock(PQ_ACTIVE);
1324 * The number of actually free pages can drop down to v_free_reserved,
1325 * we try to build the free count back above v_free_min. Note that
1326 * vm_paging_needed() also returns TRUE if v_free_count is not at
1327 * least v_free_min so that is the minimum we must build the free
1330 * We use a slightly higher target to improve hysteresis,
1331 * ((v_free_target + v_free_min) / 2). Since v_free_target
1332 * is usually the same as v_cache_min this maintains about
1333 * half the pages in the free queue as are in the cache queue,
1334 * providing pretty good pipelining for pageout operation.
1336 * The system operator can manipulate vm.v_cache_min and
1337 * vm.v_free_target to tune the pageout demon. Be sure
1338 * to keep vm.v_free_min < vm.v_free_target.
1340 * Note that the original paging target is to get at least
1341 * (free_min + cache_min) into (free + cache). The slightly
1342 * higher target will shift additional pages from cache to free
1343 * without effecting the original paging target in order to
1344 * maintain better hysteresis and not have the free count always
1345 * be dead-on v_free_min.
1347 * NOTE: we are still in a critical section.
1349 * Pages moved from PQ_CACHE to totally free are not counted in the
1350 * pages_freed counter.
1352 while (vmstats.v_free_count <
1353 (vmstats.v_free_min + vmstats.v_free_target) / 2) {
1355 * This steals some code from vm/vm_page.c
1357 static int cache_rover = 0;
1359 m = vm_page_list_find(PQ_CACHE, cache_rover & PQ_L2_MASK, FALSE);
1362 /* page is returned removed from its queue and spinlocked */
1363 if (vm_page_busy_try(m, TRUE)) {
1364 vm_page_deactivate_locked(m);
1365 vm_page_spin_unlock(m);
1367 kprintf("Warning: busy page %p found in cache\n", m);
1371 vm_page_spin_unlock(m);
1372 pagedaemon_wakeup();
1375 * Page has been successfully busied and it and its queue
1376 * is no longer spinlocked.
1378 if ((m->flags & PG_UNMANAGED) ||
1381 vm_page_deactivate(m);
1385 KKASSERT((m->flags & PG_MAPPED) == 0);
1386 KKASSERT(m->dirty == 0);
1387 cache_rover += PQ_PRIME2;
1388 vm_pageout_page_free(m);
1389 mycpu->gd_cnt.v_dfree++;
1392 #if !defined(NO_SWAPPING)
1394 * Idle process swapout -- run once per second.
1396 if (vm_swap_idle_enabled) {
1398 if (time_second != lsec) {
1399 vm_pageout_req_swapout |= VM_SWAP_IDLE;
1407 * If we didn't get enough free pages, and we have skipped a vnode
1408 * in a writeable object, wakeup the sync daemon. And kick swapout
1409 * if we did not get enough free pages.
1411 if (vm_paging_target() > 0) {
1412 if (vnodes_skipped && vm_page_count_min(0))
1414 #if !defined(NO_SWAPPING)
1415 if (vm_swap_enabled && vm_page_count_target()) {
1417 vm_pageout_req_swapout |= VM_SWAP_NORMAL;
1423 * Handle catastrophic conditions. Under good conditions we should
1424 * be at the target, well beyond our minimum. If we could not even
1425 * reach our minimum the system is under heavy stress.
1427 * Determine whether we have run out of memory. This occurs when
1428 * swap_pager_full is TRUE and the only pages left in the page
1429 * queues are dirty. We will still likely have page shortages.
1431 * - swap_pager_full is set if insufficient swap was
1432 * available to satisfy a requested pageout.
1434 * - the inactive queue is bloated (4 x size of active queue),
1435 * meaning it is unable to get rid of dirty pages and.
1437 * - vm_page_count_min() without counting pages recycled from the
1438 * active queue (recycle_count) means we could not recover
1439 * enough pages to meet bare minimum needs. This test only
1440 * works if the inactive queue is bloated.
1442 * - due to a positive inactive_shortage we shifted the remaining
1443 * dirty pages from the active queue to the inactive queue
1444 * trying to find clean ones to free.
1446 if (swap_pager_full && vm_page_count_min(recycle_count))
1447 kprintf("Warning: system low on memory+swap!\n");
1448 if (swap_pager_full && vm_page_count_min(recycle_count) &&
1449 vmstats.v_inactive_count > vmstats.v_active_count * 4 &&
1450 inactive_shortage > 0) {
1454 info.bigproc = NULL;
1456 allproc_scan(vm_pageout_scan_callback, &info);
1457 if (info.bigproc != NULL) {
1458 killproc(info.bigproc, "out of swap space");
1459 info.bigproc->p_nice = PRIO_MIN;
1460 info.bigproc->p_usched->resetpriority(
1461 FIRST_LWP_IN_PROC(info.bigproc));
1462 wakeup(&vmstats.v_free_count);
1463 PRELE(info.bigproc);
1466 return(inactive_shortage);
1470 * The caller must hold proc_token.
1473 vm_pageout_scan_callback(struct proc *p, void *data)
1475 struct vm_pageout_scan_info *info = data;
1479 * Never kill system processes or init. If we have configured swap
1480 * then try to avoid killing low-numbered pids.
1482 if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) ||
1483 ((p->p_pid < 48) && (vm_swap_size != 0))) {
1488 * if the process is in a non-running type state,
1491 if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
1495 * Get the approximate process size. Note that anonymous pages
1496 * with backing swap will be counted twice, but there should not
1497 * be too many such pages due to the stress the VM system is
1498 * under at this point.
1500 size = vmspace_anonymous_count(p->p_vmspace) +
1501 vmspace_swap_count(p->p_vmspace);
1504 * If the this process is bigger than the biggest one
1507 if (info->bigsize < size) {
1509 PRELE(info->bigproc);
1512 info->bigsize = size;
1518 * This routine tries to maintain the pseudo LRU active queue,
1519 * so that during long periods of time where there is no paging,
1520 * that some statistic accumulation still occurs. This code
1521 * helps the situation where paging just starts to occur.
1524 vm_pageout_page_stats(void)
1526 static int fullintervalcount = 0;
1527 struct vm_page marker;
1529 int pcount, tpcount; /* Number of pages to check */
1532 page_shortage = (vmstats.v_inactive_target + vmstats.v_cache_max +
1533 vmstats.v_free_min) -
1534 (vmstats.v_free_count + vmstats.v_inactive_count +
1535 vmstats.v_cache_count);
1537 if (page_shortage <= 0)
1540 pcount = vmstats.v_active_count;
1541 fullintervalcount += vm_pageout_stats_interval;
1542 if (fullintervalcount < vm_pageout_full_stats_interval) {
1543 tpcount = (vm_pageout_stats_max * vmstats.v_active_count) /
1544 vmstats.v_page_count;
1545 if (pcount > tpcount)
1548 fullintervalcount = 0;
1551 bzero(&marker, sizeof(marker));
1552 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
1553 marker.queue = PQ_ACTIVE;
1554 marker.wire_count = 1;
1556 vm_page_queues_spin_lock(PQ_ACTIVE);
1557 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_ACTIVE].pl, &marker, pageq);
1558 vm_page_queues_spin_unlock(PQ_ACTIVE);
1560 while ((m = TAILQ_NEXT(&marker, pageq)) != NULL &&
1565 vm_page_and_queue_spin_lock(m);
1566 if (m != TAILQ_NEXT(&marker, pageq)) {
1567 vm_page_and_queue_spin_unlock(m);
1571 KKASSERT(m->queue == PQ_ACTIVE);
1572 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, &marker, pageq);
1573 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_ACTIVE].pl, m,
1579 if (m->flags & PG_MARKER) {
1580 vm_page_and_queue_spin_unlock(m);
1585 * Ignore pages we can't busy
1587 if (vm_page_busy_try(m, TRUE)) {
1588 vm_page_and_queue_spin_unlock(m);
1591 vm_page_and_queue_spin_unlock(m);
1592 KKASSERT(m->queue == PQ_ACTIVE);
1595 * We now have a safely busied page, the page and queue
1596 * spinlocks have been released.
1600 if (m->hold_count) {
1606 * Calculate activity
1609 if (m->flags & PG_REFERENCED) {
1610 vm_page_flag_clear(m, PG_REFERENCED);
1613 actcount += pmap_ts_referenced(m);
1616 * Update act_count and move page to end of queue.
1619 m->act_count += ACT_ADVANCE + actcount;
1620 if (m->act_count > ACT_MAX)
1621 m->act_count = ACT_MAX;
1622 vm_page_and_queue_spin_lock(m);
1623 if (m->queue == PQ_ACTIVE) {
1624 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl,
1626 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl,
1629 vm_page_and_queue_spin_unlock(m);
1634 if (m->act_count == 0) {
1636 * We turn off page access, so that we have
1637 * more accurate RSS stats. We don't do this
1638 * in the normal page deactivation when the
1639 * system is loaded VM wise, because the
1640 * cost of the large number of page protect
1641 * operations would be higher than the value
1642 * of doing the operation.
1644 * We use the marker to save our place so
1645 * we can release the spin lock. both (m)
1646 * and (next) will be invalid.
1648 vm_page_protect(m, VM_PROT_NONE);
1649 vm_page_deactivate(m);
1651 m->act_count -= min(m->act_count, ACT_DECLINE);
1652 vm_page_and_queue_spin_lock(m);
1653 if (m->queue == PQ_ACTIVE) {
1654 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl,
1656 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl,
1659 vm_page_and_queue_spin_unlock(m);
1665 * Remove our local marker
1667 vm_page_queues_spin_lock(PQ_ACTIVE);
1668 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, &marker, pageq);
1669 vm_page_queues_spin_unlock(PQ_ACTIVE);
1674 vm_pageout_free_page_calc(vm_size_t count)
1676 if (count < vmstats.v_page_count)
1679 * free_reserved needs to include enough for the largest swap pager
1680 * structures plus enough for any pv_entry structs when paging.
1682 if (vmstats.v_page_count > 1024)
1683 vmstats.v_free_min = 4 + (vmstats.v_page_count - 1024) / 200;
1685 vmstats.v_free_min = 4;
1686 vmstats.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
1687 vmstats.v_interrupt_free_min;
1688 vmstats.v_free_reserved = vm_pageout_page_count +
1689 vmstats.v_pageout_free_min + (count / 768) + PQ_L2_SIZE;
1690 vmstats.v_free_severe = vmstats.v_free_min / 2;
1691 vmstats.v_free_min += vmstats.v_free_reserved;
1692 vmstats.v_free_severe += vmstats.v_free_reserved;
1698 * vm_pageout is the high level pageout daemon.
1703 vm_pageout_thread(void)
1706 int inactive_shortage;
1709 * Initialize some paging parameters.
1711 curthread->td_flags |= TDF_SYSTHREAD;
1713 vmstats.v_interrupt_free_min = 2;
1714 if (vmstats.v_page_count < 2000)
1715 vm_pageout_page_count = 8;
1717 vm_pageout_free_page_calc(vmstats.v_page_count);
1720 * v_free_target and v_cache_min control pageout hysteresis. Note
1721 * that these are more a measure of the VM cache queue hysteresis
1722 * then the VM free queue. Specifically, v_free_target is the
1723 * high water mark (free+cache pages).
1725 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
1726 * low water mark, while v_free_min is the stop. v_cache_min must
1727 * be big enough to handle memory needs while the pageout daemon
1728 * is signalled and run to free more pages.
1730 if (vmstats.v_free_count > 6144)
1731 vmstats.v_free_target = 4 * vmstats.v_free_min + vmstats.v_free_reserved;
1733 vmstats.v_free_target = 2 * vmstats.v_free_min + vmstats.v_free_reserved;
1736 * NOTE: With the new buffer cache b_act_count we want the default
1737 * inactive target to be a percentage of available memory.
1739 * The inactive target essentially determines the minimum
1740 * number of 'temporary' pages capable of caching one-time-use
1741 * files when the VM system is otherwise full of pages
1742 * belonging to multi-time-use files or active program data.
1744 * NOTE: The inactive target is aggressively persued only if the
1745 * inactive queue becomes too small. If the inactive queue
1746 * is large enough to satisfy page movement to free+cache
1747 * then it is repopulated more slowly from the active queue.
1748 * This allows a general inactive_target default to be set.
1750 * There is an issue here for processes which sit mostly idle
1751 * 'overnight', such as sshd, tcsh, and X. Any movement from
1752 * the active queue will eventually cause such pages to
1753 * recycle eventually causing a lot of paging in the morning.
1754 * To reduce the incidence of this pages cycled out of the
1755 * buffer cache are moved directly to the inactive queue if
1756 * they were only used once or twice.
1758 * The vfs.vm_cycle_point sysctl can be used to adjust this.
1759 * Increasing the value (up to 64) increases the number of
1760 * buffer recyclements which go directly to the inactive queue.
1762 if (vmstats.v_free_count > 2048) {
1763 vmstats.v_cache_min = vmstats.v_free_target;
1764 vmstats.v_cache_max = 2 * vmstats.v_cache_min;
1766 vmstats.v_cache_min = 0;
1767 vmstats.v_cache_max = 0;
1769 vmstats.v_inactive_target = vmstats.v_free_count / 4;
1771 /* XXX does not really belong here */
1772 if (vm_page_max_wired == 0)
1773 vm_page_max_wired = vmstats.v_free_count / 3;
1775 if (vm_pageout_stats_max == 0)
1776 vm_pageout_stats_max = vmstats.v_free_target;
1779 * Set interval in seconds for stats scan.
1781 if (vm_pageout_stats_interval == 0)
1782 vm_pageout_stats_interval = 5;
1783 if (vm_pageout_full_stats_interval == 0)
1784 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1788 * Set maximum free per pass
1790 if (vm_pageout_stats_free_max == 0)
1791 vm_pageout_stats_free_max = 5;
1793 swap_pager_swap_init();
1797 * The pageout daemon is never done, so loop forever.
1803 * Wait for an action request. If we timeout check to
1804 * see if paging is needed (in case the normal wakeup
1807 if (vm_pages_needed == 0) {
1808 error = tsleep(&vm_pages_needed,
1810 vm_pageout_stats_interval * hz);
1812 vm_paging_needed() == 0 &&
1813 vm_pages_needed == 0) {
1814 vm_pageout_page_stats();
1817 vm_pages_needed = 1;
1820 mycpu->gd_cnt.v_pdwakeups++;
1823 * Scan for pageout. Try to avoid thrashing the system
1826 inactive_shortage = vm_pageout_scan(pass);
1827 if (inactive_shortage > 0) {
1829 if (swap_pager_full) {
1831 * Running out of memory, catastrophic back-off
1832 * to one-second intervals.
1834 tsleep(&vm_pages_needed, 0, "pdelay", hz);
1835 } else if (pass < 10 && vm_pages_needed > 1) {
1837 * Normal operation, additional processes
1838 * have already kicked us. Retry immediately.
1840 } else if (pass < 10) {
1842 * Normal operation, fewer processes. Delay
1843 * a bit but allow wakeups.
1845 vm_pages_needed = 0;
1846 tsleep(&vm_pages_needed, 0, "pdelay", hz / 10);
1847 vm_pages_needed = 1;
1850 * We've taken too many passes, forced delay.
1852 tsleep(&vm_pages_needed, 0, "pdelay", hz / 10);
1856 * Interlocked wakeup of waiters (non-optional)
1859 if (vm_pages_needed && !vm_page_count_min(0)) {
1860 wakeup(&vmstats.v_free_count);
1861 vm_pages_needed = 0;
1867 static struct kproc_desc page_kp = {
1872 SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp)
1876 * Called after allocating a page out of the cache or free queue
1877 * to possibly wake the pagedaemon up to replentish our supply.
1879 * We try to generate some hysteresis by waking the pagedaemon up
1880 * when our free+cache pages go below the free_min+cache_min level.
1881 * The pagedaemon tries to get the count back up to at least the
1882 * minimum, and through to the target level if possible.
1884 * If the pagedaemon is already active bump vm_pages_needed as a hint
1885 * that there are even more requests pending.
1891 pagedaemon_wakeup(void)
1893 if (vm_paging_needed() && curthread != pagethread) {
1894 if (vm_pages_needed == 0) {
1895 vm_pages_needed = 1; /* SMP race ok */
1896 wakeup(&vm_pages_needed);
1897 } else if (vm_page_count_min(0)) {
1898 ++vm_pages_needed; /* SMP race ok */
1903 #if !defined(NO_SWAPPING)
1910 vm_req_vmdaemon(void)
1912 static int lastrun = 0;
1914 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
1915 wakeup(&vm_daemon_needed);
1920 static int vm_daemon_callback(struct proc *p, void *data __unused);
1929 * XXX vm_daemon_needed specific token?
1932 tsleep(&vm_daemon_needed, 0, "psleep", 0);
1933 if (vm_pageout_req_swapout) {
1934 swapout_procs(vm_pageout_req_swapout);
1935 vm_pageout_req_swapout = 0;
1938 * scan the processes for exceeding their rlimits or if
1939 * process is swapped out -- deactivate pages
1941 allproc_scan(vm_daemon_callback, NULL);
1946 * Caller must hold proc_token.
1949 vm_daemon_callback(struct proc *p, void *data __unused)
1951 vm_pindex_t limit, size;
1954 * if this is a system process or if we have already
1955 * looked at this process, skip it.
1957 if (p->p_flag & (P_SYSTEM | P_WEXIT))
1961 * if the process is in a non-running type state,
1964 if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
1970 limit = OFF_TO_IDX(qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
1971 p->p_rlimit[RLIMIT_RSS].rlim_max));
1974 * let processes that are swapped out really be
1975 * swapped out. Set the limit to nothing to get as
1976 * many pages out to swap as possible.
1978 if (p->p_flag & P_SWAPPEDOUT)
1981 lwkt_gettoken(&p->p_vmspace->vm_map.token);
1982 size = vmspace_resident_count(p->p_vmspace);
1983 if (limit >= 0 && size >= limit) {
1984 vm_pageout_map_deactivate_pages(&p->p_vmspace->vm_map, limit);
1986 lwkt_reltoken(&p->p_vmspace->vm_map.token);