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;
155 static int vm_pageout_stats_interval;
156 static int vm_pageout_full_stats_interval;
157 static int vm_pageout_algorithm;
158 static int defer_swap_pageouts;
159 static int disable_swap_pageouts;
161 #if defined(NO_SWAPPING)
162 static int vm_swap_enabled = 0;
163 static int vm_swap_idle_enabled = 0;
165 static int vm_swap_enabled = 1;
166 static int vm_swap_idle_enabled = 0;
169 SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
170 CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
172 SYSCTL_INT(_vm, OID_AUTO, max_launder,
173 CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
175 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
176 CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
178 SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
179 CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
181 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
182 CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
184 #if defined(NO_SWAPPING)
185 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
186 CTLFLAG_RD, &vm_swap_enabled, 0, "Enable entire process swapout");
187 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
188 CTLFLAG_RD, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
190 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
191 CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
192 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
193 CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
196 SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
197 CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
199 SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
200 CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
202 static int pageout_lock_miss;
203 SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
204 CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
206 #define VM_PAGEOUT_PAGE_COUNT 16
207 int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
209 int vm_page_max_wired; /* XXX max # of wired pages system-wide */
210 SYSCTL_INT(_vm, OID_AUTO, max_wired,
211 CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count");
213 static boolean_t vm_pageout_fallback_object_lock(vm_page_t, vm_page_t *);
214 static boolean_t vm_pageout_launder(int, int, vm_paddr_t, vm_paddr_t);
215 #if !defined(NO_SWAPPING)
216 static void vm_pageout_map_deactivate_pages(vm_map_t, long);
217 static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long);
218 static void vm_req_vmdaemon(int req);
220 static boolean_t vm_pageout_page_lock(vm_page_t, vm_page_t *);
221 static void vm_pageout_page_stats(void);
224 * Initialize a dummy page for marking the caller's place in the specified
225 * paging queue. In principle, this function only needs to set the flag
226 * PG_MARKER. Nonetheless, it sets the flag VPO_BUSY and initializes the hold
227 * count to one as safety precautions.
230 vm_pageout_init_marker(vm_page_t marker, u_short queue)
233 bzero(marker, sizeof(*marker));
234 marker->flags = PG_MARKER;
235 marker->oflags = VPO_BUSY;
236 marker->queue = queue;
237 marker->hold_count = 1;
241 * vm_pageout_fallback_object_lock:
243 * Lock vm object currently associated with `m'. VM_OBJECT_TRYLOCK is
244 * known to have failed and page queue must be either PQ_ACTIVE or
245 * PQ_INACTIVE. To avoid lock order violation, unlock the page queues
246 * while locking the vm object. Use marker page to detect page queue
247 * changes and maintain notion of next page on page queue. Return
248 * TRUE if no changes were detected, FALSE otherwise. vm object is
251 * This function depends on both the lock portion of struct vm_object
252 * and normal struct vm_page being type stable.
255 vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next)
257 struct vm_page marker;
258 struct vm_pagequeue *pq;
264 vm_pageout_init_marker(&marker, queue);
265 pq = &vm_pagequeues[queue];
268 TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, pageq);
269 vm_pagequeue_unlock(pq);
271 VM_OBJECT_LOCK(object);
273 vm_pagequeue_lock(pq);
275 /* Page queue might have changed. */
276 *next = TAILQ_NEXT(&marker, pageq);
277 unchanged = (m->queue == queue &&
278 m->object == object &&
279 &marker == TAILQ_NEXT(m, pageq));
280 TAILQ_REMOVE(&pq->pq_pl, &marker, pageq);
285 * Lock the page while holding the page queue lock. Use marker page
286 * to detect page queue changes and maintain notion of next page on
287 * page queue. Return TRUE if no changes were detected, FALSE
288 * otherwise. The page is locked on return. The page queue lock might
289 * be dropped and reacquired.
291 * This function depends on normal struct vm_page being type stable.
294 vm_pageout_page_lock(vm_page_t m, vm_page_t *next)
296 struct vm_page marker;
297 struct vm_pagequeue *pq;
301 vm_page_lock_assert(m, MA_NOTOWNED);
302 if (vm_page_trylock(m))
306 vm_pageout_init_marker(&marker, queue);
307 pq = &vm_pagequeues[queue];
309 TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, pageq);
310 vm_pagequeue_unlock(pq);
312 vm_pagequeue_lock(pq);
314 /* Page queue might have changed. */
315 *next = TAILQ_NEXT(&marker, pageq);
316 unchanged = (m->queue == queue && &marker == TAILQ_NEXT(m, pageq));
317 TAILQ_REMOVE(&pq->pq_pl, &marker, pageq);
324 * Clean the page and remove it from the laundry.
326 * We set the busy bit to cause potential page faults on this page to
327 * block. Note the careful timing, however, the busy bit isn't set till
328 * late and we cannot do anything that will mess with the page.
331 vm_pageout_clean(vm_page_t m)
334 vm_page_t mc[2*vm_pageout_page_count], pb, ps;
336 int ib, is, page_base;
337 vm_pindex_t pindex = m->pindex;
339 vm_page_lock_assert(m, MA_OWNED);
341 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
344 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
345 * with the new swapper, but we could have serious problems paging
346 * out other object types if there is insufficient memory.
348 * Unfortunately, checking free memory here is far too late, so the
349 * check has been moved up a procedural level.
353 * Can't clean the page if it's busy or held.
355 KASSERT(m->busy == 0 && (m->oflags & VPO_BUSY) == 0,
356 ("vm_pageout_clean: page %p is busy", m));
357 KASSERT(m->hold_count == 0, ("vm_pageout_clean: page %p is held", m));
360 mc[vm_pageout_page_count] = pb = ps = m;
362 page_base = vm_pageout_page_count;
367 * Scan object for clusterable pages.
369 * We can cluster ONLY if: ->> the page is NOT
370 * clean, wired, busy, held, or mapped into a
371 * buffer, and one of the following:
372 * 1) The page is inactive, or a seldom used
375 * 2) we force the issue.
377 * During heavy mmap/modification loads the pageout
378 * daemon can really fragment the underlying file
379 * due to flushing pages out of order and not trying
380 * align the clusters (which leave sporatic out-of-order
381 * holes). To solve this problem we do the reverse scan
382 * first and attempt to align our cluster, then do a
383 * forward scan if room remains.
386 while (ib && pageout_count < vm_pageout_page_count) {
394 if ((p = vm_page_prev(pb)) == NULL ||
395 (p->oflags & VPO_BUSY) != 0 || p->busy != 0) {
400 vm_page_test_dirty(p);
402 p->queue != PQ_INACTIVE ||
403 p->hold_count != 0) { /* may be undergoing I/O */
409 mc[--page_base] = pb = p;
413 * alignment boundry, stop here and switch directions. Do
416 if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
420 while (pageout_count < vm_pageout_page_count &&
421 pindex + is < object->size) {
424 if ((p = vm_page_next(ps)) == NULL ||
425 (p->oflags & VPO_BUSY) != 0 || p->busy != 0)
428 vm_page_test_dirty(p);
430 p->queue != PQ_INACTIVE ||
431 p->hold_count != 0) { /* may be undergoing I/O */
436 mc[page_base + pageout_count] = ps = p;
442 * If we exhausted our forward scan, continue with the reverse scan
443 * when possible, even past a page boundry. This catches boundry
446 if (ib && pageout_count < vm_pageout_page_count)
450 * we allow reads during pageouts...
452 return (vm_pageout_flush(&mc[page_base], pageout_count, 0, 0, NULL,
457 * vm_pageout_flush() - launder the given pages
459 * The given pages are laundered. Note that we setup for the start of
460 * I/O ( i.e. busy the page ), mark it read-only, and bump the object
461 * reference count all in here rather then in the parent. If we want
462 * the parent to do more sophisticated things we may have to change
465 * Returned runlen is the count of pages between mreq and first
466 * page after mreq with status VM_PAGER_AGAIN.
467 * *eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL
468 * for any page in runlen set.
471 vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen,
474 vm_object_t object = mc[0]->object;
475 int pageout_status[count];
479 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
482 * Initiate I/O. Bump the vm_page_t->busy counter and
483 * mark the pages read-only.
485 * We do not have to fixup the clean/dirty bits here... we can
486 * allow the pager to do it after the I/O completes.
488 * NOTE! mc[i]->dirty may be partial or fragmented due to an
489 * edge case with file fragments.
491 for (i = 0; i < count; i++) {
492 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL,
493 ("vm_pageout_flush: partially invalid page %p index %d/%d",
495 vm_page_io_start(mc[i]);
496 pmap_remove_write(mc[i]);
498 vm_object_pip_add(object, count);
500 vm_pager_put_pages(object, mc, count, flags, pageout_status);
502 runlen = count - mreq;
505 for (i = 0; i < count; i++) {
506 vm_page_t mt = mc[i];
508 KASSERT(pageout_status[i] == VM_PAGER_PEND ||
509 !pmap_page_is_write_mapped(mt),
510 ("vm_pageout_flush: page %p is not write protected", mt));
511 switch (pageout_status[i]) {
518 * Page outside of range of object. Right now we
519 * essentially lose the changes by pretending it
527 * If page couldn't be paged out, then reactivate the
528 * page so it doesn't clog the inactive list. (We
529 * will try paging out it again later).
532 vm_page_activate(mt);
534 if (eio != NULL && i >= mreq && i - mreq < runlen)
538 if (i >= mreq && i - mreq < runlen)
544 * If the operation is still going, leave the page busy to
545 * block all other accesses. Also, leave the paging in
546 * progress indicator set so that we don't attempt an object
549 if (pageout_status[i] != VM_PAGER_PEND) {
550 vm_object_pip_wakeup(object);
551 vm_page_io_finish(mt);
552 if (vm_page_count_severe()) {
554 vm_page_try_to_cache(mt);
561 return (numpagedout);
565 vm_pageout_launder(int queue, int tries, vm_paddr_t low, vm_paddr_t high)
568 struct vm_pagequeue *pq;
572 vm_page_t m, m_tmp, next;
574 pq = &vm_pagequeues[queue];
575 vm_pagequeue_lock(pq);
576 TAILQ_FOREACH_SAFE(m, &pq->pq_pl, pageq, next) {
577 KASSERT(m->queue == queue,
578 ("vm_pageout_launder: page %p's queue is not %d", m,
580 if ((m->flags & PG_MARKER) != 0)
582 pa = VM_PAGE_TO_PHYS(m);
583 if (pa < low || pa + PAGE_SIZE > high)
585 if (!vm_pageout_page_lock(m, &next) || m->hold_count != 0) {
590 if ((!VM_OBJECT_TRYLOCK(object) &&
591 (!vm_pageout_fallback_object_lock(m, &next) ||
592 m->hold_count != 0)) || (m->oflags & VPO_BUSY) != 0 ||
595 VM_OBJECT_UNLOCK(object);
598 vm_page_test_dirty(m);
599 if (m->dirty == 0 && object->ref_count != 0)
603 if (tries == 0 || (object->flags & OBJ_DEAD) != 0) {
604 VM_OBJECT_UNLOCK(object);
607 if (object->type == OBJT_VNODE) {
608 vm_pagequeue_unlock(pq);
610 vm_object_reference_locked(object);
611 VM_OBJECT_UNLOCK(object);
612 (void)vn_start_write(vp, &mp, V_WAIT);
613 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
614 VM_OBJECT_LOCK(object);
615 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
616 VM_OBJECT_UNLOCK(object);
618 vm_object_deallocate(object);
619 vn_finished_write(mp);
621 } else if (object->type == OBJT_SWAP ||
622 object->type == OBJT_DEFAULT) {
623 vm_pagequeue_unlock(pq);
625 vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC,
627 VM_OBJECT_UNLOCK(object);
632 * Dequeue here to prevent lock recursion in
635 vm_page_dequeue_locked(m);
639 VM_OBJECT_UNLOCK(object);
641 vm_pagequeue_unlock(pq);
646 * Increase the number of cached pages. The specified value, "tries",
647 * determines which categories of pages are cached:
649 * 0: All clean, inactive pages within the specified physical address range
650 * are cached. Will not sleep.
651 * 1: The vm_lowmem handlers are called. All inactive pages within
652 * the specified physical address range are cached. May sleep.
653 * 2: The vm_lowmem handlers are called. All inactive and active pages
654 * within the specified physical address range are cached. May sleep.
657 vm_pageout_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high)
659 int actl, actmax, inactl, inactmax;
663 * Decrease registered cache sizes. The vm_lowmem handlers
664 * may acquire locks and/or sleep, so they can only be invoked
665 * when "tries" is greater than zero.
667 EVENTHANDLER_INVOKE(vm_lowmem, 0);
670 * We do this explicitly after the caches have been drained
676 inactmax = cnt.v_inactive_count;
678 actmax = tries < 2 ? 0 : cnt.v_active_count;
680 if (inactl < inactmax && vm_pageout_launder(PQ_INACTIVE, tries, low,
685 if (actl < actmax && vm_pageout_launder(PQ_ACTIVE, tries, low, high)) {
691 #if !defined(NO_SWAPPING)
693 * vm_pageout_object_deactivate_pages
695 * Deactivate enough pages to satisfy the inactive target
698 * The object and map must be locked.
701 vm_pageout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object,
704 vm_object_t backing_object, object;
706 int actcount, remove_mode;
708 VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
709 if ((first_object->flags & OBJ_FICTITIOUS) != 0)
711 for (object = first_object;; object = backing_object) {
712 if (pmap_resident_count(pmap) <= desired)
714 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
715 if ((object->flags & OBJ_UNMANAGED) != 0 ||
716 object->paging_in_progress != 0)
720 if (object->shadow_count > 1)
723 * Scan the object's entire memory queue.
725 TAILQ_FOREACH(p, &object->memq, listq) {
726 if (pmap_resident_count(pmap) <= desired)
728 if ((p->oflags & VPO_BUSY) != 0 || p->busy != 0)
730 PCPU_INC(cnt.v_pdpages);
732 if (p->wire_count != 0 || p->hold_count != 0 ||
733 !pmap_page_exists_quick(pmap, p)) {
737 actcount = pmap_ts_referenced(p);
738 if ((p->aflags & PGA_REFERENCED) != 0) {
741 vm_page_aflag_clear(p, PGA_REFERENCED);
743 if (p->queue != PQ_ACTIVE && actcount != 0) {
745 p->act_count += actcount;
746 } else if (p->queue == PQ_ACTIVE) {
748 p->act_count -= min(p->act_count,
751 (vm_pageout_algorithm ||
752 p->act_count == 0)) {
754 vm_page_deactivate(p);
759 if (p->act_count < ACT_MAX -
761 p->act_count += ACT_ADVANCE;
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 struct vm_pagequeue *pq;
866 int page_shortage, maxscan, pcount;
867 int addl_page_shortage;
870 int vnodes_skipped = 0;
872 boolean_t queues_locked;
874 vm_pageout_init_marker(&marker, PQ_INACTIVE);
877 * Decrease registered cache sizes.
879 EVENTHANDLER_INVOKE(vm_lowmem, 0);
881 * We do this explicitly after the caches have been drained above.
886 * The addl_page_shortage is the number of temporarily
887 * stuck pages in the inactive queue. In other words, the
888 * number of pages from cnt.v_inactive_count that should be
889 * discounted in setting the target for the active queue scan.
891 addl_page_shortage = atomic_readandclear_int(&vm_pageout_deficit);
894 * Calculate the number of pages we want to either free or move
897 page_shortage = vm_paging_target() + addl_page_shortage;
900 * maxlaunder limits the number of dirty pages we flush per scan.
901 * For most systems a smaller value (16 or 32) is more robust under
902 * extreme memory and disk pressure because any unnecessary writes
903 * to disk can result in extreme performance degredation. However,
904 * systems with excessive dirty pages (especially when MAP_NOSYNC is
905 * used) will die horribly with limited laundering. If the pageout
906 * daemon cannot clean enough pages in the first pass, we let it go
907 * all out in succeeding passes.
909 if ((maxlaunder = vm_max_launder) <= 1)
914 maxscan = cnt.v_inactive_count;
917 * Start scanning the inactive queue for pages we can move to the
918 * cache or free. The scan will stop when the target is reached or
919 * we have scanned the entire inactive queue. Note that m->act_count
920 * is not used to form decisions for the inactive queue, only for the
923 pq = &vm_pagequeues[PQ_INACTIVE];
924 vm_pagequeue_lock(pq);
925 queues_locked = TRUE;
926 for (m = TAILQ_FIRST(&pq->pq_pl);
927 m != NULL && maxscan-- > 0 && page_shortage > 0;
929 vm_pagequeue_assert_locked(pq);
930 KASSERT(queues_locked, ("unlocked queues"));
931 KASSERT(m->queue == PQ_INACTIVE, ("Inactive queue %p", m));
933 PCPU_INC(cnt.v_pdpages);
934 next = TAILQ_NEXT(m, pageq);
939 if (m->flags & PG_MARKER)
942 KASSERT((m->flags & PG_FICTITIOUS) == 0,
943 ("Fictitious page %p cannot be in inactive queue", m));
944 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
945 ("Unmanaged page %p cannot be in inactive queue", m));
948 * The page or object lock acquisitions fail if the
949 * page was removed from the queue or moved to a
950 * different position within the queue. In either
951 * case, addl_page_shortage should not be incremented.
953 if (!vm_pageout_page_lock(m, &next)) {
958 if (!VM_OBJECT_TRYLOCK(object) &&
959 !vm_pageout_fallback_object_lock(m, &next)) {
961 VM_OBJECT_UNLOCK(object);
966 * Don't mess with busy pages, keep them at at the
967 * front of the queue, most likely they are being
968 * paged out. Increment addl_page_shortage for busy
969 * pages, because they may leave the inactive queue
970 * shortly after page scan is finished.
972 if (m->busy != 0 || (m->oflags & VPO_BUSY) != 0) {
974 VM_OBJECT_UNLOCK(object);
975 addl_page_shortage++;
980 * We unlock the inactive page queue, invalidating the
981 * 'next' pointer. Use our marker to remember our
984 TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, pageq);
985 vm_pagequeue_unlock(pq);
986 queues_locked = FALSE;
989 * If the object is not being used, we ignore previous
992 if (object->ref_count == 0) {
993 vm_page_aflag_clear(m, PGA_REFERENCED);
994 KASSERT(!pmap_page_is_mapped(m),
995 ("vm_pageout_scan: page %p is mapped", m));
998 * Otherwise, if the page has been referenced while in the
999 * inactive queue, we bump the "activation count" upwards,
1000 * making it less likely that the page will be added back to
1001 * the inactive queue prematurely again. Here we check the
1002 * page tables (or emulated bits, if any), given the upper
1003 * level VM system not knowing anything about existing
1006 } else if ((m->aflags & PGA_REFERENCED) == 0 &&
1007 (actcount = pmap_ts_referenced(m)) != 0) {
1008 vm_page_activate(m);
1010 m->act_count += actcount + ACT_ADVANCE;
1011 VM_OBJECT_UNLOCK(object);
1016 * If the upper level VM system knows about any page
1017 * references, we activate the page. We also set the
1018 * "activation count" higher than normal so that we will less
1019 * likely place pages back onto the inactive queue again.
1021 if ((m->aflags & PGA_REFERENCED) != 0) {
1022 vm_page_aflag_clear(m, PGA_REFERENCED);
1023 actcount = pmap_ts_referenced(m);
1024 vm_page_activate(m);
1026 m->act_count += actcount + ACT_ADVANCE + 1;
1027 VM_OBJECT_UNLOCK(object);
1031 if (m->hold_count != 0) {
1033 VM_OBJECT_UNLOCK(object);
1036 * Held pages are essentially stuck in the
1037 * queue. So, they ought to be discounted
1038 * from cnt.v_inactive_count. See the
1039 * calculation of the page_shortage for the
1040 * loop over the active queue below.
1042 addl_page_shortage++;
1047 * If the page appears to be clean at the machine-independent
1048 * layer, then remove all of its mappings from the pmap in
1049 * anticipation of placing it onto the cache queue. If,
1050 * however, any of the page's mappings allow write access,
1051 * then the page may still be modified until the last of those
1052 * mappings are removed.
1054 vm_page_test_dirty(m);
1055 if (m->dirty == 0 && object->ref_count != 0)
1058 if (m->valid == 0) {
1060 * Invalid pages can be easily freed
1063 PCPU_INC(cnt.v_dfree);
1065 } else if (m->dirty == 0) {
1067 * Clean pages can be placed onto the cache queue.
1068 * This effectively frees them.
1072 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
1074 * Dirty pages need to be paged out, but flushing
1075 * a page is extremely expensive verses freeing
1076 * a clean page. Rather then artificially limiting
1077 * the number of pages we can flush, we instead give
1078 * dirty pages extra priority on the inactive queue
1079 * by forcing them to be cycled through the queue
1080 * twice before being flushed, after which the
1081 * (now clean) page will cycle through once more
1082 * before being freed. This significantly extends
1083 * the thrash point for a heavily loaded machine.
1085 m->flags |= PG_WINATCFLS;
1086 vm_pagequeue_lock(pq);
1087 queues_locked = TRUE;
1088 vm_page_requeue_locked(m);
1089 } else if (maxlaunder > 0) {
1091 * We always want to try to flush some dirty pages if
1092 * we encounter them, to keep the system stable.
1093 * Normally this number is small, but under extreme
1094 * pressure where there are insufficient clean pages
1095 * on the inactive queue, we may have to go all out.
1097 int swap_pageouts_ok;
1098 struct vnode *vp = NULL;
1099 struct mount *mp = NULL;
1101 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
1102 swap_pageouts_ok = 1;
1104 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
1105 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
1106 vm_page_count_min());
1111 * We don't bother paging objects that are "dead".
1112 * Those objects are in a "rundown" state.
1114 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
1115 vm_pagequeue_lock(pq);
1117 VM_OBJECT_UNLOCK(object);
1118 queues_locked = TRUE;
1119 vm_page_requeue_locked(m);
1124 * The object is already known NOT to be dead. It
1125 * is possible for the vget() to block the whole
1126 * pageout daemon, but the new low-memory handling
1127 * code should prevent it.
1129 * The previous code skipped locked vnodes and, worse,
1130 * reordered pages in the queue. This results in
1131 * completely non-deterministic operation and, on a
1132 * busy system, can lead to extremely non-optimal
1133 * pageouts. For example, it can cause clean pages
1134 * to be freed and dirty pages to be moved to the end
1135 * of the queue. Since dirty pages are also moved to
1136 * the end of the queue once-cleaned, this gives
1137 * way too large a weighting to defering the freeing
1140 * We can't wait forever for the vnode lock, we might
1141 * deadlock due to a vn_read() getting stuck in
1142 * vm_wait while holding this vnode. We skip the
1143 * vnode if we can't get it in a reasonable amount
1146 if (object->type == OBJT_VNODE) {
1148 vp = object->handle;
1149 if (vp->v_type == VREG &&
1150 vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1152 ++pageout_lock_miss;
1153 if (object->flags & OBJ_MIGHTBEDIRTY)
1155 goto unlock_and_continue;
1158 ("vp %p with NULL v_mount", vp));
1159 vm_object_reference_locked(object);
1160 VM_OBJECT_UNLOCK(object);
1161 if (vget(vp, LK_EXCLUSIVE | LK_TIMELOCK,
1163 VM_OBJECT_LOCK(object);
1164 ++pageout_lock_miss;
1165 if (object->flags & OBJ_MIGHTBEDIRTY)
1168 goto unlock_and_continue;
1170 VM_OBJECT_LOCK(object);
1172 vm_pagequeue_lock(pq);
1173 queues_locked = TRUE;
1175 * The page might have been moved to another
1176 * queue during potential blocking in vget()
1177 * above. The page might have been freed and
1178 * reused for another vnode.
1180 if (m->queue != PQ_INACTIVE ||
1181 m->object != object ||
1182 TAILQ_NEXT(m, pageq) != &marker) {
1184 if (object->flags & OBJ_MIGHTBEDIRTY)
1186 goto unlock_and_continue;
1190 * The page may have been busied during the
1191 * blocking in vget(). We don't move the
1192 * page back onto the end of the queue so that
1193 * statistics are more correct if we don't.
1195 if (m->busy || (m->oflags & VPO_BUSY)) {
1197 goto unlock_and_continue;
1201 * If the page has become held it might
1202 * be undergoing I/O, so skip it
1204 if (m->hold_count) {
1206 vm_page_requeue_locked(m);
1207 if (object->flags & OBJ_MIGHTBEDIRTY)
1209 goto unlock_and_continue;
1211 vm_pagequeue_unlock(pq);
1212 queues_locked = FALSE;
1216 * If a page is dirty, then it is either being washed
1217 * (but not yet cleaned) or it is still in the
1218 * laundry. If it is still in the laundry, then we
1219 * start the cleaning operation.
1221 * decrement page_shortage on success to account for
1222 * the (future) cleaned page. Otherwise we could wind
1223 * up laundering or cleaning too many pages.
1225 if (vm_pageout_clean(m) != 0) {
1229 unlock_and_continue:
1230 vm_page_lock_assert(m, MA_NOTOWNED);
1231 VM_OBJECT_UNLOCK(object);
1233 if (queues_locked) {
1234 vm_pagequeue_unlock(pq);
1235 queues_locked = FALSE;
1239 vm_object_deallocate(object);
1240 vn_finished_write(mp);
1242 vm_page_lock_assert(m, MA_NOTOWNED);
1246 VM_OBJECT_UNLOCK(object);
1248 if (!queues_locked) {
1249 vm_pagequeue_lock(pq);
1250 queues_locked = TRUE;
1252 next = TAILQ_NEXT(&marker, pageq);
1253 TAILQ_REMOVE(&pq->pq_pl, &marker, pageq);
1255 vm_pagequeue_unlock(pq);
1258 * Compute the number of pages we want to try to move from the
1259 * active queue to the inactive queue.
1261 page_shortage = vm_paging_target() +
1262 cnt.v_inactive_target - cnt.v_inactive_count;
1263 page_shortage += addl_page_shortage;
1266 * Scan the active queue for things we can deactivate. We nominally
1267 * track the per-page activity counter and use it to locate
1268 * deactivation candidates.
1270 pcount = cnt.v_active_count;
1271 pq = &vm_pagequeues[PQ_ACTIVE];
1272 vm_pagequeue_lock(pq);
1273 m = TAILQ_FIRST(&pq->pq_pl);
1274 while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
1276 KASSERT(m->queue == PQ_ACTIVE,
1277 ("vm_pageout_scan: page %p isn't active", m));
1279 next = TAILQ_NEXT(m, pageq);
1280 if ((m->flags & PG_MARKER) != 0) {
1284 KASSERT((m->flags & PG_FICTITIOUS) == 0,
1285 ("Fictitious page %p cannot be in active queue", m));
1286 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1287 ("Unmanaged page %p cannot be in active queue", m));
1288 if (!vm_pageout_page_lock(m, &next)) {
1294 if (!VM_OBJECT_TRYLOCK(object) &&
1295 !vm_pageout_fallback_object_lock(m, &next)) {
1296 VM_OBJECT_UNLOCK(object);
1303 * Don't deactivate pages that are busy.
1305 if ((m->busy != 0) ||
1306 (m->oflags & VPO_BUSY) ||
1307 (m->hold_count != 0)) {
1309 VM_OBJECT_UNLOCK(object);
1310 vm_page_requeue_locked(m);
1316 * The count for pagedaemon pages is done after checking the
1317 * page for eligibility...
1319 PCPU_INC(cnt.v_pdpages);
1322 * Check to see "how much" the page has been used.
1325 if (object->ref_count != 0) {
1326 if (m->aflags & PGA_REFERENCED) {
1329 actcount += pmap_ts_referenced(m);
1331 m->act_count += ACT_ADVANCE + actcount;
1332 if (m->act_count > ACT_MAX)
1333 m->act_count = ACT_MAX;
1338 * Since we have "tested" this bit, we need to clear it now.
1340 vm_page_aflag_clear(m, PGA_REFERENCED);
1343 * Only if an object is currently being used, do we use the
1344 * page activation count stats.
1346 if (actcount != 0 && object->ref_count != 0)
1347 vm_page_requeue_locked(m);
1349 m->act_count -= min(m->act_count, ACT_DECLINE);
1350 if (vm_pageout_algorithm ||
1351 object->ref_count == 0 ||
1352 m->act_count == 0) {
1354 /* Dequeue to avoid later lock recursion. */
1355 vm_page_dequeue_locked(m);
1356 if (object->ref_count == 0) {
1357 KASSERT(!pmap_page_is_mapped(m),
1358 ("vm_pageout_scan: page %p is mapped", m));
1362 vm_page_deactivate(m);
1364 vm_page_deactivate(m);
1367 vm_page_requeue_locked(m);
1370 VM_OBJECT_UNLOCK(object);
1373 vm_pagequeue_unlock(pq);
1374 #if !defined(NO_SWAPPING)
1376 * Idle process swapout -- run once per second.
1378 if (vm_swap_idle_enabled) {
1380 if (time_second != lsec) {
1381 vm_req_vmdaemon(VM_SWAP_IDLE);
1388 * If we didn't get enough free pages, and we have skipped a vnode
1389 * in a writeable object, wakeup the sync daemon. And kick swapout
1390 * if we did not get enough free pages.
1392 if (vm_paging_target() > 0) {
1393 if (vnodes_skipped && vm_page_count_min())
1394 (void) speedup_syncer();
1395 #if !defined(NO_SWAPPING)
1396 if (vm_swap_enabled && vm_page_count_target())
1397 vm_req_vmdaemon(VM_SWAP_NORMAL);
1402 * If we are critically low on one of RAM or swap and low on
1403 * the other, kill the largest process. However, we avoid
1404 * doing this on the first pass in order to give ourselves a
1405 * chance to flush out dirty vnode-backed pages and to allow
1406 * active pages to be moved to the inactive queue and reclaimed.
1409 ((swap_pager_avail < 64 && vm_page_count_min()) ||
1410 (swap_pager_full && vm_paging_target() > 0)))
1411 vm_pageout_oom(VM_OOM_MEM);
1416 vm_pageout_oom(int shortage)
1418 struct proc *p, *bigproc;
1419 vm_offset_t size, bigsize;
1424 * We keep the process bigproc locked once we find it to keep anyone
1425 * from messing with it; however, there is a possibility of
1426 * deadlock if process B is bigproc and one of it's child processes
1427 * attempts to propagate a signal to B while we are waiting for A's
1428 * lock while walking this list. To avoid this, we don't block on
1429 * the process lock but just skip a process if it is already locked.
1433 sx_slock(&allproc_lock);
1434 FOREACH_PROC_IN_SYSTEM(p) {
1437 if (PROC_TRYLOCK(p) == 0)
1440 * If this is a system, protected or killed process, skip it.
1442 if (p->p_state != PRS_NORMAL ||
1443 (p->p_flag & (P_INEXEC | P_PROTECTED | P_SYSTEM)) ||
1444 (p->p_pid == 1) || P_KILLED(p) ||
1445 ((p->p_pid < 48) && (swap_pager_avail != 0))) {
1450 * If the process is in a non-running type state,
1451 * don't touch it. Check all the threads individually.
1454 FOREACH_THREAD_IN_PROC(p, td) {
1456 if (!TD_ON_RUNQ(td) &&
1457 !TD_IS_RUNNING(td) &&
1458 !TD_IS_SLEEPING(td) &&
1459 !TD_IS_SUSPENDED(td)) {
1471 * get the process size
1473 vm = vmspace_acquire_ref(p);
1478 if (!vm_map_trylock_read(&vm->vm_map)) {
1483 size = vmspace_swap_count(vm);
1484 vm_map_unlock_read(&vm->vm_map);
1485 if (shortage == VM_OOM_MEM)
1486 size += vmspace_resident_count(vm);
1489 * if the this process is bigger than the biggest one
1492 if (size > bigsize) {
1493 if (bigproc != NULL)
1494 PROC_UNLOCK(bigproc);
1500 sx_sunlock(&allproc_lock);
1501 if (bigproc != NULL) {
1502 killproc(bigproc, "out of swap space");
1503 sched_nice(bigproc, PRIO_MIN);
1504 PROC_UNLOCK(bigproc);
1505 wakeup(&cnt.v_free_count);
1510 * This routine tries to maintain the pseudo LRU active queue,
1511 * so that during long periods of time where there is no paging,
1512 * that some statistic accumulation still occurs. This code
1513 * helps the situation where paging just starts to occur.
1516 vm_pageout_page_stats(void)
1518 struct vm_pagequeue *pq;
1521 int pcount, tpcount; /* Number of pages to check */
1522 static int fullintervalcount = 0;
1526 (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) -
1527 (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
1529 if (page_shortage <= 0)
1532 pcount = cnt.v_active_count;
1533 fullintervalcount += vm_pageout_stats_interval;
1534 if (fullintervalcount < vm_pageout_full_stats_interval) {
1535 tpcount = (int64_t)vm_pageout_stats_max * cnt.v_active_count /
1537 if (pcount > tpcount)
1540 fullintervalcount = 0;
1543 pq = &vm_pagequeues[PQ_ACTIVE];
1544 vm_pagequeue_lock(pq);
1545 m = TAILQ_FIRST(&pq->pq_pl);
1546 while ((m != NULL) && (pcount-- > 0)) {
1549 KASSERT(m->queue == PQ_ACTIVE,
1550 ("vm_pageout_page_stats: page %p isn't active", m));
1552 next = TAILQ_NEXT(m, pageq);
1553 if ((m->flags & PG_MARKER) != 0) {
1557 vm_page_lock_assert(m, MA_NOTOWNED);
1558 if (!vm_pageout_page_lock(m, &next)) {
1564 if (!VM_OBJECT_TRYLOCK(object) &&
1565 !vm_pageout_fallback_object_lock(m, &next)) {
1566 VM_OBJECT_UNLOCK(object);
1573 * Don't deactivate pages that are busy.
1575 if ((m->busy != 0) ||
1576 (m->oflags & VPO_BUSY) ||
1577 (m->hold_count != 0)) {
1579 VM_OBJECT_UNLOCK(object);
1580 vm_page_requeue_locked(m);
1586 if (m->aflags & PGA_REFERENCED) {
1587 vm_page_aflag_clear(m, PGA_REFERENCED);
1591 actcount += pmap_ts_referenced(m);
1593 m->act_count += ACT_ADVANCE + actcount;
1594 if (m->act_count > ACT_MAX)
1595 m->act_count = ACT_MAX;
1596 vm_page_requeue_locked(m);
1598 if (m->act_count == 0) {
1600 * We turn off page access, so that we have
1601 * more accurate RSS stats. We don't do this
1602 * in the normal page deactivation when the
1603 * system is loaded VM wise, because the
1604 * cost of the large number of page protect
1605 * operations would be higher than the value
1606 * of doing the operation.
1609 /* Dequeue to avoid later lock recursion. */
1610 vm_page_dequeue_locked(m);
1611 vm_page_deactivate(m);
1613 m->act_count -= min(m->act_count, ACT_DECLINE);
1614 vm_page_requeue_locked(m);
1618 VM_OBJECT_UNLOCK(object);
1621 vm_pagequeue_unlock(pq);
1625 * vm_pageout is the high level pageout daemon.
1633 * Initialize some paging parameters.
1635 cnt.v_interrupt_free_min = 2;
1636 if (cnt.v_page_count < 2000)
1637 vm_pageout_page_count = 8;
1640 * v_free_reserved needs to include enough for the largest
1641 * swap pager structures plus enough for any pv_entry structs
1644 if (cnt.v_page_count > 1024)
1645 cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
1648 cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
1649 cnt.v_interrupt_free_min;
1650 cnt.v_free_reserved = vm_pageout_page_count +
1651 cnt.v_pageout_free_min + (cnt.v_page_count / 768);
1652 cnt.v_free_severe = cnt.v_free_min / 2;
1653 cnt.v_free_min += cnt.v_free_reserved;
1654 cnt.v_free_severe += cnt.v_free_reserved;
1657 * v_free_target and v_cache_min control pageout hysteresis. Note
1658 * that these are more a measure of the VM cache queue hysteresis
1659 * then the VM free queue. Specifically, v_free_target is the
1660 * high water mark (free+cache pages).
1662 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
1663 * low water mark, while v_free_min is the stop. v_cache_min must
1664 * be big enough to handle memory needs while the pageout daemon
1665 * is signalled and run to free more pages.
1667 if (cnt.v_free_count > 6144)
1668 cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved;
1670 cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved;
1672 if (cnt.v_free_count > 2048) {
1673 cnt.v_cache_min = cnt.v_free_target;
1674 cnt.v_cache_max = 2 * cnt.v_cache_min;
1675 cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
1677 cnt.v_cache_min = 0;
1678 cnt.v_cache_max = 0;
1679 cnt.v_inactive_target = cnt.v_free_count / 4;
1681 if (cnt.v_inactive_target > cnt.v_free_count / 3)
1682 cnt.v_inactive_target = cnt.v_free_count / 3;
1684 /* XXX does not really belong here */
1685 if (vm_page_max_wired == 0)
1686 vm_page_max_wired = cnt.v_free_count / 3;
1688 if (vm_pageout_stats_max == 0)
1689 vm_pageout_stats_max = cnt.v_free_target;
1692 * Set interval in seconds for stats scan.
1694 if (vm_pageout_stats_interval == 0)
1695 vm_pageout_stats_interval = 5;
1696 if (vm_pageout_full_stats_interval == 0)
1697 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1699 swap_pager_swap_init();
1702 * The pageout daemon is never done, so loop forever.
1706 * If we have enough free memory, wakeup waiters. Do
1707 * not clear vm_pages_needed until we reach our target,
1708 * otherwise we may be woken up over and over again and
1709 * waste a lot of cpu.
1711 mtx_lock(&vm_page_queue_free_mtx);
1712 if (vm_pages_needed && !vm_page_count_min()) {
1713 if (!vm_paging_needed())
1714 vm_pages_needed = 0;
1715 wakeup(&cnt.v_free_count);
1717 if (vm_pages_needed) {
1719 * Still not done, take a second pass without waiting
1720 * (unlimited dirty cleaning), otherwise sleep a bit
1725 msleep(&vm_pages_needed,
1726 &vm_page_queue_free_mtx, PVM, "psleep",
1730 * Good enough, sleep & handle stats. Prime the pass
1737 error = msleep(&vm_pages_needed,
1738 &vm_page_queue_free_mtx, PVM, "psleep",
1739 vm_pageout_stats_interval * hz);
1740 if (error && !vm_pages_needed) {
1741 mtx_unlock(&vm_page_queue_free_mtx);
1743 vm_pageout_page_stats();
1747 if (vm_pages_needed)
1749 mtx_unlock(&vm_page_queue_free_mtx);
1750 vm_pageout_scan(pass);
1755 * Unless the free page queue lock is held by the caller, this function
1756 * should be regarded as advisory. Specifically, the caller should
1757 * not msleep() on &cnt.v_free_count following this function unless
1758 * the free page queue lock is held until the msleep() is performed.
1761 pagedaemon_wakeup(void)
1764 if (!vm_pages_needed && curthread->td_proc != pageproc) {
1765 vm_pages_needed = 1;
1766 wakeup(&vm_pages_needed);
1770 #if !defined(NO_SWAPPING)
1772 vm_req_vmdaemon(int req)
1774 static int lastrun = 0;
1776 mtx_lock(&vm_daemon_mtx);
1777 vm_pageout_req_swapout |= req;
1778 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
1779 wakeup(&vm_daemon_needed);
1782 mtx_unlock(&vm_daemon_mtx);
1788 struct rlimit rsslim;
1792 int breakout, swapout_flags, tryagain, attempts;
1794 uint64_t rsize, ravailable;
1798 mtx_lock(&vm_daemon_mtx);
1800 msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", hz);
1802 msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0);
1804 swapout_flags = vm_pageout_req_swapout;
1805 vm_pageout_req_swapout = 0;
1806 mtx_unlock(&vm_daemon_mtx);
1808 swapout_procs(swapout_flags);
1811 * scan the processes for exceeding their rlimits or if
1812 * process is swapped out -- deactivate pages
1818 sx_slock(&allproc_lock);
1819 FOREACH_PROC_IN_SYSTEM(p) {
1820 vm_pindex_t limit, size;
1823 * if this is a system process or if we have already
1824 * looked at this process, skip it.
1827 if (p->p_state != PRS_NORMAL ||
1828 p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) {
1833 * if the process is in a non-running type state,
1837 FOREACH_THREAD_IN_PROC(p, td) {
1839 if (!TD_ON_RUNQ(td) &&
1840 !TD_IS_RUNNING(td) &&
1841 !TD_IS_SLEEPING(td) &&
1842 !TD_IS_SUSPENDED(td)) {
1856 lim_rlimit(p, RLIMIT_RSS, &rsslim);
1858 qmin(rsslim.rlim_cur, rsslim.rlim_max));
1861 * let processes that are swapped out really be
1862 * swapped out set the limit to nothing (will force a
1865 if ((p->p_flag & P_INMEM) == 0)
1866 limit = 0; /* XXX */
1867 vm = vmspace_acquire_ref(p);
1872 size = vmspace_resident_count(vm);
1873 if (size >= limit) {
1874 vm_pageout_map_deactivate_pages(
1875 &vm->vm_map, limit);
1878 rsize = IDX_TO_OFF(size);
1880 racct_set(p, RACCT_RSS, rsize);
1881 ravailable = racct_get_available(p, RACCT_RSS);
1883 if (rsize > ravailable) {
1885 * Don't be overly aggressive; this might be
1886 * an innocent process, and the limit could've
1887 * been exceeded by some memory hog. Don't
1888 * try to deactivate more than 1/4th of process'
1889 * resident set size.
1891 if (attempts <= 8) {
1892 if (ravailable < rsize - (rsize / 4))
1893 ravailable = rsize - (rsize / 4);
1895 vm_pageout_map_deactivate_pages(
1896 &vm->vm_map, OFF_TO_IDX(ravailable));
1897 /* Update RSS usage after paging out. */
1898 size = vmspace_resident_count(vm);
1899 rsize = IDX_TO_OFF(size);
1901 racct_set(p, RACCT_RSS, rsize);
1903 if (rsize > ravailable)
1909 sx_sunlock(&allproc_lock);
1910 if (tryagain != 0 && attempts <= 10)
1914 #endif /* !defined(NO_SWAPPING) */