2 * Copyright (c) 1991 Regents of the University of California.
4 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
8 * Copyright (c) 2005 Yahoo! Technologies Norway AS
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.
72 * The proverbial page-out daemon.
75 #include <sys/cdefs.h>
76 __FBSDID("$FreeBSD$");
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/eventhandler.h>
84 #include <sys/mutex.h>
86 #include <sys/kthread.h>
88 #include <sys/mount.h>
89 #include <sys/racct.h>
90 #include <sys/resourcevar.h>
91 #include <sys/sched.h>
92 #include <sys/signalvar.h>
93 #include <sys/vnode.h>
94 #include <sys/vmmeter.h>
96 #include <sys/sysctl.h>
99 #include <vm/vm_param.h>
100 #include <vm/vm_object.h>
101 #include <vm/vm_page.h>
102 #include <vm/vm_map.h>
103 #include <vm/vm_pageout.h>
104 #include <vm/vm_pager.h>
105 #include <vm/swap_pager.h>
106 #include <vm/vm_extern.h>
110 * System initialization
113 /* the kernel process "vm_pageout"*/
114 static void vm_pageout(void);
115 static int vm_pageout_clean(vm_page_t);
116 static void vm_pageout_scan(int pass);
118 struct proc *pageproc;
120 static struct kproc_desc page_kp = {
125 SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start,
128 #if !defined(NO_SWAPPING)
129 /* the kernel process "vm_daemon"*/
130 static void vm_daemon(void);
131 static struct proc *vmproc;
133 static struct kproc_desc vm_kp = {
138 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp);
142 int vm_pages_needed; /* Event on which pageout daemon sleeps */
143 int vm_pageout_deficit; /* Estimated number of pages deficit */
144 int vm_pageout_pages_needed; /* flag saying that the pageout daemon needs pages */
146 #if !defined(NO_SWAPPING)
147 static int vm_pageout_req_swapout; /* XXX */
148 static int vm_daemon_needed;
149 static struct mtx vm_daemon_mtx;
150 /* Allow for use by vm_pageout before vm_daemon is initialized. */
151 MTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF);
153 static int vm_max_launder = 32;
154 static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
155 static int vm_pageout_full_stats_interval = 0;
156 static int vm_pageout_algorithm=0;
157 static int defer_swap_pageouts=0;
158 static int disable_swap_pageouts=0;
160 #if defined(NO_SWAPPING)
161 static int vm_swap_enabled=0;
162 static int vm_swap_idle_enabled=0;
164 static int vm_swap_enabled=1;
165 static int vm_swap_idle_enabled=0;
168 SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
169 CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
171 SYSCTL_INT(_vm, OID_AUTO, max_launder,
172 CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
174 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
175 CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
177 SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
178 CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
180 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
181 CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
183 #if defined(NO_SWAPPING)
184 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
185 CTLFLAG_RD, &vm_swap_enabled, 0, "Enable entire process swapout");
186 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
187 CTLFLAG_RD, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
189 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
190 CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
191 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
192 CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
195 SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
196 CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
198 SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
199 CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
201 static int pageout_lock_miss;
202 SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
203 CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
205 #define VM_PAGEOUT_PAGE_COUNT 16
206 int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
208 int vm_page_max_wired; /* XXX max # of wired pages system-wide */
209 SYSCTL_INT(_vm, OID_AUTO, max_wired,
210 CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count");
212 static boolean_t vm_pageout_fallback_object_lock(vm_page_t, vm_page_t *);
213 static boolean_t vm_pageout_launder(int, int, vm_paddr_t, vm_paddr_t);
214 #if !defined(NO_SWAPPING)
215 static void vm_pageout_map_deactivate_pages(vm_map_t, long);
216 static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long);
217 static void vm_req_vmdaemon(int req);
219 static boolean_t vm_pageout_page_lock(vm_page_t, vm_page_t *);
220 static void vm_pageout_page_stats(void);
223 * Initialize a dummy page for marking the caller's place in the specified
224 * paging queue. In principle, this function only needs to set the flag
225 * PG_MARKER. Nonetheless, it sets the flag VPO_BUSY and initializes the hold
226 * count to one as safety precautions.
229 vm_pageout_init_marker(vm_page_t marker, u_short queue)
232 bzero(marker, sizeof(*marker));
233 marker->flags = PG_MARKER;
234 marker->oflags = VPO_BUSY;
235 marker->queue = queue;
236 marker->hold_count = 1;
240 * vm_pageout_fallback_object_lock:
242 * Lock vm object currently associated with `m'. VM_OBJECT_TRYLOCK is
243 * known to have failed and page queue must be either PQ_ACTIVE or
244 * PQ_INACTIVE. To avoid lock order violation, unlock the page queues
245 * while locking the vm object. Use marker page to detect page queue
246 * changes and maintain notion of next page on page queue. Return
247 * TRUE if no changes were detected, FALSE otherwise. vm object is
250 * This function depends on both the lock portion of struct vm_object
251 * and normal struct vm_page being type stable.
254 vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next)
256 struct vm_page marker;
262 vm_pageout_init_marker(&marker, queue);
265 TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl,
267 vm_page_unlock_queues();
269 VM_OBJECT_LOCK(object);
271 vm_page_lock_queues();
273 /* Page queue might have changed. */
274 *next = TAILQ_NEXT(&marker, pageq);
275 unchanged = (m->queue == queue &&
276 m->object == object &&
277 &marker == TAILQ_NEXT(m, pageq));
278 TAILQ_REMOVE(&vm_page_queues[queue].pl,
284 * Lock the page while holding the page queue lock. Use marker page
285 * to detect page queue changes and maintain notion of next page on
286 * page queue. Return TRUE if no changes were detected, FALSE
287 * otherwise. The page is locked on return. The page queue lock might
288 * be dropped and reacquired.
290 * This function depends on normal struct vm_page being type stable.
293 vm_pageout_page_lock(vm_page_t m, vm_page_t *next)
295 struct vm_page marker;
299 vm_page_lock_assert(m, MA_NOTOWNED);
300 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
302 if (vm_page_trylock(m))
306 vm_pageout_init_marker(&marker, queue);
308 TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl, m, &marker, pageq);
309 vm_page_unlock_queues();
311 vm_page_lock_queues();
313 /* Page queue might have changed. */
314 *next = TAILQ_NEXT(&marker, pageq);
315 unchanged = (m->queue == queue && &marker == TAILQ_NEXT(m, pageq));
316 TAILQ_REMOVE(&vm_page_queues[queue].pl, &marker, pageq);
323 * Clean the page and remove it from the laundry.
325 * We set the busy bit to cause potential page faults on this page to
326 * block. Note the careful timing, however, the busy bit isn't set till
327 * late and we cannot do anything that will mess with the page.
330 vm_pageout_clean(vm_page_t m)
333 vm_page_t mc[2*vm_pageout_page_count], pb, ps;
335 int ib, is, page_base;
336 vm_pindex_t pindex = m->pindex;
338 vm_page_lock_assert(m, MA_OWNED);
340 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
343 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
344 * with the new swapper, but we could have serious problems paging
345 * out other object types if there is insufficient memory.
347 * Unfortunately, checking free memory here is far too late, so the
348 * check has been moved up a procedural level.
352 * Can't clean the page if it's busy or held.
354 KASSERT(m->busy == 0 && (m->oflags & VPO_BUSY) == 0,
355 ("vm_pageout_clean: page %p is busy", m));
356 KASSERT(m->hold_count == 0, ("vm_pageout_clean: page %p is held", m));
359 mc[vm_pageout_page_count] = pb = ps = m;
361 page_base = vm_pageout_page_count;
366 * Scan object for clusterable pages.
368 * We can cluster ONLY if: ->> the page is NOT
369 * clean, wired, busy, held, or mapped into a
370 * buffer, and one of the following:
371 * 1) The page is inactive, or a seldom used
374 * 2) we force the issue.
376 * During heavy mmap/modification loads the pageout
377 * daemon can really fragment the underlying file
378 * due to flushing pages out of order and not trying
379 * align the clusters (which leave sporatic out-of-order
380 * holes). To solve this problem we do the reverse scan
381 * first and attempt to align our cluster, then do a
382 * forward scan if room remains.
385 while (ib && pageout_count < vm_pageout_page_count) {
393 if ((p = vm_page_prev(pb)) == NULL ||
394 (p->oflags & VPO_BUSY) != 0 || p->busy != 0) {
399 vm_page_test_dirty(p);
401 p->queue != PQ_INACTIVE ||
402 p->hold_count != 0) { /* may be undergoing I/O */
408 mc[--page_base] = pb = p;
412 * alignment boundry, stop here and switch directions. Do
415 if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
419 while (pageout_count < vm_pageout_page_count &&
420 pindex + is < object->size) {
423 if ((p = vm_page_next(ps)) == NULL ||
424 (p->oflags & VPO_BUSY) != 0 || p->busy != 0)
427 vm_page_test_dirty(p);
429 p->queue != PQ_INACTIVE ||
430 p->hold_count != 0) { /* may be undergoing I/O */
435 mc[page_base + pageout_count] = ps = p;
441 * If we exhausted our forward scan, continue with the reverse scan
442 * when possible, even past a page boundry. This catches boundry
445 if (ib && pageout_count < vm_pageout_page_count)
449 * we allow reads during pageouts...
451 return (vm_pageout_flush(&mc[page_base], pageout_count, 0, 0, NULL,
456 * vm_pageout_flush() - launder the given pages
458 * The given pages are laundered. Note that we setup for the start of
459 * I/O ( i.e. busy the page ), mark it read-only, and bump the object
460 * reference count all in here rather then in the parent. If we want
461 * the parent to do more sophisticated things we may have to change
464 * Returned runlen is the count of pages between mreq and first
465 * page after mreq with status VM_PAGER_AGAIN.
466 * *eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL
467 * for any page in runlen set.
470 vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen,
473 vm_object_t object = mc[0]->object;
474 int pageout_status[count];
478 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
481 * Initiate I/O. Bump the vm_page_t->busy counter and
482 * mark the pages read-only.
484 * We do not have to fixup the clean/dirty bits here... we can
485 * allow the pager to do it after the I/O completes.
487 * NOTE! mc[i]->dirty may be partial or fragmented due to an
488 * edge case with file fragments.
490 for (i = 0; i < count; i++) {
491 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL,
492 ("vm_pageout_flush: partially invalid page %p index %d/%d",
494 vm_page_io_start(mc[i]);
495 pmap_remove_write(mc[i]);
497 vm_object_pip_add(object, count);
499 vm_pager_put_pages(object, mc, count, flags, pageout_status);
501 runlen = count - mreq;
504 for (i = 0; i < count; i++) {
505 vm_page_t mt = mc[i];
507 KASSERT(pageout_status[i] == VM_PAGER_PEND ||
508 !pmap_page_is_write_mapped(mt),
509 ("vm_pageout_flush: page %p is not write protected", mt));
510 switch (pageout_status[i]) {
517 * Page outside of range of object. Right now we
518 * essentially lose the changes by pretending it
526 * If page couldn't be paged out, then reactivate the
527 * page so it doesn't clog the inactive list. (We
528 * will try paging out it again later).
531 vm_page_activate(mt);
533 if (eio != NULL && i >= mreq && i - mreq < runlen)
537 if (i >= mreq && i - mreq < runlen)
543 * If the operation is still going, leave the page busy to
544 * block all other accesses. Also, leave the paging in
545 * progress indicator set so that we don't attempt an object
548 if (pageout_status[i] != VM_PAGER_PEND) {
549 vm_object_pip_wakeup(object);
550 vm_page_io_finish(mt);
551 if (vm_page_count_severe()) {
553 vm_page_try_to_cache(mt);
560 return (numpagedout);
564 vm_pageout_launder(int queue, int tries, vm_paddr_t low, vm_paddr_t high)
570 vm_page_t m, m_tmp, next;
573 vm_page_lock_queues();
574 TAILQ_FOREACH_SAFE(m, &vm_page_queues[queue].pl, pageq, next) {
575 KASSERT(m->queue == queue,
576 ("vm_pageout_launder: page %p's queue is not %d", m,
578 if ((m->flags & PG_MARKER) != 0)
580 pa = VM_PAGE_TO_PHYS(m);
581 if (pa < low || pa + PAGE_SIZE > high)
583 if (!vm_pageout_page_lock(m, &next) || m->hold_count != 0) {
588 if ((!VM_OBJECT_TRYLOCK(object) &&
589 (!vm_pageout_fallback_object_lock(m, &next) ||
590 m->hold_count != 0)) || (m->oflags & VPO_BUSY) != 0 ||
593 VM_OBJECT_UNLOCK(object);
596 vm_page_test_dirty(m);
601 if (tries == 0 || (object->flags & OBJ_DEAD) != 0) {
602 VM_OBJECT_UNLOCK(object);
605 if (object->type == OBJT_VNODE) {
606 vm_page_unlock_queues();
608 vm_object_reference_locked(object);
609 VM_OBJECT_UNLOCK(object);
610 (void)vn_start_write(vp, &mp, V_WAIT);
611 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
612 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
613 VM_OBJECT_LOCK(object);
614 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
615 VM_OBJECT_UNLOCK(object);
617 VFS_UNLOCK_GIANT(vfslocked);
618 vm_object_deallocate(object);
619 vn_finished_write(mp);
621 } else if (object->type == OBJT_SWAP ||
622 object->type == OBJT_DEFAULT) {
623 vm_page_unlock_queues();
625 vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC,
627 VM_OBJECT_UNLOCK(object);
634 VM_OBJECT_UNLOCK(object);
636 vm_page_unlock_queues();
641 * Increase the number of cached pages. The specified value, "tries",
642 * determines which categories of pages are cached:
644 * 0: All clean, inactive pages within the specified physical address range
645 * are cached. Will not sleep.
646 * 1: The vm_lowmem handlers are called. All inactive pages within
647 * the specified physical address range are cached. May sleep.
648 * 2: The vm_lowmem handlers are called. All inactive and active pages
649 * within the specified physical address range are cached. May sleep.
652 vm_pageout_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high)
654 int actl, actmax, inactl, inactmax;
658 * Decrease registered cache sizes. The vm_lowmem handlers
659 * may acquire locks and/or sleep, so they can only be invoked
660 * when "tries" is greater than zero.
662 EVENTHANDLER_INVOKE(vm_lowmem, 0);
665 * We do this explicitly after the caches have been drained
671 inactmax = cnt.v_inactive_count;
673 actmax = tries < 2 ? 0 : cnt.v_active_count;
675 if (inactl < inactmax && vm_pageout_launder(PQ_INACTIVE, tries, low,
680 if (actl < actmax && vm_pageout_launder(PQ_ACTIVE, tries, low, high)) {
686 #if !defined(NO_SWAPPING)
688 * vm_pageout_object_deactivate_pages
690 * Deactivate enough pages to satisfy the inactive target
693 * The object and map must be locked.
696 vm_pageout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object,
699 vm_object_t backing_object, object;
701 int actcount, remove_mode;
703 VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
704 if (first_object->type == OBJT_DEVICE ||
705 first_object->type == OBJT_SG)
707 for (object = first_object;; object = backing_object) {
708 if (pmap_resident_count(pmap) <= desired)
710 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
711 if (object->type == OBJT_PHYS || object->paging_in_progress)
715 if (object->shadow_count > 1)
718 * Scan the object's entire memory queue.
720 TAILQ_FOREACH(p, &object->memq, listq) {
721 if (pmap_resident_count(pmap) <= desired)
723 if ((p->oflags & VPO_BUSY) != 0 || p->busy != 0)
725 PCPU_INC(cnt.v_pdpages);
727 if (p->wire_count != 0 || p->hold_count != 0 ||
728 !pmap_page_exists_quick(pmap, p)) {
732 actcount = pmap_ts_referenced(p);
733 if ((p->aflags & PGA_REFERENCED) != 0) {
736 vm_page_aflag_clear(p, PGA_REFERENCED);
738 if (p->queue != PQ_ACTIVE && actcount != 0) {
740 p->act_count += actcount;
741 } else if (p->queue == PQ_ACTIVE) {
743 p->act_count -= min(p->act_count,
746 (vm_pageout_algorithm ||
747 p->act_count == 0)) {
749 vm_page_deactivate(p);
751 vm_page_lock_queues();
753 vm_page_unlock_queues();
757 if (p->act_count < ACT_MAX -
759 p->act_count += ACT_ADVANCE;
760 vm_page_lock_queues();
762 vm_page_unlock_queues();
764 } else if (p->queue == PQ_INACTIVE)
768 if ((backing_object = object->backing_object) == NULL)
770 VM_OBJECT_LOCK(backing_object);
771 if (object != first_object)
772 VM_OBJECT_UNLOCK(object);
775 if (object != first_object)
776 VM_OBJECT_UNLOCK(object);
780 * deactivate some number of pages in a map, try to do it fairly, but
781 * that is really hard to do.
784 vm_pageout_map_deactivate_pages(map, desired)
789 vm_object_t obj, bigobj;
792 if (!vm_map_trylock(map))
799 * first, search out the biggest object, and try to free pages from
802 tmpe = map->header.next;
803 while (tmpe != &map->header) {
804 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
805 obj = tmpe->object.vm_object;
806 if (obj != NULL && VM_OBJECT_TRYLOCK(obj)) {
807 if (obj->shadow_count <= 1 &&
809 bigobj->resident_page_count < obj->resident_page_count)) {
811 VM_OBJECT_UNLOCK(bigobj);
814 VM_OBJECT_UNLOCK(obj);
817 if (tmpe->wired_count > 0)
818 nothingwired = FALSE;
822 if (bigobj != NULL) {
823 vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired);
824 VM_OBJECT_UNLOCK(bigobj);
827 * Next, hunt around for other pages to deactivate. We actually
828 * do this search sort of wrong -- .text first is not the best idea.
830 tmpe = map->header.next;
831 while (tmpe != &map->header) {
832 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
834 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
835 obj = tmpe->object.vm_object;
838 vm_pageout_object_deactivate_pages(map->pmap, obj, desired);
839 VM_OBJECT_UNLOCK(obj);
846 * Remove all mappings if a process is swapped out, this will free page
849 if (desired == 0 && nothingwired) {
850 pmap_remove(vm_map_pmap(map), vm_map_min(map),
855 #endif /* !defined(NO_SWAPPING) */
858 * vm_pageout_scan does the dirty work for the pageout daemon.
861 vm_pageout_scan(int pass)
864 struct vm_page marker;
865 int page_shortage, maxscan, pcount;
866 int addl_page_shortage;
869 int vnodes_skipped = 0;
871 boolean_t queues_locked;
874 * Decrease registered cache sizes.
876 EVENTHANDLER_INVOKE(vm_lowmem, 0);
878 * We do this explicitly after the caches have been drained above.
883 * The addl_page_shortage is the number of temporarily
884 * stuck pages in the inactive queue. In other words, the
885 * number of pages from cnt.v_inactive_count that should be
886 * discounted in setting the target for the active queue scan.
888 addl_page_shortage = atomic_readandclear_int(&vm_pageout_deficit);
891 * Calculate the number of pages we want to either free or move
894 page_shortage = vm_paging_target() + addl_page_shortage;
896 vm_pageout_init_marker(&marker, PQ_INACTIVE);
899 * Start scanning the inactive queue for pages we can move to the
900 * cache or free. The scan will stop when the target is reached or
901 * we have scanned the entire inactive queue. Note that m->act_count
902 * is not used to form decisions for the inactive queue, only for the
905 * maxlaunder limits the number of dirty pages we flush per scan.
906 * For most systems a smaller value (16 or 32) is more robust under
907 * extreme memory and disk pressure because any unnecessary writes
908 * to disk can result in extreme performance degredation. However,
909 * systems with excessive dirty pages (especially when MAP_NOSYNC is
910 * used) will die horribly with limited laundering. If the pageout
911 * daemon cannot clean enough pages in the first pass, we let it go
912 * all out in succeeding passes.
914 if ((maxlaunder = vm_max_launder) <= 1)
918 vm_page_lock_queues();
919 queues_locked = TRUE;
920 maxscan = cnt.v_inactive_count;
922 for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
923 m != NULL && maxscan-- > 0 && page_shortage > 0;
925 KASSERT(queues_locked, ("unlocked queues"));
926 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
927 KASSERT(m->queue == PQ_INACTIVE, ("Inactive queue %p", m));
930 next = TAILQ_NEXT(m, pageq);
935 if (m->flags & PG_MARKER)
938 KASSERT((m->flags & PG_FICTITIOUS) == 0,
939 ("Fictitious page %p cannot be in inactive queue", m));
940 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
941 ("Unmanaged page %p cannot be in inactive queue", m));
944 * The page or object lock acquisitions fail if the
945 * page was removed from the queue or moved to a
946 * different position within the queue. In either
947 * case, addl_page_shortage should not be incremented.
949 if (!vm_pageout_page_lock(m, &next)) {
954 if (!VM_OBJECT_TRYLOCK(object) &&
955 !vm_pageout_fallback_object_lock(m, &next)) {
957 VM_OBJECT_UNLOCK(object);
962 * Don't mess with busy pages, keep them at at the
963 * front of the queue, most likely they are being
964 * paged out. Increment addl_page_shortage for busy
965 * pages, because they may leave the inactive queue
966 * shortly after page scan is finished.
968 if (m->busy != 0 || (m->oflags & VPO_BUSY) != 0) {
970 VM_OBJECT_UNLOCK(object);
971 addl_page_shortage++;
976 * We unlock vm_page_queue_mtx, invalidating the
977 * 'next' pointer. Use our marker to remember our
980 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl,
982 vm_page_unlock_queues();
983 queues_locked = FALSE;
986 * If the object is not being used, we ignore previous
989 if (object->ref_count == 0) {
990 vm_page_aflag_clear(m, PGA_REFERENCED);
991 KASSERT(!pmap_page_is_mapped(m),
992 ("vm_pageout_scan: page %p is mapped", m));
995 * Otherwise, if the page has been referenced while in the
996 * inactive queue, we bump the "activation count" upwards,
997 * making it less likely that the page will be added back to
998 * the inactive queue prematurely again. Here we check the
999 * page tables (or emulated bits, if any), given the upper
1000 * level VM system not knowing anything about existing
1003 } else if ((m->aflags & PGA_REFERENCED) == 0 &&
1004 (actcount = pmap_ts_referenced(m)) != 0) {
1005 vm_page_activate(m);
1007 m->act_count += actcount + ACT_ADVANCE;
1008 VM_OBJECT_UNLOCK(object);
1013 * If the upper level VM system knows about any page
1014 * references, we activate the page. We also set the
1015 * "activation count" higher than normal so that we will less
1016 * likely place pages back onto the inactive queue again.
1018 if ((m->aflags & PGA_REFERENCED) != 0) {
1019 vm_page_aflag_clear(m, PGA_REFERENCED);
1020 actcount = pmap_ts_referenced(m);
1021 vm_page_activate(m);
1023 m->act_count += actcount + ACT_ADVANCE + 1;
1024 VM_OBJECT_UNLOCK(object);
1028 if (m->hold_count != 0) {
1030 VM_OBJECT_UNLOCK(object);
1033 * Held pages are essentially stuck in the
1034 * queue. So, they ought to be discounted
1035 * from cnt.v_inactive_count. See the
1036 * calculation of the page_shortage for the
1037 * loop over the active queue below.
1039 addl_page_shortage++;
1044 * If the upper level VM system does not believe that the page
1045 * is fully dirty, but it is mapped for write access, then we
1046 * consult the pmap to see if the page's dirty status should
1049 if (m->dirty != VM_PAGE_BITS_ALL &&
1050 pmap_page_is_write_mapped(m)) {
1052 * Avoid a race condition: Unless write access is
1053 * removed from the page, another processor could
1054 * modify it before all access is removed by the call
1055 * to vm_page_cache() below. If vm_page_cache() finds
1056 * that the page has been modified when it removes all
1057 * access, it panics because it cannot cache dirty
1058 * pages. In principle, we could eliminate just write
1059 * access here rather than all access. In the expected
1060 * case, when there are no last instant modifications
1061 * to the page, removing all access will be cheaper
1064 if (pmap_is_modified(m))
1066 else if (m->dirty == 0)
1070 if (m->valid == 0) {
1072 * Invalid pages can be easily freed
1075 PCPU_INC(cnt.v_dfree);
1077 } else if (m->dirty == 0) {
1079 * Clean pages can be placed onto the cache queue.
1080 * This effectively frees them.
1084 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
1086 * Dirty pages need to be paged out, but flushing
1087 * a page is extremely expensive verses freeing
1088 * a clean page. Rather then artificially limiting
1089 * the number of pages we can flush, we instead give
1090 * dirty pages extra priority on the inactive queue
1091 * by forcing them to be cycled through the queue
1092 * twice before being flushed, after which the
1093 * (now clean) page will cycle through once more
1094 * before being freed. This significantly extends
1095 * the thrash point for a heavily loaded machine.
1097 m->flags |= PG_WINATCFLS;
1098 vm_page_lock_queues();
1099 queues_locked = TRUE;
1101 } else if (maxlaunder > 0) {
1103 * We always want to try to flush some dirty pages if
1104 * we encounter them, to keep the system stable.
1105 * Normally this number is small, but under extreme
1106 * pressure where there are insufficient clean pages
1107 * on the inactive queue, we may have to go all out.
1109 int swap_pageouts_ok, vfslocked = 0;
1110 struct vnode *vp = NULL;
1111 struct mount *mp = NULL;
1113 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
1114 swap_pageouts_ok = 1;
1116 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
1117 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
1118 vm_page_count_min());
1123 * We don't bother paging objects that are "dead".
1124 * Those objects are in a "rundown" state.
1126 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
1127 vm_page_lock_queues();
1129 VM_OBJECT_UNLOCK(object);
1130 queues_locked = TRUE;
1136 * The object is already known NOT to be dead. It
1137 * is possible for the vget() to block the whole
1138 * pageout daemon, but the new low-memory handling
1139 * code should prevent it.
1141 * The previous code skipped locked vnodes and, worse,
1142 * reordered pages in the queue. This results in
1143 * completely non-deterministic operation and, on a
1144 * busy system, can lead to extremely non-optimal
1145 * pageouts. For example, it can cause clean pages
1146 * to be freed and dirty pages to be moved to the end
1147 * of the queue. Since dirty pages are also moved to
1148 * the end of the queue once-cleaned, this gives
1149 * way too large a weighting to defering the freeing
1152 * We can't wait forever for the vnode lock, we might
1153 * deadlock due to a vn_read() getting stuck in
1154 * vm_wait while holding this vnode. We skip the
1155 * vnode if we can't get it in a reasonable amount
1158 if (object->type == OBJT_VNODE) {
1160 vp = object->handle;
1161 if (vp->v_type == VREG &&
1162 vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1164 ++pageout_lock_miss;
1165 if (object->flags & OBJ_MIGHTBEDIRTY)
1167 goto unlock_and_continue;
1170 ("vp %p with NULL v_mount", vp));
1171 vm_object_reference_locked(object);
1172 VM_OBJECT_UNLOCK(object);
1173 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1174 if (vget(vp, LK_EXCLUSIVE | LK_TIMELOCK,
1176 VM_OBJECT_LOCK(object);
1177 ++pageout_lock_miss;
1178 if (object->flags & OBJ_MIGHTBEDIRTY)
1181 goto unlock_and_continue;
1183 VM_OBJECT_LOCK(object);
1185 vm_page_lock_queues();
1186 queues_locked = TRUE;
1188 * The page might have been moved to another
1189 * queue during potential blocking in vget()
1190 * above. The page might have been freed and
1191 * reused for another vnode.
1193 if (m->queue != PQ_INACTIVE ||
1194 m->object != object ||
1195 TAILQ_NEXT(m, pageq) != &marker) {
1197 if (object->flags & OBJ_MIGHTBEDIRTY)
1199 goto unlock_and_continue;
1203 * The page may have been busied during the
1204 * blocking in vget(). We don't move the
1205 * page back onto the end of the queue so that
1206 * statistics are more correct if we don't.
1208 if (m->busy || (m->oflags & VPO_BUSY)) {
1210 goto unlock_and_continue;
1214 * If the page has become held it might
1215 * be undergoing I/O, so skip it
1217 if (m->hold_count) {
1220 if (object->flags & OBJ_MIGHTBEDIRTY)
1222 goto unlock_and_continue;
1224 vm_page_unlock_queues();
1225 queues_locked = FALSE;
1229 * If a page is dirty, then it is either being washed
1230 * (but not yet cleaned) or it is still in the
1231 * laundry. If it is still in the laundry, then we
1232 * start the cleaning operation.
1234 * decrement page_shortage on success to account for
1235 * the (future) cleaned page. Otherwise we could wind
1236 * up laundering or cleaning too many pages.
1238 if (vm_pageout_clean(m) != 0) {
1242 unlock_and_continue:
1243 vm_page_lock_assert(m, MA_NOTOWNED);
1244 VM_OBJECT_UNLOCK(object);
1246 if (queues_locked) {
1247 vm_page_unlock_queues();
1248 queues_locked = FALSE;
1252 VFS_UNLOCK_GIANT(vfslocked);
1253 vm_object_deallocate(object);
1254 vn_finished_write(mp);
1256 vm_page_lock_assert(m, MA_NOTOWNED);
1260 VM_OBJECT_UNLOCK(object);
1262 if (!queues_locked) {
1263 vm_page_lock_queues();
1264 queues_locked = TRUE;
1266 next = TAILQ_NEXT(&marker, pageq);
1267 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl,
1272 * Compute the number of pages we want to try to move from the
1273 * active queue to the inactive queue.
1275 page_shortage = vm_paging_target() +
1276 cnt.v_inactive_target - cnt.v_inactive_count;
1277 page_shortage += addl_page_shortage;
1280 * Scan the active queue for things we can deactivate. We nominally
1281 * track the per-page activity counter and use it to locate
1282 * deactivation candidates.
1284 pcount = cnt.v_active_count;
1285 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1286 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1288 while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
1290 KASSERT(m->queue == PQ_ACTIVE,
1291 ("vm_pageout_scan: page %p isn't active", m));
1293 next = TAILQ_NEXT(m, pageq);
1294 if ((m->flags & PG_MARKER) != 0) {
1298 KASSERT((m->flags & PG_FICTITIOUS) == 0,
1299 ("Fictitious page %p cannot be in active queue", m));
1300 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1301 ("Unmanaged page %p cannot be in active queue", m));
1302 if (!vm_pageout_page_lock(m, &next)) {
1308 if (!VM_OBJECT_TRYLOCK(object) &&
1309 !vm_pageout_fallback_object_lock(m, &next)) {
1310 VM_OBJECT_UNLOCK(object);
1317 * Don't deactivate pages that are busy.
1319 if ((m->busy != 0) ||
1320 (m->oflags & VPO_BUSY) ||
1321 (m->hold_count != 0)) {
1323 VM_OBJECT_UNLOCK(object);
1330 * The count for pagedaemon pages is done after checking the
1331 * page for eligibility...
1336 * Check to see "how much" the page has been used.
1339 if (object->ref_count != 0) {
1340 if (m->aflags & PGA_REFERENCED) {
1343 actcount += pmap_ts_referenced(m);
1345 m->act_count += ACT_ADVANCE + actcount;
1346 if (m->act_count > ACT_MAX)
1347 m->act_count = ACT_MAX;
1352 * Since we have "tested" this bit, we need to clear it now.
1354 vm_page_aflag_clear(m, PGA_REFERENCED);
1357 * Only if an object is currently being used, do we use the
1358 * page activation count stats.
1360 if (actcount && (object->ref_count != 0)) {
1363 m->act_count -= min(m->act_count, ACT_DECLINE);
1364 if (vm_pageout_algorithm ||
1365 object->ref_count == 0 ||
1366 m->act_count == 0) {
1368 if (object->ref_count == 0) {
1369 KASSERT(!pmap_page_is_mapped(m),
1370 ("vm_pageout_scan: page %p is mapped", m));
1374 vm_page_deactivate(m);
1376 vm_page_deactivate(m);
1383 VM_OBJECT_UNLOCK(object);
1386 vm_page_unlock_queues();
1387 #if !defined(NO_SWAPPING)
1389 * Idle process swapout -- run once per second.
1391 if (vm_swap_idle_enabled) {
1393 if (time_second != lsec) {
1394 vm_req_vmdaemon(VM_SWAP_IDLE);
1401 * If we didn't get enough free pages, and we have skipped a vnode
1402 * in a writeable object, wakeup the sync daemon. And kick swapout
1403 * if we did not get enough free pages.
1405 if (vm_paging_target() > 0) {
1406 if (vnodes_skipped && vm_page_count_min())
1407 (void) speedup_syncer();
1408 #if !defined(NO_SWAPPING)
1409 if (vm_swap_enabled && vm_page_count_target())
1410 vm_req_vmdaemon(VM_SWAP_NORMAL);
1415 * If we are critically low on one of RAM or swap and low on
1416 * the other, kill the largest process. However, we avoid
1417 * doing this on the first pass in order to give ourselves a
1418 * chance to flush out dirty vnode-backed pages and to allow
1419 * active pages to be moved to the inactive queue and reclaimed.
1422 ((swap_pager_avail < 64 && vm_page_count_min()) ||
1423 (swap_pager_full && vm_paging_target() > 0)))
1424 vm_pageout_oom(VM_OOM_MEM);
1429 vm_pageout_oom(int shortage)
1431 struct proc *p, *bigproc;
1432 vm_offset_t size, bigsize;
1437 * We keep the process bigproc locked once we find it to keep anyone
1438 * from messing with it; however, there is a possibility of
1439 * deadlock if process B is bigproc and one of it's child processes
1440 * attempts to propagate a signal to B while we are waiting for A's
1441 * lock while walking this list. To avoid this, we don't block on
1442 * the process lock but just skip a process if it is already locked.
1446 sx_slock(&allproc_lock);
1447 FOREACH_PROC_IN_SYSTEM(p) {
1450 if (PROC_TRYLOCK(p) == 0)
1453 * If this is a system, protected or killed process, skip it.
1455 if (p->p_state != PRS_NORMAL ||
1456 (p->p_flag & (P_INEXEC | P_PROTECTED | P_SYSTEM)) ||
1457 (p->p_pid == 1) || P_KILLED(p) ||
1458 ((p->p_pid < 48) && (swap_pager_avail != 0))) {
1463 * If the process is in a non-running type state,
1464 * don't touch it. Check all the threads individually.
1467 FOREACH_THREAD_IN_PROC(p, td) {
1469 if (!TD_ON_RUNQ(td) &&
1470 !TD_IS_RUNNING(td) &&
1471 !TD_IS_SLEEPING(td) &&
1472 !TD_IS_SUSPENDED(td)) {
1484 * get the process size
1486 vm = vmspace_acquire_ref(p);
1491 if (!vm_map_trylock_read(&vm->vm_map)) {
1496 size = vmspace_swap_count(vm);
1497 vm_map_unlock_read(&vm->vm_map);
1498 if (shortage == VM_OOM_MEM)
1499 size += vmspace_resident_count(vm);
1502 * if the this process is bigger than the biggest one
1505 if (size > bigsize) {
1506 if (bigproc != NULL)
1507 PROC_UNLOCK(bigproc);
1513 sx_sunlock(&allproc_lock);
1514 if (bigproc != NULL) {
1515 killproc(bigproc, "out of swap space");
1516 sched_nice(bigproc, PRIO_MIN);
1517 PROC_UNLOCK(bigproc);
1518 wakeup(&cnt.v_free_count);
1523 * This routine tries to maintain the pseudo LRU active queue,
1524 * so that during long periods of time where there is no paging,
1525 * that some statistic accumulation still occurs. This code
1526 * helps the situation where paging just starts to occur.
1529 vm_pageout_page_stats()
1533 int pcount,tpcount; /* Number of pages to check */
1534 static int fullintervalcount = 0;
1538 (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) -
1539 (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
1541 if (page_shortage <= 0)
1544 vm_page_lock_queues();
1545 pcount = cnt.v_active_count;
1546 fullintervalcount += vm_pageout_stats_interval;
1547 if (fullintervalcount < vm_pageout_full_stats_interval) {
1548 tpcount = (int64_t)vm_pageout_stats_max * cnt.v_active_count /
1550 if (pcount > tpcount)
1553 fullintervalcount = 0;
1556 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1557 while ((m != NULL) && (pcount-- > 0)) {
1560 KASSERT(m->queue == PQ_ACTIVE,
1561 ("vm_pageout_page_stats: page %p isn't active", m));
1563 next = TAILQ_NEXT(m, pageq);
1564 if ((m->flags & PG_MARKER) != 0) {
1568 vm_page_lock_assert(m, MA_NOTOWNED);
1569 if (!vm_pageout_page_lock(m, &next)) {
1575 if (!VM_OBJECT_TRYLOCK(object) &&
1576 !vm_pageout_fallback_object_lock(m, &next)) {
1577 VM_OBJECT_UNLOCK(object);
1584 * Don't deactivate pages that are busy.
1586 if ((m->busy != 0) ||
1587 (m->oflags & VPO_BUSY) ||
1588 (m->hold_count != 0)) {
1590 VM_OBJECT_UNLOCK(object);
1597 if (m->aflags & PGA_REFERENCED) {
1598 vm_page_aflag_clear(m, PGA_REFERENCED);
1602 actcount += pmap_ts_referenced(m);
1604 m->act_count += ACT_ADVANCE + actcount;
1605 if (m->act_count > ACT_MAX)
1606 m->act_count = ACT_MAX;
1609 if (m->act_count == 0) {
1611 * We turn off page access, so that we have
1612 * more accurate RSS stats. We don't do this
1613 * in the normal page deactivation when the
1614 * system is loaded VM wise, because the
1615 * cost of the large number of page protect
1616 * operations would be higher than the value
1617 * of doing the operation.
1620 vm_page_deactivate(m);
1622 m->act_count -= min(m->act_count, ACT_DECLINE);
1627 VM_OBJECT_UNLOCK(object);
1630 vm_page_unlock_queues();
1634 * vm_pageout is the high level pageout daemon.
1642 * Initialize some paging parameters.
1644 cnt.v_interrupt_free_min = 2;
1645 if (cnt.v_page_count < 2000)
1646 vm_pageout_page_count = 8;
1649 * v_free_reserved needs to include enough for the largest
1650 * swap pager structures plus enough for any pv_entry structs
1653 if (cnt.v_page_count > 1024)
1654 cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
1657 cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
1658 cnt.v_interrupt_free_min;
1659 cnt.v_free_reserved = vm_pageout_page_count +
1660 cnt.v_pageout_free_min + (cnt.v_page_count / 768);
1661 cnt.v_free_severe = cnt.v_free_min / 2;
1662 cnt.v_free_min += cnt.v_free_reserved;
1663 cnt.v_free_severe += cnt.v_free_reserved;
1666 * v_free_target and v_cache_min control pageout hysteresis. Note
1667 * that these are more a measure of the VM cache queue hysteresis
1668 * then the VM free queue. Specifically, v_free_target is the
1669 * high water mark (free+cache pages).
1671 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
1672 * low water mark, while v_free_min is the stop. v_cache_min must
1673 * be big enough to handle memory needs while the pageout daemon
1674 * is signalled and run to free more pages.
1676 if (cnt.v_free_count > 6144)
1677 cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved;
1679 cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved;
1681 if (cnt.v_free_count > 2048) {
1682 cnt.v_cache_min = cnt.v_free_target;
1683 cnt.v_cache_max = 2 * cnt.v_cache_min;
1684 cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
1686 cnt.v_cache_min = 0;
1687 cnt.v_cache_max = 0;
1688 cnt.v_inactive_target = cnt.v_free_count / 4;
1690 if (cnt.v_inactive_target > cnt.v_free_count / 3)
1691 cnt.v_inactive_target = cnt.v_free_count / 3;
1693 /* XXX does not really belong here */
1694 if (vm_page_max_wired == 0)
1695 vm_page_max_wired = cnt.v_free_count / 3;
1697 if (vm_pageout_stats_max == 0)
1698 vm_pageout_stats_max = cnt.v_free_target;
1701 * Set interval in seconds for stats scan.
1703 if (vm_pageout_stats_interval == 0)
1704 vm_pageout_stats_interval = 5;
1705 if (vm_pageout_full_stats_interval == 0)
1706 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1708 swap_pager_swap_init();
1711 * The pageout daemon is never done, so loop forever.
1715 * If we have enough free memory, wakeup waiters. Do
1716 * not clear vm_pages_needed until we reach our target,
1717 * otherwise we may be woken up over and over again and
1718 * waste a lot of cpu.
1720 mtx_lock(&vm_page_queue_free_mtx);
1721 if (vm_pages_needed && !vm_page_count_min()) {
1722 if (!vm_paging_needed())
1723 vm_pages_needed = 0;
1724 wakeup(&cnt.v_free_count);
1726 if (vm_pages_needed) {
1728 * Still not done, take a second pass without waiting
1729 * (unlimited dirty cleaning), otherwise sleep a bit
1734 msleep(&vm_pages_needed,
1735 &vm_page_queue_free_mtx, PVM, "psleep",
1739 * Good enough, sleep & handle stats. Prime the pass
1746 error = msleep(&vm_pages_needed,
1747 &vm_page_queue_free_mtx, PVM, "psleep",
1748 vm_pageout_stats_interval * hz);
1749 if (error && !vm_pages_needed) {
1750 mtx_unlock(&vm_page_queue_free_mtx);
1752 vm_pageout_page_stats();
1756 if (vm_pages_needed)
1758 mtx_unlock(&vm_page_queue_free_mtx);
1759 vm_pageout_scan(pass);
1764 * Unless the free page queue lock is held by the caller, this function
1765 * should be regarded as advisory. Specifically, the caller should
1766 * not msleep() on &cnt.v_free_count following this function unless
1767 * the free page queue lock is held until the msleep() is performed.
1773 if (!vm_pages_needed && curthread->td_proc != pageproc) {
1774 vm_pages_needed = 1;
1775 wakeup(&vm_pages_needed);
1779 #if !defined(NO_SWAPPING)
1781 vm_req_vmdaemon(int req)
1783 static int lastrun = 0;
1785 mtx_lock(&vm_daemon_mtx);
1786 vm_pageout_req_swapout |= req;
1787 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
1788 wakeup(&vm_daemon_needed);
1791 mtx_unlock(&vm_daemon_mtx);
1797 struct rlimit rsslim;
1801 int breakout, swapout_flags, tryagain, attempts;
1803 uint64_t rsize, ravailable;
1807 mtx_lock(&vm_daemon_mtx);
1809 msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", hz);
1811 msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0);
1813 swapout_flags = vm_pageout_req_swapout;
1814 vm_pageout_req_swapout = 0;
1815 mtx_unlock(&vm_daemon_mtx);
1817 swapout_procs(swapout_flags);
1820 * scan the processes for exceeding their rlimits or if
1821 * process is swapped out -- deactivate pages
1827 sx_slock(&allproc_lock);
1828 FOREACH_PROC_IN_SYSTEM(p) {
1829 vm_pindex_t limit, size;
1832 * if this is a system process or if we have already
1833 * looked at this process, skip it.
1836 if (p->p_state != PRS_NORMAL ||
1837 p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) {
1842 * if the process is in a non-running type state,
1846 FOREACH_THREAD_IN_PROC(p, td) {
1848 if (!TD_ON_RUNQ(td) &&
1849 !TD_IS_RUNNING(td) &&
1850 !TD_IS_SLEEPING(td) &&
1851 !TD_IS_SUSPENDED(td)) {
1865 lim_rlimit(p, RLIMIT_RSS, &rsslim);
1867 qmin(rsslim.rlim_cur, rsslim.rlim_max));
1870 * let processes that are swapped out really be
1871 * swapped out set the limit to nothing (will force a
1874 if ((p->p_flag & P_INMEM) == 0)
1875 limit = 0; /* XXX */
1876 vm = vmspace_acquire_ref(p);
1881 size = vmspace_resident_count(vm);
1882 if (limit >= 0 && size >= limit) {
1883 vm_pageout_map_deactivate_pages(
1884 &vm->vm_map, limit);
1887 rsize = IDX_TO_OFF(size);
1889 racct_set(p, RACCT_RSS, rsize);
1890 ravailable = racct_get_available(p, RACCT_RSS);
1892 if (rsize > ravailable) {
1894 * Don't be overly aggressive; this might be
1895 * an innocent process, and the limit could've
1896 * been exceeded by some memory hog. Don't
1897 * try to deactivate more than 1/4th of process'
1898 * resident set size.
1900 if (attempts <= 8) {
1901 if (ravailable < rsize - (rsize / 4))
1902 ravailable = rsize - (rsize / 4);
1904 vm_pageout_map_deactivate_pages(
1905 &vm->vm_map, OFF_TO_IDX(ravailable));
1906 /* Update RSS usage after paging out. */
1907 size = vmspace_resident_count(vm);
1908 rsize = IDX_TO_OFF(size);
1910 racct_set(p, RACCT_RSS, rsize);
1912 if (rsize > ravailable)
1918 sx_sunlock(&allproc_lock);
1919 if (tryagain != 0 && attempts <= 10)
1923 #endif /* !defined(NO_SWAPPING) */