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>
95 #include <sys/rwlock.h>
97 #include <sys/sysctl.h>
100 #include <vm/vm_param.h>
101 #include <vm/vm_object.h>
102 #include <vm/vm_page.h>
103 #include <vm/vm_map.h>
104 #include <vm/vm_pageout.h>
105 #include <vm/vm_pager.h>
106 #include <vm/swap_pager.h>
107 #include <vm/vm_extern.h>
111 * System initialization
114 /* the kernel process "vm_pageout"*/
115 static void vm_pageout(void);
116 static int vm_pageout_clean(vm_page_t);
117 static void vm_pageout_scan(int pass);
119 struct proc *pageproc;
121 static struct kproc_desc page_kp = {
126 SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start,
129 #if !defined(NO_SWAPPING)
130 /* the kernel process "vm_daemon"*/
131 static void vm_daemon(void);
132 static struct proc *vmproc;
134 static struct kproc_desc vm_kp = {
139 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp);
143 int vm_pages_needed; /* Event on which pageout daemon sleeps */
144 int vm_pageout_deficit; /* Estimated number of pages deficit */
145 int vm_pageout_pages_needed; /* flag saying that the pageout daemon needs pages */
147 #if !defined(NO_SWAPPING)
148 static int vm_pageout_req_swapout; /* XXX */
149 static int vm_daemon_needed;
150 static struct mtx vm_daemon_mtx;
151 /* Allow for use by vm_pageout before vm_daemon is initialized. */
152 MTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF);
154 static int vm_max_launder = 32;
155 static int vm_pageout_stats_max;
156 static int vm_pageout_stats;
157 static int vm_pageout_stats_interval;
158 static int vm_pageout_full_stats;
159 static int vm_pageout_full_stats_interval;
160 static int defer_swap_pageouts;
161 static int disable_swap_pageouts;
163 #if defined(NO_SWAPPING)
164 static int vm_swap_enabled = 0;
165 static int vm_swap_idle_enabled = 0;
167 static int vm_swap_enabled = 1;
168 static int vm_swap_idle_enabled = 0;
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_stats,
178 CTLFLAG_RD, &vm_pageout_stats, 0, "Number of partial stats scans");
180 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
181 CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
183 SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats,
184 CTLFLAG_RD, &vm_pageout_full_stats, 0, "Number of full stats scans");
186 SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
187 CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
189 #if defined(NO_SWAPPING)
190 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
191 CTLFLAG_RD, &vm_swap_enabled, 0, "Enable entire process swapout");
192 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
193 CTLFLAG_RD, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
195 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
196 CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
197 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
198 CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
201 SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
202 CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
204 SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
205 CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
207 static int pageout_lock_miss;
208 SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
209 CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
211 #define VM_PAGEOUT_PAGE_COUNT 16
212 int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
214 int vm_page_max_wired; /* XXX max # of wired pages system-wide */
215 SYSCTL_INT(_vm, OID_AUTO, max_wired,
216 CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count");
218 static boolean_t vm_pageout_fallback_object_lock(vm_page_t, vm_page_t *);
219 static boolean_t vm_pageout_launder(int, int, vm_paddr_t, vm_paddr_t);
220 #if !defined(NO_SWAPPING)
221 static void vm_pageout_map_deactivate_pages(vm_map_t, long);
222 static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long);
223 static void vm_req_vmdaemon(int req);
225 static boolean_t vm_pageout_page_lock(vm_page_t, vm_page_t *);
226 static void vm_pageout_page_stats(void);
229 * Initialize a dummy page for marking the caller's place in the specified
230 * paging queue. In principle, this function only needs to set the flag
231 * PG_MARKER. Nonetheless, it sets the flag VPO_BUSY and initializes the hold
232 * count to one as safety precautions.
235 vm_pageout_init_marker(vm_page_t marker, u_short queue)
238 bzero(marker, sizeof(*marker));
239 marker->flags = PG_MARKER;
240 marker->oflags = VPO_BUSY;
241 marker->queue = queue;
242 marker->hold_count = 1;
246 * vm_pageout_fallback_object_lock:
248 * Lock vm object currently associated with `m'. VM_OBJECT_TRYWLOCK is
249 * known to have failed and page queue must be either PQ_ACTIVE or
250 * PQ_INACTIVE. To avoid lock order violation, unlock the page queues
251 * while locking the vm object. Use marker page to detect page queue
252 * changes and maintain notion of next page on page queue. Return
253 * TRUE if no changes were detected, FALSE otherwise. vm object is
256 * This function depends on both the lock portion of struct vm_object
257 * and normal struct vm_page being type stable.
260 vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next)
262 struct vm_page marker;
263 struct vm_pagequeue *pq;
269 vm_pageout_init_marker(&marker, queue);
270 pq = &vm_pagequeues[queue];
273 TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, pageq);
274 vm_pagequeue_unlock(pq);
276 VM_OBJECT_WLOCK(object);
278 vm_pagequeue_lock(pq);
280 /* Page queue might have changed. */
281 *next = TAILQ_NEXT(&marker, pageq);
282 unchanged = (m->queue == queue &&
283 m->object == object &&
284 &marker == TAILQ_NEXT(m, pageq));
285 TAILQ_REMOVE(&pq->pq_pl, &marker, pageq);
290 * Lock the page while holding the page queue lock. Use marker page
291 * to detect page queue changes and maintain notion of next page on
292 * page queue. Return TRUE if no changes were detected, FALSE
293 * otherwise. The page is locked on return. The page queue lock might
294 * be dropped and reacquired.
296 * This function depends on normal struct vm_page being type stable.
299 vm_pageout_page_lock(vm_page_t m, vm_page_t *next)
301 struct vm_page marker;
302 struct vm_pagequeue *pq;
306 vm_page_lock_assert(m, MA_NOTOWNED);
307 if (vm_page_trylock(m))
311 vm_pageout_init_marker(&marker, queue);
312 pq = &vm_pagequeues[queue];
314 TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, pageq);
315 vm_pagequeue_unlock(pq);
317 vm_pagequeue_lock(pq);
319 /* Page queue might have changed. */
320 *next = TAILQ_NEXT(&marker, pageq);
321 unchanged = (m->queue == queue && &marker == TAILQ_NEXT(m, pageq));
322 TAILQ_REMOVE(&pq->pq_pl, &marker, pageq);
329 * Clean the page and remove it from the laundry.
331 * We set the busy bit to cause potential page faults on this page to
332 * block. Note the careful timing, however, the busy bit isn't set till
333 * late and we cannot do anything that will mess with the page.
336 vm_pageout_clean(vm_page_t m)
339 vm_page_t mc[2*vm_pageout_page_count], pb, ps;
341 int ib, is, page_base;
342 vm_pindex_t pindex = m->pindex;
344 vm_page_lock_assert(m, MA_OWNED);
346 VM_OBJECT_ASSERT_WLOCKED(object);
349 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
350 * with the new swapper, but we could have serious problems paging
351 * out other object types if there is insufficient memory.
353 * Unfortunately, checking free memory here is far too late, so the
354 * check has been moved up a procedural level.
358 * Can't clean the page if it's busy or held.
360 KASSERT(m->busy == 0 && (m->oflags & VPO_BUSY) == 0,
361 ("vm_pageout_clean: page %p is busy", m));
362 KASSERT(m->hold_count == 0, ("vm_pageout_clean: page %p is held", m));
365 mc[vm_pageout_page_count] = pb = ps = m;
367 page_base = vm_pageout_page_count;
372 * Scan object for clusterable pages.
374 * We can cluster ONLY if: ->> the page is NOT
375 * clean, wired, busy, held, or mapped into a
376 * buffer, and one of the following:
377 * 1) The page is inactive, or a seldom used
380 * 2) we force the issue.
382 * During heavy mmap/modification loads the pageout
383 * daemon can really fragment the underlying file
384 * due to flushing pages out of order and not trying
385 * align the clusters (which leave sporatic out-of-order
386 * holes). To solve this problem we do the reverse scan
387 * first and attempt to align our cluster, then do a
388 * forward scan if room remains.
391 while (ib && pageout_count < vm_pageout_page_count) {
399 if ((p = vm_page_prev(pb)) == NULL ||
400 (p->oflags & VPO_BUSY) != 0 || p->busy != 0) {
405 vm_page_test_dirty(p);
407 p->queue != PQ_INACTIVE ||
408 p->hold_count != 0) { /* may be undergoing I/O */
414 mc[--page_base] = pb = p;
418 * alignment boundry, stop here and switch directions. Do
421 if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
425 while (pageout_count < vm_pageout_page_count &&
426 pindex + is < object->size) {
429 if ((p = vm_page_next(ps)) == NULL ||
430 (p->oflags & VPO_BUSY) != 0 || p->busy != 0)
433 vm_page_test_dirty(p);
435 p->queue != PQ_INACTIVE ||
436 p->hold_count != 0) { /* may be undergoing I/O */
441 mc[page_base + pageout_count] = ps = p;
447 * If we exhausted our forward scan, continue with the reverse scan
448 * when possible, even past a page boundry. This catches boundry
451 if (ib && pageout_count < vm_pageout_page_count)
455 * we allow reads during pageouts...
457 return (vm_pageout_flush(&mc[page_base], pageout_count, 0, 0, NULL,
462 * vm_pageout_flush() - launder the given pages
464 * The given pages are laundered. Note that we setup for the start of
465 * I/O ( i.e. busy the page ), mark it read-only, and bump the object
466 * reference count all in here rather then in the parent. If we want
467 * the parent to do more sophisticated things we may have to change
470 * Returned runlen is the count of pages between mreq and first
471 * page after mreq with status VM_PAGER_AGAIN.
472 * *eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL
473 * for any page in runlen set.
476 vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen,
479 vm_object_t object = mc[0]->object;
480 int pageout_status[count];
484 VM_OBJECT_ASSERT_WLOCKED(object);
487 * Initiate I/O. Bump the vm_page_t->busy counter and
488 * mark the pages read-only.
490 * We do not have to fixup the clean/dirty bits here... we can
491 * allow the pager to do it after the I/O completes.
493 * NOTE! mc[i]->dirty may be partial or fragmented due to an
494 * edge case with file fragments.
496 for (i = 0; i < count; i++) {
497 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL,
498 ("vm_pageout_flush: partially invalid page %p index %d/%d",
500 vm_page_io_start(mc[i]);
501 pmap_remove_write(mc[i]);
503 vm_object_pip_add(object, count);
505 vm_pager_put_pages(object, mc, count, flags, pageout_status);
507 runlen = count - mreq;
510 for (i = 0; i < count; i++) {
511 vm_page_t mt = mc[i];
513 KASSERT(pageout_status[i] == VM_PAGER_PEND ||
514 !pmap_page_is_write_mapped(mt),
515 ("vm_pageout_flush: page %p is not write protected", mt));
516 switch (pageout_status[i]) {
523 * Page outside of range of object. Right now we
524 * essentially lose the changes by pretending it
532 * If page couldn't be paged out, then reactivate the
533 * page so it doesn't clog the inactive list. (We
534 * will try paging out it again later).
537 vm_page_activate(mt);
539 if (eio != NULL && i >= mreq && i - mreq < runlen)
543 if (i >= mreq && i - mreq < runlen)
549 * If the operation is still going, leave the page busy to
550 * block all other accesses. Also, leave the paging in
551 * progress indicator set so that we don't attempt an object
554 if (pageout_status[i] != VM_PAGER_PEND) {
555 vm_object_pip_wakeup(object);
556 vm_page_io_finish(mt);
557 if (vm_page_count_severe()) {
559 vm_page_try_to_cache(mt);
566 return (numpagedout);
570 vm_pageout_launder(int queue, int tries, vm_paddr_t low, vm_paddr_t high)
573 struct vm_pagequeue *pq;
577 vm_page_t m, m_tmp, next;
579 pq = &vm_pagequeues[queue];
580 vm_pagequeue_lock(pq);
581 TAILQ_FOREACH_SAFE(m, &pq->pq_pl, pageq, next) {
582 KASSERT(m->queue == queue,
583 ("vm_pageout_launder: page %p's queue is not %d", m,
585 if ((m->flags & PG_MARKER) != 0)
587 pa = VM_PAGE_TO_PHYS(m);
588 if (pa < low || pa + PAGE_SIZE > high)
590 if (!vm_pageout_page_lock(m, &next) || m->hold_count != 0) {
595 if ((!VM_OBJECT_TRYWLOCK(object) &&
596 (!vm_pageout_fallback_object_lock(m, &next) ||
597 m->hold_count != 0)) || (m->oflags & VPO_BUSY) != 0 ||
600 VM_OBJECT_WUNLOCK(object);
603 vm_page_test_dirty(m);
604 if (m->dirty == 0 && object->ref_count != 0)
608 if (tries == 0 || (object->flags & OBJ_DEAD) != 0) {
609 VM_OBJECT_WUNLOCK(object);
612 if (object->type == OBJT_VNODE) {
613 vm_pagequeue_unlock(pq);
615 vm_object_reference_locked(object);
616 VM_OBJECT_WUNLOCK(object);
617 (void)vn_start_write(vp, &mp, V_WAIT);
618 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
619 VM_OBJECT_WLOCK(object);
620 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
621 VM_OBJECT_WUNLOCK(object);
623 vm_object_deallocate(object);
624 vn_finished_write(mp);
626 } else if (object->type == OBJT_SWAP ||
627 object->type == OBJT_DEFAULT) {
628 vm_pagequeue_unlock(pq);
630 vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC,
632 VM_OBJECT_WUNLOCK(object);
637 * Dequeue here to prevent lock recursion in
640 vm_page_dequeue_locked(m);
644 VM_OBJECT_WUNLOCK(object);
646 vm_pagequeue_unlock(pq);
651 * Increase the number of cached pages. The specified value, "tries",
652 * determines which categories of pages are cached:
654 * 0: All clean, inactive pages within the specified physical address range
655 * are cached. Will not sleep.
656 * 1: The vm_lowmem handlers are called. All inactive pages within
657 * the specified physical address range are cached. May sleep.
658 * 2: The vm_lowmem handlers are called. All inactive and active pages
659 * within the specified physical address range are cached. May sleep.
662 vm_pageout_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high)
664 int actl, actmax, inactl, inactmax;
668 * Decrease registered cache sizes. The vm_lowmem handlers
669 * may acquire locks and/or sleep, so they can only be invoked
670 * when "tries" is greater than zero.
672 EVENTHANDLER_INVOKE(vm_lowmem, 0);
675 * We do this explicitly after the caches have been drained
681 inactmax = cnt.v_inactive_count;
683 actmax = tries < 2 ? 0 : cnt.v_active_count;
685 if (inactl < inactmax && vm_pageout_launder(PQ_INACTIVE, tries, low,
690 if (actl < actmax && vm_pageout_launder(PQ_ACTIVE, tries, low, high)) {
696 #if !defined(NO_SWAPPING)
698 * vm_pageout_object_deactivate_pages
700 * Deactivate enough pages to satisfy the inactive target
703 * The object and map must be locked.
706 vm_pageout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object,
709 vm_object_t backing_object, object;
711 int act_delta, remove_mode;
713 VM_OBJECT_ASSERT_LOCKED(first_object);
714 if ((first_object->flags & OBJ_FICTITIOUS) != 0)
716 for (object = first_object;; object = backing_object) {
717 if (pmap_resident_count(pmap) <= desired)
719 VM_OBJECT_ASSERT_LOCKED(object);
720 if ((object->flags & OBJ_UNMANAGED) != 0 ||
721 object->paging_in_progress != 0)
725 if (object->shadow_count > 1)
728 * Scan the object's entire memory queue.
730 TAILQ_FOREACH(p, &object->memq, listq) {
731 if (pmap_resident_count(pmap) <= desired)
733 if ((p->oflags & VPO_BUSY) != 0 || p->busy != 0)
735 PCPU_INC(cnt.v_pdpages);
737 if (p->wire_count != 0 || p->hold_count != 0 ||
738 !pmap_page_exists_quick(pmap, p)) {
742 act_delta = pmap_ts_referenced(p);
743 if ((p->aflags & PGA_REFERENCED) != 0) {
746 vm_page_aflag_clear(p, PGA_REFERENCED);
748 if (p->queue != PQ_ACTIVE && act_delta != 0) {
750 p->act_count += act_delta;
751 } else if (p->queue == PQ_ACTIVE) {
752 if (act_delta == 0) {
753 p->act_count -= min(p->act_count,
755 if (!remove_mode && p->act_count == 0) {
757 vm_page_deactivate(p);
762 if (p->act_count < ACT_MAX -
764 p->act_count += ACT_ADVANCE;
767 } else if (p->queue == PQ_INACTIVE)
771 if ((backing_object = object->backing_object) == NULL)
773 VM_OBJECT_RLOCK(backing_object);
774 if (object != first_object)
775 VM_OBJECT_RUNLOCK(object);
778 if (object != first_object)
779 VM_OBJECT_RUNLOCK(object);
783 * deactivate some number of pages in a map, try to do it fairly, but
784 * that is really hard to do.
787 vm_pageout_map_deactivate_pages(map, desired)
792 vm_object_t obj, bigobj;
795 if (!vm_map_trylock(map))
802 * first, search out the biggest object, and try to free pages from
805 tmpe = map->header.next;
806 while (tmpe != &map->header) {
807 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
808 obj = tmpe->object.vm_object;
809 if (obj != NULL && VM_OBJECT_TRYRLOCK(obj)) {
810 if (obj->shadow_count <= 1 &&
812 bigobj->resident_page_count < obj->resident_page_count)) {
814 VM_OBJECT_RUNLOCK(bigobj);
817 VM_OBJECT_RUNLOCK(obj);
820 if (tmpe->wired_count > 0)
821 nothingwired = FALSE;
825 if (bigobj != NULL) {
826 vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired);
827 VM_OBJECT_RUNLOCK(bigobj);
830 * Next, hunt around for other pages to deactivate. We actually
831 * do this search sort of wrong -- .text first is not the best idea.
833 tmpe = map->header.next;
834 while (tmpe != &map->header) {
835 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
837 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
838 obj = tmpe->object.vm_object;
840 VM_OBJECT_RLOCK(obj);
841 vm_pageout_object_deactivate_pages(map->pmap, obj, desired);
842 VM_OBJECT_RUNLOCK(obj);
849 * Remove all mappings if a process is swapped out, this will free page
852 if (desired == 0 && nothingwired) {
853 pmap_remove(vm_map_pmap(map), vm_map_min(map),
858 #endif /* !defined(NO_SWAPPING) */
861 * vm_pageout_scan does the dirty work for the pageout daemon.
864 vm_pageout_scan(int pass)
867 struct vm_page marker;
868 struct vm_pagequeue *pq;
869 int page_shortage, maxscan, pcount;
870 int addl_page_shortage;
873 int vnodes_skipped = 0;
875 boolean_t queues_locked;
877 vm_pageout_init_marker(&marker, PQ_INACTIVE);
880 * Decrease registered cache sizes.
882 EVENTHANDLER_INVOKE(vm_lowmem, 0);
884 * We do this explicitly after the caches have been drained above.
889 * The addl_page_shortage is the number of temporarily
890 * stuck pages in the inactive queue. In other words, the
891 * number of pages from cnt.v_inactive_count that should be
892 * discounted in setting the target for the active queue scan.
894 addl_page_shortage = atomic_readandclear_int(&vm_pageout_deficit);
897 * Calculate the number of pages we want to either free or move
900 page_shortage = vm_paging_target() + addl_page_shortage;
903 * maxlaunder limits the number of dirty pages we flush per scan.
904 * For most systems a smaller value (16 or 32) is more robust under
905 * extreme memory and disk pressure because any unnecessary writes
906 * to disk can result in extreme performance degredation. However,
907 * systems with excessive dirty pages (especially when MAP_NOSYNC is
908 * used) will die horribly with limited laundering. If the pageout
909 * daemon cannot clean enough pages in the first pass, we let it go
910 * all out in succeeding passes.
912 if ((maxlaunder = vm_max_launder) <= 1)
917 maxscan = cnt.v_inactive_count;
920 * Start scanning the inactive queue for pages we can move to the
921 * cache or free. The scan will stop when the target is reached or
922 * we have scanned the entire inactive queue. Note that m->act_count
923 * is not used to form decisions for the inactive queue, only for the
926 pq = &vm_pagequeues[PQ_INACTIVE];
927 vm_pagequeue_lock(pq);
928 queues_locked = TRUE;
929 for (m = TAILQ_FIRST(&pq->pq_pl);
930 m != NULL && maxscan-- > 0 && page_shortage > 0;
932 vm_pagequeue_assert_locked(pq);
933 KASSERT(queues_locked, ("unlocked queues"));
934 KASSERT(m->queue == PQ_INACTIVE, ("Inactive queue %p", m));
936 PCPU_INC(cnt.v_pdpages);
937 next = TAILQ_NEXT(m, pageq);
942 if (m->flags & PG_MARKER)
945 KASSERT((m->flags & PG_FICTITIOUS) == 0,
946 ("Fictitious page %p cannot be in inactive queue", m));
947 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
948 ("Unmanaged page %p cannot be in inactive queue", m));
951 * The page or object lock acquisitions fail if the
952 * page was removed from the queue or moved to a
953 * different position within the queue. In either
954 * case, addl_page_shortage should not be incremented.
956 if (!vm_pageout_page_lock(m, &next)) {
961 if (!VM_OBJECT_TRYWLOCK(object) &&
962 !vm_pageout_fallback_object_lock(m, &next)) {
964 VM_OBJECT_WUNLOCK(object);
969 * Don't mess with busy pages, keep them at at the
970 * front of the queue, most likely they are being
971 * paged out. Increment addl_page_shortage for busy
972 * pages, because they may leave the inactive queue
973 * shortly after page scan is finished.
975 if (m->busy != 0 || (m->oflags & VPO_BUSY) != 0) {
977 VM_OBJECT_WUNLOCK(object);
978 addl_page_shortage++;
983 * We unlock the inactive page queue, invalidating the
984 * 'next' pointer. Use our marker to remember our
987 TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, pageq);
988 vm_pagequeue_unlock(pq);
989 queues_locked = FALSE;
992 * We bump the activation count if the page has been
993 * referenced while in the inactive queue. This makes
994 * it less likely that the page will be added back to the
995 * inactive queue prematurely again. Here we check the
996 * page tables (or emulated bits, if any), given the upper
997 * level VM system not knowing anything about existing
1001 if ((m->aflags & PGA_REFERENCED) != 0) {
1002 vm_page_aflag_clear(m, PGA_REFERENCED);
1005 if (object->ref_count != 0) {
1006 act_delta += pmap_ts_referenced(m);
1008 KASSERT(!pmap_page_is_mapped(m),
1009 ("vm_pageout_scan: page %p is mapped", m));
1013 * If the upper level VM system knows about any page
1014 * references, we reactivate the page or requeue it.
1016 if (act_delta != 0) {
1017 if (object->ref_count) {
1018 vm_page_activate(m);
1019 m->act_count += act_delta + ACT_ADVANCE;
1021 vm_pagequeue_lock(pq);
1022 queues_locked = TRUE;
1023 vm_page_requeue_locked(m);
1025 VM_OBJECT_WUNLOCK(object);
1030 if (m->hold_count != 0) {
1032 VM_OBJECT_WUNLOCK(object);
1035 * Held pages are essentially stuck in the
1036 * queue. So, they ought to be discounted
1037 * from cnt.v_inactive_count. See the
1038 * calculation of the page_shortage for the
1039 * loop over the active queue below.
1041 addl_page_shortage++;
1046 * If the page appears to be clean at the machine-independent
1047 * layer, then remove all of its mappings from the pmap in
1048 * anticipation of placing it onto the cache queue. If,
1049 * however, any of the page's mappings allow write access,
1050 * then the page may still be modified until the last of those
1051 * mappings are removed.
1053 vm_page_test_dirty(m);
1054 if (m->dirty == 0 && object->ref_count != 0)
1057 if (m->valid == 0) {
1059 * Invalid pages can be easily freed
1062 PCPU_INC(cnt.v_dfree);
1064 } else if (m->dirty == 0) {
1066 * Clean pages can be placed onto the cache queue.
1067 * This effectively frees them.
1071 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
1073 * Dirty pages need to be paged out, but flushing
1074 * a page is extremely expensive verses freeing
1075 * a clean page. Rather then artificially limiting
1076 * the number of pages we can flush, we instead give
1077 * dirty pages extra priority on the inactive queue
1078 * by forcing them to be cycled through the queue
1079 * twice before being flushed, after which the
1080 * (now clean) page will cycle through once more
1081 * before being freed. This significantly extends
1082 * the thrash point for a heavily loaded machine.
1084 m->flags |= PG_WINATCFLS;
1085 vm_pagequeue_lock(pq);
1086 queues_locked = TRUE;
1087 vm_page_requeue_locked(m);
1088 } else if (maxlaunder > 0) {
1090 * We always want to try to flush some dirty pages if
1091 * we encounter them, to keep the system stable.
1092 * Normally this number is small, but under extreme
1093 * pressure where there are insufficient clean pages
1094 * on the inactive queue, we may have to go all out.
1096 int swap_pageouts_ok;
1097 struct vnode *vp = NULL;
1098 struct mount *mp = NULL;
1100 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
1101 swap_pageouts_ok = 1;
1103 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
1104 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
1105 vm_page_count_min());
1110 * We don't bother paging objects that are "dead".
1111 * Those objects are in a "rundown" state.
1113 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
1114 vm_pagequeue_lock(pq);
1116 VM_OBJECT_WUNLOCK(object);
1117 queues_locked = TRUE;
1118 vm_page_requeue_locked(m);
1123 * The object is already known NOT to be dead. It
1124 * is possible for the vget() to block the whole
1125 * pageout daemon, but the new low-memory handling
1126 * code should prevent it.
1128 * The previous code skipped locked vnodes and, worse,
1129 * reordered pages in the queue. This results in
1130 * completely non-deterministic operation and, on a
1131 * busy system, can lead to extremely non-optimal
1132 * pageouts. For example, it can cause clean pages
1133 * to be freed and dirty pages to be moved to the end
1134 * of the queue. Since dirty pages are also moved to
1135 * the end of the queue once-cleaned, this gives
1136 * way too large a weighting to defering the freeing
1139 * We can't wait forever for the vnode lock, we might
1140 * deadlock due to a vn_read() getting stuck in
1141 * vm_wait while holding this vnode. We skip the
1142 * vnode if we can't get it in a reasonable amount
1145 if (object->type == OBJT_VNODE) {
1147 vp = object->handle;
1148 if (vp->v_type == VREG &&
1149 vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1151 ++pageout_lock_miss;
1152 if (object->flags & OBJ_MIGHTBEDIRTY)
1154 goto unlock_and_continue;
1157 ("vp %p with NULL v_mount", vp));
1158 vm_object_reference_locked(object);
1159 VM_OBJECT_WUNLOCK(object);
1160 if (vget(vp, LK_EXCLUSIVE | LK_TIMELOCK,
1162 VM_OBJECT_WLOCK(object);
1163 ++pageout_lock_miss;
1164 if (object->flags & OBJ_MIGHTBEDIRTY)
1167 goto unlock_and_continue;
1169 VM_OBJECT_WLOCK(object);
1171 vm_pagequeue_lock(pq);
1172 queues_locked = TRUE;
1174 * The page might have been moved to another
1175 * queue during potential blocking in vget()
1176 * above. The page might have been freed and
1177 * reused for another vnode.
1179 if (m->queue != PQ_INACTIVE ||
1180 m->object != object ||
1181 TAILQ_NEXT(m, pageq) != &marker) {
1183 if (object->flags & OBJ_MIGHTBEDIRTY)
1185 goto unlock_and_continue;
1189 * The page may have been busied during the
1190 * blocking in vget(). We don't move the
1191 * page back onto the end of the queue so that
1192 * statistics are more correct if we don't.
1194 if (m->busy || (m->oflags & VPO_BUSY)) {
1196 goto unlock_and_continue;
1200 * If the page has become held it might
1201 * be undergoing I/O, so skip it
1203 if (m->hold_count) {
1205 vm_page_requeue_locked(m);
1206 if (object->flags & OBJ_MIGHTBEDIRTY)
1208 goto unlock_and_continue;
1210 vm_pagequeue_unlock(pq);
1211 queues_locked = FALSE;
1215 * If a page is dirty, then it is either being washed
1216 * (but not yet cleaned) or it is still in the
1217 * laundry. If it is still in the laundry, then we
1218 * start the cleaning operation.
1220 * decrement page_shortage on success to account for
1221 * the (future) cleaned page. Otherwise we could wind
1222 * up laundering or cleaning too many pages.
1224 if (vm_pageout_clean(m) != 0) {
1228 unlock_and_continue:
1229 vm_page_lock_assert(m, MA_NOTOWNED);
1230 VM_OBJECT_WUNLOCK(object);
1232 if (queues_locked) {
1233 vm_pagequeue_unlock(pq);
1234 queues_locked = FALSE;
1238 vm_object_deallocate(object);
1239 vn_finished_write(mp);
1241 vm_page_lock_assert(m, MA_NOTOWNED);
1245 VM_OBJECT_WUNLOCK(object);
1247 if (!queues_locked) {
1248 vm_pagequeue_lock(pq);
1249 queues_locked = TRUE;
1251 next = TAILQ_NEXT(&marker, pageq);
1252 TAILQ_REMOVE(&pq->pq_pl, &marker, pageq);
1254 vm_pagequeue_unlock(pq);
1257 * Compute the number of pages we want to try to move from the
1258 * active queue to the inactive queue.
1260 page_shortage = vm_paging_target() +
1261 cnt.v_inactive_target - cnt.v_inactive_count;
1262 page_shortage += addl_page_shortage;
1265 * Scan the active queue for things we can deactivate. We nominally
1266 * track the per-page activity counter and use it to locate
1267 * deactivation candidates.
1269 pcount = cnt.v_active_count;
1270 pq = &vm_pagequeues[PQ_ACTIVE];
1271 vm_pagequeue_lock(pq);
1272 m = TAILQ_FIRST(&pq->pq_pl);
1273 while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
1275 KASSERT(m->queue == PQ_ACTIVE,
1276 ("vm_pageout_scan: page %p isn't active", m));
1278 next = TAILQ_NEXT(m, pageq);
1279 if ((m->flags & PG_MARKER) != 0) {
1283 KASSERT((m->flags & PG_FICTITIOUS) == 0,
1284 ("Fictitious page %p cannot be in active queue", m));
1285 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1286 ("Unmanaged page %p cannot be in active queue", m));
1287 if (!vm_pageout_page_lock(m, &next)) {
1293 if (!VM_OBJECT_TRYWLOCK(object) &&
1294 !vm_pageout_fallback_object_lock(m, &next)) {
1295 VM_OBJECT_WUNLOCK(object);
1302 * Don't deactivate pages that are busy.
1304 if ((m->busy != 0) ||
1305 (m->oflags & VPO_BUSY) ||
1306 (m->hold_count != 0)) {
1308 VM_OBJECT_WUNLOCK(object);
1309 vm_page_requeue_locked(m);
1315 * The count for pagedaemon pages is done after checking the
1316 * page for eligibility...
1318 PCPU_INC(cnt.v_pdpages);
1321 * Check to see "how much" the page has been used.
1324 if (m->aflags & PGA_REFERENCED) {
1325 vm_page_aflag_clear(m, PGA_REFERENCED);
1328 if (object->ref_count != 0)
1329 act_delta += pmap_ts_referenced(m);
1332 * Advance or decay the act_count based on recent usage.
1335 m->act_count += ACT_ADVANCE + act_delta;
1336 if (m->act_count > ACT_MAX)
1337 m->act_count = ACT_MAX;
1339 m->act_count -= min(m->act_count, ACT_DECLINE);
1340 act_delta = m->act_count;
1344 * Move this page to the tail of the active or inactive
1345 * queue depending on usage.
1347 if (act_delta == 0) {
1348 KASSERT(object->ref_count != 0 ||
1349 !pmap_page_is_mapped(m),
1350 ("vm_pageout_scan: page %p is mapped", m));
1351 /* Dequeue to avoid later lock recursion. */
1352 vm_page_dequeue_locked(m);
1353 vm_page_deactivate(m);
1356 vm_page_requeue_locked(m);
1358 VM_OBJECT_WUNLOCK(object);
1361 vm_pagequeue_unlock(pq);
1362 #if !defined(NO_SWAPPING)
1364 * Idle process swapout -- run once per second.
1366 if (vm_swap_idle_enabled) {
1368 if (time_second != lsec) {
1369 vm_req_vmdaemon(VM_SWAP_IDLE);
1376 * If we didn't get enough free pages, and we have skipped a vnode
1377 * in a writeable object, wakeup the sync daemon. And kick swapout
1378 * if we did not get enough free pages.
1380 if (vm_paging_target() > 0) {
1381 if (vnodes_skipped && vm_page_count_min())
1382 (void) speedup_syncer();
1383 #if !defined(NO_SWAPPING)
1384 if (vm_swap_enabled && vm_page_count_target())
1385 vm_req_vmdaemon(VM_SWAP_NORMAL);
1390 * If we are critically low on one of RAM or swap and low on
1391 * the other, kill the largest process. However, we avoid
1392 * doing this on the first pass in order to give ourselves a
1393 * chance to flush out dirty vnode-backed pages and to allow
1394 * active pages to be moved to the inactive queue and reclaimed.
1397 ((swap_pager_avail < 64 && vm_page_count_min()) ||
1398 (swap_pager_full && vm_paging_target() > 0)))
1399 vm_pageout_oom(VM_OOM_MEM);
1404 vm_pageout_oom(int shortage)
1406 struct proc *p, *bigproc;
1407 vm_offset_t size, bigsize;
1412 * We keep the process bigproc locked once we find it to keep anyone
1413 * from messing with it; however, there is a possibility of
1414 * deadlock if process B is bigproc and one of it's child processes
1415 * attempts to propagate a signal to B while we are waiting for A's
1416 * lock while walking this list. To avoid this, we don't block on
1417 * the process lock but just skip a process if it is already locked.
1421 sx_slock(&allproc_lock);
1422 FOREACH_PROC_IN_SYSTEM(p) {
1425 if (PROC_TRYLOCK(p) == 0)
1428 * If this is a system, protected or killed process, skip it.
1430 if (p->p_state != PRS_NORMAL ||
1431 (p->p_flag & (P_INEXEC | P_PROTECTED | P_SYSTEM)) ||
1432 (p->p_pid == 1) || P_KILLED(p) ||
1433 ((p->p_pid < 48) && (swap_pager_avail != 0))) {
1438 * If the process is in a non-running type state,
1439 * don't touch it. Check all the threads individually.
1442 FOREACH_THREAD_IN_PROC(p, td) {
1444 if (!TD_ON_RUNQ(td) &&
1445 !TD_IS_RUNNING(td) &&
1446 !TD_IS_SLEEPING(td) &&
1447 !TD_IS_SUSPENDED(td)) {
1459 * get the process size
1461 vm = vmspace_acquire_ref(p);
1466 if (!vm_map_trylock_read(&vm->vm_map)) {
1471 size = vmspace_swap_count(vm);
1472 vm_map_unlock_read(&vm->vm_map);
1473 if (shortage == VM_OOM_MEM)
1474 size += vmspace_resident_count(vm);
1477 * if the this process is bigger than the biggest one
1480 if (size > bigsize) {
1481 if (bigproc != NULL)
1482 PROC_UNLOCK(bigproc);
1488 sx_sunlock(&allproc_lock);
1489 if (bigproc != NULL) {
1490 killproc(bigproc, "out of swap space");
1491 sched_nice(bigproc, PRIO_MIN);
1492 PROC_UNLOCK(bigproc);
1493 wakeup(&cnt.v_free_count);
1498 * This routine tries to maintain the pseudo LRU active queue,
1499 * so that during long periods of time where there is no paging,
1500 * that some statistic accumulation still occurs. This code
1501 * helps the situation where paging just starts to occur.
1504 vm_pageout_page_stats(void)
1506 struct vm_pagequeue *pq;
1509 int pcount, tpcount; /* Number of pages to check */
1510 static int fullintervalcount = 0;
1514 (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) -
1515 (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
1517 if (page_shortage <= 0)
1520 pcount = cnt.v_active_count;
1521 fullintervalcount += vm_pageout_stats_interval;
1522 if (fullintervalcount < vm_pageout_full_stats_interval) {
1524 tpcount = (int64_t)vm_pageout_stats_max * cnt.v_active_count /
1526 if (pcount > tpcount)
1529 vm_pageout_full_stats++;
1530 fullintervalcount = 0;
1533 pq = &vm_pagequeues[PQ_ACTIVE];
1534 vm_pagequeue_lock(pq);
1535 m = TAILQ_FIRST(&pq->pq_pl);
1536 while ((m != NULL) && (pcount-- > 0)) {
1539 KASSERT(m->queue == PQ_ACTIVE,
1540 ("vm_pageout_page_stats: page %p isn't active", m));
1542 next = TAILQ_NEXT(m, pageq);
1543 if ((m->flags & PG_MARKER) != 0) {
1547 vm_page_lock_assert(m, MA_NOTOWNED);
1548 if (!vm_pageout_page_lock(m, &next)) {
1554 if (!VM_OBJECT_TRYWLOCK(object) &&
1555 !vm_pageout_fallback_object_lock(m, &next)) {
1556 VM_OBJECT_WUNLOCK(object);
1563 * Don't deactivate pages that are busy.
1565 if ((m->busy != 0) ||
1566 (m->oflags & VPO_BUSY) ||
1567 (m->hold_count != 0)) {
1569 VM_OBJECT_WUNLOCK(object);
1570 vm_page_requeue_locked(m);
1576 if (m->aflags & PGA_REFERENCED) {
1577 vm_page_aflag_clear(m, PGA_REFERENCED);
1581 actcount += pmap_ts_referenced(m);
1583 m->act_count += ACT_ADVANCE + actcount;
1584 if (m->act_count > ACT_MAX)
1585 m->act_count = ACT_MAX;
1586 vm_page_requeue_locked(m);
1588 if (m->act_count == 0) {
1590 * We turn off page access, so that we have
1591 * more accurate RSS stats. We don't do this
1592 * in the normal page deactivation when the
1593 * system is loaded VM wise, because the
1594 * cost of the large number of page protect
1595 * operations would be higher than the value
1596 * of doing the operation.
1599 /* Dequeue to avoid later lock recursion. */
1600 vm_page_dequeue_locked(m);
1601 vm_page_deactivate(m);
1603 m->act_count -= min(m->act_count, ACT_DECLINE);
1604 vm_page_requeue_locked(m);
1608 VM_OBJECT_WUNLOCK(object);
1611 vm_pagequeue_unlock(pq);
1615 * vm_pageout is the high level pageout daemon.
1623 * Initialize some paging parameters.
1625 cnt.v_interrupt_free_min = 2;
1626 if (cnt.v_page_count < 2000)
1627 vm_pageout_page_count = 8;
1630 * v_free_reserved needs to include enough for the largest
1631 * swap pager structures plus enough for any pv_entry structs
1634 if (cnt.v_page_count > 1024)
1635 cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
1638 cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
1639 cnt.v_interrupt_free_min;
1640 cnt.v_free_reserved = vm_pageout_page_count +
1641 cnt.v_pageout_free_min + (cnt.v_page_count / 768);
1642 cnt.v_free_severe = cnt.v_free_min / 2;
1643 cnt.v_free_min += cnt.v_free_reserved;
1644 cnt.v_free_severe += cnt.v_free_reserved;
1647 * v_free_target and v_cache_min control pageout hysteresis. Note
1648 * that these are more a measure of the VM cache queue hysteresis
1649 * then the VM free queue. Specifically, v_free_target is the
1650 * high water mark (free+cache pages).
1652 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
1653 * low water mark, while v_free_min is the stop. v_cache_min must
1654 * be big enough to handle memory needs while the pageout daemon
1655 * is signalled and run to free more pages.
1657 if (cnt.v_free_count > 6144)
1658 cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved;
1660 cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved;
1662 if (cnt.v_free_count > 2048) {
1663 cnt.v_cache_min = cnt.v_free_target;
1664 cnt.v_cache_max = 2 * cnt.v_cache_min;
1665 cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
1667 cnt.v_cache_min = 0;
1668 cnt.v_cache_max = 0;
1669 cnt.v_inactive_target = cnt.v_free_count / 4;
1671 if (cnt.v_inactive_target > cnt.v_free_count / 3)
1672 cnt.v_inactive_target = cnt.v_free_count / 3;
1674 /* XXX does not really belong here */
1675 if (vm_page_max_wired == 0)
1676 vm_page_max_wired = cnt.v_free_count / 3;
1678 if (vm_pageout_stats_max == 0)
1679 vm_pageout_stats_max = cnt.v_free_target;
1682 * Set interval in seconds for stats scan.
1684 if (vm_pageout_stats_interval == 0)
1685 vm_pageout_stats_interval = 5;
1686 if (vm_pageout_full_stats_interval == 0)
1687 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1689 swap_pager_swap_init();
1692 * The pageout daemon is never done, so loop forever.
1696 * If we have enough free memory, wakeup waiters. Do
1697 * not clear vm_pages_needed until we reach our target,
1698 * otherwise we may be woken up over and over again and
1699 * waste a lot of cpu.
1701 mtx_lock(&vm_page_queue_free_mtx);
1702 if (vm_pages_needed && !vm_page_count_min()) {
1703 if (!vm_paging_needed())
1704 vm_pages_needed = 0;
1705 wakeup(&cnt.v_free_count);
1707 if (vm_pages_needed) {
1709 * Still not done, take a second pass without waiting
1710 * (unlimited dirty cleaning), otherwise sleep a bit
1715 msleep(&vm_pages_needed,
1716 &vm_page_queue_free_mtx, PVM, "psleep",
1720 * Good enough, sleep & handle stats. Prime the pass
1727 error = msleep(&vm_pages_needed,
1728 &vm_page_queue_free_mtx, PVM, "psleep",
1729 vm_pageout_stats_interval * hz);
1730 if (error && !vm_pages_needed) {
1731 mtx_unlock(&vm_page_queue_free_mtx);
1733 vm_pageout_page_stats();
1737 if (vm_pages_needed)
1739 mtx_unlock(&vm_page_queue_free_mtx);
1740 vm_pageout_scan(pass);
1745 * Unless the free page queue lock is held by the caller, this function
1746 * should be regarded as advisory. Specifically, the caller should
1747 * not msleep() on &cnt.v_free_count following this function unless
1748 * the free page queue lock is held until the msleep() is performed.
1751 pagedaemon_wakeup(void)
1754 if (!vm_pages_needed && curthread->td_proc != pageproc) {
1755 vm_pages_needed = 1;
1756 wakeup(&vm_pages_needed);
1760 #if !defined(NO_SWAPPING)
1762 vm_req_vmdaemon(int req)
1764 static int lastrun = 0;
1766 mtx_lock(&vm_daemon_mtx);
1767 vm_pageout_req_swapout |= req;
1768 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
1769 wakeup(&vm_daemon_needed);
1772 mtx_unlock(&vm_daemon_mtx);
1778 struct rlimit rsslim;
1782 int breakout, swapout_flags, tryagain, attempts;
1784 uint64_t rsize, ravailable;
1788 mtx_lock(&vm_daemon_mtx);
1790 msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", hz);
1792 msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0);
1794 swapout_flags = vm_pageout_req_swapout;
1795 vm_pageout_req_swapout = 0;
1796 mtx_unlock(&vm_daemon_mtx);
1798 swapout_procs(swapout_flags);
1801 * scan the processes for exceeding their rlimits or if
1802 * process is swapped out -- deactivate pages
1808 sx_slock(&allproc_lock);
1809 FOREACH_PROC_IN_SYSTEM(p) {
1810 vm_pindex_t limit, size;
1813 * if this is a system process or if we have already
1814 * looked at this process, skip it.
1817 if (p->p_state != PRS_NORMAL ||
1818 p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) {
1823 * if the process is in a non-running type state,
1827 FOREACH_THREAD_IN_PROC(p, td) {
1829 if (!TD_ON_RUNQ(td) &&
1830 !TD_IS_RUNNING(td) &&
1831 !TD_IS_SLEEPING(td) &&
1832 !TD_IS_SUSPENDED(td)) {
1846 lim_rlimit(p, RLIMIT_RSS, &rsslim);
1848 qmin(rsslim.rlim_cur, rsslim.rlim_max));
1851 * let processes that are swapped out really be
1852 * swapped out set the limit to nothing (will force a
1855 if ((p->p_flag & P_INMEM) == 0)
1856 limit = 0; /* XXX */
1857 vm = vmspace_acquire_ref(p);
1862 size = vmspace_resident_count(vm);
1863 if (size >= limit) {
1864 vm_pageout_map_deactivate_pages(
1865 &vm->vm_map, limit);
1868 rsize = IDX_TO_OFF(size);
1870 racct_set(p, RACCT_RSS, rsize);
1871 ravailable = racct_get_available(p, RACCT_RSS);
1873 if (rsize > ravailable) {
1875 * Don't be overly aggressive; this might be
1876 * an innocent process, and the limit could've
1877 * been exceeded by some memory hog. Don't
1878 * try to deactivate more than 1/4th of process'
1879 * resident set size.
1881 if (attempts <= 8) {
1882 if (ravailable < rsize - (rsize / 4))
1883 ravailable = rsize - (rsize / 4);
1885 vm_pageout_map_deactivate_pages(
1886 &vm->vm_map, OFF_TO_IDX(ravailable));
1887 /* Update RSS usage after paging out. */
1888 size = vmspace_resident_count(vm);
1889 rsize = IDX_TO_OFF(size);
1891 racct_set(p, RACCT_RSS, rsize);
1893 if (rsize > ravailable)
1899 sx_sunlock(&allproc_lock);
1900 if (tryagain != 0 && attempts <= 10)
1904 #endif /* !defined(NO_SWAPPING) */