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 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91
45 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46 * All rights reserved.
48 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
50 * Permission to use, copy, modify and distribute this software and
51 * its documentation is hereby granted, provided that both the copyright
52 * notice and this permission notice appear in all copies of the
53 * software, derivative works or modified versions, and any portions
54 * thereof, and that both notices appear in supporting documentation.
56 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
57 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
58 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
60 * Carnegie Mellon requests users of this software to return to
62 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
63 * School of Computer Science
64 * Carnegie Mellon University
65 * Pittsburgh PA 15213-3890
67 * any improvements or extensions that they make and grant Carnegie the
68 * rights to redistribute these changes.
70 * $FreeBSD: src/sys/vm/vm_pageout.c,v 1.151.2.15 2002/12/29 18:21:04 dillon Exp $
71 * $DragonFly: src/sys/vm/vm_pageout.c,v 1.36 2008/07/01 02:02:56 dillon Exp $
75 * The proverbial page-out daemon.
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
83 #include <sys/kthread.h>
84 #include <sys/resourcevar.h>
85 #include <sys/signalvar.h>
86 #include <sys/vnode.h>
87 #include <sys/vmmeter.h>
88 #include <sys/sysctl.h>
91 #include <vm/vm_param.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_page.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_pageout.h>
97 #include <vm/vm_pager.h>
98 #include <vm/swap_pager.h>
99 #include <vm/vm_extern.h>
101 #include <sys/thread2.h>
102 #include <vm/vm_page2.h>
105 * System initialization
108 /* the kernel process "vm_pageout"*/
109 static int vm_pageout_clean (vm_page_t);
110 static int vm_pageout_scan (int pass);
111 static int vm_pageout_free_page_calc (vm_size_t count);
112 struct thread *pagethread;
114 #if !defined(NO_SWAPPING)
115 /* the kernel process "vm_daemon"*/
116 static void vm_daemon (void);
117 static struct thread *vmthread;
119 static struct kproc_desc vm_kp = {
124 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp)
128 int vm_pages_needed=0; /* Event on which pageout daemon sleeps */
129 int vm_pageout_deficit=0; /* Estimated number of pages deficit */
130 int vm_pageout_pages_needed=0; /* flag saying that the pageout daemon needs pages */
132 #if !defined(NO_SWAPPING)
133 static int vm_pageout_req_swapout; /* XXX */
134 static int vm_daemon_needed;
136 static int vm_max_launder = 32;
137 static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
138 static int vm_pageout_full_stats_interval = 0;
139 static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0;
140 static int defer_swap_pageouts=0;
141 static int disable_swap_pageouts=0;
143 #if defined(NO_SWAPPING)
144 static int vm_swap_enabled=0;
145 static int vm_swap_idle_enabled=0;
147 static int vm_swap_enabled=1;
148 static int vm_swap_idle_enabled=0;
151 SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
152 CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
154 SYSCTL_INT(_vm, OID_AUTO, max_launder,
155 CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
157 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
158 CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
160 SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
161 CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
163 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
164 CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
166 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max,
167 CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented");
169 #if defined(NO_SWAPPING)
170 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
171 CTLFLAG_RD, &vm_swap_enabled, 0, "");
172 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
173 CTLFLAG_RD, &vm_swap_idle_enabled, 0, "");
175 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
176 CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
177 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
178 CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
181 SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
182 CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
184 SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
185 CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
187 static int pageout_lock_miss;
188 SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
189 CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
192 SYSCTL_INT(_vm, OID_AUTO, vm_load,
193 CTLFLAG_RD, &vm_load, 0, "load on the VM system");
194 int vm_load_enable = 1;
195 SYSCTL_INT(_vm, OID_AUTO, vm_load_enable,
196 CTLFLAG_RW, &vm_load_enable, 0, "enable vm_load rate limiting");
199 SYSCTL_INT(_vm, OID_AUTO, vm_load_debug,
200 CTLFLAG_RW, &vm_load_debug, 0, "debug vm_load");
203 #define VM_PAGEOUT_PAGE_COUNT 16
204 int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
206 int vm_page_max_wired; /* XXX max # of wired pages system-wide */
208 #if !defined(NO_SWAPPING)
209 typedef void freeer_fcn_t (vm_map_t, vm_object_t, vm_pindex_t, int);
210 static void vm_pageout_map_deactivate_pages (vm_map_t, vm_pindex_t);
211 static freeer_fcn_t vm_pageout_object_deactivate_pages;
212 static void vm_req_vmdaemon (void);
214 static void vm_pageout_page_stats(void);
217 * Update vm_load to slow down faulting processes.
223 vm_fault_ratecheck(void)
225 if (vm_pages_needed) {
237 * Clean the page and remove it from the laundry. The page must not be
240 * We set the busy bit to cause potential page faults on this page to
241 * block. Note the careful timing, however, the busy bit isn't set till
242 * late and we cannot do anything that will mess with the page.
244 * The caller must hold vm_token.
247 vm_pageout_clean(vm_page_t m)
250 vm_page_t mc[2*vm_pageout_page_count];
252 int ib, is, page_base;
253 vm_pindex_t pindex = m->pindex;
258 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
259 * with the new swapper, but we could have serious problems paging
260 * out other object types if there is insufficient memory.
262 * Unfortunately, checking free memory here is far too late, so the
263 * check has been moved up a procedural level.
267 * Don't mess with the page if it's busy, held, or special
269 if ((m->hold_count != 0) ||
270 ((m->busy != 0) || (m->flags & (PG_BUSY|PG_UNMANAGED)))) {
274 mc[vm_pageout_page_count] = m;
276 page_base = vm_pageout_page_count;
281 * Scan object for clusterable pages.
283 * We can cluster ONLY if: ->> the page is NOT
284 * clean, wired, busy, held, or mapped into a
285 * buffer, and one of the following:
286 * 1) The page is inactive, or a seldom used
289 * 2) we force the issue.
291 * During heavy mmap/modification loads the pageout
292 * daemon can really fragment the underlying file
293 * due to flushing pages out of order and not trying
294 * align the clusters (which leave sporatic out-of-order
295 * holes). To solve this problem we do the reverse scan
296 * first and attempt to align our cluster, then do a
297 * forward scan if room remains.
301 while (ib && pageout_count < vm_pageout_page_count) {
309 if ((p = vm_page_lookup(object, pindex - ib)) == NULL) {
313 if (((p->queue - p->pc) == PQ_CACHE) ||
314 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
318 vm_page_test_dirty(p);
319 if ((p->dirty & p->valid) == 0 ||
320 p->queue != PQ_INACTIVE ||
321 p->wire_count != 0 || /* may be held by buf cache */
322 p->hold_count != 0) { /* may be undergoing I/O */
330 * alignment boundry, stop here and switch directions. Do
333 if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
337 while (pageout_count < vm_pageout_page_count &&
338 pindex + is < object->size) {
341 if ((p = vm_page_lookup(object, pindex + is)) == NULL)
343 if (((p->queue - p->pc) == PQ_CACHE) ||
344 (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) {
347 vm_page_test_dirty(p);
348 if ((p->dirty & p->valid) == 0 ||
349 p->queue != PQ_INACTIVE ||
350 p->wire_count != 0 || /* may be held by buf cache */
351 p->hold_count != 0) { /* may be undergoing I/O */
354 mc[page_base + pageout_count] = p;
360 * If we exhausted our forward scan, continue with the reverse scan
361 * when possible, even past a page boundry. This catches boundry
364 if (ib && pageout_count < vm_pageout_page_count)
368 * we allow reads during pageouts...
370 return vm_pageout_flush(&mc[page_base], pageout_count, 0);
374 * vm_pageout_flush() - launder the given pages
376 * The given pages are laundered. Note that we setup for the start of
377 * I/O ( i.e. busy the page ), mark it read-only, and bump the object
378 * reference count all in here rather then in the parent. If we want
379 * the parent to do more sophisticated things we may have to change
382 * The caller must hold vm_token.
385 vm_pageout_flush(vm_page_t *mc, int count, int flags)
388 int pageout_status[count];
392 ASSERT_LWKT_TOKEN_HELD(&vm_token);
395 * Initiate I/O. Bump the vm_page_t->busy counter.
397 for (i = 0; i < count; i++) {
398 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, ("vm_pageout_flush page %p index %d/%d: partially invalid page", mc[i], i, count));
399 vm_page_io_start(mc[i]);
403 * We must make the pages read-only. This will also force the
404 * modified bit in the related pmaps to be cleared. The pager
405 * cannot clear the bit for us since the I/O completion code
406 * typically runs from an interrupt. The act of making the page
407 * read-only handles the case for us.
409 for (i = 0; i < count; i++) {
410 vm_page_protect(mc[i], VM_PROT_READ);
413 object = mc[0]->object;
414 vm_object_pip_add(object, count);
416 vm_pager_put_pages(object, mc, count,
417 (flags | ((object == &kernel_object) ? VM_PAGER_PUT_SYNC : 0)),
420 for (i = 0; i < count; i++) {
421 vm_page_t mt = mc[i];
423 switch (pageout_status[i]) {
432 * Page outside of range of object. Right now we
433 * essentially lose the changes by pretending it
436 pmap_clear_modify(mt);
442 * A page typically cannot be paged out when we
443 * have run out of swap. We leave the page
444 * marked inactive and will try to page it out
447 * Starvation of the active page list is used to
448 * determine when the system is massively memory
457 * If the operation is still going, leave the page busy to
458 * block all other accesses. Also, leave the paging in
459 * progress indicator set so that we don't attempt an object
462 * For any pages which have completed synchronously,
463 * deactivate the page if we are under a severe deficit.
464 * Do not try to enter them into the cache, though, they
465 * might still be read-heavy.
467 if (pageout_status[i] != VM_PAGER_PEND) {
468 if (vm_page_count_severe())
469 vm_page_deactivate(mt);
471 if (!vm_page_count_severe() || !vm_page_try_to_cache(mt))
472 vm_page_protect(mt, VM_PROT_READ);
474 vm_page_io_finish(mt);
475 vm_object_pip_wakeup(object);
481 #if !defined(NO_SWAPPING)
483 * vm_pageout_object_deactivate_pages
485 * deactivate enough pages to satisfy the inactive target
486 * requirements or if vm_page_proc_limit is set, then
487 * deactivate all of the pages in the object and its
490 * The map must be locked.
491 * The caller must hold vm_token.
493 static int vm_pageout_object_deactivate_pages_callback(vm_page_t, void *);
496 vm_pageout_object_deactivate_pages(vm_map_t map, vm_object_t object,
497 vm_pindex_t desired, int map_remove_only)
499 struct rb_vm_page_scan_info info;
502 if (object->type == OBJT_DEVICE || object->type == OBJT_PHYS)
506 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
508 if (object->paging_in_progress)
511 remove_mode = map_remove_only;
512 if (object->shadow_count > 1)
516 * scan the objects entire memory queue. spl protection is
517 * required to avoid an interrupt unbusy/free race against
521 info.limit = remove_mode;
523 info.desired = desired;
524 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
525 vm_pageout_object_deactivate_pages_callback,
529 object = object->backing_object;
534 * The caller must hold vm_token.
537 vm_pageout_object_deactivate_pages_callback(vm_page_t p, void *data)
539 struct rb_vm_page_scan_info *info = data;
542 if (pmap_resident_count(vm_map_pmap(info->map)) <= info->desired) {
545 mycpu->gd_cnt.v_pdpages++;
546 if (p->wire_count != 0 || p->hold_count != 0 || p->busy != 0 ||
547 (p->flags & (PG_BUSY|PG_UNMANAGED)) ||
548 !pmap_page_exists_quick(vm_map_pmap(info->map), p)) {
552 actcount = pmap_ts_referenced(p);
554 vm_page_flag_set(p, PG_REFERENCED);
555 } else if (p->flags & PG_REFERENCED) {
559 if ((p->queue != PQ_ACTIVE) &&
560 (p->flags & PG_REFERENCED)) {
562 p->act_count += actcount;
563 vm_page_flag_clear(p, PG_REFERENCED);
564 } else if (p->queue == PQ_ACTIVE) {
565 if ((p->flags & PG_REFERENCED) == 0) {
566 p->act_count -= min(p->act_count, ACT_DECLINE);
567 if (!info->limit && (vm_pageout_algorithm || (p->act_count == 0))) {
569 vm_page_protect(p, VM_PROT_NONE);
570 vm_page_deactivate(p);
573 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
574 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
578 vm_page_flag_clear(p, PG_REFERENCED);
579 if (p->act_count < (ACT_MAX - ACT_ADVANCE))
580 p->act_count += ACT_ADVANCE;
581 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
582 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq);
584 } else if (p->queue == PQ_INACTIVE) {
586 vm_page_protect(p, VM_PROT_NONE);
593 * Deactivate some number of pages in a map, try to do it fairly, but
594 * that is really hard to do.
596 * The caller must hold vm_token.
599 vm_pageout_map_deactivate_pages(vm_map_t map, vm_pindex_t desired)
602 vm_object_t obj, bigobj;
605 if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT)) {
613 * first, search out the biggest object, and try to free pages from
616 tmpe = map->header.next;
617 while (tmpe != &map->header) {
618 switch(tmpe->maptype) {
619 case VM_MAPTYPE_NORMAL:
620 case VM_MAPTYPE_VPAGETABLE:
621 obj = tmpe->object.vm_object;
622 if ((obj != NULL) && (obj->shadow_count <= 1) &&
624 (bigobj->resident_page_count < obj->resident_page_count))) {
631 if (tmpe->wired_count > 0)
632 nothingwired = FALSE;
637 vm_pageout_object_deactivate_pages(map, bigobj, desired, 0);
640 * Next, hunt around for other pages to deactivate. We actually
641 * do this search sort of wrong -- .text first is not the best idea.
643 tmpe = map->header.next;
644 while (tmpe != &map->header) {
645 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
647 switch(tmpe->maptype) {
648 case VM_MAPTYPE_NORMAL:
649 case VM_MAPTYPE_VPAGETABLE:
650 obj = tmpe->object.vm_object;
652 vm_pageout_object_deactivate_pages(map, obj, desired, 0);
661 * Remove all mappings if a process is swapped out, this will free page
664 if (desired == 0 && nothingwired)
665 pmap_remove(vm_map_pmap(map),
666 VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
672 * Don't try to be fancy - being fancy can lead to vnode deadlocks. We
673 * only do it for OBJT_DEFAULT and OBJT_SWAP objects which we know can
674 * be trivially freed.
676 * The caller must hold vm_token.
678 * WARNING: vm_object_reference() can block.
681 vm_pageout_page_free(vm_page_t m)
683 vm_object_t object = m->object;
684 int type = object->type;
687 if (type == OBJT_SWAP || type == OBJT_DEFAULT)
688 vm_object_reference(object);
689 vm_page_protect(m, VM_PROT_NONE);
691 if (type == OBJT_SWAP || type == OBJT_DEFAULT)
692 vm_object_deallocate(object);
696 * vm_pageout_scan does the dirty work for the pageout daemon.
698 struct vm_pageout_scan_info {
699 struct proc *bigproc;
703 static int vm_pageout_scan_callback(struct proc *p, void *data);
706 * The caller must hold vm_token.
709 vm_pageout_scan(int pass)
711 struct vm_pageout_scan_info info;
713 struct vm_page marker;
714 struct vnode *vpfailed; /* warning, allowed to be stale */
717 int inactive_shortage, active_shortage;
718 int inactive_original_shortage;
721 int vnodes_skipped = 0;
725 * Do whatever cleanup that the pmap code can.
730 * Calculate our target for the number of free+cache pages we
731 * want to get to. This is higher then the number that causes
732 * allocations to stall (severe) in order to provide hysteresis,
733 * and if we don't make it all the way but get to the minimum
736 inactive_shortage = vm_paging_target() + vm_pageout_deficit;
737 inactive_original_shortage = inactive_shortage;
738 vm_pageout_deficit = 0;
741 * Initialize our marker
743 bzero(&marker, sizeof(marker));
744 marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
745 marker.queue = PQ_INACTIVE;
746 marker.wire_count = 1;
749 * Start scanning the inactive queue for pages we can move to the
750 * cache or free. The scan will stop when the target is reached or
751 * we have scanned the entire inactive queue. Note that m->act_count
752 * is not used to form decisions for the inactive queue, only for the
755 * maxlaunder limits the number of dirty pages we flush per scan.
756 * For most systems a smaller value (16 or 32) is more robust under
757 * extreme memory and disk pressure because any unnecessary writes
758 * to disk can result in extreme performance degredation. However,
759 * systems with excessive dirty pages (especially when MAP_NOSYNC is
760 * used) will die horribly with limited laundering. If the pageout
761 * daemon cannot clean enough pages in the first pass, we let it go
762 * all out in succeeding passes.
764 if ((maxlaunder = vm_max_launder) <= 1)
770 * We will generally be in a critical section throughout the
771 * scan, but we can release it temporarily when we are sitting on a
772 * non-busy page without fear. this is required to prevent an
773 * interrupt from unbusying or freeing a page prior to our busy
774 * check, leaving us on the wrong queue or checking the wrong
780 maxscan = vmstats.v_inactive_count;
781 for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
782 m != NULL && maxscan-- > 0 && inactive_shortage > 0;
785 mycpu->gd_cnt.v_pdpages++;
788 * Give interrupts a chance
794 * It's easier for some of the conditions below to just loop
795 * and catch queue changes here rather then check everywhere
798 if (m->queue != PQ_INACTIVE)
800 next = TAILQ_NEXT(m, pageq);
805 if (m->flags & PG_MARKER)
809 * A held page may be undergoing I/O, so skip it.
812 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
813 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
814 ++vm_swapcache_inactive_heuristic;
819 * Dont mess with busy pages, keep in the front of the
820 * queue, most likely are being paged out.
822 if (m->busy || (m->flags & PG_BUSY)) {
826 if (m->object->ref_count == 0) {
828 * If the object is not being used, we ignore previous
831 vm_page_flag_clear(m, PG_REFERENCED);
832 pmap_clear_reference(m);
834 } else if (((m->flags & PG_REFERENCED) == 0) &&
835 (actcount = pmap_ts_referenced(m))) {
837 * Otherwise, if the page has been referenced while
838 * in the inactive queue, we bump the "activation
839 * count" upwards, making it less likely that the
840 * page will be added back to the inactive queue
841 * prematurely again. Here we check the page tables
842 * (or emulated bits, if any), given the upper level
843 * VM system not knowing anything about existing
847 m->act_count += (actcount + ACT_ADVANCE);
852 * If the upper level VM system knows about any page
853 * references, we activate the page. We also set the
854 * "activation count" higher than normal so that we will less
855 * likely place pages back onto the inactive queue again.
857 if ((m->flags & PG_REFERENCED) != 0) {
858 vm_page_flag_clear(m, PG_REFERENCED);
859 actcount = pmap_ts_referenced(m);
861 m->act_count += (actcount + ACT_ADVANCE + 1);
866 * If the upper level VM system doesn't know anything about
867 * the page being dirty, we have to check for it again. As
868 * far as the VM code knows, any partially dirty pages are
871 * Pages marked PG_WRITEABLE may be mapped into the user
872 * address space of a process running on another cpu. A
873 * user process (without holding the MP lock) running on
874 * another cpu may be able to touch the page while we are
875 * trying to remove it. vm_page_cache() will handle this
879 vm_page_test_dirty(m);
886 * Invalid pages can be easily freed
888 vm_pageout_page_free(m);
889 mycpu->gd_cnt.v_dfree++;
891 } else if (m->dirty == 0) {
893 * Clean pages can be placed onto the cache queue.
894 * This effectively frees them.
899 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
901 * Dirty pages need to be paged out, but flushing
902 * a page is extremely expensive verses freeing
903 * a clean page. Rather then artificially limiting
904 * the number of pages we can flush, we instead give
905 * dirty pages extra priority on the inactive queue
906 * by forcing them to be cycled through the queue
907 * twice before being flushed, after which the
908 * (now clean) page will cycle through once more
909 * before being freed. This significantly extends
910 * the thrash point for a heavily loaded machine.
912 vm_page_flag_set(m, PG_WINATCFLS);
913 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
914 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
915 ++vm_swapcache_inactive_heuristic;
916 } else if (maxlaunder > 0) {
918 * We always want to try to flush some dirty pages if
919 * we encounter them, to keep the system stable.
920 * Normally this number is small, but under extreme
921 * pressure where there are insufficient clean pages
922 * on the inactive queue, we may have to go all out.
924 int swap_pageouts_ok;
925 struct vnode *vp = NULL;
929 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
930 swap_pageouts_ok = 1;
932 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
933 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
934 vm_page_count_min(0));
939 * We don't bother paging objects that are "dead".
940 * Those objects are in a "rundown" state.
942 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
943 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
944 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
945 ++vm_swapcache_inactive_heuristic;
950 * The object is already known NOT to be dead. It
951 * is possible for the vget() to block the whole
952 * pageout daemon, but the new low-memory handling
953 * code should prevent it.
955 * The previous code skipped locked vnodes and, worse,
956 * reordered pages in the queue. This results in
957 * completely non-deterministic operation because,
958 * quite often, a vm_fault has initiated an I/O and
959 * is holding a locked vnode at just the point where
960 * the pageout daemon is woken up.
962 * We can't wait forever for the vnode lock, we might
963 * deadlock due to a vn_read() getting stuck in
964 * vm_wait while holding this vnode. We skip the
965 * vnode if we can't get it in a reasonable amount
968 * vpfailed is used to (try to) avoid the case where
969 * a large number of pages are associated with a
970 * locked vnode, which could cause the pageout daemon
971 * to stall for an excessive amount of time.
973 if (object->type == OBJT_VNODE) {
977 flags = LK_EXCLUSIVE | LK_NOOBJ;
981 flags |= LK_TIMELOCK;
982 if (vget(vp, flags) != 0) {
985 if (object->flags & OBJ_MIGHTBEDIRTY)
991 * The page might have been moved to another
992 * queue during potential blocking in vget()
993 * above. The page might have been freed and
994 * reused for another vnode. The object might
995 * have been reused for another vnode.
997 if (m->queue != PQ_INACTIVE ||
998 m->object != object ||
999 object->handle != vp) {
1000 if (object->flags & OBJ_MIGHTBEDIRTY)
1007 * The page may have been busied during the
1008 * blocking in vput(); We don't move the
1009 * page back onto the end of the queue so that
1010 * statistics are more correct if we don't.
1012 if (m->busy || (m->flags & PG_BUSY)) {
1018 * If the page has become held it might
1019 * be undergoing I/O, so skip it
1021 if (m->hold_count) {
1022 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1023 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1024 ++vm_swapcache_inactive_heuristic;
1025 if (object->flags & OBJ_MIGHTBEDIRTY)
1033 * If a page is dirty, then it is either being washed
1034 * (but not yet cleaned) or it is still in the
1035 * laundry. If it is still in the laundry, then we
1036 * start the cleaning operation.
1038 * This operation may cluster, invalidating the 'next'
1039 * pointer. To prevent an inordinate number of
1040 * restarts we use our marker to remember our place.
1042 * decrement inactive_shortage on success to account
1043 * for the (future) cleaned page. Otherwise we
1044 * could wind up laundering or cleaning too many
1047 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m, &marker, pageq);
1048 if (vm_pageout_clean(m) != 0) {
1049 --inactive_shortage;
1052 next = TAILQ_NEXT(&marker, pageq);
1053 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq);
1060 * We want to move pages from the active queue to the inactive
1061 * queue to get the inactive queue to the inactive target. If
1062 * we still have a page shortage from above we try to directly free
1063 * clean pages instead of moving them.
1065 * If we do still have a shortage we keep track of the number of
1066 * pages we free or cache (recycle_count) as a measure of thrashing
1067 * between the active and inactive queues.
1069 * If we were able to completely satisfy the free+cache targets
1070 * from the inactive pool we limit the number of pages we move
1071 * from the active pool to the inactive pool to 2x the pages we
1072 * had removed from the inactive pool (with a minimum of 1/5 the
1073 * inactive target). If we were not able to completely satisfy
1074 * the free+cache targets we go for the whole target aggressively.
1076 * NOTE: Both variables can end up negative.
1077 * NOTE: We are still in a critical section.
1079 active_shortage = vmstats.v_inactive_target - vmstats.v_inactive_count;
1080 if (inactive_original_shortage < vmstats.v_inactive_target / 10)
1081 inactive_original_shortage = vmstats.v_inactive_target / 10;
1082 if (inactive_shortage <= 0 &&
1083 active_shortage > inactive_original_shortage * 2) {
1084 active_shortage = inactive_original_shortage * 2;
1087 pcount = vmstats.v_active_count;
1089 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1091 while ((m != NULL) && (pcount-- > 0) &&
1092 (inactive_shortage > 0 || active_shortage > 0)
1095 * Give interrupts a chance.
1101 * If the page was ripped out from under us, just stop.
1103 if (m->queue != PQ_ACTIVE)
1105 next = TAILQ_NEXT(m, pageq);
1108 * Don't deactivate pages that are busy.
1110 if ((m->busy != 0) ||
1111 (m->flags & PG_BUSY) ||
1112 (m->hold_count != 0)) {
1113 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1114 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1120 * The count for pagedaemon pages is done after checking the
1121 * page for eligibility...
1123 mycpu->gd_cnt.v_pdpages++;
1126 * Check to see "how much" the page has been used and clear
1127 * the tracking access bits. If the object has no references
1128 * don't bother paying the expense.
1131 if (m->object->ref_count != 0) {
1132 if (m->flags & PG_REFERENCED)
1134 actcount += pmap_ts_referenced(m);
1136 m->act_count += ACT_ADVANCE + actcount;
1137 if (m->act_count > ACT_MAX)
1138 m->act_count = ACT_MAX;
1141 vm_page_flag_clear(m, PG_REFERENCED);
1144 * actcount is only valid if the object ref_count is non-zero.
1146 if (actcount && m->object->ref_count != 0) {
1147 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1148 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1150 m->act_count -= min(m->act_count, ACT_DECLINE);
1151 if (vm_pageout_algorithm ||
1152 m->object->ref_count == 0 ||
1153 m->act_count < pass + 1
1156 * Deactivate the page. If we had a
1157 * shortage from our inactive scan try to
1158 * free (cache) the page instead.
1160 * Don't just blindly cache the page if
1161 * we do not have a shortage from the
1162 * inactive scan, that could lead to
1163 * gigabytes being moved.
1166 if (inactive_shortage > 0 ||
1167 m->object->ref_count == 0) {
1168 if (inactive_shortage > 0)
1171 vm_page_protect(m, VM_PROT_NONE);
1172 if (m->dirty == 0 &&
1173 inactive_shortage > 0) {
1174 --inactive_shortage;
1177 vm_page_deactivate(m);
1181 vm_page_deactivate(m);
1184 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1185 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1192 * The number of actually free pages can drop down to v_free_reserved,
1193 * we try to build the free count back above v_free_min. Note that
1194 * vm_paging_needed() also returns TRUE if v_free_count is not at
1195 * least v_free_min so that is the minimum we must build the free
1198 * We use a slightly higher target to improve hysteresis,
1199 * ((v_free_target + v_free_min) / 2). Since v_free_target
1200 * is usually the same as v_cache_min this maintains about
1201 * half the pages in the free queue as are in the cache queue,
1202 * providing pretty good pipelining for pageout operation.
1204 * The system operator can manipulate vm.v_cache_min and
1205 * vm.v_free_target to tune the pageout demon. Be sure
1206 * to keep vm.v_free_min < vm.v_free_target.
1208 * Note that the original paging target is to get at least
1209 * (free_min + cache_min) into (free + cache). The slightly
1210 * higher target will shift additional pages from cache to free
1211 * without effecting the original paging target in order to
1212 * maintain better hysteresis and not have the free count always
1213 * be dead-on v_free_min.
1215 * NOTE: we are still in a critical section.
1217 * Pages moved from PQ_CACHE to totally free are not counted in the
1218 * pages_freed counter.
1220 while (vmstats.v_free_count <
1221 (vmstats.v_free_min + vmstats.v_free_target) / 2) {
1225 static int cache_rover = 0;
1226 m = vm_page_list_find(PQ_CACHE, cache_rover, FALSE);
1229 if ((m->flags & (PG_BUSY|PG_UNMANAGED)) ||
1234 kprintf("Warning: busy page %p found in cache\n", m);
1236 vm_page_deactivate(m);
1239 KKASSERT((m->flags & PG_MAPPED) == 0);
1240 KKASSERT(m->dirty == 0);
1241 cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK;
1242 vm_pageout_page_free(m);
1243 mycpu->gd_cnt.v_dfree++;
1248 #if !defined(NO_SWAPPING)
1250 * Idle process swapout -- run once per second.
1252 if (vm_swap_idle_enabled) {
1254 if (time_second != lsec) {
1255 vm_pageout_req_swapout |= VM_SWAP_IDLE;
1263 * If we didn't get enough free pages, and we have skipped a vnode
1264 * in a writeable object, wakeup the sync daemon. And kick swapout
1265 * if we did not get enough free pages.
1267 if (vm_paging_target() > 0) {
1268 if (vnodes_skipped && vm_page_count_min(0))
1270 #if !defined(NO_SWAPPING)
1271 if (vm_swap_enabled && vm_page_count_target()) {
1273 vm_pageout_req_swapout |= VM_SWAP_NORMAL;
1279 * Handle catastrophic conditions. Under good conditions we should
1280 * be at the target, well beyond our minimum. If we could not even
1281 * reach our minimum the system is under heavy stress.
1283 * Determine whether we have run out of memory. This occurs when
1284 * swap_pager_full is TRUE and the only pages left in the page
1285 * queues are dirty. We will still likely have page shortages.
1287 * - swap_pager_full is set if insufficient swap was
1288 * available to satisfy a requested pageout.
1290 * - the inactive queue is bloated (4 x size of active queue),
1291 * meaning it is unable to get rid of dirty pages and.
1293 * - vm_page_count_min() without counting pages recycled from the
1294 * active queue (recycle_count) means we could not recover
1295 * enough pages to meet bare minimum needs. This test only
1296 * works if the inactive queue is bloated.
1298 * - due to a positive inactive_shortage we shifted the remaining
1299 * dirty pages from the active queue to the inactive queue
1300 * trying to find clean ones to free.
1302 if (swap_pager_full && vm_page_count_min(recycle_count))
1303 kprintf("Warning: system low on memory+swap!\n");
1304 if (swap_pager_full && vm_page_count_min(recycle_count) &&
1305 vmstats.v_inactive_count > vmstats.v_active_count * 4 &&
1306 inactive_shortage > 0) {
1310 info.bigproc = NULL;
1312 allproc_scan(vm_pageout_scan_callback, &info);
1313 if (info.bigproc != NULL) {
1314 killproc(info.bigproc, "out of swap space");
1315 info.bigproc->p_nice = PRIO_MIN;
1316 info.bigproc->p_usched->resetpriority(
1317 FIRST_LWP_IN_PROC(info.bigproc));
1318 wakeup(&vmstats.v_free_count);
1319 PRELE(info.bigproc);
1322 return(inactive_shortage);
1326 * The caller must hold vm_token and proc_token.
1329 vm_pageout_scan_callback(struct proc *p, void *data)
1331 struct vm_pageout_scan_info *info = data;
1335 * Never kill system processes or init. If we have configured swap
1336 * then try to avoid killing low-numbered pids.
1338 if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) ||
1339 ((p->p_pid < 48) && (vm_swap_size != 0))) {
1344 * if the process is in a non-running type state,
1347 if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
1351 * Get the approximate process size. Note that anonymous pages
1352 * with backing swap will be counted twice, but there should not
1353 * be too many such pages due to the stress the VM system is
1354 * under at this point.
1356 size = vmspace_anonymous_count(p->p_vmspace) +
1357 vmspace_swap_count(p->p_vmspace);
1360 * If the this process is bigger than the biggest one
1363 if (info->bigsize < size) {
1365 PRELE(info->bigproc);
1368 info->bigsize = size;
1374 * This routine tries to maintain the pseudo LRU active queue,
1375 * so that during long periods of time where there is no paging,
1376 * that some statistic accumulation still occurs. This code
1377 * helps the situation where paging just starts to occur.
1379 * The caller must hold vm_token.
1382 vm_pageout_page_stats(void)
1385 int pcount,tpcount; /* Number of pages to check */
1386 static int fullintervalcount = 0;
1390 (vmstats.v_inactive_target + vmstats.v_cache_max + vmstats.v_free_min) -
1391 (vmstats.v_free_count + vmstats.v_inactive_count + vmstats.v_cache_count);
1393 if (page_shortage <= 0)
1398 pcount = vmstats.v_active_count;
1399 fullintervalcount += vm_pageout_stats_interval;
1400 if (fullintervalcount < vm_pageout_full_stats_interval) {
1401 tpcount = (vm_pageout_stats_max * vmstats.v_active_count) / vmstats.v_page_count;
1402 if (pcount > tpcount)
1405 fullintervalcount = 0;
1408 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1409 while ((m != NULL) && (pcount-- > 0)) {
1412 if (m->queue != PQ_ACTIVE) {
1416 next = TAILQ_NEXT(m, pageq);
1418 * Don't deactivate pages that are busy.
1420 if ((m->busy != 0) ||
1421 (m->flags & PG_BUSY) ||
1422 (m->hold_count != 0)) {
1423 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1424 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1430 if (m->flags & PG_REFERENCED) {
1431 vm_page_flag_clear(m, PG_REFERENCED);
1435 actcount += pmap_ts_referenced(m);
1437 m->act_count += ACT_ADVANCE + actcount;
1438 if (m->act_count > ACT_MAX)
1439 m->act_count = ACT_MAX;
1440 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1441 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1443 if (m->act_count == 0) {
1445 * We turn off page access, so that we have
1446 * more accurate RSS stats. We don't do this
1447 * in the normal page deactivation when the
1448 * system is loaded VM wise, because the
1449 * cost of the large number of page protect
1450 * operations would be higher than the value
1451 * of doing the operation.
1454 vm_page_protect(m, VM_PROT_NONE);
1455 vm_page_deactivate(m);
1458 m->act_count -= min(m->act_count, ACT_DECLINE);
1459 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1460 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq);
1470 * The caller must hold vm_token.
1473 vm_pageout_free_page_calc(vm_size_t count)
1475 if (count < vmstats.v_page_count)
1478 * free_reserved needs to include enough for the largest swap pager
1479 * structures plus enough for any pv_entry structs when paging.
1481 if (vmstats.v_page_count > 1024)
1482 vmstats.v_free_min = 4 + (vmstats.v_page_count - 1024) / 200;
1484 vmstats.v_free_min = 4;
1485 vmstats.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
1486 vmstats.v_interrupt_free_min;
1487 vmstats.v_free_reserved = vm_pageout_page_count +
1488 vmstats.v_pageout_free_min + (count / 768) + PQ_L2_SIZE;
1489 vmstats.v_free_severe = vmstats.v_free_min / 2;
1490 vmstats.v_free_min += vmstats.v_free_reserved;
1491 vmstats.v_free_severe += vmstats.v_free_reserved;
1497 * vm_pageout is the high level pageout daemon.
1502 vm_pageout_thread(void)
1505 int inactive_shortage;
1508 * Permanently hold vm_token.
1510 lwkt_gettoken(&vm_token);
1513 * Initialize some paging parameters.
1515 curthread->td_flags |= TDF_SYSTHREAD;
1517 vmstats.v_interrupt_free_min = 2;
1518 if (vmstats.v_page_count < 2000)
1519 vm_pageout_page_count = 8;
1521 vm_pageout_free_page_calc(vmstats.v_page_count);
1524 * v_free_target and v_cache_min control pageout hysteresis. Note
1525 * that these are more a measure of the VM cache queue hysteresis
1526 * then the VM free queue. Specifically, v_free_target is the
1527 * high water mark (free+cache pages).
1529 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
1530 * low water mark, while v_free_min is the stop. v_cache_min must
1531 * be big enough to handle memory needs while the pageout daemon
1532 * is signalled and run to free more pages.
1534 if (vmstats.v_free_count > 6144)
1535 vmstats.v_free_target = 4 * vmstats.v_free_min + vmstats.v_free_reserved;
1537 vmstats.v_free_target = 2 * vmstats.v_free_min + vmstats.v_free_reserved;
1540 * NOTE: With the new buffer cache b_act_count we want the default
1541 * inactive target to be a percentage of available memory.
1543 * The inactive target essentially determines the minimum
1544 * number of 'temporary' pages capable of caching one-time-use
1545 * files when the VM system is otherwise full of pages
1546 * belonging to multi-time-use files or active program data.
1548 * NOTE: The inactive target is aggressively persued only if the
1549 * inactive queue becomes too small. If the inactive queue
1550 * is large enough to satisfy page movement to free+cache
1551 * then it is repopulated more slowly from the active queue.
1552 * This allows a general inactive_target default to be set.
1554 * There is an issue here for processes which sit mostly idle
1555 * 'overnight', such as sshd, tcsh, and X. Any movement from
1556 * the active queue will eventually cause such pages to
1557 * recycle eventually causing a lot of paging in the morning.
1558 * To reduce the incidence of this pages cycled out of the
1559 * buffer cache are moved directly to the inactive queue if
1560 * they were only used once or twice.
1562 * The vfs.vm_cycle_point sysctl can be used to adjust this.
1563 * Increasing the value (up to 64) increases the number of
1564 * buffer recyclements which go directly to the inactive queue.
1566 if (vmstats.v_free_count > 2048) {
1567 vmstats.v_cache_min = vmstats.v_free_target;
1568 vmstats.v_cache_max = 2 * vmstats.v_cache_min;
1570 vmstats.v_cache_min = 0;
1571 vmstats.v_cache_max = 0;
1573 vmstats.v_inactive_target = vmstats.v_free_count / 4;
1575 /* XXX does not really belong here */
1576 if (vm_page_max_wired == 0)
1577 vm_page_max_wired = vmstats.v_free_count / 3;
1579 if (vm_pageout_stats_max == 0)
1580 vm_pageout_stats_max = vmstats.v_free_target;
1583 * Set interval in seconds for stats scan.
1585 if (vm_pageout_stats_interval == 0)
1586 vm_pageout_stats_interval = 5;
1587 if (vm_pageout_full_stats_interval == 0)
1588 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1592 * Set maximum free per pass
1594 if (vm_pageout_stats_free_max == 0)
1595 vm_pageout_stats_free_max = 5;
1597 swap_pager_swap_init();
1601 * The pageout daemon is never done, so loop forever.
1607 * Wait for an action request. If we timeout check to
1608 * see if paging is needed (in case the normal wakeup
1611 if (vm_pages_needed == 0) {
1612 error = tsleep(&vm_pages_needed,
1614 vm_pageout_stats_interval * hz);
1616 vm_paging_needed() == 0 &&
1617 vm_pages_needed == 0) {
1618 vm_pageout_page_stats();
1621 vm_pages_needed = 1;
1624 mycpu->gd_cnt.v_pdwakeups++;
1627 * Scan for pageout. Try to avoid thrashing the system
1630 inactive_shortage = vm_pageout_scan(pass);
1631 if (inactive_shortage > 0) {
1633 if (swap_pager_full) {
1635 * Running out of memory, catastrophic back-off
1636 * to one-second intervals.
1638 tsleep(&vm_pages_needed, 0, "pdelay", hz);
1639 } else if (pass < 10 && vm_pages_needed > 1) {
1641 * Normal operation, additional processes
1642 * have already kicked us. Retry immediately.
1644 } else if (pass < 10) {
1646 * Normal operation, fewer processes. Delay
1647 * a bit but allow wakeups.
1649 vm_pages_needed = 0;
1650 tsleep(&vm_pages_needed, 0, "pdelay", hz / 10);
1651 vm_pages_needed = 1;
1654 * We've taken too many passes, forced delay.
1656 tsleep(&vm_pages_needed, 0, "pdelay", hz / 10);
1660 * Interlocked wakeup of waiters (non-optional)
1663 if (vm_pages_needed && !vm_page_count_min(0)) {
1664 wakeup(&vmstats.v_free_count);
1665 vm_pages_needed = 0;
1671 static struct kproc_desc page_kp = {
1676 SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp)
1680 * Called after allocating a page out of the cache or free queue
1681 * to possibly wake the pagedaemon up to replentish our supply.
1683 * We try to generate some hysteresis by waking the pagedaemon up
1684 * when our free+cache pages go below the free_min+cache_min level.
1685 * The pagedaemon tries to get the count back up to at least the
1686 * minimum, and through to the target level if possible.
1688 * If the pagedaemon is already active bump vm_pages_needed as a hint
1689 * that there are even more requests pending.
1695 pagedaemon_wakeup(void)
1697 if (vm_paging_needed() && curthread != pagethread) {
1698 if (vm_pages_needed == 0) {
1699 vm_pages_needed = 1; /* SMP race ok */
1700 wakeup(&vm_pages_needed);
1701 } else if (vm_page_count_min(0)) {
1702 ++vm_pages_needed; /* SMP race ok */
1707 #if !defined(NO_SWAPPING)
1714 vm_req_vmdaemon(void)
1716 static int lastrun = 0;
1718 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
1719 wakeup(&vm_daemon_needed);
1724 static int vm_daemon_callback(struct proc *p, void *data __unused);
1733 * Permanently hold vm_token.
1735 lwkt_gettoken(&vm_token);
1738 tsleep(&vm_daemon_needed, 0, "psleep", 0);
1739 if (vm_pageout_req_swapout) {
1740 swapout_procs(vm_pageout_req_swapout);
1741 vm_pageout_req_swapout = 0;
1744 * scan the processes for exceeding their rlimits or if
1745 * process is swapped out -- deactivate pages
1747 allproc_scan(vm_daemon_callback, NULL);
1752 * Caller must hold vm_token and proc_token.
1755 vm_daemon_callback(struct proc *p, void *data __unused)
1757 vm_pindex_t limit, size;
1760 * if this is a system process or if we have already
1761 * looked at this process, skip it.
1763 if (p->p_flag & (P_SYSTEM | P_WEXIT))
1767 * if the process is in a non-running type state,
1770 if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
1776 limit = OFF_TO_IDX(qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
1777 p->p_rlimit[RLIMIT_RSS].rlim_max));
1780 * let processes that are swapped out really be
1781 * swapped out. Set the limit to nothing to get as
1782 * many pages out to swap as possible.
1784 if (p->p_flag & P_SWAPPEDOUT)
1787 size = vmspace_resident_count(p->p_vmspace);
1788 if (limit >= 0 && size >= limit) {
1789 vm_pageout_map_deactivate_pages(
1790 &p->p_vmspace->vm_map, limit);