2 * Copyright (c) 1991 Regents of the University of California.
4 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
8 * Copyright (c) 2005 Yahoo! Technologies Norway AS
11 * This code is derived from software contributed to Berkeley by
12 * The Mach Operating System project at Carnegie-Mellon University.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91
45 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46 * All rights reserved.
48 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
50 * Permission to use, copy, modify and distribute this software and
51 * its documentation is hereby granted, provided that both the copyright
52 * notice and this permission notice appear in all copies of the
53 * software, derivative works or modified versions, and any portions
54 * thereof, and that both notices appear in supporting documentation.
56 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
57 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
58 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
60 * Carnegie Mellon requests users of this software to return to
62 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
63 * School of Computer Science
64 * Carnegie Mellon University
65 * Pittsburgh PA 15213-3890
67 * any improvements or extensions that they make and grant Carnegie the
68 * rights to redistribute these changes.
72 * The proverbial page-out daemon.
75 #include <sys/cdefs.h>
76 __FBSDID("$FreeBSD$");
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/eventhandler.h>
84 #include <sys/mutex.h>
86 #include <sys/kthread.h>
88 #include <sys/mount.h>
89 #include <sys/racct.h>
90 #include <sys/resourcevar.h>
91 #include <sys/sched.h>
92 #include <sys/signalvar.h>
93 #include <sys/vnode.h>
94 #include <sys/vmmeter.h>
96 #include <sys/sysctl.h>
99 #include <vm/vm_param.h>
100 #include <vm/vm_object.h>
101 #include <vm/vm_page.h>
102 #include <vm/vm_map.h>
103 #include <vm/vm_pageout.h>
104 #include <vm/vm_pager.h>
105 #include <vm/swap_pager.h>
106 #include <vm/vm_extern.h>
110 * System initialization
113 /* the kernel process "vm_pageout"*/
114 static void vm_pageout(void);
115 static int vm_pageout_clean(vm_page_t);
116 static void vm_pageout_scan(int pass);
118 struct proc *pageproc;
120 static struct kproc_desc page_kp = {
125 SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start,
128 #if !defined(NO_SWAPPING)
129 /* the kernel process "vm_daemon"*/
130 static void vm_daemon(void);
131 static struct proc *vmproc;
133 static struct kproc_desc vm_kp = {
138 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp);
142 int vm_pages_needed; /* Event on which pageout daemon sleeps */
143 int vm_pageout_deficit; /* Estimated number of pages deficit */
144 int vm_pageout_pages_needed; /* flag saying that the pageout daemon needs pages */
146 #if !defined(NO_SWAPPING)
147 static int vm_pageout_req_swapout; /* XXX */
148 static int vm_daemon_needed;
149 static struct mtx vm_daemon_mtx;
150 /* Allow for use by vm_pageout before vm_daemon is initialized. */
151 MTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF);
153 static int vm_max_launder = 32;
154 static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
155 static int vm_pageout_full_stats_interval = 0;
156 static int vm_pageout_algorithm=0;
157 static int defer_swap_pageouts=0;
158 static int disable_swap_pageouts=0;
160 #if defined(NO_SWAPPING)
161 static int vm_swap_enabled=0;
162 static int vm_swap_idle_enabled=0;
164 static int vm_swap_enabled=1;
165 static int vm_swap_idle_enabled=0;
168 SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
169 CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
171 SYSCTL_INT(_vm, OID_AUTO, max_launder,
172 CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
174 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
175 CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
177 SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
178 CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
180 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
181 CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
183 #if defined(NO_SWAPPING)
184 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
185 CTLFLAG_RD, &vm_swap_enabled, 0, "Enable entire process swapout");
186 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
187 CTLFLAG_RD, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
189 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
190 CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
191 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
192 CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
195 SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
196 CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
198 SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
199 CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
201 static int pageout_lock_miss;
202 SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
203 CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
205 #define VM_PAGEOUT_PAGE_COUNT 16
206 int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
208 int vm_page_max_wired; /* XXX max # of wired pages system-wide */
209 SYSCTL_INT(_vm, OID_AUTO, max_wired,
210 CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count");
212 #if !defined(NO_SWAPPING)
213 static void vm_pageout_map_deactivate_pages(vm_map_t, long);
214 static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long);
215 static void vm_req_vmdaemon(int req);
217 static void vm_pageout_page_stats(void);
220 * Initialize a dummy page for marking the caller's place in the specified
221 * paging queue. In principle, this function only needs to set the flag
222 * PG_MARKER. Nonetheless, it sets the flag VPO_BUSY and initializes the hold
223 * count to one as safety precautions.
226 vm_pageout_init_marker(vm_page_t marker, u_short queue)
229 bzero(marker, sizeof(*marker));
230 marker->flags = PG_MARKER;
231 marker->oflags = VPO_BUSY;
232 marker->queue = queue;
233 marker->hold_count = 1;
237 * vm_pageout_fallback_object_lock:
239 * Lock vm object currently associated with `m'. VM_OBJECT_TRYLOCK is
240 * known to have failed and page queue must be either PQ_ACTIVE or
241 * PQ_INACTIVE. To avoid lock order violation, unlock the page queues
242 * while locking the vm object. Use marker page to detect page queue
243 * changes and maintain notion of next page on page queue. Return
244 * TRUE if no changes were detected, FALSE otherwise. vm object is
247 * This function depends on both the lock portion of struct vm_object
248 * and normal struct vm_page being type stable.
251 vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next)
253 struct vm_page marker;
259 vm_pageout_init_marker(&marker, queue);
262 TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl,
264 vm_page_unlock_queues();
266 VM_OBJECT_LOCK(object);
268 vm_page_lock_queues();
270 /* Page queue might have changed. */
271 *next = TAILQ_NEXT(&marker, pageq);
272 unchanged = (m->queue == queue &&
273 m->object == object &&
274 &marker == TAILQ_NEXT(m, pageq));
275 TAILQ_REMOVE(&vm_page_queues[queue].pl,
281 * Lock the page while holding the page queue lock. Use marker page
282 * to detect page queue changes and maintain notion of next page on
283 * page queue. Return TRUE if no changes were detected, FALSE
284 * otherwise. The page is locked on return. The page queue lock might
285 * be dropped and reacquired.
287 * This function depends on normal struct vm_page being type stable.
290 vm_pageout_page_lock(vm_page_t m, vm_page_t *next)
292 struct vm_page marker;
296 vm_page_lock_assert(m, MA_NOTOWNED);
297 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
299 if (vm_page_trylock(m))
303 vm_pageout_init_marker(&marker, queue);
305 TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl, m, &marker, pageq);
306 vm_page_unlock_queues();
308 vm_page_lock_queues();
310 /* Page queue might have changed. */
311 *next = TAILQ_NEXT(&marker, pageq);
312 unchanged = (m->queue == queue && &marker == TAILQ_NEXT(m, pageq));
313 TAILQ_REMOVE(&vm_page_queues[queue].pl, &marker, pageq);
320 * Clean the page and remove it from the laundry.
322 * We set the busy bit to cause potential page faults on this page to
323 * block. Note the careful timing, however, the busy bit isn't set till
324 * late and we cannot do anything that will mess with the page.
327 vm_pageout_clean(vm_page_t m)
330 vm_page_t mc[2*vm_pageout_page_count], pb, ps;
332 int ib, is, page_base;
333 vm_pindex_t pindex = m->pindex;
335 vm_page_lock_assert(m, MA_OWNED);
337 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
340 * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
341 * with the new swapper, but we could have serious problems paging
342 * out other object types if there is insufficient memory.
344 * Unfortunately, checking free memory here is far too late, so the
345 * check has been moved up a procedural level.
349 * Can't clean the page if it's busy or held.
351 KASSERT(m->busy == 0 && (m->oflags & VPO_BUSY) == 0,
352 ("vm_pageout_clean: page %p is busy", m));
353 KASSERT(m->hold_count == 0, ("vm_pageout_clean: page %p is held", m));
356 mc[vm_pageout_page_count] = pb = ps = m;
358 page_base = vm_pageout_page_count;
363 * Scan object for clusterable pages.
365 * We can cluster ONLY if: ->> the page is NOT
366 * clean, wired, busy, held, or mapped into a
367 * buffer, and one of the following:
368 * 1) The page is inactive, or a seldom used
371 * 2) we force the issue.
373 * During heavy mmap/modification loads the pageout
374 * daemon can really fragment the underlying file
375 * due to flushing pages out of order and not trying
376 * align the clusters (which leave sporatic out-of-order
377 * holes). To solve this problem we do the reverse scan
378 * first and attempt to align our cluster, then do a
379 * forward scan if room remains.
382 while (ib && pageout_count < vm_pageout_page_count) {
390 if ((p = vm_page_prev(pb)) == NULL ||
391 (p->oflags & VPO_BUSY) != 0 || p->busy != 0) {
396 vm_page_test_dirty(p);
398 p->queue != PQ_INACTIVE ||
399 p->hold_count != 0) { /* may be undergoing I/O */
405 mc[--page_base] = pb = p;
409 * alignment boundry, stop here and switch directions. Do
412 if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
416 while (pageout_count < vm_pageout_page_count &&
417 pindex + is < object->size) {
420 if ((p = vm_page_next(ps)) == NULL ||
421 (p->oflags & VPO_BUSY) != 0 || p->busy != 0)
424 vm_page_test_dirty(p);
426 p->queue != PQ_INACTIVE ||
427 p->hold_count != 0) { /* may be undergoing I/O */
432 mc[page_base + pageout_count] = ps = p;
438 * If we exhausted our forward scan, continue with the reverse scan
439 * when possible, even past a page boundry. This catches boundry
442 if (ib && pageout_count < vm_pageout_page_count)
446 * we allow reads during pageouts...
448 return (vm_pageout_flush(&mc[page_base], pageout_count, 0, 0, NULL,
453 * vm_pageout_flush() - launder the given pages
455 * The given pages are laundered. Note that we setup for the start of
456 * I/O ( i.e. busy the page ), mark it read-only, and bump the object
457 * reference count all in here rather then in the parent. If we want
458 * the parent to do more sophisticated things we may have to change
461 * Returned runlen is the count of pages between mreq and first
462 * page after mreq with status VM_PAGER_AGAIN.
463 * *eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL
464 * for any page in runlen set.
467 vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen,
470 vm_object_t object = mc[0]->object;
471 int pageout_status[count];
475 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
476 mtx_assert(&vm_page_queue_mtx, MA_NOTOWNED);
479 * Initiate I/O. Bump the vm_page_t->busy counter and
480 * mark the pages read-only.
482 * We do not have to fixup the clean/dirty bits here... we can
483 * allow the pager to do it after the I/O completes.
485 * NOTE! mc[i]->dirty may be partial or fragmented due to an
486 * edge case with file fragments.
488 for (i = 0; i < count; i++) {
489 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL,
490 ("vm_pageout_flush: partially invalid page %p index %d/%d",
492 vm_page_io_start(mc[i]);
493 pmap_remove_write(mc[i]);
495 vm_object_pip_add(object, count);
497 vm_pager_put_pages(object, mc, count, flags, pageout_status);
499 runlen = count - mreq;
502 for (i = 0; i < count; i++) {
503 vm_page_t mt = mc[i];
505 KASSERT(pageout_status[i] == VM_PAGER_PEND ||
506 (mt->aflags & PGA_WRITEABLE) == 0,
507 ("vm_pageout_flush: page %p is not write protected", mt));
508 switch (pageout_status[i]) {
515 * Page outside of range of object. Right now we
516 * essentially lose the changes by pretending it
524 * If page couldn't be paged out, then reactivate the
525 * page so it doesn't clog the inactive list. (We
526 * will try paging out it again later).
529 vm_page_activate(mt);
531 if (eio != NULL && i >= mreq && i - mreq < runlen)
535 if (i >= mreq && i - mreq < runlen)
541 * If the operation is still going, leave the page busy to
542 * block all other accesses. Also, leave the paging in
543 * progress indicator set so that we don't attempt an object
546 if (pageout_status[i] != VM_PAGER_PEND) {
547 vm_object_pip_wakeup(object);
548 vm_page_io_finish(mt);
549 if (vm_page_count_severe()) {
551 vm_page_try_to_cache(mt);
558 return (numpagedout);
561 #if !defined(NO_SWAPPING)
563 * vm_pageout_object_deactivate_pages
565 * Deactivate enough pages to satisfy the inactive target
568 * The object and map must be locked.
571 vm_pageout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object,
574 vm_object_t backing_object, object;
576 int actcount, remove_mode;
578 VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
579 if (first_object->type == OBJT_DEVICE ||
580 first_object->type == OBJT_SG)
582 for (object = first_object;; object = backing_object) {
583 if (pmap_resident_count(pmap) <= desired)
585 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
586 if (object->type == OBJT_PHYS || object->paging_in_progress)
590 if (object->shadow_count > 1)
593 * Scan the object's entire memory queue.
595 TAILQ_FOREACH(p, &object->memq, listq) {
596 if (pmap_resident_count(pmap) <= desired)
598 if ((p->oflags & VPO_BUSY) != 0 || p->busy != 0)
600 PCPU_INC(cnt.v_pdpages);
602 if (p->wire_count != 0 || p->hold_count != 0 ||
603 !pmap_page_exists_quick(pmap, p)) {
607 actcount = pmap_ts_referenced(p);
608 if ((p->aflags & PGA_REFERENCED) != 0) {
611 vm_page_aflag_clear(p, PGA_REFERENCED);
613 if (p->queue != PQ_ACTIVE && actcount != 0) {
615 p->act_count += actcount;
616 } else if (p->queue == PQ_ACTIVE) {
618 p->act_count -= min(p->act_count,
621 (vm_pageout_algorithm ||
622 p->act_count == 0)) {
624 vm_page_deactivate(p);
626 vm_page_lock_queues();
628 vm_page_unlock_queues();
632 if (p->act_count < ACT_MAX -
634 p->act_count += ACT_ADVANCE;
635 vm_page_lock_queues();
637 vm_page_unlock_queues();
639 } else if (p->queue == PQ_INACTIVE)
643 if ((backing_object = object->backing_object) == NULL)
645 VM_OBJECT_LOCK(backing_object);
646 if (object != first_object)
647 VM_OBJECT_UNLOCK(object);
650 if (object != first_object)
651 VM_OBJECT_UNLOCK(object);
655 * deactivate some number of pages in a map, try to do it fairly, but
656 * that is really hard to do.
659 vm_pageout_map_deactivate_pages(map, desired)
664 vm_object_t obj, bigobj;
667 if (!vm_map_trylock(map))
674 * first, search out the biggest object, and try to free pages from
677 tmpe = map->header.next;
678 while (tmpe != &map->header) {
679 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
680 obj = tmpe->object.vm_object;
681 if (obj != NULL && VM_OBJECT_TRYLOCK(obj)) {
682 if (obj->shadow_count <= 1 &&
684 bigobj->resident_page_count < obj->resident_page_count)) {
686 VM_OBJECT_UNLOCK(bigobj);
689 VM_OBJECT_UNLOCK(obj);
692 if (tmpe->wired_count > 0)
693 nothingwired = FALSE;
697 if (bigobj != NULL) {
698 vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired);
699 VM_OBJECT_UNLOCK(bigobj);
702 * Next, hunt around for other pages to deactivate. We actually
703 * do this search sort of wrong -- .text first is not the best idea.
705 tmpe = map->header.next;
706 while (tmpe != &map->header) {
707 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
709 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
710 obj = tmpe->object.vm_object;
713 vm_pageout_object_deactivate_pages(map->pmap, obj, desired);
714 VM_OBJECT_UNLOCK(obj);
721 * Remove all mappings if a process is swapped out, this will free page
724 if (desired == 0 && nothingwired) {
725 pmap_remove(vm_map_pmap(map), vm_map_min(map),
730 #endif /* !defined(NO_SWAPPING) */
733 * vm_pageout_scan does the dirty work for the pageout daemon.
736 vm_pageout_scan(int pass)
739 struct vm_page marker;
740 int page_shortage, maxscan, pcount;
741 int addl_page_shortage;
744 int vnodes_skipped = 0;
746 boolean_t queues_locked;
749 * Decrease registered cache sizes.
751 EVENTHANDLER_INVOKE(vm_lowmem, 0);
753 * We do this explicitly after the caches have been drained above.
757 addl_page_shortage = atomic_readandclear_int(&vm_pageout_deficit);
760 * Calculate the number of pages we want to either free or move
763 page_shortage = vm_paging_target() + addl_page_shortage;
765 vm_pageout_init_marker(&marker, PQ_INACTIVE);
768 * Start scanning the inactive queue for pages we can move to the
769 * cache or free. The scan will stop when the target is reached or
770 * we have scanned the entire inactive queue. Note that m->act_count
771 * is not used to form decisions for the inactive queue, only for the
774 * maxlaunder limits the number of dirty pages we flush per scan.
775 * For most systems a smaller value (16 or 32) is more robust under
776 * extreme memory and disk pressure because any unnecessary writes
777 * to disk can result in extreme performance degredation. However,
778 * systems with excessive dirty pages (especially when MAP_NOSYNC is
779 * used) will die horribly with limited laundering. If the pageout
780 * daemon cannot clean enough pages in the first pass, we let it go
781 * all out in succeeding passes.
783 if ((maxlaunder = vm_max_launder) <= 1)
787 vm_page_lock_queues();
788 queues_locked = TRUE;
789 maxscan = cnt.v_inactive_count;
791 for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
792 m != NULL && maxscan-- > 0 && page_shortage > 0;
794 KASSERT(queues_locked, ("unlocked queues"));
795 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
796 KASSERT(m->queue == PQ_INACTIVE, ("Inactive queue %p", m));
799 next = TAILQ_NEXT(m, pageq);
804 if (m->flags & PG_MARKER)
807 KASSERT((m->flags & PG_FICTITIOUS) == 0,
808 ("Fictitious page %p cannot be in inactive queue", m));
809 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
810 ("Unmanaged page %p cannot be in inactive queue", m));
815 if (!vm_pageout_page_lock(m, &next)) {
817 addl_page_shortage++;
822 * A held page may be undergoing I/O, so skip it.
827 addl_page_shortage++;
832 * Don't mess with busy pages, keep in the front of the
833 * queue, most likely are being paged out.
836 if (!VM_OBJECT_TRYLOCK(object) &&
837 (!vm_pageout_fallback_object_lock(m, &next) ||
838 m->hold_count != 0)) {
839 VM_OBJECT_UNLOCK(object);
841 addl_page_shortage++;
844 if (m->busy || (m->oflags & VPO_BUSY)) {
846 VM_OBJECT_UNLOCK(object);
847 addl_page_shortage++;
852 * We unlock vm_page_queue_mtx, invalidating the
853 * 'next' pointer. Use our marker to remember our
856 TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl,
858 vm_page_unlock_queues();
859 queues_locked = FALSE;
862 * If the object is not being used, we ignore previous
865 if (object->ref_count == 0) {
866 vm_page_aflag_clear(m, PGA_REFERENCED);
867 KASSERT(!pmap_page_is_mapped(m),
868 ("vm_pageout_scan: page %p is mapped", m));
871 * Otherwise, if the page has been referenced while in the
872 * inactive queue, we bump the "activation count" upwards,
873 * making it less likely that the page will be added back to
874 * the inactive queue prematurely again. Here we check the
875 * page tables (or emulated bits, if any), given the upper
876 * level VM system not knowing anything about existing
879 } else if ((m->aflags & PGA_REFERENCED) == 0 &&
880 (actcount = pmap_ts_referenced(m)) != 0) {
883 m->act_count += actcount + ACT_ADVANCE;
884 VM_OBJECT_UNLOCK(object);
889 * If the upper level VM system knows about any page
890 * references, we activate the page. We also set the
891 * "activation count" higher than normal so that we will less
892 * likely place pages back onto the inactive queue again.
894 if ((m->aflags & PGA_REFERENCED) != 0) {
895 vm_page_aflag_clear(m, PGA_REFERENCED);
896 actcount = pmap_ts_referenced(m);
899 m->act_count += actcount + ACT_ADVANCE + 1;
900 VM_OBJECT_UNLOCK(object);
905 * If the upper level VM system does not believe that the page
906 * is fully dirty, but it is mapped for write access, then we
907 * consult the pmap to see if the page's dirty status should
910 if (m->dirty != VM_PAGE_BITS_ALL &&
911 (m->aflags & PGA_WRITEABLE) != 0) {
913 * Avoid a race condition: Unless write access is
914 * removed from the page, another processor could
915 * modify it before all access is removed by the call
916 * to vm_page_cache() below. If vm_page_cache() finds
917 * that the page has been modified when it removes all
918 * access, it panics because it cannot cache dirty
919 * pages. In principle, we could eliminate just write
920 * access here rather than all access. In the expected
921 * case, when there are no last instant modifications
922 * to the page, removing all access will be cheaper
925 if (pmap_is_modified(m))
927 else if (m->dirty == 0)
933 * Invalid pages can be easily freed
936 PCPU_INC(cnt.v_dfree);
938 } else if (m->dirty == 0) {
940 * Clean pages can be placed onto the cache queue.
941 * This effectively frees them.
945 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
947 * Dirty pages need to be paged out, but flushing
948 * a page is extremely expensive verses freeing
949 * a clean page. Rather then artificially limiting
950 * the number of pages we can flush, we instead give
951 * dirty pages extra priority on the inactive queue
952 * by forcing them to be cycled through the queue
953 * twice before being flushed, after which the
954 * (now clean) page will cycle through once more
955 * before being freed. This significantly extends
956 * the thrash point for a heavily loaded machine.
958 m->flags |= PG_WINATCFLS;
959 vm_page_lock_queues();
960 queues_locked = TRUE;
962 } else if (maxlaunder > 0) {
964 * We always want to try to flush some dirty pages if
965 * we encounter them, to keep the system stable.
966 * Normally this number is small, but under extreme
967 * pressure where there are insufficient clean pages
968 * on the inactive queue, we may have to go all out.
970 int swap_pageouts_ok, vfslocked = 0;
971 struct vnode *vp = NULL;
972 struct mount *mp = NULL;
974 if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
975 swap_pageouts_ok = 1;
977 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
978 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
979 vm_page_count_min());
984 * We don't bother paging objects that are "dead".
985 * Those objects are in a "rundown" state.
987 if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
989 VM_OBJECT_UNLOCK(object);
990 vm_page_lock_queues();
991 queues_locked = TRUE;
997 * The object is already known NOT to be dead. It
998 * is possible for the vget() to block the whole
999 * pageout daemon, but the new low-memory handling
1000 * code should prevent it.
1002 * The previous code skipped locked vnodes and, worse,
1003 * reordered pages in the queue. This results in
1004 * completely non-deterministic operation and, on a
1005 * busy system, can lead to extremely non-optimal
1006 * pageouts. For example, it can cause clean pages
1007 * to be freed and dirty pages to be moved to the end
1008 * of the queue. Since dirty pages are also moved to
1009 * the end of the queue once-cleaned, this gives
1010 * way too large a weighting to defering the freeing
1013 * We can't wait forever for the vnode lock, we might
1014 * deadlock due to a vn_read() getting stuck in
1015 * vm_wait while holding this vnode. We skip the
1016 * vnode if we can't get it in a reasonable amount
1019 if (object->type == OBJT_VNODE) {
1021 vp = object->handle;
1022 if (vp->v_type == VREG &&
1023 vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1025 ++pageout_lock_miss;
1026 if (object->flags & OBJ_MIGHTBEDIRTY)
1028 goto unlock_and_continue;
1031 ("vp %p with NULL v_mount", vp));
1032 vm_object_reference_locked(object);
1033 VM_OBJECT_UNLOCK(object);
1034 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1035 if (vget(vp, LK_EXCLUSIVE | LK_TIMELOCK,
1037 VM_OBJECT_LOCK(object);
1038 ++pageout_lock_miss;
1039 if (object->flags & OBJ_MIGHTBEDIRTY)
1042 goto unlock_and_continue;
1044 VM_OBJECT_LOCK(object);
1046 vm_page_lock_queues();
1047 queues_locked = TRUE;
1049 * The page might have been moved to another
1050 * queue during potential blocking in vget()
1051 * above. The page might have been freed and
1052 * reused for another vnode.
1054 if (m->queue != PQ_INACTIVE ||
1055 m->object != object ||
1056 TAILQ_NEXT(m, pageq) != &marker) {
1058 if (object->flags & OBJ_MIGHTBEDIRTY)
1060 goto unlock_and_continue;
1064 * The page may have been busied during the
1065 * blocking in vget(). We don't move the
1066 * page back onto the end of the queue so that
1067 * statistics are more correct if we don't.
1069 if (m->busy || (m->oflags & VPO_BUSY)) {
1071 goto unlock_and_continue;
1075 * If the page has become held it might
1076 * be undergoing I/O, so skip it
1078 if (m->hold_count) {
1081 if (object->flags & OBJ_MIGHTBEDIRTY)
1083 goto unlock_and_continue;
1085 vm_page_unlock_queues();
1086 queues_locked = FALSE;
1090 * If a page is dirty, then it is either being washed
1091 * (but not yet cleaned) or it is still in the
1092 * laundry. If it is still in the laundry, then we
1093 * start the cleaning operation.
1095 * decrement page_shortage on success to account for
1096 * the (future) cleaned page. Otherwise we could wind
1097 * up laundering or cleaning too many pages.
1099 if (vm_pageout_clean(m) != 0) {
1103 unlock_and_continue:
1104 vm_page_lock_assert(m, MA_NOTOWNED);
1105 VM_OBJECT_UNLOCK(object);
1107 if (queues_locked) {
1108 vm_page_unlock_queues();
1109 queues_locked = FALSE;
1113 VFS_UNLOCK_GIANT(vfslocked);
1114 vm_object_deallocate(object);
1115 vn_finished_write(mp);
1117 vm_page_lock_assert(m, MA_NOTOWNED);
1121 VM_OBJECT_UNLOCK(object);
1123 if (!queues_locked) {
1124 vm_page_lock_queues();
1125 queues_locked = TRUE;
1127 next = TAILQ_NEXT(&marker, pageq);
1128 TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl,
1133 * Compute the number of pages we want to try to move from the
1134 * active queue to the inactive queue.
1136 page_shortage = vm_paging_target() +
1137 cnt.v_inactive_target - cnt.v_inactive_count;
1138 page_shortage += addl_page_shortage;
1141 * Scan the active queue for things we can deactivate. We nominally
1142 * track the per-page activity counter and use it to locate
1143 * deactivation candidates.
1145 pcount = cnt.v_active_count;
1146 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1147 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1149 while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
1151 KASSERT(m->queue == PQ_ACTIVE,
1152 ("vm_pageout_scan: page %p isn't active", m));
1154 next = TAILQ_NEXT(m, pageq);
1155 if ((m->flags & PG_MARKER) != 0) {
1159 KASSERT((m->flags & PG_FICTITIOUS) == 0,
1160 ("Fictitious page %p cannot be in active queue", m));
1161 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1162 ("Unmanaged page %p cannot be in active queue", m));
1163 if (!vm_pageout_page_lock(m, &next)) {
1169 if (!VM_OBJECT_TRYLOCK(object) &&
1170 !vm_pageout_fallback_object_lock(m, &next)) {
1171 VM_OBJECT_UNLOCK(object);
1178 * Don't deactivate pages that are busy.
1180 if ((m->busy != 0) ||
1181 (m->oflags & VPO_BUSY) ||
1182 (m->hold_count != 0)) {
1184 VM_OBJECT_UNLOCK(object);
1191 * The count for pagedaemon pages is done after checking the
1192 * page for eligibility...
1197 * Check to see "how much" the page has been used.
1200 if (object->ref_count != 0) {
1201 if (m->aflags & PGA_REFERENCED) {
1204 actcount += pmap_ts_referenced(m);
1206 m->act_count += ACT_ADVANCE + actcount;
1207 if (m->act_count > ACT_MAX)
1208 m->act_count = ACT_MAX;
1213 * Since we have "tested" this bit, we need to clear it now.
1215 vm_page_aflag_clear(m, PGA_REFERENCED);
1218 * Only if an object is currently being used, do we use the
1219 * page activation count stats.
1221 if (actcount && (object->ref_count != 0)) {
1224 m->act_count -= min(m->act_count, ACT_DECLINE);
1225 if (vm_pageout_algorithm ||
1226 object->ref_count == 0 ||
1227 m->act_count == 0) {
1229 if (object->ref_count == 0) {
1230 KASSERT(!pmap_page_is_mapped(m),
1231 ("vm_pageout_scan: page %p is mapped", m));
1235 vm_page_deactivate(m);
1237 vm_page_deactivate(m);
1244 VM_OBJECT_UNLOCK(object);
1247 vm_page_unlock_queues();
1248 #if !defined(NO_SWAPPING)
1250 * Idle process swapout -- run once per second.
1252 if (vm_swap_idle_enabled) {
1254 if (time_second != lsec) {
1255 vm_req_vmdaemon(VM_SWAP_IDLE);
1262 * If we didn't get enough free pages, and we have skipped a vnode
1263 * in a writeable object, wakeup the sync daemon. And kick swapout
1264 * if we did not get enough free pages.
1266 if (vm_paging_target() > 0) {
1267 if (vnodes_skipped && vm_page_count_min())
1268 (void) speedup_syncer();
1269 #if !defined(NO_SWAPPING)
1270 if (vm_swap_enabled && vm_page_count_target())
1271 vm_req_vmdaemon(VM_SWAP_NORMAL);
1276 * If we are critically low on one of RAM or swap and low on
1277 * the other, kill the largest process. However, we avoid
1278 * doing this on the first pass in order to give ourselves a
1279 * chance to flush out dirty vnode-backed pages and to allow
1280 * active pages to be moved to the inactive queue and reclaimed.
1283 ((swap_pager_avail < 64 && vm_page_count_min()) ||
1284 (swap_pager_full && vm_paging_target() > 0)))
1285 vm_pageout_oom(VM_OOM_MEM);
1290 vm_pageout_oom(int shortage)
1292 struct proc *p, *bigproc;
1293 vm_offset_t size, bigsize;
1298 * We keep the process bigproc locked once we find it to keep anyone
1299 * from messing with it; however, there is a possibility of
1300 * deadlock if process B is bigproc and one of it's child processes
1301 * attempts to propagate a signal to B while we are waiting for A's
1302 * lock while walking this list. To avoid this, we don't block on
1303 * the process lock but just skip a process if it is already locked.
1307 sx_slock(&allproc_lock);
1308 FOREACH_PROC_IN_SYSTEM(p) {
1311 if (PROC_TRYLOCK(p) == 0)
1314 * If this is a system, protected or killed process, skip it.
1316 if (p->p_state != PRS_NORMAL ||
1317 (p->p_flag & (P_INEXEC | P_PROTECTED | P_SYSTEM)) ||
1318 (p->p_pid == 1) || P_KILLED(p) ||
1319 ((p->p_pid < 48) && (swap_pager_avail != 0))) {
1324 * If the process is in a non-running type state,
1325 * don't touch it. Check all the threads individually.
1328 FOREACH_THREAD_IN_PROC(p, td) {
1330 if (!TD_ON_RUNQ(td) &&
1331 !TD_IS_RUNNING(td) &&
1332 !TD_IS_SLEEPING(td) &&
1333 !TD_IS_SUSPENDED(td)) {
1345 * get the process size
1347 vm = vmspace_acquire_ref(p);
1352 if (!vm_map_trylock_read(&vm->vm_map)) {
1357 size = vmspace_swap_count(vm);
1358 vm_map_unlock_read(&vm->vm_map);
1359 if (shortage == VM_OOM_MEM)
1360 size += vmspace_resident_count(vm);
1363 * if the this process is bigger than the biggest one
1366 if (size > bigsize) {
1367 if (bigproc != NULL)
1368 PROC_UNLOCK(bigproc);
1374 sx_sunlock(&allproc_lock);
1375 if (bigproc != NULL) {
1376 killproc(bigproc, "out of swap space");
1377 sched_nice(bigproc, PRIO_MIN);
1378 PROC_UNLOCK(bigproc);
1379 wakeup(&cnt.v_free_count);
1384 * This routine tries to maintain the pseudo LRU active queue,
1385 * so that during long periods of time where there is no paging,
1386 * that some statistic accumulation still occurs. This code
1387 * helps the situation where paging just starts to occur.
1390 vm_pageout_page_stats()
1394 int pcount,tpcount; /* Number of pages to check */
1395 static int fullintervalcount = 0;
1399 (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) -
1400 (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
1402 if (page_shortage <= 0)
1405 vm_page_lock_queues();
1406 pcount = cnt.v_active_count;
1407 fullintervalcount += vm_pageout_stats_interval;
1408 if (fullintervalcount < vm_pageout_full_stats_interval) {
1409 tpcount = (int64_t)vm_pageout_stats_max * cnt.v_active_count /
1411 if (pcount > tpcount)
1414 fullintervalcount = 0;
1417 m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
1418 while ((m != NULL) && (pcount-- > 0)) {
1421 KASSERT(m->queue == PQ_ACTIVE,
1422 ("vm_pageout_page_stats: page %p isn't active", m));
1424 next = TAILQ_NEXT(m, pageq);
1425 if ((m->flags & PG_MARKER) != 0) {
1429 vm_page_lock_assert(m, MA_NOTOWNED);
1430 if (!vm_pageout_page_lock(m, &next)) {
1436 if (!VM_OBJECT_TRYLOCK(object) &&
1437 !vm_pageout_fallback_object_lock(m, &next)) {
1438 VM_OBJECT_UNLOCK(object);
1445 * Don't deactivate pages that are busy.
1447 if ((m->busy != 0) ||
1448 (m->oflags & VPO_BUSY) ||
1449 (m->hold_count != 0)) {
1451 VM_OBJECT_UNLOCK(object);
1458 if (m->aflags & PGA_REFERENCED) {
1459 vm_page_aflag_clear(m, PGA_REFERENCED);
1463 actcount += pmap_ts_referenced(m);
1465 m->act_count += ACT_ADVANCE + actcount;
1466 if (m->act_count > ACT_MAX)
1467 m->act_count = ACT_MAX;
1470 if (m->act_count == 0) {
1472 * We turn off page access, so that we have
1473 * more accurate RSS stats. We don't do this
1474 * in the normal page deactivation when the
1475 * system is loaded VM wise, because the
1476 * cost of the large number of page protect
1477 * operations would be higher than the value
1478 * of doing the operation.
1481 vm_page_deactivate(m);
1483 m->act_count -= min(m->act_count, ACT_DECLINE);
1488 VM_OBJECT_UNLOCK(object);
1491 vm_page_unlock_queues();
1495 * vm_pageout is the high level pageout daemon.
1503 * Initialize some paging parameters.
1505 cnt.v_interrupt_free_min = 2;
1506 if (cnt.v_page_count < 2000)
1507 vm_pageout_page_count = 8;
1510 * v_free_reserved needs to include enough for the largest
1511 * swap pager structures plus enough for any pv_entry structs
1514 if (cnt.v_page_count > 1024)
1515 cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
1518 cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
1519 cnt.v_interrupt_free_min;
1520 cnt.v_free_reserved = vm_pageout_page_count +
1521 cnt.v_pageout_free_min + (cnt.v_page_count / 768);
1522 cnt.v_free_severe = cnt.v_free_min / 2;
1523 cnt.v_free_min += cnt.v_free_reserved;
1524 cnt.v_free_severe += cnt.v_free_reserved;
1527 * v_free_target and v_cache_min control pageout hysteresis. Note
1528 * that these are more a measure of the VM cache queue hysteresis
1529 * then the VM free queue. Specifically, v_free_target is the
1530 * high water mark (free+cache pages).
1532 * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
1533 * low water mark, while v_free_min is the stop. v_cache_min must
1534 * be big enough to handle memory needs while the pageout daemon
1535 * is signalled and run to free more pages.
1537 if (cnt.v_free_count > 6144)
1538 cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved;
1540 cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved;
1542 if (cnt.v_free_count > 2048) {
1543 cnt.v_cache_min = cnt.v_free_target;
1544 cnt.v_cache_max = 2 * cnt.v_cache_min;
1545 cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
1547 cnt.v_cache_min = 0;
1548 cnt.v_cache_max = 0;
1549 cnt.v_inactive_target = cnt.v_free_count / 4;
1551 if (cnt.v_inactive_target > cnt.v_free_count / 3)
1552 cnt.v_inactive_target = cnt.v_free_count / 3;
1554 /* XXX does not really belong here */
1555 if (vm_page_max_wired == 0)
1556 vm_page_max_wired = cnt.v_free_count / 3;
1558 if (vm_pageout_stats_max == 0)
1559 vm_pageout_stats_max = cnt.v_free_target;
1562 * Set interval in seconds for stats scan.
1564 if (vm_pageout_stats_interval == 0)
1565 vm_pageout_stats_interval = 5;
1566 if (vm_pageout_full_stats_interval == 0)
1567 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
1569 swap_pager_swap_init();
1572 * The pageout daemon is never done, so loop forever.
1576 * If we have enough free memory, wakeup waiters. Do
1577 * not clear vm_pages_needed until we reach our target,
1578 * otherwise we may be woken up over and over again and
1579 * waste a lot of cpu.
1581 mtx_lock(&vm_page_queue_free_mtx);
1582 if (vm_pages_needed && !vm_page_count_min()) {
1583 if (!vm_paging_needed())
1584 vm_pages_needed = 0;
1585 wakeup(&cnt.v_free_count);
1587 if (vm_pages_needed) {
1589 * Still not done, take a second pass without waiting
1590 * (unlimited dirty cleaning), otherwise sleep a bit
1595 msleep(&vm_pages_needed,
1596 &vm_page_queue_free_mtx, PVM, "psleep",
1600 * Good enough, sleep & handle stats. Prime the pass
1607 error = msleep(&vm_pages_needed,
1608 &vm_page_queue_free_mtx, PVM, "psleep",
1609 vm_pageout_stats_interval * hz);
1610 if (error && !vm_pages_needed) {
1611 mtx_unlock(&vm_page_queue_free_mtx);
1613 vm_pageout_page_stats();
1617 if (vm_pages_needed)
1619 mtx_unlock(&vm_page_queue_free_mtx);
1620 vm_pageout_scan(pass);
1625 * Unless the free page queue lock is held by the caller, this function
1626 * should be regarded as advisory. Specifically, the caller should
1627 * not msleep() on &cnt.v_free_count following this function unless
1628 * the free page queue lock is held until the msleep() is performed.
1634 if (!vm_pages_needed && curthread->td_proc != pageproc) {
1635 vm_pages_needed = 1;
1636 wakeup(&vm_pages_needed);
1640 #if !defined(NO_SWAPPING)
1642 vm_req_vmdaemon(int req)
1644 static int lastrun = 0;
1646 mtx_lock(&vm_daemon_mtx);
1647 vm_pageout_req_swapout |= req;
1648 if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
1649 wakeup(&vm_daemon_needed);
1652 mtx_unlock(&vm_daemon_mtx);
1658 struct rlimit rsslim;
1662 int breakout, swapout_flags, tryagain, attempts;
1664 uint64_t rsize, ravailable;
1668 mtx_lock(&vm_daemon_mtx);
1670 msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", hz);
1672 msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0);
1674 swapout_flags = vm_pageout_req_swapout;
1675 vm_pageout_req_swapout = 0;
1676 mtx_unlock(&vm_daemon_mtx);
1678 swapout_procs(swapout_flags);
1681 * scan the processes for exceeding their rlimits or if
1682 * process is swapped out -- deactivate pages
1688 sx_slock(&allproc_lock);
1689 FOREACH_PROC_IN_SYSTEM(p) {
1690 vm_pindex_t limit, size;
1693 * if this is a system process or if we have already
1694 * looked at this process, skip it.
1697 if (p->p_state != PRS_NORMAL ||
1698 p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) {
1703 * if the process is in a non-running type state,
1707 FOREACH_THREAD_IN_PROC(p, td) {
1709 if (!TD_ON_RUNQ(td) &&
1710 !TD_IS_RUNNING(td) &&
1711 !TD_IS_SLEEPING(td) &&
1712 !TD_IS_SUSPENDED(td)) {
1726 lim_rlimit(p, RLIMIT_RSS, &rsslim);
1728 qmin(rsslim.rlim_cur, rsslim.rlim_max));
1731 * let processes that are swapped out really be
1732 * swapped out set the limit to nothing (will force a
1735 if ((p->p_flag & P_INMEM) == 0)
1736 limit = 0; /* XXX */
1737 vm = vmspace_acquire_ref(p);
1742 size = vmspace_resident_count(vm);
1743 if (limit >= 0 && size >= limit) {
1744 vm_pageout_map_deactivate_pages(
1745 &vm->vm_map, limit);
1748 rsize = IDX_TO_OFF(size);
1750 racct_set(p, RACCT_RSS, rsize);
1751 ravailable = racct_get_available(p, RACCT_RSS);
1753 if (rsize > ravailable) {
1755 * Don't be overly aggressive; this might be
1756 * an innocent process, and the limit could've
1757 * been exceeded by some memory hog. Don't
1758 * try to deactivate more than 1/4th of process'
1759 * resident set size.
1761 if (attempts <= 8) {
1762 if (ravailable < rsize - (rsize / 4))
1763 ravailable = rsize - (rsize / 4);
1765 vm_pageout_map_deactivate_pages(
1766 &vm->vm_map, OFF_TO_IDX(ravailable));
1767 /* Update RSS usage after paging out. */
1768 size = vmspace_resident_count(vm);
1769 rsize = IDX_TO_OFF(size);
1771 racct_set(p, RACCT_RSS, rsize);
1773 if (rsize > ravailable)
1779 sx_sunlock(&allproc_lock);
1780 if (tryagain != 0 && attempts <= 10)
1784 #endif /* !defined(NO_SWAPPING) */