2 * Copyright (c) 2003-2020 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1991 Regents of the University of California.
35 * All rights reserved.
36 * Copyright (c) 1994 John S. Dyson
37 * All rights reserved.
38 * Copyright (c) 1994 David Greenman
39 * All rights reserved.
41 * This code is derived from software contributed to Berkeley by
42 * The Mach Operating System project at Carnegie-Mellon University.
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91
71 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
72 * All rights reserved.
74 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
76 * Permission to use, copy, modify and distribute this software and
77 * its documentation is hereby granted, provided that both the copyright
78 * notice and this permission notice appear in all copies of the
79 * software, derivative works or modified versions, and any portions
80 * thereof, and that both notices appear in supporting documentation.
82 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
83 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
84 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
86 * Carnegie Mellon requests users of this software to return to
88 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
89 * School of Computer Science
90 * Carnegie Mellon University
91 * Pittsburgh PA 15213-3890
93 * any improvements or extensions that they make and grant Carnegie the
94 * rights to redistribute these changes.
98 * The proverbial page-out daemon, rewritten many times over the decades.
102 #include <sys/param.h>
103 #include <sys/systm.h>
104 #include <sys/kernel.h>
105 #include <sys/proc.h>
106 #include <sys/kthread.h>
107 #include <sys/resourcevar.h>
108 #include <sys/signalvar.h>
109 #include <sys/vnode.h>
110 #include <sys/vmmeter.h>
111 #include <sys/conf.h>
112 #include <sys/sysctl.h>
115 #include <vm/vm_param.h>
116 #include <sys/lock.h>
117 #include <vm/vm_object.h>
118 #include <vm/vm_page.h>
119 #include <vm/vm_map.h>
120 #include <vm/vm_pageout.h>
121 #include <vm/vm_pager.h>
122 #include <vm/swap_pager.h>
123 #include <vm/vm_extern.h>
125 #include <sys/spinlock2.h>
126 #include <vm/vm_page2.h>
129 * System initialization
132 /* the kernel process "vm_pageout"*/
133 static int vm_pageout_page(vm_page_t m, long *max_launderp,
134 long *vnodes_skippedp, struct vnode **vpfailedp,
135 int pass, int vmflush_flags);
136 static int vm_pageout_clean_helper (vm_page_t, int);
137 static void vm_pageout_free_page_calc (vm_size_t count);
138 static void vm_pageout_page_free(vm_page_t m) ;
139 struct thread *emergpager;
140 struct thread *pagethread;
141 static int sequence_emerg_pager;
143 #if !defined(NO_SWAPPING)
144 /* the kernel process "vm_daemon"*/
145 static void vm_daemon (void);
146 static struct thread *vmthread;
148 static struct kproc_desc vm_kp = {
153 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp);
156 int vm_pages_needed = 0; /* Event on which pageout daemon sleeps */
157 int vm_pageout_deficit = 0; /* Estimated number of pages deficit */
158 int vm_pageout_pages_needed = 0;/* pageout daemon needs pages */
159 int vm_page_free_hysteresis = 16;
160 static int vm_pagedaemon_time;
162 #if !defined(NO_SWAPPING)
163 static int vm_pageout_req_swapout;
164 static int vm_daemon_needed;
166 __read_mostly static int vm_max_launder = 4096;
167 __read_mostly static int vm_emerg_launder = 100;
168 __read_mostly static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
169 __read_mostly static int vm_pageout_full_stats_interval = 0;
170 __read_mostly static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0;
171 __read_mostly static int defer_swap_pageouts=0;
172 __read_mostly static int disable_swap_pageouts=0;
173 __read_mostly static u_int vm_anonmem_decline = ACT_DECLINE;
174 __read_mostly static u_int vm_filemem_decline = ACT_DECLINE * 2;
175 __read_mostly static int vm_pageout_debug;
177 #if defined(NO_SWAPPING)
178 __read_mostly static int vm_swap_enabled=0;
179 __read_mostly static int vm_swap_idle_enabled=0;
181 __read_mostly static int vm_swap_enabled=1;
182 __read_mostly static int vm_swap_idle_enabled=0;
185 /* 0-disable, 1-passive, 2-active swp*/
186 __read_mostly int vm_pageout_memuse_mode=1;
188 SYSCTL_UINT(_vm, VM_PAGEOUT_ALGORITHM, anonmem_decline,
189 CTLFLAG_RW, &vm_anonmem_decline, 0, "active->inactive anon memory");
191 SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, filemem_decline,
192 CTLFLAG_RW, &vm_filemem_decline, 0, "active->inactive file cache");
194 SYSCTL_INT(_vm, OID_AUTO, page_free_hysteresis,
195 CTLFLAG_RW, &vm_page_free_hysteresis, 0,
196 "Free more pages than the minimum required");
198 SYSCTL_INT(_vm, OID_AUTO, max_launder,
199 CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
200 SYSCTL_INT(_vm, OID_AUTO, emerg_launder,
201 CTLFLAG_RW, &vm_emerg_launder, 0, "Emergency pager minimum");
203 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
204 CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
206 SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
207 CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
209 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
210 CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
212 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max,
213 CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented");
214 SYSCTL_INT(_vm, OID_AUTO, pageout_memuse_mode,
215 CTLFLAG_RW, &vm_pageout_memuse_mode, 0, "memoryuse resource mode");
216 SYSCTL_INT(_vm, OID_AUTO, pageout_debug,
217 CTLFLAG_RW, &vm_pageout_debug, 0, "debug pageout pages (count)");
220 #if defined(NO_SWAPPING)
221 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
222 CTLFLAG_RD, &vm_swap_enabled, 0, "");
223 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
224 CTLFLAG_RD, &vm_swap_idle_enabled, 0, "");
226 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
227 CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
228 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
229 CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
232 SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
233 CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
235 SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
236 CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
238 static int pageout_lock_miss;
239 SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
240 CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
242 int vm_page_max_wired; /* XXX max # of wired pages system-wide */
244 #if !defined(NO_SWAPPING)
245 static void vm_req_vmdaemon (void);
247 static void vm_pageout_page_stats(int q);
250 * Calculate approximately how many pages on each queue to try to
251 * clean. An exact calculation creates an edge condition when the
252 * queues are unbalanced so add significant slop. The queue scans
253 * will stop early when targets are reached and will start where they
254 * left off on the next pass.
256 * We need to be generous here because there are all sorts of loading
257 * conditions that can cause edge cases if try to average over all queues.
258 * In particular, storage subsystems have become so fast that paging
259 * activity can become quite frantic. Eventually we will probably need
260 * two paging threads, one for dirty pages and one for clean, to deal
261 * with the bandwidth requirements.
263 * So what we do is calculate a value that can be satisfied nominally by
264 * only having to scan half the queues.
272 avg = ((n + (PQ_L2_SIZE - 1)) / (PQ_L2_SIZE / 2) + 1);
274 avg = ((n - (PQ_L2_SIZE - 1)) / (PQ_L2_SIZE / 2) - 1);
280 * vm_pageout_clean_helper:
282 * Clean the page and remove it from the laundry. The page must be busied
283 * by the caller and will be disposed of (put away, flushed) by this routine.
286 vm_pageout_clean_helper(vm_page_t m, int vmflush_flags)
289 vm_page_t mc[BLIST_MAX_ALLOC];
291 int ib, is, page_base;
292 vm_pindex_t pindex = m->pindex;
297 * Don't mess with the page if it's held or special. Theoretically
298 * we can pageout held pages but there is no real need to press our
301 if (m->hold_count != 0 || (m->flags & PG_UNQUEUED)) {
307 * Place page in cluster. Align cluster for optimal swap space
308 * allocation (whether it is swap or not). This is typically ~16-32
309 * pages, which also tends to align the cluster to multiples of the
310 * filesystem block size if backed by a filesystem.
312 page_base = pindex % BLIST_MAX_ALLOC;
318 * Scan object for clusterable pages.
320 * We can cluster ONLY if: ->> the page is NOT
321 * clean, wired, busy, held, or mapped into a
322 * buffer, and one of the following:
323 * 1) The page is inactive, or a seldom used
326 * 2) we force the issue.
328 * During heavy mmap/modification loads the pageout
329 * daemon can really fragment the underlying file
330 * due to flushing pages out of order and not trying
331 * align the clusters (which leave sporatic out-of-order
332 * holes). To solve this problem we do the reverse scan
333 * first and attempt to align our cluster, then do a
334 * forward scan if room remains.
336 vm_object_hold(object);
341 p = vm_page_lookup_busy_try(object, pindex - page_base + ib,
343 if (error || p == NULL)
345 if ((p->queue - p->pc) == PQ_CACHE ||
346 (p->flags & PG_UNQUEUED)) {
350 vm_page_test_dirty(p);
351 if (((p->dirty & p->valid) == 0 &&
352 (p->flags & PG_NEED_COMMIT) == 0) ||
353 p->wire_count != 0 || /* may be held by buf cache */
354 p->hold_count != 0) { /* may be undergoing I/O */
358 if (p->queue - p->pc != PQ_INACTIVE) {
359 if (p->queue - p->pc != PQ_ACTIVE ||
360 (vmflush_flags & VM_PAGER_ALLOW_ACTIVE) == 0) {
367 * Try to maintain page groupings in the cluster.
369 if (m->flags & PG_WINATCFLS)
370 vm_page_flag_set(p, PG_WINATCFLS);
372 vm_page_flag_clear(p, PG_WINATCFLS);
373 p->act_count = m->act_count;
380 while (is < BLIST_MAX_ALLOC &&
381 pindex - page_base + is < object->size) {
384 p = vm_page_lookup_busy_try(object, pindex - page_base + is,
386 if (error || p == NULL)
388 if (((p->queue - p->pc) == PQ_CACHE) ||
389 (p->flags & PG_UNQUEUED)) {
393 vm_page_test_dirty(p);
394 if (((p->dirty & p->valid) == 0 &&
395 (p->flags & PG_NEED_COMMIT) == 0) ||
396 p->wire_count != 0 || /* may be held by buf cache */
397 p->hold_count != 0) { /* may be undergoing I/O */
401 if (p->queue - p->pc != PQ_INACTIVE) {
402 if (p->queue - p->pc != PQ_ACTIVE ||
403 (vmflush_flags & VM_PAGER_ALLOW_ACTIVE) == 0) {
410 * Try to maintain page groupings in the cluster.
412 if (m->flags & PG_WINATCFLS)
413 vm_page_flag_set(p, PG_WINATCFLS);
415 vm_page_flag_clear(p, PG_WINATCFLS);
416 p->act_count = m->act_count;
422 vm_object_drop(object);
425 * we allow reads during pageouts...
427 return vm_pageout_flush(&mc[ib], is - ib, vmflush_flags);
431 * vm_pageout_flush() - launder the given pages
433 * The given pages are laundered. Note that we setup for the start of
434 * I/O ( i.e. busy the page ), mark it read-only, and bump the object
435 * reference count all in here rather then in the parent. If we want
436 * the parent to do more sophisticated things we may have to change
439 * The pages in the array must be busied by the caller and will be
440 * unbusied by this function.
443 vm_pageout_flush(vm_page_t *mc, int count, int vmflush_flags)
446 int pageout_status[count];
451 if (vm_pageout_debug > 0) {
459 * Initiate I/O. Bump the vm_page_t->busy counter.
461 for (i = 0; i < count; i++) {
462 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL,
463 ("vm_pageout_flush page %p index %d/%d: partially "
464 "invalid page", mc[i], i, count));
465 vm_page_io_start(mc[i]);
469 * We must make the pages read-only. This will also force the
470 * modified bit in the related pmaps to be cleared. The pager
471 * cannot clear the bit for us since the I/O completion code
472 * typically runs from an interrupt. The act of making the page
473 * read-only handles the case for us.
475 * Then we can unbusy the pages, we still hold a reference by virtue
479 kprintf("pageout: ");
480 for (i = 0; i < count; i++) {
481 if (vmflush_flags & VM_PAGER_TRY_TO_CACHE)
482 vm_page_protect(mc[i], VM_PROT_NONE);
484 vm_page_protect(mc[i], VM_PROT_READ);
485 vm_page_wakeup(mc[i]);
487 kprintf(" %p", mc[i]);
492 object = mc[0]->object;
493 vm_object_pip_add(object, count);
495 vm_pager_put_pages(object, mc, count,
497 ((object == &kernel_object) ?
498 VM_PAGER_PUT_SYNC : 0)),
503 for (i = 0; i < count; i++) {
504 vm_page_t mt = mc[i];
507 kprintf(" S%d", pageout_status[i]);
509 switch (pageout_status[i]) {
518 * Page outside of range of object. Right now we
519 * essentially lose the changes by pretending it
522 vm_page_busy_wait(mt, FALSE, "pgbad");
523 pmap_clear_modify(mt);
530 * A page typically cannot be paged out when we
531 * have run out of swap. We leave the page
532 * marked inactive and will try to page it out
535 * Starvation of the active page list is used to
536 * determine when the system is massively memory
545 * If not PENDing this was a synchronous operation and we
546 * clean up after the I/O. If it is PENDing the mess is
547 * cleaned up asynchronously.
549 * Also nominally act on the caller's wishes if the caller
550 * wants to try to really clean (cache or free) the page.
552 * Also nominally deactivate the page if the system is
555 if (pageout_status[i] != VM_PAGER_PEND) {
556 vm_page_busy_wait(mt, FALSE, "pgouw");
557 vm_page_io_finish(mt);
558 if (vmflush_flags & VM_PAGER_TRY_TO_CACHE) {
559 vm_page_try_to_cache(mt);
561 kprintf("A[pq_cache=%d]",
562 ((mt->queue - mt->pc) == PQ_CACHE));
563 } else if (vm_page_count_severe()) {
564 vm_page_deactivate(mt);
573 vm_object_pip_wakeup(object);
581 #if !defined(NO_SWAPPING)
584 * Callback function, page busied for us. We must dispose of the busy
585 * condition. Any related pmap pages may be held but will not be locked.
589 vm_pageout_mdp_callback(struct pmap_pgscan_info *info, vm_offset_t va,
596 * Basic tests - There should never be a marker, and we can stop
597 * once the RSS is below the required level.
599 KKASSERT((p->flags & PG_MARKER) == 0);
600 if (pmap_resident_tlnw_count(info->pmap) <= info->limit) {
605 mycpu->gd_cnt.v_pdpages++;
607 if (p->wire_count || p->hold_count || (p->flags & PG_UNQUEUED)) {
615 * Check if the page has been referened recently. If it has,
616 * activate it and skip.
618 actcount = pmap_ts_referenced(p);
620 vm_page_flag_set(p, PG_REFERENCED);
621 } else if (p->flags & PG_REFERENCED) {
626 if (p->queue - p->pc != PQ_ACTIVE) {
627 vm_page_and_queue_spin_lock(p);
628 if (p->queue - p->pc != PQ_ACTIVE) {
629 vm_page_and_queue_spin_unlock(p);
632 vm_page_and_queue_spin_unlock(p);
635 p->act_count += actcount;
636 if (p->act_count > ACT_MAX)
637 p->act_count = ACT_MAX;
639 vm_page_flag_clear(p, PG_REFERENCED);
645 * Remove the page from this particular pmap. Once we do this, our
646 * pmap scans will not see it again (unless it gets faulted in), so
647 * we must actively dispose of or deal with the page.
649 pmap_remove_specific(info->pmap, p);
652 * If the page is not mapped to another process (i.e. as would be
653 * typical if this were a shared page from a library) then deactivate
654 * the page and clean it in two passes only.
656 * If the page hasn't been referenced since the last check, remove it
657 * from the pmap. If it is no longer mapped, deactivate it
658 * immediately, accelerating the normal decline.
660 * Once the page has been removed from the pmap the RSS code no
661 * longer tracks it so we have to make sure that it is staged for
662 * potential flush action.
664 if ((p->flags & PG_MAPPED) == 0 ||
665 (pmap_mapped_sync(p) & PG_MAPPED) == 0) {
666 if (p->queue - p->pc == PQ_ACTIVE) {
667 vm_page_deactivate(p);
669 if (p->queue - p->pc == PQ_INACTIVE) {
675 * Ok, try to fully clean the page and any nearby pages such that at
676 * least the requested page is freed or moved to the cache queue.
678 * We usually do this synchronously to allow us to get the page into
679 * the CACHE queue quickly, which will prevent memory exhaustion if
680 * a process with a memoryuse limit is running away. However, the
681 * sysadmin may desire to set vm.swap_user_async which relaxes this
682 * and improves write performance.
685 long max_launder = 0x7FFF;
686 long vnodes_skipped = 0;
688 struct vnode *vpfailed = NULL;
692 if (vm_pageout_memuse_mode >= 2) {
693 vmflush_flags = VM_PAGER_TRY_TO_CACHE |
694 VM_PAGER_ALLOW_ACTIVE;
695 if (swap_user_async == 0)
696 vmflush_flags |= VM_PAGER_PUT_SYNC;
697 vm_page_flag_set(p, PG_WINATCFLS);
699 vm_pageout_page(p, &max_launder,
701 &vpfailed, 1, vmflush_flags);
711 * Must be at end to avoid SMP races.
719 * Deactivate some number of pages in a map due to set RLIMIT_RSS limits.
720 * that is relatively difficult to do. We try to keep track of where we
721 * left off last time to reduce scan overhead.
723 * Called when vm_pageout_memuse_mode is >= 1.
726 vm_pageout_map_deactivate_pages(vm_map_t map, vm_pindex_t limit)
728 vm_offset_t pgout_offset;
729 struct pmap_pgscan_info info;
732 pgout_offset = map->pgout_offset;
735 kprintf("%016jx ", pgout_offset);
737 if (pgout_offset < VM_MIN_USER_ADDRESS)
738 pgout_offset = VM_MIN_USER_ADDRESS;
739 if (pgout_offset >= VM_MAX_USER_ADDRESS)
741 info.pmap = vm_map_pmap(map);
743 info.beg_addr = pgout_offset;
744 info.end_addr = VM_MAX_USER_ADDRESS;
745 info.callback = vm_pageout_mdp_callback;
747 info.actioncount = 0;
751 pgout_offset = info.offset;
753 kprintf("%016jx %08lx %08lx\n", pgout_offset,
754 info.cleancount, info.actioncount);
757 if (pgout_offset != VM_MAX_USER_ADDRESS &&
758 pmap_resident_tlnw_count(vm_map_pmap(map)) > limit) {
760 } else if (retries &&
761 pmap_resident_tlnw_count(vm_map_pmap(map)) > limit) {
765 map->pgout_offset = pgout_offset;
770 * Called when the pageout scan wants to free a page. We no longer
771 * try to cycle the vm_object here with a reference & dealloc, which can
772 * cause a non-trivial object collapse in a critical path.
774 * It is unclear why we cycled the ref_count in the past, perhaps to try
775 * to optimize shadow chain collapses but I don't quite see why it would
776 * be necessary. An OBJ_DEAD object should terminate any and all vm_pages
777 * synchronously and not have to be kicked-start.
780 vm_pageout_page_free(vm_page_t m)
782 vm_page_protect(m, VM_PROT_NONE);
787 * vm_pageout_scan does the dirty work for the pageout daemon.
789 struct vm_pageout_scan_info {
790 struct proc *bigproc;
794 static int vm_pageout_scan_callback(struct proc *p, void *data);
797 * Scan inactive queue
799 * WARNING! Can be called from two pagedaemon threads simultaneously.
802 vm_pageout_scan_inactive(int pass, int q, long avail_shortage,
803 long *vnodes_skipped)
806 struct vm_page marker;
807 struct vnode *vpfailed; /* warning, allowed to be stale */
814 isep = (curthread == emergpager);
817 * Start scanning the inactive queue for pages we can move to the
818 * cache or free. The scan will stop when the target is reached or
819 * we have scanned the entire inactive queue. Note that m->act_count
820 * is not used to form decisions for the inactive queue, only for the
823 * max_launder limits the number of dirty pages we flush per scan.
824 * For most systems a smaller value (16 or 32) is more robust under
825 * extreme memory and disk pressure because any unnecessary writes
826 * to disk can result in extreme performance degredation. However,
827 * systems with excessive dirty pages (especially when MAP_NOSYNC is
828 * used) will die horribly with limited laundering. If the pageout
829 * daemon cannot clean enough pages in the first pass, we let it go
830 * all out in succeeding passes.
832 * NOTE! THE EMERGENCY PAGER (isep) DOES NOT LAUNDER VNODE-BACKED
835 if ((max_launder = vm_max_launder) <= 1)
841 * Initialize our marker
843 bzero(&marker, sizeof(marker));
844 marker.flags = PG_FICTITIOUS | PG_MARKER;
845 marker.busy_count = PBUSY_LOCKED;
846 marker.queue = PQ_INACTIVE + q;
848 marker.wire_count = 1;
851 * Inactive queue scan.
853 * NOTE: The vm_page must be spinlocked before the queue to avoid
854 * deadlocks, so it is easiest to simply iterate the loop
855 * with the queue unlocked at the top.
859 vm_page_queues_spin_lock(PQ_INACTIVE + q);
860 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE + q].pl, &marker, pageq);
861 maxscan = vm_page_queues[PQ_INACTIVE + q].lcnt;
864 * Queue locked at top of loop to avoid stack marker issues.
866 while ((m = TAILQ_NEXT(&marker, pageq)) != NULL &&
867 maxscan-- > 0 && avail_shortage - delta > 0)
871 KKASSERT(m->queue == PQ_INACTIVE + q);
872 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE + q].pl,
874 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE + q].pl, m,
876 mycpu->gd_cnt.v_pdpages++;
879 * Skip marker pages (atomic against other markers to avoid
880 * infinite hop-over scans).
882 if (m->flags & PG_MARKER)
886 * Try to busy the page. Don't mess with pages which are
887 * already busy or reorder them in the queue.
889 if (vm_page_busy_try(m, TRUE))
893 * Remaining operations run with the page busy and neither
894 * the page or the queue will be spin-locked.
896 KKASSERT(m->queue == PQ_INACTIVE + q);
897 vm_page_queues_spin_unlock(PQ_INACTIVE + q);
900 * The emergency pager runs when the primary pager gets
901 * stuck, which typically means the primary pager deadlocked
902 * on a vnode-backed page. Therefore, the emergency pager
903 * must skip any complex objects.
905 * We disallow VNODEs unless they are VCHR whos device ops
906 * does not flag D_NOEMERGPGR.
908 if (isep && m->object) {
911 switch(m->object->type) {
915 * Allow anonymous memory and assume that
916 * swap devices are not complex, since its
917 * kinda worthless if we can't swap out dirty
923 * Allow VCHR device if the D_NOEMERGPGR
924 * flag is not set, deny other vnode types
925 * as being too complex.
927 vp = m->object->handle;
928 if (vp && vp->v_type == VCHR &&
929 vp->v_rdev && vp->v_rdev->si_ops &&
930 (vp->v_rdev->si_ops->head.flags &
931 D_NOEMERGPGR) == 0) {
934 /* Deny - fall through */
940 vm_page_queues_spin_lock(PQ_INACTIVE + q);
947 * Try to pageout the page and perhaps other nearby pages.
948 * We want to get the pages into the cache on the second
949 * pass. Otherwise the pages can wind up just cycling in
950 * the inactive queue, getting flushed over and over again.
952 if (m->flags & PG_WINATCFLS)
953 vmflush_flags = VM_PAGER_TRY_TO_CACHE;
956 count = vm_pageout_page(m, &max_launder, vnodes_skipped,
957 &vpfailed, pass, vmflush_flags);
961 * Systems with a ton of memory can wind up with huge
962 * deactivation counts. Because the inactive scan is
963 * doing a lot of flushing, the combination can result
964 * in excessive paging even in situations where other
965 * unrelated threads free up sufficient VM.
967 * To deal with this we abort the nominal active->inactive
968 * scan before we hit the inactive target when free+cache
969 * levels have reached a reasonable target.
971 * When deciding to stop early we need to add some slop to
972 * the test and we need to return full completion to the caller
973 * to prevent the caller from thinking there is something
974 * wrong and issuing a low-memory+swap warning or pkill.
976 * A deficit forces paging regardless of the state of the
977 * VM page queues (used for RSS enforcement).
980 vm_page_queues_spin_lock(PQ_INACTIVE + q);
981 if (vm_paging_target() < -vm_max_launder) {
983 * Stopping early, return full completion to caller.
985 if (delta < avail_shortage)
986 delta = avail_shortage;
991 /* page queue still spin-locked */
992 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE + q].pl, &marker, pageq);
993 vm_page_queues_spin_unlock(PQ_INACTIVE + q);
999 * Pageout the specified page, return the total number of pages paged out
1000 * (this routine may cluster).
1002 * The page must be busied and soft-busied by the caller and will be disposed
1003 * of by this function.
1006 vm_pageout_page(vm_page_t m, long *max_launderp, long *vnodes_skippedp,
1007 struct vnode **vpfailedp, int pass, int vmflush_flags)
1014 * Wiring no longer removes a page from its queue. The last unwiring
1015 * will requeue the page. Obviously wired pages cannot be paged out
1016 * so unqueue it and return.
1018 if (m->wire_count) {
1019 vm_page_unqueue_nowakeup(m);
1025 * A held page may be undergoing I/O, so skip it.
1027 if (m->hold_count) {
1028 vm_page_and_queue_spin_lock(m);
1029 if (m->queue - m->pc == PQ_INACTIVE) {
1031 &vm_page_queues[m->queue].pl, m, pageq);
1033 &vm_page_queues[m->queue].pl, m, pageq);
1035 vm_page_and_queue_spin_unlock(m);
1040 if (m->object == NULL || m->object->ref_count == 0) {
1042 * If the object is not being used, we ignore previous
1045 vm_page_flag_clear(m, PG_REFERENCED);
1046 pmap_clear_reference(m);
1047 /* fall through to end */
1048 } else if (((m->flags & PG_REFERENCED) == 0) &&
1049 (actcount = pmap_ts_referenced(m))) {
1051 * Otherwise, if the page has been referenced while
1052 * in the inactive queue, we bump the "activation
1053 * count" upwards, making it less likely that the
1054 * page will be added back to the inactive queue
1055 * prematurely again. Here we check the page tables
1056 * (or emulated bits, if any), given the upper level
1057 * VM system not knowing anything about existing
1060 vm_page_activate(m);
1061 m->act_count += (actcount + ACT_ADVANCE);
1067 * (m) is still busied.
1069 * If the upper level VM system knows about any page
1070 * references, we activate the page. We also set the
1071 * "activation count" higher than normal so that we will less
1072 * likely place pages back onto the inactive queue again.
1074 if ((m->flags & PG_REFERENCED) != 0) {
1075 vm_page_flag_clear(m, PG_REFERENCED);
1076 actcount = pmap_ts_referenced(m);
1077 vm_page_activate(m);
1078 m->act_count += (actcount + ACT_ADVANCE + 1);
1084 * If the upper level VM system doesn't know anything about
1085 * the page being dirty, we have to check for it again. As
1086 * far as the VM code knows, any partially dirty pages are
1089 * Pages marked PG_WRITEABLE may be mapped into the user
1090 * address space of a process running on another cpu. A
1091 * user process (without holding the MP lock) running on
1092 * another cpu may be able to touch the page while we are
1093 * trying to remove it. vm_page_cache() will handle this
1096 if (m->dirty == 0) {
1097 vm_page_test_dirty(m);
1102 if (m->valid == 0 && (m->flags & PG_NEED_COMMIT) == 0) {
1104 * Invalid pages can be easily freed
1106 vm_pageout_page_free(m);
1107 mycpu->gd_cnt.v_dfree++;
1109 } else if (m->dirty == 0 && (m->flags & PG_NEED_COMMIT) == 0) {
1111 * Clean pages can be placed onto the cache queue.
1112 * This effectively frees them.
1116 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
1118 * Dirty pages need to be paged out, but flushing
1119 * a page is extremely expensive verses freeing
1120 * a clean page. Rather then artificially limiting
1121 * the number of pages we can flush, we instead give
1122 * dirty pages extra priority on the inactive queue
1123 * by forcing them to be cycled through the queue
1124 * twice before being flushed, after which the
1125 * (now clean) page will cycle through once more
1126 * before being freed. This significantly extends
1127 * the thrash point for a heavily loaded machine.
1129 vm_page_flag_set(m, PG_WINATCFLS);
1130 vm_page_and_queue_spin_lock(m);
1131 if (m->queue - m->pc == PQ_INACTIVE) {
1133 &vm_page_queues[m->queue].pl, m, pageq);
1135 &vm_page_queues[m->queue].pl, m, pageq);
1137 vm_page_and_queue_spin_unlock(m);
1139 } else if (*max_launderp > 0) {
1141 * We always want to try to flush some dirty pages if
1142 * we encounter them, to keep the system stable.
1143 * Normally this number is small, but under extreme
1144 * pressure where there are insufficient clean pages
1145 * on the inactive queue, we may have to go all out.
1147 int swap_pageouts_ok;
1148 struct vnode *vp = NULL;
1150 swap_pageouts_ok = 0;
1153 (object->type != OBJT_SWAP) &&
1154 (object->type != OBJT_DEFAULT)) {
1155 swap_pageouts_ok = 1;
1157 swap_pageouts_ok = !(defer_swap_pageouts ||
1158 disable_swap_pageouts);
1159 swap_pageouts_ok |= (!disable_swap_pageouts &&
1160 defer_swap_pageouts &&
1161 vm_page_count_min(0));
1165 * We don't bother paging objects that are "dead".
1166 * Those objects are in a "rundown" state.
1168 if (!swap_pageouts_ok ||
1170 (object->flags & OBJ_DEAD)) {
1171 vm_page_and_queue_spin_lock(m);
1172 if (m->queue - m->pc == PQ_INACTIVE) {
1174 &vm_page_queues[m->queue].pl,
1177 &vm_page_queues[m->queue].pl,
1180 vm_page_and_queue_spin_unlock(m);
1186 * (m) is still busied.
1188 * The object is already known NOT to be dead. It
1189 * is possible for the vget() to block the whole
1190 * pageout daemon, but the new low-memory handling
1191 * code should prevent it.
1193 * The previous code skipped locked vnodes and, worse,
1194 * reordered pages in the queue. This results in
1195 * completely non-deterministic operation because,
1196 * quite often, a vm_fault has initiated an I/O and
1197 * is holding a locked vnode at just the point where
1198 * the pageout daemon is woken up.
1200 * We can't wait forever for the vnode lock, we might
1201 * deadlock due to a vn_read() getting stuck in
1202 * vm_wait while holding this vnode. We skip the
1203 * vnode if we can't get it in a reasonable amount
1206 * vpfailed is used to (try to) avoid the case where
1207 * a large number of pages are associated with a
1208 * locked vnode, which could cause the pageout daemon
1209 * to stall for an excessive amount of time.
1211 if (object->type == OBJT_VNODE) {
1214 vp = object->handle;
1215 flags = LK_EXCLUSIVE;
1216 if (vp == *vpfailedp)
1219 flags |= LK_TIMELOCK;
1224 * We have unbusied (m) temporarily so we can
1225 * acquire the vp lock without deadlocking.
1226 * (m) is held to prevent destruction.
1228 if (vget(vp, flags) != 0) {
1230 ++pageout_lock_miss;
1231 if (object->flags & OBJ_MIGHTBEDIRTY)
1238 * The page might have been moved to another
1239 * queue during potential blocking in vget()
1240 * above. The page might have been freed and
1241 * reused for another vnode. The object might
1242 * have been reused for another vnode.
1244 if (m->queue - m->pc != PQ_INACTIVE ||
1245 m->object != object ||
1246 object->handle != vp) {
1247 if (object->flags & OBJ_MIGHTBEDIRTY)
1255 * The page may have been busied during the
1256 * blocking in vput(); We don't move the
1257 * page back onto the end of the queue so that
1258 * statistics are more correct if we don't.
1260 if (vm_page_busy_try(m, TRUE)) {
1268 * If it was wired while we didn't own it.
1270 if (m->wire_count) {
1271 vm_page_unqueue_nowakeup(m);
1278 * (m) is busied again
1280 * We own the busy bit and remove our hold
1281 * bit. If the page is still held it
1282 * might be undergoing I/O, so skip it.
1284 if (m->hold_count) {
1285 vm_page_and_queue_spin_lock(m);
1286 if (m->queue - m->pc == PQ_INACTIVE) {
1287 TAILQ_REMOVE(&vm_page_queues[m->queue].pl, m, pageq);
1288 TAILQ_INSERT_TAIL(&vm_page_queues[m->queue].pl, m, pageq);
1290 vm_page_and_queue_spin_unlock(m);
1291 if (object->flags & OBJ_MIGHTBEDIRTY)
1297 /* (m) is left busied as we fall through */
1301 * page is busy and not held here.
1303 * If a page is dirty, then it is either being washed
1304 * (but not yet cleaned) or it is still in the
1305 * laundry. If it is still in the laundry, then we
1306 * start the cleaning operation.
1308 * decrement inactive_shortage on success to account
1309 * for the (future) cleaned page. Otherwise we
1310 * could wind up laundering or cleaning too many
1313 * NOTE: Cleaning the page here does not cause
1314 * force_deficit to be adjusted, because the
1315 * page is not being freed or moved to the
1318 count = vm_pageout_clean_helper(m, vmflush_flags);
1319 *max_launderp -= count;
1322 * Clean ate busy, page no longer accessible
1335 * WARNING! Can be called from two pagedaemon threads simultaneously.
1338 vm_pageout_scan_active(int pass, int q,
1339 long avail_shortage, long inactive_shortage,
1340 long *recycle_countp)
1342 struct vm_page marker;
1349 isep = (curthread == emergpager);
1352 * We want to move pages from the active queue to the inactive
1353 * queue to get the inactive queue to the inactive target. If
1354 * we still have a page shortage from above we try to directly free
1355 * clean pages instead of moving them.
1357 * If we do still have a shortage we keep track of the number of
1358 * pages we free or cache (recycle_count) as a measure of thrashing
1359 * between the active and inactive queues.
1361 * If we were able to completely satisfy the free+cache targets
1362 * from the inactive pool we limit the number of pages we move
1363 * from the active pool to the inactive pool to 2x the pages we
1364 * had removed from the inactive pool (with a minimum of 1/5 the
1365 * inactive target). If we were not able to completely satisfy
1366 * the free+cache targets we go for the whole target aggressively.
1368 * NOTE: Both variables can end up negative.
1369 * NOTE: We are still in a critical section.
1371 * NOTE! THE EMERGENCY PAGER (isep) DOES NOT LAUNDER VNODE-BACKED
1375 bzero(&marker, sizeof(marker));
1376 marker.flags = PG_FICTITIOUS | PG_MARKER;
1377 marker.busy_count = PBUSY_LOCKED;
1378 marker.queue = PQ_ACTIVE + q;
1380 marker.wire_count = 1;
1382 vm_page_queues_spin_lock(PQ_ACTIVE + q);
1383 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_ACTIVE + q].pl, &marker, pageq);
1384 maxscan = vm_page_queues[PQ_ACTIVE + q].lcnt;
1387 * Queue locked at top of loop to avoid stack marker issues.
1389 while ((m = TAILQ_NEXT(&marker, pageq)) != NULL &&
1390 maxscan-- > 0 && (avail_shortage - delta > 0 ||
1391 inactive_shortage > 0))
1393 KKASSERT(m->queue == PQ_ACTIVE + q);
1394 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE + q].pl,
1396 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_ACTIVE + q].pl, m,
1400 * Skip marker pages (atomic against other markers to avoid
1401 * infinite hop-over scans).
1403 if (m->flags & PG_MARKER)
1407 * Try to busy the page. Don't mess with pages which are
1408 * already busy or reorder them in the queue.
1410 if (vm_page_busy_try(m, TRUE))
1414 * Remaining operations run with the page busy and neither
1415 * the page or the queue will be spin-locked.
1417 KKASSERT(m->queue == PQ_ACTIVE + q);
1418 vm_page_queues_spin_unlock(PQ_ACTIVE + q);
1422 * Don't deactivate pages that are held, even if we can
1423 * busy them. (XXX why not?)
1425 if (m->hold_count) {
1426 vm_page_and_queue_spin_lock(m);
1427 if (m->queue - m->pc == PQ_ACTIVE) {
1429 &vm_page_queues[PQ_ACTIVE + q].pl,
1432 &vm_page_queues[PQ_ACTIVE + q].pl,
1435 vm_page_and_queue_spin_unlock(m);
1441 * We can just remove wired pages from the queue
1443 if (m->wire_count) {
1444 vm_page_unqueue_nowakeup(m);
1450 * The emergency pager ignores vnode-backed pages as these
1451 * are the pages that probably bricked the main pager.
1453 if (isep && m->object && m->object->type == OBJT_VNODE) {
1454 vm_page_and_queue_spin_lock(m);
1455 if (m->queue - m->pc == PQ_ACTIVE) {
1457 &vm_page_queues[PQ_ACTIVE + q].pl,
1460 &vm_page_queues[PQ_ACTIVE + q].pl,
1463 vm_page_and_queue_spin_unlock(m);
1469 * The count for pagedaemon pages is done after checking the
1470 * page for eligibility...
1472 mycpu->gd_cnt.v_pdpages++;
1475 * Check to see "how much" the page has been used and clear
1476 * the tracking access bits. If the object has no references
1477 * don't bother paying the expense.
1480 if (m->object && m->object->ref_count != 0) {
1481 if (m->flags & PG_REFERENCED)
1483 actcount += pmap_ts_referenced(m);
1485 m->act_count += ACT_ADVANCE + actcount;
1486 if (m->act_count > ACT_MAX)
1487 m->act_count = ACT_MAX;
1490 vm_page_flag_clear(m, PG_REFERENCED);
1493 * actcount is only valid if the object ref_count is non-zero.
1494 * If the page does not have an object, actcount will be zero.
1496 if (actcount && m->object->ref_count != 0) {
1497 vm_page_and_queue_spin_lock(m);
1498 if (m->queue - m->pc == PQ_ACTIVE) {
1500 &vm_page_queues[PQ_ACTIVE + q].pl,
1503 &vm_page_queues[PQ_ACTIVE + q].pl,
1506 vm_page_and_queue_spin_unlock(m);
1509 switch(m->object->type) {
1512 m->act_count -= min(m->act_count,
1513 vm_anonmem_decline);
1516 m->act_count -= min(m->act_count,
1517 vm_filemem_decline);
1520 if (vm_pageout_algorithm ||
1521 (m->object == NULL) ||
1522 (m->object && (m->object->ref_count == 0)) ||
1523 m->act_count < pass + 1
1526 * Deactivate the page. If we had a
1527 * shortage from our inactive scan try to
1528 * free (cache) the page instead.
1530 * Don't just blindly cache the page if
1531 * we do not have a shortage from the
1532 * inactive scan, that could lead to
1533 * gigabytes being moved.
1535 --inactive_shortage;
1536 if (avail_shortage - delta > 0 ||
1537 (m->object && (m->object->ref_count == 0)))
1539 if (avail_shortage - delta > 0)
1541 vm_page_protect(m, VM_PROT_NONE);
1542 if (m->dirty == 0 &&
1543 (m->flags & PG_NEED_COMMIT) == 0 &&
1544 avail_shortage - delta > 0) {
1547 vm_page_deactivate(m);
1551 vm_page_deactivate(m);
1556 vm_page_and_queue_spin_lock(m);
1557 if (m->queue - m->pc == PQ_ACTIVE) {
1559 &vm_page_queues[PQ_ACTIVE + q].pl,
1562 &vm_page_queues[PQ_ACTIVE + q].pl,
1565 vm_page_and_queue_spin_unlock(m);
1571 vm_page_queues_spin_lock(PQ_ACTIVE + q);
1575 * Clean out our local marker.
1577 * Page queue still spin-locked.
1579 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE + q].pl, &marker, pageq);
1580 vm_page_queues_spin_unlock(PQ_ACTIVE + q);
1586 * The number of actually free pages can drop down to v_free_reserved,
1587 * we try to build the free count back above v_free_min. Note that
1588 * vm_paging_needed() also returns TRUE if v_free_count is not at
1589 * least v_free_min so that is the minimum we must build the free
1592 * We use a slightly higher target to improve hysteresis,
1593 * ((v_free_target + v_free_min) / 2). Since v_free_target
1594 * is usually the same as v_cache_min this maintains about
1595 * half the pages in the free queue as are in the cache queue,
1596 * providing pretty good pipelining for pageout operation.
1598 * The system operator can manipulate vm.v_cache_min and
1599 * vm.v_free_target to tune the pageout demon. Be sure
1600 * to keep vm.v_free_min < vm.v_free_target.
1602 * Note that the original paging target is to get at least
1603 * (free_min + cache_min) into (free + cache). The slightly
1604 * higher target will shift additional pages from cache to free
1605 * without effecting the original paging target in order to
1606 * maintain better hysteresis and not have the free count always
1607 * be dead-on v_free_min.
1609 * NOTE: we are still in a critical section.
1611 * Pages moved from PQ_CACHE to totally free are not counted in the
1612 * pages_freed counter.
1614 * WARNING! Can be called from two pagedaemon threads simultaneously.
1617 vm_pageout_scan_cache(long avail_shortage, int pass,
1618 long vnodes_skipped, long recycle_count)
1620 static int lastkillticks;
1621 struct vm_pageout_scan_info info;
1625 isep = (curthread == emergpager);
1627 while (vmstats.v_free_count <
1628 (vmstats.v_free_min + vmstats.v_free_target) / 2) {
1630 * This steals some code from vm/vm_page.c
1632 * Create two rovers and adjust the code to reduce
1633 * chances of them winding up at the same index (which
1634 * can cause a lot of contention).
1636 static int cache_rover[2] = { 0, PQ_L2_MASK / 2 };
1638 if (((cache_rover[0] ^ cache_rover[1]) & PQ_L2_MASK) == 0)
1641 m = vm_page_list_find(PQ_CACHE, cache_rover[isep] & PQ_L2_MASK);
1646 * If the busy attempt fails we can still deactivate the page.
1648 /* page is returned removed from its queue and spinlocked */
1649 if (vm_page_busy_try(m, TRUE)) {
1650 vm_page_deactivate_locked(m);
1651 vm_page_spin_unlock(m);
1654 vm_page_spin_unlock(m);
1655 pagedaemon_wakeup();
1659 * Remaining operations run with the page busy and neither
1660 * the page or the queue will be spin-locked.
1662 if ((m->flags & (PG_UNQUEUED | PG_NEED_COMMIT)) ||
1665 vm_page_deactivate(m);
1669 pmap_mapped_sync(m);
1670 KKASSERT((m->flags & PG_MAPPED) == 0);
1671 KKASSERT(m->dirty == 0);
1672 vm_pageout_page_free(m);
1673 mycpu->gd_cnt.v_dfree++;
1676 cache_rover[1] -= PQ_PRIME2;
1678 cache_rover[0] += PQ_PRIME2;
1681 #if !defined(NO_SWAPPING)
1683 * Idle process swapout -- run once per second.
1685 if (vm_swap_idle_enabled) {
1687 if (time_uptime != lsec) {
1688 atomic_set_int(&vm_pageout_req_swapout, VM_SWAP_IDLE);
1696 * If we didn't get enough free pages, and we have skipped a vnode
1697 * in a writeable object, wakeup the sync daemon. And kick swapout
1698 * if we did not get enough free pages.
1700 if (vm_paging_target() > 0) {
1701 if (vnodes_skipped && vm_page_count_min(0))
1702 speedup_syncer(NULL);
1703 #if !defined(NO_SWAPPING)
1704 if (vm_swap_enabled && vm_page_count_target()) {
1705 atomic_set_int(&vm_pageout_req_swapout, VM_SWAP_NORMAL);
1712 * Handle catastrophic conditions. Under good conditions we should
1713 * be at the target, well beyond our minimum. If we could not even
1714 * reach our minimum the system is under heavy stress. But just being
1715 * under heavy stress does not trigger process killing.
1717 * We consider ourselves to have run out of memory if the swap pager
1718 * is full and avail_shortage is still positive. The secondary check
1719 * ensures that we do not kill processes if the instantanious
1720 * availability is good, even if the pageout demon pass says it
1721 * couldn't get to the target.
1723 * NOTE! THE EMERGENCY PAGER (isep) DOES NOT HANDLE SWAP FULL
1726 if (swap_pager_almost_full &&
1729 (vm_page_count_min(recycle_count) || avail_shortage > 0)) {
1730 kprintf("Warning: system low on memory+swap "
1731 "shortage %ld for %d ticks!\n",
1732 avail_shortage, ticks - swap_fail_ticks);
1734 kprintf("Metrics: spaf=%d spf=%d pass=%d "
1735 "avail=%ld target=%ld last=%u\n",
1736 swap_pager_almost_full,
1741 (unsigned int)(ticks - lastkillticks));
1743 if (swap_pager_full &&
1746 avail_shortage > 0 &&
1747 vm_paging_target() > 0 &&
1748 (unsigned int)(ticks - lastkillticks) >= hz) {
1750 * Kill something, maximum rate once per second to give
1751 * the process time to free up sufficient memory.
1753 lastkillticks = ticks;
1754 info.bigproc = NULL;
1756 allproc_scan(vm_pageout_scan_callback, &info, 0);
1757 if (info.bigproc != NULL) {
1758 kprintf("Try to kill process %d %s\n",
1759 info.bigproc->p_pid, info.bigproc->p_comm);
1760 info.bigproc->p_nice = PRIO_MIN;
1761 info.bigproc->p_usched->resetpriority(
1762 FIRST_LWP_IN_PROC(info.bigproc));
1763 atomic_set_int(&info.bigproc->p_flags, P_LOWMEMKILL);
1764 killproc(info.bigproc, "out of swap space");
1765 wakeup(&vmstats.v_free_count);
1766 PRELE(info.bigproc);
1772 vm_pageout_scan_callback(struct proc *p, void *data)
1774 struct vm_pageout_scan_info *info = data;
1778 * Never kill system processes or init. If we have configured swap
1779 * then try to avoid killing low-numbered pids.
1781 if ((p->p_flags & P_SYSTEM) || (p->p_pid == 1) ||
1782 ((p->p_pid < 48) && (vm_swap_size != 0))) {
1786 lwkt_gettoken(&p->p_token);
1789 * if the process is in a non-running type state,
1792 if (p->p_stat != SACTIVE && p->p_stat != SSTOP && p->p_stat != SCORE) {
1793 lwkt_reltoken(&p->p_token);
1798 * Get the approximate process size. Note that anonymous pages
1799 * with backing swap will be counted twice, but there should not
1800 * be too many such pages due to the stress the VM system is
1801 * under at this point.
1803 size = vmspace_anonymous_count(p->p_vmspace) +
1804 vmspace_swap_count(p->p_vmspace);
1807 * If the this process is bigger than the biggest one
1810 if (info->bigsize < size) {
1812 PRELE(info->bigproc);
1815 info->bigsize = size;
1817 lwkt_reltoken(&p->p_token);
1824 * This old guy slowly walks PQ_HOLD looking for pages which need to be
1825 * moved back to PQ_FREE. It is possible for pages to accumulate here
1826 * when vm_page_free() races against vm_page_unhold(), resulting in a
1827 * page being left on a PQ_HOLD queue with hold_count == 0.
1829 * It is easier to handle this edge condition here, in non-critical code,
1830 * rather than enforce a spin-lock for every 1->0 transition in
1833 * NOTE: TAILQ_FOREACH becomes invalid the instant we unlock the queue.
1836 vm_pageout_scan_hold(int q)
1840 vm_page_queues_spin_lock(PQ_HOLD + q);
1841 TAILQ_FOREACH(m, &vm_page_queues[PQ_HOLD + q].pl, pageq) {
1842 if (m->flags & PG_MARKER)
1846 * Process one page and return
1850 kprintf("DEBUG: pageout HOLD->FREE %p\n", m);
1852 vm_page_queues_spin_unlock(PQ_HOLD + q);
1853 vm_page_unhold(m); /* reprocess */
1856 vm_page_queues_spin_unlock(PQ_HOLD + q);
1860 * This routine tries to maintain the pseudo LRU active queue,
1861 * so that during long periods of time where there is no paging,
1862 * that some statistic accumulation still occurs. This code
1863 * helps the situation where paging just starts to occur.
1866 vm_pageout_page_stats(int q)
1868 static int fullintervalcount = 0;
1869 struct vm_page marker;
1871 long pcount, tpcount; /* Number of pages to check */
1874 page_shortage = (vmstats.v_inactive_target + vmstats.v_cache_max +
1875 vmstats.v_free_min) -
1876 (vmstats.v_free_count + vmstats.v_inactive_count +
1877 vmstats.v_cache_count);
1879 if (page_shortage <= 0)
1882 pcount = vm_page_queues[PQ_ACTIVE + q].lcnt;
1883 fullintervalcount += vm_pageout_stats_interval;
1884 if (fullintervalcount < vm_pageout_full_stats_interval) {
1885 tpcount = (vm_pageout_stats_max * pcount) /
1886 vmstats.v_page_count + 1;
1887 if (pcount > tpcount)
1890 fullintervalcount = 0;
1893 bzero(&marker, sizeof(marker));
1894 marker.flags = PG_FICTITIOUS | PG_MARKER;
1895 marker.busy_count = PBUSY_LOCKED;
1896 marker.queue = PQ_ACTIVE + q;
1898 marker.wire_count = 1;
1900 vm_page_queues_spin_lock(PQ_ACTIVE + q);
1901 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_ACTIVE + q].pl, &marker, pageq);
1904 * Queue locked at top of loop to avoid stack marker issues.
1906 while ((m = TAILQ_NEXT(&marker, pageq)) != NULL &&
1911 KKASSERT(m->queue == PQ_ACTIVE + q);
1912 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE + q].pl, &marker, pageq);
1913 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_ACTIVE + q].pl, m,
1917 * Skip marker pages (atomic against other markers to avoid
1918 * infinite hop-over scans).
1920 if (m->flags & PG_MARKER)
1924 * Ignore pages we can't busy
1926 if (vm_page_busy_try(m, TRUE))
1930 * Remaining operations run with the page busy and neither
1931 * the page or the queue will be spin-locked.
1933 KKASSERT(m->queue == PQ_ACTIVE + q);
1934 vm_page_queues_spin_unlock(PQ_ACTIVE + q);
1937 * We can just remove wired pages from the queue
1939 if (m->wire_count) {
1940 vm_page_unqueue_nowakeup(m);
1947 * We now have a safely busied page, the page and queue
1948 * spinlocks have been released.
1950 * Ignore held and wired pages
1952 if (m->hold_count || m->wire_count) {
1958 * Calculate activity
1961 if (m->flags & PG_REFERENCED) {
1962 vm_page_flag_clear(m, PG_REFERENCED);
1965 actcount += pmap_ts_referenced(m);
1968 * Update act_count and move page to end of queue.
1971 m->act_count += ACT_ADVANCE + actcount;
1972 if (m->act_count > ACT_MAX)
1973 m->act_count = ACT_MAX;
1974 vm_page_and_queue_spin_lock(m);
1975 if (m->queue - m->pc == PQ_ACTIVE) {
1977 &vm_page_queues[PQ_ACTIVE + q].pl,
1980 &vm_page_queues[PQ_ACTIVE + q].pl,
1983 vm_page_and_queue_spin_unlock(m);
1988 if (m->act_count == 0) {
1990 * We turn off page access, so that we have
1991 * more accurate RSS stats. We don't do this
1992 * in the normal page deactivation when the
1993 * system is loaded VM wise, because the
1994 * cost of the large number of page protect
1995 * operations would be higher than the value
1996 * of doing the operation.
1998 * We use the marker to save our place so
1999 * we can release the spin lock. both (m)
2000 * and (next) will be invalid.
2002 vm_page_protect(m, VM_PROT_NONE);
2003 vm_page_deactivate(m);
2005 m->act_count -= min(m->act_count, ACT_DECLINE);
2006 vm_page_and_queue_spin_lock(m);
2007 if (m->queue - m->pc == PQ_ACTIVE) {
2009 &vm_page_queues[PQ_ACTIVE + q].pl,
2012 &vm_page_queues[PQ_ACTIVE + q].pl,
2015 vm_page_and_queue_spin_unlock(m);
2019 vm_page_queues_spin_lock(PQ_ACTIVE + q);
2023 * Remove our local marker
2025 * Page queue still spin-locked.
2027 TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE + q].pl, &marker, pageq);
2028 vm_page_queues_spin_unlock(PQ_ACTIVE + q);
2032 vm_pageout_free_page_calc(vm_size_t count)
2035 * v_free_min normal allocations
2036 * v_free_reserved system allocations
2037 * v_pageout_free_min allocations by pageout daemon
2038 * v_interrupt_free_min low level allocations (e.g swap structures)
2040 * v_free_min is used to generate several other baselines, and they
2041 * can get pretty silly on systems with a lot of memory.
2043 vmstats.v_free_min = 64 + vmstats.v_page_count / 200;
2044 vmstats.v_free_reserved = vmstats.v_free_min * 4 / 8 + 7;
2045 vmstats.v_free_severe = vmstats.v_free_min * 4 / 8 + 0;
2046 vmstats.v_pageout_free_min = vmstats.v_free_min * 2 / 8 + 7;
2047 vmstats.v_interrupt_free_min = vmstats.v_free_min * 1 / 8 + 7;
2052 * vm_pageout is the high level pageout daemon. TWO kernel threads run
2053 * this daemon, the primary pageout daemon and the emergency pageout daemon.
2055 * The emergency pageout daemon takes over when the primary pageout daemon
2056 * deadlocks. The emergency pageout daemon ONLY pages out to swap, thus
2057 * avoiding the many low-memory deadlocks which can occur when paging out
2061 vm_pageout_thread(void)
2070 curthread->td_flags |= TDF_SYSTHREAD;
2073 * We only need to setup once.
2076 if (curthread == emergpager) {
2082 * Initialize some paging parameters.
2084 vm_pageout_free_page_calc(vmstats.v_page_count);
2087 * v_free_target and v_cache_min control pageout hysteresis. Note
2088 * that these are more a measure of the VM cache queue hysteresis
2089 * then the VM free queue. Specifically, v_free_target is the
2090 * high water mark (free+cache pages).
2092 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
2093 * low water mark, while v_free_min is the stop. v_cache_min must
2094 * be big enough to handle memory needs while the pageout daemon
2095 * is signalled and run to free more pages.
2097 vmstats.v_free_target = 4 * vmstats.v_free_min +
2098 vmstats.v_free_reserved;
2101 * NOTE: With the new buffer cache b_act_count we want the default
2102 * inactive target to be a percentage of available memory.
2104 * The inactive target essentially determines the minimum
2105 * number of 'temporary' pages capable of caching one-time-use
2106 * files when the VM system is otherwise full of pages
2107 * belonging to multi-time-use files or active program data.
2109 * NOTE: The inactive target is aggressively persued only if the
2110 * inactive queue becomes too small. If the inactive queue
2111 * is large enough to satisfy page movement to free+cache
2112 * then it is repopulated more slowly from the active queue.
2113 * This allows a general inactive_target default to be set.
2115 * There is an issue here for processes which sit mostly idle
2116 * 'overnight', such as sshd, tcsh, and X. Any movement from
2117 * the active queue will eventually cause such pages to
2118 * recycle eventually causing a lot of paging in the morning.
2119 * To reduce the incidence of this pages cycled out of the
2120 * buffer cache are moved directly to the inactive queue if
2121 * they were only used once or twice.
2123 * The vfs.vm_cycle_point sysctl can be used to adjust this.
2124 * Increasing the value (up to 64) increases the number of
2125 * buffer recyclements which go directly to the inactive queue.
2127 if (vmstats.v_free_count > 2048) {
2128 vmstats.v_cache_min = vmstats.v_free_target;
2129 vmstats.v_cache_max = 2 * vmstats.v_cache_min;
2131 vmstats.v_cache_min = 0;
2132 vmstats.v_cache_max = 0;
2134 vmstats.v_inactive_target = vmstats.v_free_count / 4;
2136 /* XXX does not really belong here */
2137 if (vm_page_max_wired == 0)
2138 vm_page_max_wired = vmstats.v_free_count / 3;
2140 if (vm_pageout_stats_max == 0)
2141 vm_pageout_stats_max = vmstats.v_free_target;
2144 * Set interval in seconds for stats scan.
2146 if (vm_pageout_stats_interval == 0)
2147 vm_pageout_stats_interval = 5;
2148 if (vm_pageout_full_stats_interval == 0)
2149 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
2153 * Set maximum free per pass
2155 if (vm_pageout_stats_free_max == 0)
2156 vm_pageout_stats_free_max = 5;
2158 swap_pager_swap_init();
2161 atomic_swap_int(&sequence_emerg_pager, 1);
2162 wakeup(&sequence_emerg_pager);
2166 * Sequence emergency pager startup
2169 while (sequence_emerg_pager == 0)
2170 tsleep(&sequence_emerg_pager, 0, "pstartup", hz);
2174 * The pageout daemon is never done, so loop forever.
2176 * WARNING! This code is being executed by two kernel threads
2177 * potentially simultaneously.
2181 long avail_shortage;
2182 long inactive_shortage;
2183 long vnodes_skipped = 0;
2184 long recycle_count = 0;
2188 * Wait for an action request. If we timeout check to
2189 * see if paging is needed (in case the normal wakeup
2194 * Emergency pagedaemon monitors the primary
2195 * pagedaemon while vm_pages_needed != 0.
2197 * The emergency pagedaemon only runs if VM paging
2198 * is needed and the primary pagedaemon has not
2199 * updated vm_pagedaemon_time for more than 2 seconds.
2201 if (vm_pages_needed)
2202 tsleep(&vm_pagedaemon_time, 0, "psleep", hz);
2204 tsleep(&vm_pagedaemon_time, 0, "psleep", hz*10);
2205 if (vm_pages_needed == 0) {
2209 if ((int)(ticks - vm_pagedaemon_time) < hz * 2) {
2215 * Primary pagedaemon
2217 * NOTE: We unconditionally cleanup PQ_HOLD even
2218 * when there is no work to do.
2220 vm_pageout_scan_hold(q3iterator & PQ_L2_MASK);
2223 if (vm_pages_needed == 0) {
2224 error = tsleep(&vm_pages_needed,
2226 vm_pageout_stats_interval * hz);
2228 vm_paging_needed(0) == 0 &&
2229 vm_pages_needed == 0) {
2230 for (q = 0; q < PQ_L2_SIZE; ++q)
2231 vm_pageout_page_stats(q);
2234 vm_pagedaemon_time = ticks;
2235 vm_pages_needed = 1;
2238 * Wake the emergency pagedaemon up so it
2239 * can monitor us. It will automatically
2240 * go back into a long sleep when
2241 * vm_pages_needed returns to 0.
2243 wakeup(&vm_pagedaemon_time);
2247 mycpu->gd_cnt.v_pdwakeups++;
2250 * Scan for INACTIVE->CLEAN/PAGEOUT
2252 * This routine tries to avoid thrashing the system with
2253 * unnecessary activity.
2255 * Calculate our target for the number of free+cache pages we
2256 * want to get to. This is higher then the number that causes
2257 * allocations to stall (severe) in order to provide hysteresis,
2258 * and if we don't make it all the way but get to the minimum
2259 * we're happy. Goose it a bit if there are multiple requests
2262 * Don't reduce avail_shortage inside the loop or the
2263 * PQAVERAGE() calculation will break.
2265 * NOTE! deficit is differentiated from avail_shortage as
2266 * REQUIRING at least (deficit) pages to be cleaned,
2267 * even if the page queues are in good shape. This
2268 * is used primarily for handling per-process
2269 * RLIMIT_RSS and may also see small values when
2270 * processes block due to low memory.
2274 vm_pagedaemon_time = ticks;
2275 avail_shortage = vm_paging_target() + vm_pageout_deficit;
2276 vm_pageout_deficit = 0;
2278 if (avail_shortage > 0) {
2283 for (q = 0; q < PQ_L2_SIZE; ++q) {
2284 delta += vm_pageout_scan_inactive(
2287 PQAVERAGE(avail_shortage),
2293 if (avail_shortage - delta <= 0)
2297 * It is possible for avail_shortage to be
2298 * very large. If a large program exits or
2299 * frees a ton of memory all at once, we do
2300 * not have to continue deactivations.
2302 * (We will still run the active->inactive
2305 if (!vm_page_count_target() &&
2307 vm_page_free_hysteresis)) {
2312 avail_shortage -= delta;
2317 * Figure out how many active pages we must deactivate. If
2318 * we were able to reach our target with just the inactive
2319 * scan above we limit the number of active pages we
2320 * deactivate to reduce unnecessary work.
2324 vm_pagedaemon_time = ticks;
2325 inactive_shortage = vmstats.v_inactive_target -
2326 vmstats.v_inactive_count;
2329 * If we were unable to free sufficient inactive pages to
2330 * satisfy the free/cache queue requirements then simply
2331 * reaching the inactive target may not be good enough.
2332 * Try to deactivate pages in excess of the target based
2335 * However to prevent thrashing the VM system do not
2336 * deactivate more than an additional 1/10 the inactive
2337 * target's worth of active pages.
2339 if (avail_shortage > 0) {
2340 tmp = avail_shortage * 2;
2341 if (tmp > vmstats.v_inactive_target / 10)
2342 tmp = vmstats.v_inactive_target / 10;
2343 inactive_shortage += tmp;
2347 * Only trigger a pmap cleanup on inactive shortage.
2349 if (isep == 0 && inactive_shortage > 0) {
2354 * Scan for ACTIVE->INACTIVE
2356 * Only trigger on inactive shortage. Triggering on
2357 * avail_shortage can starve the active queue with
2358 * unnecessary active->inactive transitions and destroy
2361 * If this is the emergency pager, always try to move
2362 * a few pages from active to inactive because the inactive
2363 * queue might have enough pages, but not enough anonymous
2366 if (isep && inactive_shortage < vm_emerg_launder)
2367 inactive_shortage = vm_emerg_launder;
2369 if (/*avail_shortage > 0 ||*/ inactive_shortage > 0) {
2374 for (q = 0; q < PQ_L2_SIZE; ++q) {
2375 delta += vm_pageout_scan_active(
2378 PQAVERAGE(avail_shortage),
2379 PQAVERAGE(inactive_shortage),
2385 if (inactive_shortage - delta <= 0 &&
2386 avail_shortage - delta <= 0) {
2391 * inactive_shortage can be a very large
2392 * number. This is intended to break out
2393 * early if our inactive_target has been
2394 * reached due to other system activity.
2396 if (vmstats.v_inactive_count >
2397 vmstats.v_inactive_target) {
2398 inactive_shortage = 0;
2402 inactive_shortage -= delta;
2403 avail_shortage -= delta;
2408 * Scan for CACHE->FREE
2410 * Finally free enough cache pages to meet our free page
2411 * requirement and take more drastic measures if we are
2416 vm_pagedaemon_time = ticks;
2417 vm_pageout_scan_cache(avail_shortage, pass,
2418 vnodes_skipped, recycle_count);
2421 * This is a bit sophisticated because we do not necessarily
2422 * want to force paging until our targets are reached if we
2423 * were able to successfully retire the shortage we calculated.
2425 if (avail_shortage > 0) {
2427 * If we did not retire enough pages continue the
2428 * pageout operation until we are able to.
2432 if (pass < 10 && vm_pages_needed > 1) {
2434 * Normal operation, additional processes
2435 * have already kicked us. Retry immediately
2436 * unless swap space is completely full in
2437 * which case delay a bit.
2439 if (swap_pager_full) {
2440 tsleep(&vm_pages_needed, 0, "pdelay",
2442 } /* else immediate retry */
2443 } else if (pass < 10) {
2445 * Do a short sleep for the first 10 passes,
2446 * allow the sleep to be woken up by resetting
2447 * vm_pages_needed to 1 (NOTE: we are still
2451 vm_pages_needed = 1;
2452 tsleep(&vm_pages_needed, 0, "pdelay", 2);
2453 } else if (swap_pager_full == 0) {
2455 * We've taken too many passes, force a
2458 tsleep(&vm_pages_needed, 0, "pdelay", hz / 10);
2461 * Running out of memory, catastrophic
2462 * back-off to one-second intervals.
2464 tsleep(&vm_pages_needed, 0, "pdelay", hz);
2466 } else if (vm_pages_needed) {
2468 * We retired our calculated shortage but we may have
2469 * to continue paging if threads drain memory too far
2472 * Similar to vm_page_free_wakeup() in vm_page.c.
2475 if (!vm_paging_needed(0)) {
2476 /* still more than half-way to our target */
2477 vm_pages_needed = 0;
2478 wakeup(&vmstats.v_free_count);
2480 if (!vm_page_count_min(vm_page_free_hysteresis)) {
2482 * Continue operations with wakeup
2483 * (set variable to avoid overflow)
2485 vm_pages_needed = 2;
2486 wakeup(&vmstats.v_free_count);
2489 * No wakeup() needed, continue operations.
2490 * (set variable to avoid overflow)
2492 vm_pages_needed = 2;
2496 * Turn paging back on immediately if we are under
2504 static struct kproc_desc pg1_kp = {
2509 SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &pg1_kp);
2511 static struct kproc_desc pg2_kp = {
2516 SYSINIT(emergpager, SI_SUB_KTHREAD_PAGE, SI_ORDER_ANY, kproc_start, &pg2_kp);
2520 * Called after allocating a page out of the cache or free queue
2521 * to possibly wake the pagedaemon up to replentish our supply.
2523 * We try to generate some hysteresis by waking the pagedaemon up
2524 * when our free+cache pages go below the free_min+cache_min level.
2525 * The pagedaemon tries to get the count back up to at least the
2526 * minimum, and through to the target level if possible.
2528 * If the pagedaemon is already active bump vm_pages_needed as a hint
2529 * that there are even more requests pending.
2535 pagedaemon_wakeup(void)
2537 if (vm_paging_needed(0) && curthread != pagethread) {
2538 if (vm_pages_needed <= 1) {
2539 vm_pages_needed = 1; /* SMP race ok */
2540 wakeup(&vm_pages_needed); /* tickle pageout */
2541 } else if (vm_page_count_min(0)) {
2542 ++vm_pages_needed; /* SMP race ok */
2543 /* a wakeup() would be wasted here */
2548 #if !defined(NO_SWAPPING)
2555 vm_req_vmdaemon(void)
2557 static int lastrun = 0;
2559 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
2560 wakeup(&vm_daemon_needed);
2565 static int vm_daemon_callback(struct proc *p, void *data __unused);
2576 tsleep(&vm_daemon_needed, 0, "psleep", 0);
2577 req_swapout = atomic_swap_int(&vm_pageout_req_swapout, 0);
2583 swapout_procs(vm_pageout_req_swapout);
2586 * scan the processes for exceeding their rlimits or if
2587 * process is swapped out -- deactivate pages
2589 allproc_scan(vm_daemon_callback, NULL, 0);
2594 vm_daemon_callback(struct proc *p, void *data __unused)
2597 vm_pindex_t limit, size;
2600 * if this is a system process or if we have already
2601 * looked at this process, skip it.
2603 lwkt_gettoken(&p->p_token);
2605 if (p->p_flags & (P_SYSTEM | P_WEXIT)) {
2606 lwkt_reltoken(&p->p_token);
2611 * if the process is in a non-running type state,
2614 if (p->p_stat != SACTIVE && p->p_stat != SSTOP && p->p_stat != SCORE) {
2615 lwkt_reltoken(&p->p_token);
2622 limit = OFF_TO_IDX(qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
2623 p->p_rlimit[RLIMIT_RSS].rlim_max));
2626 * let processes that are swapped out really be
2627 * swapped out. Set the limit to nothing to get as
2628 * many pages out to swap as possible.
2630 if (p->p_flags & P_SWAPPEDOUT)
2635 size = pmap_resident_tlnw_count(&vm->vm_pmap);
2636 if (limit >= 0 && size > 4096 &&
2637 size - 4096 >= limit && vm_pageout_memuse_mode >= 1) {
2638 vm_pageout_map_deactivate_pages(&vm->vm_map, limit);
2642 lwkt_reltoken(&p->p_token);