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 <vm/vm_page2.h>
100 * System initialization
103 /* the kernel process "vm_pageout"*/
104 static int vm_pageout_clean (vm_page_t);
105 static int vm_pageout_scan (int pass);
106 static int vm_pageout_free_page_calc (vm_size_t count);
107 struct thread *pagethread;
109 #if !defined(NO_SWAPPING)
110 /* the kernel process "vm_daemon"*/
111 static void vm_daemon (void);
112 static struct thread *vmthread;
114 static struct kproc_desc vm_kp = {
119 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp)
123 int vm_pages_needed=0; /* Event on which pageout daemon sleeps */
124 int vm_pageout_deficit=0; /* Estimated number of pages deficit */
125 int vm_pageout_pages_needed=0; /* flag saying that the pageout daemon needs pages */
127 #if !defined(NO_SWAPPING)
128 static int vm_pageout_req_swapout; /* XXX */
129 static int vm_daemon_needed;
131 static int vm_max_launder = 32;
132 static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
133 static int vm_pageout_full_stats_interval = 0;
134 static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0;
135 static int defer_swap_pageouts=0;
136 static int disable_swap_pageouts=0;
138 #if defined(NO_SWAPPING)
139 static int vm_swap_enabled=0;
140 static int vm_swap_idle_enabled=0;
142 static int vm_swap_enabled=1;
143 static int vm_swap_idle_enabled=0;
146 SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
147 CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
149 SYSCTL_INT(_vm, OID_AUTO, max_launder,
150 CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
152 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
153 CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
155 SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
156 CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
158 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
159 CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
161 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max,
162 CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented");
164 #if defined(NO_SWAPPING)
165 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
166 CTLFLAG_RD, &vm_swap_enabled, 0, "");
167 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
168 CTLFLAG_RD, &vm_swap_idle_enabled, 0, "");
170 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
171 CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
172 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
173 CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
176 SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
177 CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
179 SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
180 CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
182 static int pageout_lock_miss;
183 SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
184 CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
187 SYSCTL_INT(_vm, OID_AUTO, vm_load,
188 CTLFLAG_RD, &vm_load, 0, "load on the VM system");
189 int vm_load_enable = 1;
190 SYSCTL_INT(_vm, OID_AUTO, vm_load_enable,
191 CTLFLAG_RW, &vm_load_enable, 0, "enable vm_load rate limiting");
194 SYSCTL_INT(_vm, OID_AUTO, vm_load_debug,
195 CTLFLAG_RW, &vm_load_debug, 0, "debug vm_load");
198 #define VM_PAGEOUT_PAGE_COUNT 16
199 int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
201 int vm_page_max_wired; /* XXX max # of wired pages system-wide */
203 #if !defined(NO_SWAPPING)
204 typedef void freeer_fcn_t (vm_map_t, vm_object_t, vm_pindex_t, int);
205 static void vm_pageout_map_deactivate_pages (vm_map_t, vm_pindex_t);
206 static freeer_fcn_t vm_pageout_object_deactivate_pages;
207 static void vm_req_vmdaemon (void);
209 static void vm_pageout_page_stats(void);
212 * Update vm_load to slow down faulting processes.
218 vm_fault_ratecheck(void)
220 if (vm_pages_needed) {
232 * Clean the page and remove it from the laundry. The page must not be
235 * We set the busy bit to cause potential page faults on this page to
236 * block. Note the careful timing, however, the busy bit isn't set till
237 * late and we cannot do anything that will mess with the page.
239 * The caller must hold vm_token.
242 vm_pageout_clean(vm_page_t m)
245 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 if ((m->hold_count != 0) ||
265 ((m->busy != 0) || (m->flags & (PG_BUSY|PG_UNMANAGED)))) {
269 mc[vm_pageout_page_count] = m;
271 page_base = vm_pageout_page_count;
276 * Scan object for clusterable pages.
278 * We can cluster ONLY if: ->> the page is NOT
279 * clean, wired, busy, held, or mapped into a
280 * buffer, and one of the following:
281 * 1) The page is inactive, or a seldom used
284 * 2) we force the issue.
286 * During heavy mmap/modification loads the pageout
287 * daemon can really fragment the underlying file
288 * due to flushing pages out of order and not trying
289 * align the clusters (which leave sporatic out-of-order
290 * holes). To solve this problem we do the reverse scan
291 * first and attempt to align our cluster, then do a
292 * forward scan if room remains.
295 vm_object_hold(object);
297 while (ib && pageout_count < vm_pageout_page_count) {
305 if ((p = vm_page_lookup(object, pindex - ib)) == NULL) {
309 if (((p->queue - p->pc) == PQ_CACHE) ||
310 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
314 vm_page_test_dirty(p);
315 if ((p->dirty & p->valid) == 0 ||
316 p->queue != PQ_INACTIVE ||
317 p->wire_count != 0 || /* may be held by buf cache */
318 p->hold_count != 0) { /* may be undergoing I/O */
326 * alignment boundry, stop here and switch directions. Do
329 if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
333 while (pageout_count < vm_pageout_page_count &&
334 pindex + is < object->size) {
337 if ((p = vm_page_lookup(object, pindex + is)) == NULL)
339 if (((p->queue - p->pc) == PQ_CACHE) ||
340 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
343 vm_page_test_dirty(p);
344 if ((p->dirty & p->valid) == 0 ||
345 p->queue != PQ_INACTIVE ||
346 p->wire_count != 0 || /* may be held by buf cache */
347 p->hold_count != 0) { /* may be undergoing I/O */
350 mc[page_base + pageout_count] = p;
356 * If we exhausted our forward scan, continue with the reverse scan
357 * when possible, even past a page boundry. This catches boundry
360 if (ib && pageout_count < vm_pageout_page_count)
363 vm_object_drop(object);
366 * we allow reads during pageouts...
368 return vm_pageout_flush(&mc[page_base], pageout_count, 0);
372 * vm_pageout_flush() - launder the given pages
374 * The given pages are laundered. Note that we setup for the start of
375 * I/O ( i.e. busy the page ), mark it read-only, and bump the object
376 * reference count all in here rather then in the parent. If we want
377 * the parent to do more sophisticated things we may have to change
380 * The caller must hold vm_token.
383 vm_pageout_flush(vm_page_t *mc, int count, int flags)
386 int pageout_status[count];
390 ASSERT_LWKT_TOKEN_HELD(&vm_token);
393 * Initiate I/O. Bump the vm_page_t->busy counter.
395 for (i = 0; i < count; i++) {
396 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, ("vm_pageout_flush page %p index %d/%d: partially invalid page", mc[i], i, count));
397 vm_page_io_start(mc[i]);
401 * We must make the pages read-only. This will also force the
402 * modified bit in the related pmaps to be cleared. The pager
403 * cannot clear the bit for us since the I/O completion code
404 * typically runs from an interrupt. The act of making the page
405 * read-only handles the case for us.
407 for (i = 0; i < count; i++) {
408 vm_page_protect(mc[i], VM_PROT_READ);
411 object = mc[0]->object;
412 vm_object_pip_add(object, count);
414 vm_pager_put_pages(object, mc, count,
415 (flags | ((object == &kernel_object) ? VM_PAGER_PUT_SYNC : 0)),
418 for (i = 0; i < count; i++) {
419 vm_page_t mt = mc[i];
421 switch (pageout_status[i]) {
430 * Page outside of range of object. Right now we
431 * essentially lose the changes by pretending it
434 pmap_clear_modify(mt);
440 * A page typically cannot be paged out when we
441 * have run out of swap. We leave the page
442 * marked inactive and will try to page it out
445 * Starvation of the active page list is used to
446 * determine when the system is massively memory
455 * If the operation is still going, leave the page busy to
456 * block all other accesses. Also, leave the paging in
457 * progress indicator set so that we don't attempt an object
460 * For any pages which have completed synchronously,
461 * deactivate the page if we are under a severe deficit.
462 * Do not try to enter them into the cache, though, they
463 * might still be read-heavy.
465 if (pageout_status[i] != VM_PAGER_PEND) {
466 if (vm_page_count_severe())
467 vm_page_deactivate(mt);
469 if (!vm_page_count_severe() || !vm_page_try_to_cache(mt))
470 vm_page_protect(mt, VM_PROT_READ);
472 vm_page_io_finish(mt);
473 vm_object_pip_wakeup(object);
479 #if !defined(NO_SWAPPING)
481 * vm_pageout_object_deactivate_pages
483 * deactivate enough pages to satisfy the inactive target
484 * requirements or if vm_page_proc_limit is set, then
485 * deactivate all of the pages in the object and its
488 * The map must be locked.
489 * The caller must hold vm_token.
490 * The caller must hold the vm_object.
492 static int vm_pageout_object_deactivate_pages_callback(vm_page_t, void *);
495 vm_pageout_object_deactivate_pages(vm_map_t map, vm_object_t object,
496 vm_pindex_t desired, int map_remove_only)
498 struct rb_vm_page_scan_info info;
503 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
506 vm_object_hold(object);
508 if (object->type == OBJT_DEVICE || object->type == OBJT_PHYS) {
509 vm_object_drop(object);
512 if (object->paging_in_progress) {
513 vm_object_drop(object);
517 remove_mode = map_remove_only;
518 if (object->shadow_count > 1)
522 * scan the objects entire memory queue. We hold the
523 * object's token so the scan should not race anything.
525 info.limit = remove_mode;
527 info.desired = desired;
528 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
529 vm_pageout_object_deactivate_pages_callback,
532 tmp = object->backing_object;
533 vm_object_drop(object);
539 * The caller must hold vm_token.
540 * The caller must hold the vm_object.
543 vm_pageout_object_deactivate_pages_callback(vm_page_t p, void *data)
545 struct rb_vm_page_scan_info *info = data;
548 if (pmap_resident_count(vm_map_pmap(info->map)) <= info->desired) {
551 mycpu->gd_cnt.v_pdpages++;
552 if (p->wire_count != 0 || p->hold_count != 0 || p->busy != 0 ||
553 (p->flags & (PG_BUSY|PG_UNMANAGED)) ||
554 !pmap_page_exists_quick(vm_map_pmap(info->map), p)) {
558 actcount = pmap_ts_referenced(p);
560 vm_page_flag_set(p, PG_REFERENCED);
561 } else if (p->flags & PG_REFERENCED) {
565 if ((p->queue != PQ_ACTIVE) &&
566 (p->flags & PG_REFERENCED)) {
568 p->act_count += actcount;
569 vm_page_flag_clear(p, PG_REFERENCED);
570 } else if (p->queue == PQ_ACTIVE) {
571 if ((p->flags & PG_REFERENCED) == 0) {
572 p->act_count -= min(p->act_count, ACT_DECLINE);
573 if (!info->limit && (vm_pageout_algorithm || (p->act_count == 0))) {
575 vm_page_protect(p, VM_PROT_NONE);
576 vm_page_deactivate(p);
579 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
580 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
584 vm_page_flag_clear(p, PG_REFERENCED);
585 if (p->act_count < (ACT_MAX - ACT_ADVANCE))
586 p->act_count += ACT_ADVANCE;
587 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
588 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
590 } else if (p->queue == PQ_INACTIVE) {
592 vm_page_protect(p, VM_PROT_NONE);
599 * Deactivate some number of pages in a map, try to do it fairly, but
600 * that is really hard to do.
602 * The caller must hold vm_token.
605 vm_pageout_map_deactivate_pages(vm_map_t map, vm_pindex_t desired)
608 vm_object_t obj, bigobj;
611 if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT)) {
619 * first, search out the biggest object, and try to free pages from
622 tmpe = map->header.next;
623 while (tmpe != &map->header) {
624 switch(tmpe->maptype) {
625 case VM_MAPTYPE_NORMAL:
626 case VM_MAPTYPE_VPAGETABLE:
627 obj = tmpe->object.vm_object;
628 if ((obj != NULL) && (obj->shadow_count <= 1) &&
630 (bigobj->resident_page_count < obj->resident_page_count))) {
637 if (tmpe->wired_count > 0)
638 nothingwired = FALSE;
643 vm_pageout_object_deactivate_pages(map, bigobj, desired, 0);
646 * Next, hunt around for other pages to deactivate. We actually
647 * do this search sort of wrong -- .text first is not the best idea.
649 tmpe = map->header.next;
650 while (tmpe != &map->header) {
651 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
653 switch(tmpe->maptype) {
654 case VM_MAPTYPE_NORMAL:
655 case VM_MAPTYPE_VPAGETABLE:
656 obj = tmpe->object.vm_object;
658 vm_pageout_object_deactivate_pages(map, obj, desired, 0);
667 * Remove all mappings if a process is swapped out, this will free page
670 if (desired == 0 && nothingwired)
671 pmap_remove(vm_map_pmap(map),
672 VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
678 * Called when the pageout scan wants to free a page. We no longer
679 * try to cycle the vm_object here with a reference & dealloc, which can
680 * cause a non-trivial object collapse in a critical path.
682 * It is unclear why we cycled the ref_count in the past, perhaps to try
683 * to optimize shadow chain collapses but I don't quite see why it would
684 * be necessary. An OBJ_DEAD object should terminate any and all vm_pages
685 * synchronously and not have to be kicked-start.
687 * The caller must hold vm_token.
690 vm_pageout_page_free(vm_page_t m)
693 vm_page_protect(m, VM_PROT_NONE);
698 * vm_pageout_scan does the dirty work for the pageout daemon.
700 struct vm_pageout_scan_info {
701 struct proc *bigproc;
705 static int vm_pageout_scan_callback(struct proc *p, void *data);
708 * The caller must hold vm_token.
711 vm_pageout_scan(int pass)
713 struct vm_pageout_scan_info info;
715 struct vm_page marker;
716 struct vnode *vpfailed; /* warning, allowed to be stale */
719 int inactive_shortage, active_shortage;
720 int inactive_original_shortage;
723 int vnodes_skipped = 0;
727 * Do whatever cleanup that the pmap code can.
732 * Calculate our target for the number of free+cache pages we
733 * want to get to. This is higher then the number that causes
734 * allocations to stall (severe) in order to provide hysteresis,
735 * and if we don't make it all the way but get to the minimum
738 inactive_shortage = vm_paging_target() + vm_pageout_deficit;
739 inactive_original_shortage = inactive_shortage;
740 vm_pageout_deficit = 0;
743 * Initialize our marker
745 bzero(&marker, sizeof(marker));
746 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
747 marker.queue = PQ_INACTIVE;
748 marker.wire_count = 1;
751 * Start scanning the inactive queue for pages we can move to the
752 * cache or free. The scan will stop when the target is reached or
753 * we have scanned the entire inactive queue. Note that m->act_count
754 * is not used to form decisions for the inactive queue, only for the
757 * maxlaunder limits the number of dirty pages we flush per scan.
758 * For most systems a smaller value (16 or 32) is more robust under
759 * extreme memory and disk pressure because any unnecessary writes
760 * to disk can result in extreme performance degredation. However,
761 * systems with excessive dirty pages (especially when MAP_NOSYNC is
762 * used) will die horribly with limited laundering. If the pageout
763 * daemon cannot clean enough pages in the first pass, we let it go
764 * all out in succeeding passes.
766 if ((maxlaunder = vm_max_launder) <= 1)
772 * We will generally be in a critical section throughout the
773 * scan, but we can release it temporarily when we are sitting on a
774 * non-busy page without fear. this is required to prevent an
775 * interrupt from unbusying or freeing a page prior to our busy
776 * check, leaving us on the wrong queue or checking the wrong
781 maxscan = vmstats.v_inactive_count;
782 for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
783 m != NULL && maxscan-- > 0 && inactive_shortage > 0;
786 mycpu->gd_cnt.v_pdpages++;
789 * It's easier for some of the conditions below to just loop
790 * and catch queue changes here rather then check everywhere
793 if (m->queue != PQ_INACTIVE)
795 next = TAILQ_NEXT(m, pageq);
800 if (m->flags & PG_MARKER)
804 * A held page may be undergoing I/O, so skip it.
807 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
808 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
809 ++vm_swapcache_inactive_heuristic;
814 * Dont mess with busy pages, keep in the front of the
815 * queue, most likely are being paged out.
817 if (m->busy || (m->flags & PG_BUSY)) {
821 if (m->object->ref_count == 0) {
823 * If the object is not being used, we ignore previous
826 vm_page_flag_clear(m, PG_REFERENCED);
827 pmap_clear_reference(m);
829 } else if (((m->flags & PG_REFERENCED) == 0) &&
830 (actcount = pmap_ts_referenced(m))) {
832 * Otherwise, if the page has been referenced while
833 * in the inactive queue, we bump the "activation
834 * count" upwards, making it less likely that the
835 * page will be added back to the inactive queue
836 * prematurely again. Here we check the page tables
837 * (or emulated bits, if any), given the upper level
838 * VM system not knowing anything about existing
842 m->act_count += (actcount + ACT_ADVANCE);
847 * If the upper level VM system knows about any page
848 * references, we activate the page. We also set the
849 * "activation count" higher than normal so that we will less
850 * likely place pages back onto the inactive queue again.
852 if ((m->flags & PG_REFERENCED) != 0) {
853 vm_page_flag_clear(m, PG_REFERENCED);
854 actcount = pmap_ts_referenced(m);
856 m->act_count += (actcount + ACT_ADVANCE + 1);
861 * If the upper level VM system doesn't know anything about
862 * the page being dirty, we have to check for it again. As
863 * far as the VM code knows, any partially dirty pages are
866 * Pages marked PG_WRITEABLE may be mapped into the user
867 * address space of a process running on another cpu. A
868 * user process (without holding the MP lock) running on
869 * another cpu may be able to touch the page while we are
870 * trying to remove it. vm_page_cache() will handle this
874 vm_page_test_dirty(m);
881 * Invalid pages can be easily freed
883 vm_pageout_page_free(m);
884 mycpu->gd_cnt.v_dfree++;
886 } else if (m->dirty == 0) {
888 * Clean pages can be placed onto the cache queue.
889 * This effectively frees them.
894 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
896 * Dirty pages need to be paged out, but flushing
897 * a page is extremely expensive verses freeing
898 * a clean page. Rather then artificially limiting
899 * the number of pages we can flush, we instead give
900 * dirty pages extra priority on the inactive queue
901 * by forcing them to be cycled through the queue
902 * twice before being flushed, after which the
903 * (now clean) page will cycle through once more
904 * before being freed. This significantly extends
905 * the thrash point for a heavily loaded machine.
907 vm_page_flag_set(m, PG_WINATCFLS);
908 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
909 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
910 ++vm_swapcache_inactive_heuristic;
911 } else if (maxlaunder > 0) {
913 * We always want to try to flush some dirty pages if
914 * we encounter them, to keep the system stable.
915 * Normally this number is small, but under extreme
916 * pressure where there are insufficient clean pages
917 * on the inactive queue, we may have to go all out.
919 int swap_pageouts_ok;
920 struct vnode *vp = NULL;
924 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
925 swap_pageouts_ok = 1;
927 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
928 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
929 vm_page_count_min(0));
934 * We don't bother paging objects that are "dead".
935 * Those objects are in a "rundown" state.
937 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
938 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
939 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
940 ++vm_swapcache_inactive_heuristic;
945 * The object is already known NOT to be dead. It
946 * is possible for the vget() to block the whole
947 * pageout daemon, but the new low-memory handling
948 * code should prevent it.
950 * The previous code skipped locked vnodes and, worse,
951 * reordered pages in the queue. This results in
952 * completely non-deterministic operation because,
953 * quite often, a vm_fault has initiated an I/O and
954 * is holding a locked vnode at just the point where
955 * the pageout daemon is woken up.
957 * We can't wait forever for the vnode lock, we might
958 * deadlock due to a vn_read() getting stuck in
959 * vm_wait while holding this vnode. We skip the
960 * vnode if we can't get it in a reasonable amount
963 * vpfailed is used to (try to) avoid the case where
964 * a large number of pages are associated with a
965 * locked vnode, which could cause the pageout daemon
966 * to stall for an excessive amount of time.
968 if (object->type == OBJT_VNODE) {
972 flags = LK_EXCLUSIVE | LK_NOOBJ;
976 flags |= LK_TIMELOCK;
977 if (vget(vp, flags) != 0) {
980 if (object->flags & OBJ_MIGHTBEDIRTY)
986 * The page might have been moved to another
987 * queue during potential blocking in vget()
988 * above. The page might have been freed and
989 * reused for another vnode. The object might
990 * have been reused for another vnode.
992 if (m->queue != PQ_INACTIVE ||
993 m->object != object ||
994 object->handle != vp) {
995 if (object->flags & OBJ_MIGHTBEDIRTY)
1002 * The page may have been busied during the
1003 * blocking in vput(); We don't move the
1004 * page back onto the end of the queue so that
1005 * statistics are more correct if we don't.
1007 if (m->busy || (m->flags & PG_BUSY)) {
1013 * If the page has become held it might
1014 * be undergoing I/O, so skip it
1016 if (m->hold_count) {
1017 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1018 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1019 ++vm_swapcache_inactive_heuristic;
1020 if (object->flags & OBJ_MIGHTBEDIRTY)
1028 * If a page is dirty, then it is either being washed
1029 * (but not yet cleaned) or it is still in the
1030 * laundry. If it is still in the laundry, then we
1031 * start the cleaning operation.
1033 * This operation may cluster, invalidating the 'next'
1034 * pointer. To prevent an inordinate number of
1035 * restarts we use our marker to remember our place.
1037 * decrement inactive_shortage on success to account
1038 * for the (future) cleaned page. Otherwise we
1039 * could wind up laundering or cleaning too many
1042 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m, &marker, pageq);
1043 if (vm_pageout_clean(m) != 0) {
1044 --inactive_shortage;
1047 next = TAILQ_NEXT(&marker, pageq);
1048 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq);
1055 * We want to move pages from the active queue to the inactive
1056 * queue to get the inactive queue to the inactive target. If
1057 * we still have a page shortage from above we try to directly free
1058 * clean pages instead of moving them.
1060 * If we do still have a shortage we keep track of the number of
1061 * pages we free or cache (recycle_count) as a measure of thrashing
1062 * between the active and inactive queues.
1064 * If we were able to completely satisfy the free+cache targets
1065 * from the inactive pool we limit the number of pages we move
1066 * from the active pool to the inactive pool to 2x the pages we
1067 * had removed from the inactive pool (with a minimum of 1/5 the
1068 * inactive target). If we were not able to completely satisfy
1069 * the free+cache targets we go for the whole target aggressively.
1071 * NOTE: Both variables can end up negative.
1072 * NOTE: We are still in a critical section.
1074 active_shortage = vmstats.v_inactive_target - vmstats.v_inactive_count;
1075 if (inactive_original_shortage < vmstats.v_inactive_target / 10)
1076 inactive_original_shortage = vmstats.v_inactive_target / 10;
1077 if (inactive_shortage <= 0 &&
1078 active_shortage > inactive_original_shortage * 2) {
1079 active_shortage = inactive_original_shortage * 2;
1082 pcount = vmstats.v_active_count;
1084 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1086 while ((m != NULL) && (pcount-- > 0) &&
1087 (inactive_shortage > 0 || active_shortage > 0)
1090 * If the page was ripped out from under us, just stop.
1092 if (m->queue != PQ_ACTIVE)
1094 next = TAILQ_NEXT(m, pageq);
1097 * Don't deactivate pages that are busy.
1099 if ((m->busy != 0) ||
1100 (m->flags & PG_BUSY) ||
1101 (m->hold_count != 0)) {
1102 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1103 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1109 * The count for pagedaemon pages is done after checking the
1110 * page for eligibility...
1112 mycpu->gd_cnt.v_pdpages++;
1115 * Check to see "how much" the page has been used and clear
1116 * the tracking access bits. If the object has no references
1117 * don't bother paying the expense.
1120 if (m->object->ref_count != 0) {
1121 if (m->flags & PG_REFERENCED)
1123 actcount += pmap_ts_referenced(m);
1125 m->act_count += ACT_ADVANCE + actcount;
1126 if (m->act_count > ACT_MAX)
1127 m->act_count = ACT_MAX;
1130 vm_page_flag_clear(m, PG_REFERENCED);
1133 * actcount is only valid if the object ref_count is non-zero.
1135 if (actcount && m->object->ref_count != 0) {
1136 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1137 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1139 m->act_count -= min(m->act_count, ACT_DECLINE);
1140 if (vm_pageout_algorithm ||
1141 m->object->ref_count == 0 ||
1142 m->act_count < pass + 1
1145 * Deactivate the page. If we had a
1146 * shortage from our inactive scan try to
1147 * free (cache) the page instead.
1149 * Don't just blindly cache the page if
1150 * we do not have a shortage from the
1151 * inactive scan, that could lead to
1152 * gigabytes being moved.
1155 if (inactive_shortage > 0 ||
1156 m->object->ref_count == 0) {
1157 if (inactive_shortage > 0)
1160 vm_page_protect(m, VM_PROT_NONE);
1161 if (m->dirty == 0 &&
1162 inactive_shortage > 0) {
1163 --inactive_shortage;
1166 vm_page_deactivate(m);
1170 vm_page_deactivate(m);
1173 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1174 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1181 * The number of actually free pages can drop down to v_free_reserved,
1182 * we try to build the free count back above v_free_min. Note that
1183 * vm_paging_needed() also returns TRUE if v_free_count is not at
1184 * least v_free_min so that is the minimum we must build the free
1187 * We use a slightly higher target to improve hysteresis,
1188 * ((v_free_target + v_free_min) / 2). Since v_free_target
1189 * is usually the same as v_cache_min this maintains about
1190 * half the pages in the free queue as are in the cache queue,
1191 * providing pretty good pipelining for pageout operation.
1193 * The system operator can manipulate vm.v_cache_min and
1194 * vm.v_free_target to tune the pageout demon. Be sure
1195 * to keep vm.v_free_min < vm.v_free_target.
1197 * Note that the original paging target is to get at least
1198 * (free_min + cache_min) into (free + cache). The slightly
1199 * higher target will shift additional pages from cache to free
1200 * without effecting the original paging target in order to
1201 * maintain better hysteresis and not have the free count always
1202 * be dead-on v_free_min.
1204 * NOTE: we are still in a critical section.
1206 * Pages moved from PQ_CACHE to totally free are not counted in the
1207 * pages_freed counter.
1209 while (vmstats.v_free_count <
1210 (vmstats.v_free_min + vmstats.v_free_target) / 2) {
1214 static int cache_rover = 0;
1215 m = vm_page_list_find(PQ_CACHE, cache_rover, FALSE);
1218 if ((m->flags & (PG_BUSY|PG_UNMANAGED)) ||
1223 kprintf("Warning: busy page %p found in cache\n", m);
1225 vm_page_deactivate(m);
1228 KKASSERT((m->flags & PG_MAPPED) == 0);
1229 KKASSERT(m->dirty == 0);
1230 cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK;
1231 vm_pageout_page_free(m);
1232 mycpu->gd_cnt.v_dfree++;
1235 #if !defined(NO_SWAPPING)
1237 * Idle process swapout -- run once per second.
1239 if (vm_swap_idle_enabled) {
1241 if (time_second != lsec) {
1242 vm_pageout_req_swapout |= VM_SWAP_IDLE;
1250 * If we didn't get enough free pages, and we have skipped a vnode
1251 * in a writeable object, wakeup the sync daemon. And kick swapout
1252 * if we did not get enough free pages.
1254 if (vm_paging_target() > 0) {
1255 if (vnodes_skipped && vm_page_count_min(0))
1257 #if !defined(NO_SWAPPING)
1258 if (vm_swap_enabled && vm_page_count_target()) {
1260 vm_pageout_req_swapout |= VM_SWAP_NORMAL;
1266 * Handle catastrophic conditions. Under good conditions we should
1267 * be at the target, well beyond our minimum. If we could not even
1268 * reach our minimum the system is under heavy stress.
1270 * Determine whether we have run out of memory. This occurs when
1271 * swap_pager_full is TRUE and the only pages left in the page
1272 * queues are dirty. We will still likely have page shortages.
1274 * - swap_pager_full is set if insufficient swap was
1275 * available to satisfy a requested pageout.
1277 * - the inactive queue is bloated (4 x size of active queue),
1278 * meaning it is unable to get rid of dirty pages and.
1280 * - vm_page_count_min() without counting pages recycled from the
1281 * active queue (recycle_count) means we could not recover
1282 * enough pages to meet bare minimum needs. This test only
1283 * works if the inactive queue is bloated.
1285 * - due to a positive inactive_shortage we shifted the remaining
1286 * dirty pages from the active queue to the inactive queue
1287 * trying to find clean ones to free.
1289 if (swap_pager_full && vm_page_count_min(recycle_count))
1290 kprintf("Warning: system low on memory+swap!\n");
1291 if (swap_pager_full && vm_page_count_min(recycle_count) &&
1292 vmstats.v_inactive_count > vmstats.v_active_count * 4 &&
1293 inactive_shortage > 0) {
1297 info.bigproc = NULL;
1299 allproc_scan(vm_pageout_scan_callback, &info);
1300 if (info.bigproc != NULL) {
1301 killproc(info.bigproc, "out of swap space");
1302 info.bigproc->p_nice = PRIO_MIN;
1303 info.bigproc->p_usched->resetpriority(
1304 FIRST_LWP_IN_PROC(info.bigproc));
1305 wakeup(&vmstats.v_free_count);
1306 PRELE(info.bigproc);
1309 return(inactive_shortage);
1313 * The caller must hold vm_token and proc_token.
1316 vm_pageout_scan_callback(struct proc *p, void *data)
1318 struct vm_pageout_scan_info *info = data;
1322 * Never kill system processes or init. If we have configured swap
1323 * then try to avoid killing low-numbered pids.
1325 if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) ||
1326 ((p->p_pid < 48) && (vm_swap_size != 0))) {
1331 * if the process is in a non-running type state,
1334 if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
1338 * Get the approximate process size. Note that anonymous pages
1339 * with backing swap will be counted twice, but there should not
1340 * be too many such pages due to the stress the VM system is
1341 * under at this point.
1343 size = vmspace_anonymous_count(p->p_vmspace) +
1344 vmspace_swap_count(p->p_vmspace);
1347 * If the this process is bigger than the biggest one
1350 if (info->bigsize < size) {
1352 PRELE(info->bigproc);
1355 info->bigsize = size;
1361 * This routine tries to maintain the pseudo LRU active queue,
1362 * so that during long periods of time where there is no paging,
1363 * that some statistic accumulation still occurs. This code
1364 * helps the situation where paging just starts to occur.
1366 * The caller must hold vm_token.
1369 vm_pageout_page_stats(void)
1372 int pcount,tpcount; /* Number of pages to check */
1373 static int fullintervalcount = 0;
1377 (vmstats.v_inactive_target + vmstats.v_cache_max + vmstats.v_free_min) -
1378 (vmstats.v_free_count + vmstats.v_inactive_count + vmstats.v_cache_count);
1380 if (page_shortage <= 0)
1383 pcount = vmstats.v_active_count;
1384 fullintervalcount += vm_pageout_stats_interval;
1385 if (fullintervalcount < vm_pageout_full_stats_interval) {
1386 tpcount = (vm_pageout_stats_max * vmstats.v_active_count) / vmstats.v_page_count;
1387 if (pcount > tpcount)
1390 fullintervalcount = 0;
1393 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1394 while ((m != NULL) && (pcount-- > 0)) {
1397 if (m->queue != PQ_ACTIVE) {
1401 next = TAILQ_NEXT(m, pageq);
1403 * Don't deactivate pages that are busy.
1405 if ((m->busy != 0) ||
1406 (m->flags & PG_BUSY) ||
1407 (m->hold_count != 0)) {
1408 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1409 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1415 if (m->flags & PG_REFERENCED) {
1416 vm_page_flag_clear(m, PG_REFERENCED);
1420 actcount += pmap_ts_referenced(m);
1422 m->act_count += ACT_ADVANCE + actcount;
1423 if (m->act_count > ACT_MAX)
1424 m->act_count = ACT_MAX;
1425 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1426 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1428 if (m->act_count == 0) {
1430 * We turn off page access, so that we have
1431 * more accurate RSS stats. We don't do this
1432 * in the normal page deactivation when the
1433 * system is loaded VM wise, because the
1434 * cost of the large number of page protect
1435 * operations would be higher than the value
1436 * of doing the operation.
1439 vm_page_protect(m, VM_PROT_NONE);
1440 vm_page_deactivate(m);
1443 m->act_count -= min(m->act_count, ACT_DECLINE);
1444 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1445 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1454 * The caller must hold vm_token.
1457 vm_pageout_free_page_calc(vm_size_t count)
1459 if (count < vmstats.v_page_count)
1462 * free_reserved needs to include enough for the largest swap pager
1463 * structures plus enough for any pv_entry structs when paging.
1465 if (vmstats.v_page_count > 1024)
1466 vmstats.v_free_min = 4 + (vmstats.v_page_count - 1024) / 200;
1468 vmstats.v_free_min = 4;
1469 vmstats.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
1470 vmstats.v_interrupt_free_min;
1471 vmstats.v_free_reserved = vm_pageout_page_count +
1472 vmstats.v_pageout_free_min + (count / 768) + PQ_L2_SIZE;
1473 vmstats.v_free_severe = vmstats.v_free_min / 2;
1474 vmstats.v_free_min += vmstats.v_free_reserved;
1475 vmstats.v_free_severe += vmstats.v_free_reserved;
1481 * vm_pageout is the high level pageout daemon.
1486 vm_pageout_thread(void)
1489 int inactive_shortage;
1492 * Permanently hold vm_token.
1494 lwkt_gettoken(&vm_token);
1497 * Initialize some paging parameters.
1499 curthread->td_flags |= TDF_SYSTHREAD;
1501 vmstats.v_interrupt_free_min = 2;
1502 if (vmstats.v_page_count < 2000)
1503 vm_pageout_page_count = 8;
1505 vm_pageout_free_page_calc(vmstats.v_page_count);
1508 * v_free_target and v_cache_min control pageout hysteresis. Note
1509 * that these are more a measure of the VM cache queue hysteresis
1510 * then the VM free queue. Specifically, v_free_target is the
1511 * high water mark (free+cache pages).
1513 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
1514 * low water mark, while v_free_min is the stop. v_cache_min must
1515 * be big enough to handle memory needs while the pageout daemon
1516 * is signalled and run to free more pages.
1518 if (vmstats.v_free_count > 6144)
1519 vmstats.v_free_target = 4 * vmstats.v_free_min + vmstats.v_free_reserved;
1521 vmstats.v_free_target = 2 * vmstats.v_free_min + vmstats.v_free_reserved;
1524 * NOTE: With the new buffer cache b_act_count we want the default
1525 * inactive target to be a percentage of available memory.
1527 * The inactive target essentially determines the minimum
1528 * number of 'temporary' pages capable of caching one-time-use
1529 * files when the VM system is otherwise full of pages
1530 * belonging to multi-time-use files or active program data.
1532 * NOTE: The inactive target is aggressively persued only if the
1533 * inactive queue becomes too small. If the inactive queue
1534 * is large enough to satisfy page movement to free+cache
1535 * then it is repopulated more slowly from the active queue.
1536 * This allows a general inactive_target default to be set.
1538 * There is an issue here for processes which sit mostly idle
1539 * 'overnight', such as sshd, tcsh, and X. Any movement from
1540 * the active queue will eventually cause such pages to
1541 * recycle eventually causing a lot of paging in the morning.
1542 * To reduce the incidence of this pages cycled out of the
1543 * buffer cache are moved directly to the inactive queue if
1544 * they were only used once or twice.
1546 * The vfs.vm_cycle_point sysctl can be used to adjust this.
1547 * Increasing the value (up to 64) increases the number of
1548 * buffer recyclements which go directly to the inactive queue.
1550 if (vmstats.v_free_count > 2048) {
1551 vmstats.v_cache_min = vmstats.v_free_target;
1552 vmstats.v_cache_max = 2 * vmstats.v_cache_min;
1554 vmstats.v_cache_min = 0;
1555 vmstats.v_cache_max = 0;
1557 vmstats.v_inactive_target = vmstats.v_free_count / 4;
1559 /* XXX does not really belong here */
1560 if (vm_page_max_wired == 0)
1561 vm_page_max_wired = vmstats.v_free_count / 3;
1563 if (vm_pageout_stats_max == 0)
1564 vm_pageout_stats_max = vmstats.v_free_target;
1567 * Set interval in seconds for stats scan.
1569 if (vm_pageout_stats_interval == 0)
1570 vm_pageout_stats_interval = 5;
1571 if (vm_pageout_full_stats_interval == 0)
1572 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1576 * Set maximum free per pass
1578 if (vm_pageout_stats_free_max == 0)
1579 vm_pageout_stats_free_max = 5;
1581 swap_pager_swap_init();
1585 * The pageout daemon is never done, so loop forever.
1591 * Wait for an action request. If we timeout check to
1592 * see if paging is needed (in case the normal wakeup
1595 if (vm_pages_needed == 0) {
1596 error = tsleep(&vm_pages_needed,
1598 vm_pageout_stats_interval * hz);
1600 vm_paging_needed() == 0 &&
1601 vm_pages_needed == 0) {
1602 vm_pageout_page_stats();
1605 vm_pages_needed = 1;
1608 mycpu->gd_cnt.v_pdwakeups++;
1611 * Scan for pageout. Try to avoid thrashing the system
1614 inactive_shortage = vm_pageout_scan(pass);
1615 if (inactive_shortage > 0) {
1617 if (swap_pager_full) {
1619 * Running out of memory, catastrophic back-off
1620 * to one-second intervals.
1622 tsleep(&vm_pages_needed, 0, "pdelay", hz);
1623 } else if (pass < 10 && vm_pages_needed > 1) {
1625 * Normal operation, additional processes
1626 * have already kicked us. Retry immediately.
1628 } else if (pass < 10) {
1630 * Normal operation, fewer processes. Delay
1631 * a bit but allow wakeups.
1633 vm_pages_needed = 0;
1634 tsleep(&vm_pages_needed, 0, "pdelay", hz / 10);
1635 vm_pages_needed = 1;
1638 * We've taken too many passes, forced delay.
1640 tsleep(&vm_pages_needed, 0, "pdelay", hz / 10);
1644 * Interlocked wakeup of waiters (non-optional)
1647 if (vm_pages_needed && !vm_page_count_min(0)) {
1648 wakeup(&vmstats.v_free_count);
1649 vm_pages_needed = 0;
1655 static struct kproc_desc page_kp = {
1660 SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp)
1664 * Called after allocating a page out of the cache or free queue
1665 * to possibly wake the pagedaemon up to replentish our supply.
1667 * We try to generate some hysteresis by waking the pagedaemon up
1668 * when our free+cache pages go below the free_min+cache_min level.
1669 * The pagedaemon tries to get the count back up to at least the
1670 * minimum, and through to the target level if possible.
1672 * If the pagedaemon is already active bump vm_pages_needed as a hint
1673 * that there are even more requests pending.
1679 pagedaemon_wakeup(void)
1681 if (vm_paging_needed() && curthread != pagethread) {
1682 if (vm_pages_needed == 0) {
1683 vm_pages_needed = 1; /* SMP race ok */
1684 wakeup(&vm_pages_needed);
1685 } else if (vm_page_count_min(0)) {
1686 ++vm_pages_needed; /* SMP race ok */
1691 #if !defined(NO_SWAPPING)
1698 vm_req_vmdaemon(void)
1700 static int lastrun = 0;
1702 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
1703 wakeup(&vm_daemon_needed);
1708 static int vm_daemon_callback(struct proc *p, void *data __unused);
1717 * Permanently hold vm_token.
1719 lwkt_gettoken(&vm_token);
1722 tsleep(&vm_daemon_needed, 0, "psleep", 0);
1723 if (vm_pageout_req_swapout) {
1724 swapout_procs(vm_pageout_req_swapout);
1725 vm_pageout_req_swapout = 0;
1728 * scan the processes for exceeding their rlimits or if
1729 * process is swapped out -- deactivate pages
1731 allproc_scan(vm_daemon_callback, NULL);
1736 * Caller must hold vm_token and proc_token.
1739 vm_daemon_callback(struct proc *p, void *data __unused)
1741 vm_pindex_t limit, size;
1744 * if this is a system process or if we have already
1745 * looked at this process, skip it.
1747 if (p->p_flag & (P_SYSTEM | P_WEXIT))
1751 * if the process is in a non-running type state,
1754 if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
1760 limit = OFF_TO_IDX(qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
1761 p->p_rlimit[RLIMIT_RSS].rlim_max));
1764 * let processes that are swapped out really be
1765 * swapped out. Set the limit to nothing to get as
1766 * many pages out to swap as possible.
1768 if (p->p_flag & P_SWAPPEDOUT)
1771 size = vmspace_resident_count(p->p_vmspace);
1772 if (limit >= 0 && size >= limit) {
1773 vm_pageout_map_deactivate_pages(
1774 &p->p_vmspace->vm_map, limit);