The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/vm/vm_pageout.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1991 Regents of the University of California.
    3  * All rights reserved.
    4  * Copyright (c) 1994 John S. Dyson
    5  * All rights reserved.
    6  * Copyright (c) 1994 David Greenman
    7  * All rights reserved.
    8  * Copyright (c) 2005 Yahoo! Technologies Norway AS
    9  * All rights reserved.
   10  *
   11  * This code is derived from software contributed to Berkeley by
   12  * The Mach Operating System project at Carnegie-Mellon University.
   13  *
   14  * Redistribution and use in source and binary forms, with or without
   15  * modification, are permitted provided that the following conditions
   16  * are met:
   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.
   29  *
   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
   40  * SUCH DAMAGE.
   41  *
   42  *      from: @(#)vm_pageout.c  7.4 (Berkeley) 5/7/91
   43  *
   44  *
   45  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
   46  * All rights reserved.
   47  *
   48  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
   49  *
   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.
   55  *
   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.
   59  *
   60  * Carnegie Mellon requests users of this software to return to
   61  *
   62  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
   63  *  School of Computer Science
   64  *  Carnegie Mellon University
   65  *  Pittsburgh PA 15213-3890
   66  *
   67  * any improvements or extensions that they make and grant Carnegie the
   68  * rights to redistribute these changes.
   69  */
   70 
   71 /*
   72  *      The proverbial page-out daemon.
   73  */
   74 
   75 #include <sys/cdefs.h>
   76 __FBSDID("$FreeBSD: releng/8.2/sys/vm/vm_pageout.c 215866 2010-11-26 15:30:02Z kib $");
   77 
   78 #include "opt_vm.h"
   79 #include <sys/param.h>
   80 #include <sys/systm.h>
   81 #include <sys/kernel.h>
   82 #include <sys/eventhandler.h>
   83 #include <sys/lock.h>
   84 #include <sys/mutex.h>
   85 #include <sys/proc.h>
   86 #include <sys/kthread.h>
   87 #include <sys/ktr.h>
   88 #include <sys/mount.h>
   89 #include <sys/resourcevar.h>
   90 #include <sys/sched.h>
   91 #include <sys/signalvar.h>
   92 #include <sys/vnode.h>
   93 #include <sys/vmmeter.h>
   94 #include <sys/sx.h>
   95 #include <sys/sysctl.h>
   96 
   97 #include <vm/vm.h>
   98 #include <vm/vm_param.h>
   99 #include <vm/vm_object.h>
  100 #include <vm/vm_page.h>
  101 #include <vm/vm_map.h>
  102 #include <vm/vm_pageout.h>
  103 #include <vm/vm_pager.h>
  104 #include <vm/swap_pager.h>
  105 #include <vm/vm_extern.h>
  106 #include <vm/uma.h>
  107 
  108 /*
  109  * System initialization
  110  */
  111 
  112 /* the kernel process "vm_pageout"*/
  113 static void vm_pageout(void);
  114 static int vm_pageout_clean(vm_page_t);
  115 static void vm_pageout_scan(int pass);
  116 
  117 struct proc *pageproc;
  118 
  119 static struct kproc_desc page_kp = {
  120         "pagedaemon",
  121         vm_pageout,
  122         &pageproc
  123 };
  124 SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start,
  125     &page_kp);
  126 
  127 #if !defined(NO_SWAPPING)
  128 /* the kernel process "vm_daemon"*/
  129 static void vm_daemon(void);
  130 static struct   proc *vmproc;
  131 
  132 static struct kproc_desc vm_kp = {
  133         "vmdaemon",
  134         vm_daemon,
  135         &vmproc
  136 };
  137 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp);
  138 #endif
  139 
  140 
  141 int vm_pages_needed;            /* Event on which pageout daemon sleeps */
  142 int vm_pageout_deficit;         /* Estimated number of pages deficit */
  143 int vm_pageout_pages_needed;    /* flag saying that the pageout daemon needs pages */
  144 
  145 #if !defined(NO_SWAPPING)
  146 static int vm_pageout_req_swapout;      /* XXX */
  147 static int vm_daemon_needed;
  148 static struct mtx vm_daemon_mtx;
  149 /* Allow for use by vm_pageout before vm_daemon is initialized. */
  150 MTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF);
  151 #endif
  152 static int vm_max_launder = 32;
  153 static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0;
  154 static int vm_pageout_full_stats_interval = 0;
  155 static int vm_pageout_algorithm=0;
  156 static int defer_swap_pageouts=0;
  157 static int disable_swap_pageouts=0;
  158 
  159 #if defined(NO_SWAPPING)
  160 static int vm_swap_enabled=0;
  161 static int vm_swap_idle_enabled=0;
  162 #else
  163 static int vm_swap_enabled=1;
  164 static int vm_swap_idle_enabled=0;
  165 #endif
  166 
  167 SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm,
  168         CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt");
  169 
  170 SYSCTL_INT(_vm, OID_AUTO, max_launder,
  171         CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
  172 
  173 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max,
  174         CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length");
  175 
  176 SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval,
  177         CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan");
  178 
  179 SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval,
  180         CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan");
  181 
  182 #if defined(NO_SWAPPING)
  183 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
  184         CTLFLAG_RD, &vm_swap_enabled, 0, "Enable entire process swapout");
  185 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
  186         CTLFLAG_RD, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
  187 #else
  188 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
  189         CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
  190 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
  191         CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
  192 #endif
  193 
  194 SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
  195         CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
  196 
  197 SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
  198         CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
  199 
  200 static int pageout_lock_miss;
  201 SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
  202         CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
  203 
  204 #define VM_PAGEOUT_PAGE_COUNT 16
  205 int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
  206 
  207 int vm_page_max_wired;          /* XXX max # of wired pages system-wide */
  208 SYSCTL_INT(_vm, OID_AUTO, max_wired,
  209         CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count");
  210 
  211 #if !defined(NO_SWAPPING)
  212 static void vm_pageout_map_deactivate_pages(vm_map_t, long);
  213 static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long);
  214 static void vm_req_vmdaemon(int req);
  215 #endif
  216 static void vm_pageout_page_stats(void);
  217 
  218 /*
  219  * vm_pageout_fallback_object_lock:
  220  * 
  221  * Lock vm object currently associated with `m'. VM_OBJECT_TRYLOCK is
  222  * known to have failed and page queue must be either PQ_ACTIVE or
  223  * PQ_INACTIVE.  To avoid lock order violation, unlock the page queues
  224  * while locking the vm object.  Use marker page to detect page queue
  225  * changes and maintain notion of next page on page queue.  Return
  226  * TRUE if no changes were detected, FALSE otherwise.  vm object is
  227  * locked on return.
  228  * 
  229  * This function depends on both the lock portion of struct vm_object
  230  * and normal struct vm_page being type stable.
  231  */
  232 boolean_t
  233 vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next)
  234 {
  235         struct vm_page marker;
  236         boolean_t unchanged;
  237         u_short queue;
  238         vm_object_t object;
  239 
  240         /*
  241          * Initialize our marker
  242          */
  243         bzero(&marker, sizeof(marker));
  244         marker.flags = PG_FICTITIOUS | PG_MARKER;
  245         marker.oflags = VPO_BUSY;
  246         marker.queue = m->queue;
  247         marker.wire_count = 1;
  248 
  249         queue = m->queue;
  250         object = m->object;
  251         
  252         TAILQ_INSERT_AFTER(&vm_page_queues[queue].pl,
  253                            m, &marker, pageq);
  254         vm_page_unlock_queues();
  255         VM_OBJECT_LOCK(object);
  256         vm_page_lock_queues();
  257 
  258         /* Page queue might have changed. */
  259         *next = TAILQ_NEXT(&marker, pageq);
  260         unchanged = (m->queue == queue &&
  261                      m->object == object &&
  262                      &marker == TAILQ_NEXT(m, pageq));
  263         TAILQ_REMOVE(&vm_page_queues[queue].pl,
  264                      &marker, pageq);
  265         return (unchanged);
  266 }
  267 
  268 /*
  269  * vm_pageout_clean:
  270  *
  271  * Clean the page and remove it from the laundry.
  272  * 
  273  * We set the busy bit to cause potential page faults on this page to
  274  * block.  Note the careful timing, however, the busy bit isn't set till
  275  * late and we cannot do anything that will mess with the page.
  276  */
  277 static int
  278 vm_pageout_clean(m)
  279         vm_page_t m;
  280 {
  281         vm_object_t object;
  282         vm_page_t mc[2*vm_pageout_page_count], pb, ps;
  283         int pageout_count;
  284         int ib, is, page_base;
  285         vm_pindex_t pindex = m->pindex;
  286 
  287         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
  288         VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
  289 
  290         /*
  291          * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
  292          * with the new swapper, but we could have serious problems paging
  293          * out other object types if there is insufficient memory.  
  294          *
  295          * Unfortunately, checking free memory here is far too late, so the
  296          * check has been moved up a procedural level.
  297          */
  298 
  299         /*
  300          * Can't clean the page if it's busy or held.
  301          */
  302         if ((m->hold_count != 0) ||
  303             ((m->busy != 0) || (m->oflags & VPO_BUSY))) {
  304                 return 0;
  305         }
  306 
  307         mc[vm_pageout_page_count] = pb = ps = m;
  308         pageout_count = 1;
  309         page_base = vm_pageout_page_count;
  310         ib = 1;
  311         is = 1;
  312 
  313         /*
  314          * Scan object for clusterable pages.
  315          *
  316          * We can cluster ONLY if: ->> the page is NOT
  317          * clean, wired, busy, held, or mapped into a
  318          * buffer, and one of the following:
  319          * 1) The page is inactive, or a seldom used
  320          *    active page.
  321          * -or-
  322          * 2) we force the issue.
  323          *
  324          * During heavy mmap/modification loads the pageout
  325          * daemon can really fragment the underlying file
  326          * due to flushing pages out of order and not trying
  327          * align the clusters (which leave sporatic out-of-order
  328          * holes).  To solve this problem we do the reverse scan
  329          * first and attempt to align our cluster, then do a 
  330          * forward scan if room remains.
  331          */
  332         object = m->object;
  333 more:
  334         while (ib && pageout_count < vm_pageout_page_count) {
  335                 vm_page_t p;
  336 
  337                 if (ib > pindex) {
  338                         ib = 0;
  339                         break;
  340                 }
  341 
  342                 if ((p = vm_page_prev(pb)) == NULL ||
  343                     (p->oflags & VPO_BUSY) != 0 || p->busy != 0) {
  344                         ib = 0;
  345                         break;
  346                 }
  347                 vm_page_test_dirty(p);
  348                 if (p->dirty == 0 ||
  349                     p->queue != PQ_INACTIVE ||
  350                     p->hold_count != 0) {       /* may be undergoing I/O */
  351                         ib = 0;
  352                         break;
  353                 }
  354                 mc[--page_base] = pb = p;
  355                 ++pageout_count;
  356                 ++ib;
  357                 /*
  358                  * alignment boundry, stop here and switch directions.  Do
  359                  * not clear ib.
  360                  */
  361                 if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
  362                         break;
  363         }
  364 
  365         while (pageout_count < vm_pageout_page_count && 
  366             pindex + is < object->size) {
  367                 vm_page_t p;
  368 
  369                 if ((p = vm_page_next(ps)) == NULL ||
  370                     (p->oflags & VPO_BUSY) != 0 || p->busy != 0)
  371                         break;
  372                 vm_page_test_dirty(p);
  373                 if (p->dirty == 0 ||
  374                     p->queue != PQ_INACTIVE ||
  375                     p->hold_count != 0) {       /* may be undergoing I/O */
  376                         break;
  377                 }
  378                 mc[page_base + pageout_count] = ps = p;
  379                 ++pageout_count;
  380                 ++is;
  381         }
  382 
  383         /*
  384          * If we exhausted our forward scan, continue with the reverse scan
  385          * when possible, even past a page boundry.  This catches boundry
  386          * conditions.
  387          */
  388         if (ib && pageout_count < vm_pageout_page_count)
  389                 goto more;
  390 
  391         /*
  392          * we allow reads during pageouts...
  393          */
  394         return (vm_pageout_flush(&mc[page_base], pageout_count, 0, 0, NULL));
  395 }
  396 
  397 /*
  398  * vm_pageout_flush() - launder the given pages
  399  *
  400  *      The given pages are laundered.  Note that we setup for the start of
  401  *      I/O ( i.e. busy the page ), mark it read-only, and bump the object
  402  *      reference count all in here rather then in the parent.  If we want
  403  *      the parent to do more sophisticated things we may have to change
  404  *      the ordering.
  405  *
  406  *      Returned runlen is the count of pages between mreq and first
  407  *      page after mreq with status VM_PAGER_AGAIN.
  408  */
  409 int
  410 vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen)
  411 {
  412         vm_object_t object = mc[0]->object;
  413         int pageout_status[count];
  414         int numpagedout = 0;
  415         int i, runlen;
  416 
  417         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
  418         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
  419         /*
  420          * Initiate I/O.  Bump the vm_page_t->busy counter and
  421          * mark the pages read-only.
  422          *
  423          * We do not have to fixup the clean/dirty bits here... we can
  424          * allow the pager to do it after the I/O completes.
  425          *
  426          * NOTE! mc[i]->dirty may be partial or fragmented due to an
  427          * edge case with file fragments.
  428          */
  429         for (i = 0; i < count; i++) {
  430                 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL,
  431                     ("vm_pageout_flush: partially invalid page %p index %d/%d",
  432                         mc[i], i, count));
  433                 vm_page_io_start(mc[i]);
  434                 pmap_remove_write(mc[i]);
  435         }
  436         vm_page_unlock_queues();
  437         vm_object_pip_add(object, count);
  438 
  439         vm_pager_put_pages(object, mc, count, flags, pageout_status);
  440 
  441         runlen = count - mreq;
  442         vm_page_lock_queues();
  443         for (i = 0; i < count; i++) {
  444                 vm_page_t mt = mc[i];
  445 
  446                 KASSERT(pageout_status[i] == VM_PAGER_PEND ||
  447                     (mt->flags & PG_WRITEABLE) == 0,
  448                     ("vm_pageout_flush: page %p is not write protected", mt));
  449                 switch (pageout_status[i]) {
  450                 case VM_PAGER_OK:
  451                 case VM_PAGER_PEND:
  452                         numpagedout++;
  453                         break;
  454                 case VM_PAGER_BAD:
  455                         /*
  456                          * Page outside of range of object. Right now we
  457                          * essentially lose the changes by pretending it
  458                          * worked.
  459                          */
  460                         vm_page_undirty(mt);
  461                         break;
  462                 case VM_PAGER_ERROR:
  463                 case VM_PAGER_FAIL:
  464                         /*
  465                          * If page couldn't be paged out, then reactivate the
  466                          * page so it doesn't clog the inactive list.  (We
  467                          * will try paging out it again later).
  468                          */
  469                         vm_page_activate(mt);
  470                         break;
  471                 case VM_PAGER_AGAIN:
  472                         if (i >= mreq && i - mreq < runlen)
  473                                 runlen = i - mreq;
  474                         break;
  475                 }
  476 
  477                 /*
  478                  * If the operation is still going, leave the page busy to
  479                  * block all other accesses. Also, leave the paging in
  480                  * progress indicator set so that we don't attempt an object
  481                  * collapse.
  482                  */
  483                 if (pageout_status[i] != VM_PAGER_PEND) {
  484                         vm_object_pip_wakeup(object);
  485                         vm_page_io_finish(mt);
  486                         if (vm_page_count_severe())
  487                                 vm_page_try_to_cache(mt);
  488                 }
  489         }
  490         if (prunlen != NULL)
  491                 *prunlen = runlen;
  492         return (numpagedout);
  493 }
  494 
  495 #if !defined(NO_SWAPPING)
  496 /*
  497  *      vm_pageout_object_deactivate_pages
  498  *
  499  *      deactivate enough pages to satisfy the inactive target
  500  *      requirements or if vm_page_proc_limit is set, then
  501  *      deactivate all of the pages in the object and its
  502  *      backing_objects.
  503  *
  504  *      The object and map must be locked.
  505  */
  506 static void
  507 vm_pageout_object_deactivate_pages(pmap, first_object, desired)
  508         pmap_t pmap;
  509         vm_object_t first_object;
  510         long desired;
  511 {
  512         vm_object_t backing_object, object;
  513         vm_page_t p, next;
  514         int actcount, rcount, remove_mode;
  515 
  516         VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
  517         if (first_object->type == OBJT_DEVICE ||
  518             first_object->type == OBJT_SG ||
  519             first_object->type == OBJT_PHYS)
  520                 return;
  521         for (object = first_object;; object = backing_object) {
  522                 if (pmap_resident_count(pmap) <= desired)
  523                         goto unlock_return;
  524                 if (object->paging_in_progress)
  525                         goto unlock_return;
  526 
  527                 remove_mode = 0;
  528                 if (object->shadow_count > 1)
  529                         remove_mode = 1;
  530                 /*
  531                  * scan the objects entire memory queue
  532                  */
  533                 rcount = object->resident_page_count;
  534                 p = TAILQ_FIRST(&object->memq);
  535                 vm_page_lock_queues();
  536                 while (p && (rcount-- > 0)) {
  537                         if (pmap_resident_count(pmap) <= desired) {
  538                                 vm_page_unlock_queues();
  539                                 goto unlock_return;
  540                         }
  541                         next = TAILQ_NEXT(p, listq);
  542                         cnt.v_pdpages++;
  543                         if (p->wire_count != 0 ||
  544                             p->hold_count != 0 ||
  545                             p->busy != 0 ||
  546                             (p->oflags & VPO_BUSY) ||
  547                             (p->flags & PG_UNMANAGED) ||
  548                             !pmap_page_exists_quick(pmap, p)) {
  549                                 p = next;
  550                                 continue;
  551                         }
  552                         actcount = pmap_ts_referenced(p);
  553                         if (actcount) {
  554                                 vm_page_flag_set(p, PG_REFERENCED);
  555                         } else if (p->flags & PG_REFERENCED) {
  556                                 actcount = 1;
  557                         }
  558                         if ((p->queue != PQ_ACTIVE) &&
  559                                 (p->flags & PG_REFERENCED)) {
  560                                 vm_page_activate(p);
  561                                 p->act_count += actcount;
  562                                 vm_page_flag_clear(p, PG_REFERENCED);
  563                         } else if (p->queue == PQ_ACTIVE) {
  564                                 if ((p->flags & PG_REFERENCED) == 0) {
  565                                         p->act_count -= min(p->act_count, ACT_DECLINE);
  566                                         if (!remove_mode && (vm_pageout_algorithm || (p->act_count == 0))) {
  567                                                 pmap_remove_all(p);
  568                                                 vm_page_deactivate(p);
  569                                         } else {
  570                                                 vm_page_requeue(p);
  571                                         }
  572                                 } else {
  573                                         vm_page_activate(p);
  574                                         vm_page_flag_clear(p, PG_REFERENCED);
  575                                         if (p->act_count < (ACT_MAX - ACT_ADVANCE))
  576                                                 p->act_count += ACT_ADVANCE;
  577                                         vm_page_requeue(p);
  578                                 }
  579                         } else if (p->queue == PQ_INACTIVE) {
  580                                 pmap_remove_all(p);
  581                         }
  582                         p = next;
  583                 }
  584                 vm_page_unlock_queues();
  585                 if ((backing_object = object->backing_object) == NULL)
  586                         goto unlock_return;
  587                 VM_OBJECT_LOCK(backing_object);
  588                 if (object != first_object)
  589                         VM_OBJECT_UNLOCK(object);
  590         }
  591 unlock_return:
  592         if (object != first_object)
  593                 VM_OBJECT_UNLOCK(object);
  594 }
  595 
  596 /*
  597  * deactivate some number of pages in a map, try to do it fairly, but
  598  * that is really hard to do.
  599  */
  600 static void
  601 vm_pageout_map_deactivate_pages(map, desired)
  602         vm_map_t map;
  603         long desired;
  604 {
  605         vm_map_entry_t tmpe;
  606         vm_object_t obj, bigobj;
  607         int nothingwired;
  608 
  609         if (!vm_map_trylock(map))
  610                 return;
  611 
  612         bigobj = NULL;
  613         nothingwired = TRUE;
  614 
  615         /*
  616          * first, search out the biggest object, and try to free pages from
  617          * that.
  618          */
  619         tmpe = map->header.next;
  620         while (tmpe != &map->header) {
  621                 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
  622                         obj = tmpe->object.vm_object;
  623                         if (obj != NULL && VM_OBJECT_TRYLOCK(obj)) {
  624                                 if (obj->shadow_count <= 1 &&
  625                                     (bigobj == NULL ||
  626                                      bigobj->resident_page_count < obj->resident_page_count)) {
  627                                         if (bigobj != NULL)
  628                                                 VM_OBJECT_UNLOCK(bigobj);
  629                                         bigobj = obj;
  630                                 } else
  631                                         VM_OBJECT_UNLOCK(obj);
  632                         }
  633                 }
  634                 if (tmpe->wired_count > 0)
  635                         nothingwired = FALSE;
  636                 tmpe = tmpe->next;
  637         }
  638 
  639         if (bigobj != NULL) {
  640                 vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired);
  641                 VM_OBJECT_UNLOCK(bigobj);
  642         }
  643         /*
  644          * Next, hunt around for other pages to deactivate.  We actually
  645          * do this search sort of wrong -- .text first is not the best idea.
  646          */
  647         tmpe = map->header.next;
  648         while (tmpe != &map->header) {
  649                 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
  650                         break;
  651                 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
  652                         obj = tmpe->object.vm_object;
  653                         if (obj != NULL) {
  654                                 VM_OBJECT_LOCK(obj);
  655                                 vm_pageout_object_deactivate_pages(map->pmap, obj, desired);
  656                                 VM_OBJECT_UNLOCK(obj);
  657                         }
  658                 }
  659                 tmpe = tmpe->next;
  660         }
  661 
  662         /*
  663          * Remove all mappings if a process is swapped out, this will free page
  664          * table pages.
  665          */
  666         if (desired == 0 && nothingwired) {
  667                 tmpe = map->header.next;
  668                 while (tmpe != &map->header) {
  669                         pmap_remove(vm_map_pmap(map), tmpe->start, tmpe->end);
  670                         tmpe = tmpe->next;
  671                 }
  672         }
  673         vm_map_unlock(map);
  674 }
  675 #endif          /* !defined(NO_SWAPPING) */
  676 
  677 /*
  678  *      vm_pageout_scan does the dirty work for the pageout daemon.
  679  */
  680 static void
  681 vm_pageout_scan(int pass)
  682 {
  683         vm_page_t m, next;
  684         struct vm_page marker;
  685         int page_shortage, maxscan, pcount;
  686         int addl_page_shortage, addl_page_shortage_init;
  687         vm_object_t object;
  688         int actcount;
  689         int vnodes_skipped = 0;
  690         int maxlaunder;
  691 
  692         /*
  693          * Decrease registered cache sizes.
  694          */
  695         EVENTHANDLER_INVOKE(vm_lowmem, 0);
  696         /*
  697          * We do this explicitly after the caches have been drained above.
  698          */
  699         uma_reclaim();
  700 
  701         addl_page_shortage_init = atomic_readandclear_int(&vm_pageout_deficit);
  702 
  703         /*
  704          * Calculate the number of pages we want to either free or move
  705          * to the cache.
  706          */
  707         page_shortage = vm_paging_target() + addl_page_shortage_init;
  708 
  709         /*
  710          * Initialize our marker
  711          */
  712         bzero(&marker, sizeof(marker));
  713         marker.flags = PG_FICTITIOUS | PG_MARKER;
  714         marker.oflags = VPO_BUSY;
  715         marker.queue = PQ_INACTIVE;
  716         marker.wire_count = 1;
  717 
  718         /*
  719          * Start scanning the inactive queue for pages we can move to the
  720          * cache or free.  The scan will stop when the target is reached or
  721          * we have scanned the entire inactive queue.  Note that m->act_count
  722          * is not used to form decisions for the inactive queue, only for the
  723          * active queue.
  724          *
  725          * maxlaunder limits the number of dirty pages we flush per scan.
  726          * For most systems a smaller value (16 or 32) is more robust under
  727          * extreme memory and disk pressure because any unnecessary writes
  728          * to disk can result in extreme performance degredation.  However,
  729          * systems with excessive dirty pages (especially when MAP_NOSYNC is
  730          * used) will die horribly with limited laundering.  If the pageout
  731          * daemon cannot clean enough pages in the first pass, we let it go
  732          * all out in succeeding passes.
  733          */
  734         if ((maxlaunder = vm_max_launder) <= 1)
  735                 maxlaunder = 1;
  736         if (pass)
  737                 maxlaunder = 10000;
  738         vm_page_lock_queues();
  739 rescan0:
  740         addl_page_shortage = addl_page_shortage_init;
  741         maxscan = cnt.v_inactive_count;
  742 
  743         for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl);
  744              m != NULL && maxscan-- > 0 && page_shortage > 0;
  745              m = next) {
  746 
  747                 cnt.v_pdpages++;
  748 
  749                 if (VM_PAGE_GETQUEUE(m) != PQ_INACTIVE) {
  750                         goto rescan0;
  751                 }
  752 
  753                 next = TAILQ_NEXT(m, pageq);
  754                 object = m->object;
  755 
  756                 /*
  757                  * skip marker pages
  758                  */
  759                 if (m->flags & PG_MARKER)
  760                         continue;
  761 
  762                 /*
  763                  * A held page may be undergoing I/O, so skip it.
  764                  */
  765                 if (m->hold_count) {
  766                         vm_page_requeue(m);
  767                         addl_page_shortage++;
  768                         continue;
  769                 }
  770                 /*
  771                  * Don't mess with busy pages, keep in the front of the
  772                  * queue, most likely are being paged out.
  773                  */
  774                 if (!VM_OBJECT_TRYLOCK(object) &&
  775                     (!vm_pageout_fallback_object_lock(m, &next) ||
  776                      m->hold_count != 0)) {
  777                         VM_OBJECT_UNLOCK(object);
  778                         addl_page_shortage++;
  779                         continue;
  780                 }
  781                 if (m->busy || (m->oflags & VPO_BUSY)) {
  782                         VM_OBJECT_UNLOCK(object);
  783                         addl_page_shortage++;
  784                         continue;
  785                 }
  786 
  787                 /*
  788                  * If the object is not being used, we ignore previous 
  789                  * references.
  790                  */
  791                 if (object->ref_count == 0) {
  792                         vm_page_flag_clear(m, PG_REFERENCED);
  793                         KASSERT(!pmap_page_is_mapped(m),
  794                             ("vm_pageout_scan: page %p is mapped", m));
  795 
  796                 /*
  797                  * Otherwise, if the page has been referenced while in the 
  798                  * inactive queue, we bump the "activation count" upwards, 
  799                  * making it less likely that the page will be added back to 
  800                  * the inactive queue prematurely again.  Here we check the 
  801                  * page tables (or emulated bits, if any), given the upper 
  802                  * level VM system not knowing anything about existing 
  803                  * references.
  804                  */
  805                 } else if (((m->flags & PG_REFERENCED) == 0) &&
  806                         (actcount = pmap_ts_referenced(m))) {
  807                         vm_page_activate(m);
  808                         VM_OBJECT_UNLOCK(object);
  809                         m->act_count += (actcount + ACT_ADVANCE);
  810                         continue;
  811                 }
  812 
  813                 /*
  814                  * If the upper level VM system knows about any page 
  815                  * references, we activate the page.  We also set the 
  816                  * "activation count" higher than normal so that we will less 
  817                  * likely place pages back onto the inactive queue again.
  818                  */
  819                 if ((m->flags & PG_REFERENCED) != 0) {
  820                         vm_page_flag_clear(m, PG_REFERENCED);
  821                         actcount = pmap_ts_referenced(m);
  822                         vm_page_activate(m);
  823                         VM_OBJECT_UNLOCK(object);
  824                         m->act_count += (actcount + ACT_ADVANCE + 1);
  825                         continue;
  826                 }
  827 
  828                 /*
  829                  * If the upper level VM system does not believe that the page
  830                  * is fully dirty, but it is mapped for write access, then we
  831                  * consult the pmap to see if the page's dirty status should
  832                  * be updated.
  833                  */
  834                 if (m->dirty != VM_PAGE_BITS_ALL &&
  835                     (m->flags & PG_WRITEABLE) != 0) {
  836                         /*
  837                          * Avoid a race condition: Unless write access is
  838                          * removed from the page, another processor could
  839                          * modify it before all access is removed by the call
  840                          * to vm_page_cache() below.  If vm_page_cache() finds
  841                          * that the page has been modified when it removes all
  842                          * access, it panics because it cannot cache dirty
  843                          * pages.  In principle, we could eliminate just write
  844                          * access here rather than all access.  In the expected
  845                          * case, when there are no last instant modifications
  846                          * to the page, removing all access will be cheaper
  847                          * overall.
  848                          */
  849                         if (pmap_is_modified(m))
  850                                 vm_page_dirty(m);
  851                         else if (m->dirty == 0)
  852                                 pmap_remove_all(m);
  853                 }
  854 
  855                 if (m->valid == 0) {
  856                         /*
  857                          * Invalid pages can be easily freed
  858                          */
  859                         vm_page_free(m);
  860                         cnt.v_dfree++;
  861                         --page_shortage;
  862                 } else if (m->dirty == 0) {
  863                         /*
  864                          * Clean pages can be placed onto the cache queue.
  865                          * This effectively frees them.
  866                          */
  867                         vm_page_cache(m);
  868                         --page_shortage;
  869                 } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) {
  870                         /*
  871                          * Dirty pages need to be paged out, but flushing
  872                          * a page is extremely expensive verses freeing
  873                          * a clean page.  Rather then artificially limiting
  874                          * the number of pages we can flush, we instead give
  875                          * dirty pages extra priority on the inactive queue
  876                          * by forcing them to be cycled through the queue
  877                          * twice before being flushed, after which the
  878                          * (now clean) page will cycle through once more
  879                          * before being freed.  This significantly extends
  880                          * the thrash point for a heavily loaded machine.
  881                          */
  882                         vm_page_flag_set(m, PG_WINATCFLS);
  883                         vm_page_requeue(m);
  884                 } else if (maxlaunder > 0) {
  885                         /*
  886                          * We always want to try to flush some dirty pages if
  887                          * we encounter them, to keep the system stable.
  888                          * Normally this number is small, but under extreme
  889                          * pressure where there are insufficient clean pages
  890                          * on the inactive queue, we may have to go all out.
  891                          */
  892                         int swap_pageouts_ok, vfslocked = 0;
  893                         struct vnode *vp = NULL;
  894                         struct mount *mp = NULL;
  895 
  896                         if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
  897                                 swap_pageouts_ok = 1;
  898                         } else {
  899                                 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
  900                                 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
  901                                 vm_page_count_min());
  902                                                                                 
  903                         }
  904 
  905                         /*
  906                          * We don't bother paging objects that are "dead".  
  907                          * Those objects are in a "rundown" state.
  908                          */
  909                         if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
  910                                 VM_OBJECT_UNLOCK(object);
  911                                 vm_page_requeue(m);
  912                                 continue;
  913                         }
  914 
  915                         /*
  916                          * Following operations may unlock
  917                          * vm_page_queue_mtx, invalidating the 'next'
  918                          * pointer.  To prevent an inordinate number
  919                          * of restarts we use our marker to remember
  920                          * our place.
  921                          *
  922                          */
  923                         TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl,
  924                                            m, &marker, pageq);
  925                         /*
  926                          * The object is already known NOT to be dead.   It
  927                          * is possible for the vget() to block the whole
  928                          * pageout daemon, but the new low-memory handling
  929                          * code should prevent it.
  930                          *
  931                          * The previous code skipped locked vnodes and, worse,
  932                          * reordered pages in the queue.  This results in
  933                          * completely non-deterministic operation and, on a
  934                          * busy system, can lead to extremely non-optimal
  935                          * pageouts.  For example, it can cause clean pages
  936                          * to be freed and dirty pages to be moved to the end
  937                          * of the queue.  Since dirty pages are also moved to
  938                          * the end of the queue once-cleaned, this gives
  939                          * way too large a weighting to defering the freeing
  940                          * of dirty pages.
  941                          *
  942                          * We can't wait forever for the vnode lock, we might
  943                          * deadlock due to a vn_read() getting stuck in
  944                          * vm_wait while holding this vnode.  We skip the 
  945                          * vnode if we can't get it in a reasonable amount
  946                          * of time.
  947                          */
  948                         if (object->type == OBJT_VNODE) {
  949                                 vp = object->handle;
  950                                 if (vp->v_type == VREG &&
  951                                     vn_start_write(vp, &mp, V_NOWAIT) != 0) {
  952                                         mp = NULL;
  953                                         ++pageout_lock_miss;
  954                                         if (object->flags & OBJ_MIGHTBEDIRTY)
  955                                                 vnodes_skipped++;
  956                                         goto unlock_and_continue;
  957                                 }
  958                                 KASSERT(mp != NULL,
  959                                     ("vp %p with NULL v_mount", vp));
  960                                 vm_page_unlock_queues();
  961                                 vm_object_reference_locked(object);
  962                                 VM_OBJECT_UNLOCK(object);
  963                                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
  964                                 if (vget(vp, LK_EXCLUSIVE | LK_TIMELOCK,
  965                                     curthread)) {
  966                                         VM_OBJECT_LOCK(object);
  967                                         vm_page_lock_queues();
  968                                         ++pageout_lock_miss;
  969                                         if (object->flags & OBJ_MIGHTBEDIRTY)
  970                                                 vnodes_skipped++;
  971                                         vp = NULL;
  972                                         goto unlock_and_continue;
  973                                 }
  974                                 VM_OBJECT_LOCK(object);
  975                                 vm_page_lock_queues();
  976                                 /*
  977                                  * The page might have been moved to another
  978                                  * queue during potential blocking in vget()
  979                                  * above.  The page might have been freed and
  980                                  * reused for another vnode.
  981                                  */
  982                                 if (VM_PAGE_GETQUEUE(m) != PQ_INACTIVE ||
  983                                     m->object != object ||
  984                                     TAILQ_NEXT(m, pageq) != &marker) {
  985                                         if (object->flags & OBJ_MIGHTBEDIRTY)
  986                                                 vnodes_skipped++;
  987                                         goto unlock_and_continue;
  988                                 }
  989         
  990                                 /*
  991                                  * The page may have been busied during the
  992                                  * blocking in vget().  We don't move the
  993                                  * page back onto the end of the queue so that
  994                                  * statistics are more correct if we don't.
  995                                  */
  996                                 if (m->busy || (m->oflags & VPO_BUSY)) {
  997                                         goto unlock_and_continue;
  998                                 }
  999 
 1000                                 /*
 1001                                  * If the page has become held it might
 1002                                  * be undergoing I/O, so skip it
 1003                                  */
 1004                                 if (m->hold_count) {
 1005                                         vm_page_requeue(m);
 1006                                         if (object->flags & OBJ_MIGHTBEDIRTY)
 1007                                                 vnodes_skipped++;
 1008                                         goto unlock_and_continue;
 1009                                 }
 1010                         }
 1011 
 1012                         /*
 1013                          * If a page is dirty, then it is either being washed
 1014                          * (but not yet cleaned) or it is still in the
 1015                          * laundry.  If it is still in the laundry, then we
 1016                          * start the cleaning operation. 
 1017                          *
 1018                          * decrement page_shortage on success to account for
 1019                          * the (future) cleaned page.  Otherwise we could wind
 1020                          * up laundering or cleaning too many pages.
 1021                          */
 1022                         if (vm_pageout_clean(m) != 0) {
 1023                                 --page_shortage;
 1024                                 --maxlaunder;
 1025                         }
 1026 unlock_and_continue:
 1027                         VM_OBJECT_UNLOCK(object);
 1028                         if (mp != NULL) {
 1029                                 vm_page_unlock_queues();
 1030                                 if (vp != NULL)
 1031                                         vput(vp);
 1032                                 VFS_UNLOCK_GIANT(vfslocked);
 1033                                 vm_object_deallocate(object);
 1034                                 vn_finished_write(mp);
 1035                                 vm_page_lock_queues();
 1036                         }
 1037                         next = TAILQ_NEXT(&marker, pageq);
 1038                         TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl,
 1039                                      &marker, pageq);
 1040                         continue;
 1041                 }
 1042                 VM_OBJECT_UNLOCK(object);
 1043         }
 1044 
 1045         /*
 1046          * Compute the number of pages we want to try to move from the
 1047          * active queue to the inactive queue.
 1048          */
 1049         page_shortage = vm_paging_target() +
 1050                 cnt.v_inactive_target - cnt.v_inactive_count;
 1051         page_shortage += addl_page_shortage;
 1052 
 1053         /*
 1054          * Scan the active queue for things we can deactivate. We nominally
 1055          * track the per-page activity counter and use it to locate
 1056          * deactivation candidates.
 1057          */
 1058         pcount = cnt.v_active_count;
 1059         m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
 1060 
 1061         while ((m != NULL) && (pcount-- > 0) && (page_shortage > 0)) {
 1062 
 1063                 KASSERT(VM_PAGE_INQUEUE2(m, PQ_ACTIVE),
 1064                     ("vm_pageout_scan: page %p isn't active", m));
 1065 
 1066                 next = TAILQ_NEXT(m, pageq);
 1067                 object = m->object;
 1068                 if ((m->flags & PG_MARKER) != 0) {
 1069                         m = next;
 1070                         continue;
 1071                 }
 1072                 if (!VM_OBJECT_TRYLOCK(object) &&
 1073                     !vm_pageout_fallback_object_lock(m, &next)) {
 1074                         VM_OBJECT_UNLOCK(object);
 1075                         m = next;
 1076                         continue;
 1077                 }
 1078 
 1079                 /*
 1080                  * Don't deactivate pages that are busy.
 1081                  */
 1082                 if ((m->busy != 0) ||
 1083                     (m->oflags & VPO_BUSY) ||
 1084                     (m->hold_count != 0)) {
 1085                         VM_OBJECT_UNLOCK(object);
 1086                         vm_page_requeue(m);
 1087                         m = next;
 1088                         continue;
 1089                 }
 1090 
 1091                 /*
 1092                  * The count for pagedaemon pages is done after checking the
 1093                  * page for eligibility...
 1094                  */
 1095                 cnt.v_pdpages++;
 1096 
 1097                 /*
 1098                  * Check to see "how much" the page has been used.
 1099                  */
 1100                 actcount = 0;
 1101                 if (object->ref_count != 0) {
 1102                         if (m->flags & PG_REFERENCED) {
 1103                                 actcount += 1;
 1104                         }
 1105                         actcount += pmap_ts_referenced(m);
 1106                         if (actcount) {
 1107                                 m->act_count += ACT_ADVANCE + actcount;
 1108                                 if (m->act_count > ACT_MAX)
 1109                                         m->act_count = ACT_MAX;
 1110                         }
 1111                 }
 1112 
 1113                 /*
 1114                  * Since we have "tested" this bit, we need to clear it now.
 1115                  */
 1116                 vm_page_flag_clear(m, PG_REFERENCED);
 1117 
 1118                 /*
 1119                  * Only if an object is currently being used, do we use the
 1120                  * page activation count stats.
 1121                  */
 1122                 if (actcount && (object->ref_count != 0)) {
 1123                         vm_page_requeue(m);
 1124                 } else {
 1125                         m->act_count -= min(m->act_count, ACT_DECLINE);
 1126                         if (vm_pageout_algorithm ||
 1127                             object->ref_count == 0 ||
 1128                             m->act_count == 0) {
 1129                                 page_shortage--;
 1130                                 if (object->ref_count == 0) {
 1131                                         pmap_remove_all(m);
 1132                                         if (m->dirty == 0)
 1133                                                 vm_page_cache(m);
 1134                                         else
 1135                                                 vm_page_deactivate(m);
 1136                                 } else {
 1137                                         vm_page_deactivate(m);
 1138                                 }
 1139                         } else {
 1140                                 vm_page_requeue(m);
 1141                         }
 1142                 }
 1143                 VM_OBJECT_UNLOCK(object);
 1144                 m = next;
 1145         }
 1146         vm_page_unlock_queues();
 1147 #if !defined(NO_SWAPPING)
 1148         /*
 1149          * Idle process swapout -- run once per second.
 1150          */
 1151         if (vm_swap_idle_enabled) {
 1152                 static long lsec;
 1153                 if (time_second != lsec) {
 1154                         vm_req_vmdaemon(VM_SWAP_IDLE);
 1155                         lsec = time_second;
 1156                 }
 1157         }
 1158 #endif
 1159                 
 1160         /*
 1161          * If we didn't get enough free pages, and we have skipped a vnode
 1162          * in a writeable object, wakeup the sync daemon.  And kick swapout
 1163          * if we did not get enough free pages.
 1164          */
 1165         if (vm_paging_target() > 0) {
 1166                 if (vnodes_skipped && vm_page_count_min())
 1167                         (void) speedup_syncer();
 1168 #if !defined(NO_SWAPPING)
 1169                 if (vm_swap_enabled && vm_page_count_target())
 1170                         vm_req_vmdaemon(VM_SWAP_NORMAL);
 1171 #endif
 1172         }
 1173 
 1174         /*
 1175          * If we are critically low on one of RAM or swap and low on
 1176          * the other, kill the largest process.  However, we avoid
 1177          * doing this on the first pass in order to give ourselves a
 1178          * chance to flush out dirty vnode-backed pages and to allow
 1179          * active pages to be moved to the inactive queue and reclaimed.
 1180          */
 1181         if (pass != 0 &&
 1182             ((swap_pager_avail < 64 && vm_page_count_min()) ||
 1183              (swap_pager_full && vm_paging_target() > 0)))
 1184                 vm_pageout_oom(VM_OOM_MEM);
 1185 }
 1186 
 1187 
 1188 void
 1189 vm_pageout_oom(int shortage)
 1190 {
 1191         struct proc *p, *bigproc;
 1192         vm_offset_t size, bigsize;
 1193         struct thread *td;
 1194         struct vmspace *vm;
 1195 
 1196         /*
 1197          * We keep the process bigproc locked once we find it to keep anyone
 1198          * from messing with it; however, there is a possibility of
 1199          * deadlock if process B is bigproc and one of it's child processes
 1200          * attempts to propagate a signal to B while we are waiting for A's
 1201          * lock while walking this list.  To avoid this, we don't block on
 1202          * the process lock but just skip a process if it is already locked.
 1203          */
 1204         bigproc = NULL;
 1205         bigsize = 0;
 1206         sx_slock(&allproc_lock);
 1207         FOREACH_PROC_IN_SYSTEM(p) {
 1208                 int breakout;
 1209 
 1210                 if (PROC_TRYLOCK(p) == 0)
 1211                         continue;
 1212                 /*
 1213                  * If this is a system, protected or killed process, skip it.
 1214                  */
 1215                 if ((p->p_flag & (P_INEXEC | P_PROTECTED | P_SYSTEM)) ||
 1216                     (p->p_pid == 1) || P_KILLED(p) ||
 1217                     ((p->p_pid < 48) && (swap_pager_avail != 0))) {
 1218                         PROC_UNLOCK(p);
 1219                         continue;
 1220                 }
 1221                 /*
 1222                  * If the process is in a non-running type state,
 1223                  * don't touch it.  Check all the threads individually.
 1224                  */
 1225                 breakout = 0;
 1226                 FOREACH_THREAD_IN_PROC(p, td) {
 1227                         thread_lock(td);
 1228                         if (!TD_ON_RUNQ(td) &&
 1229                             !TD_IS_RUNNING(td) &&
 1230                             !TD_IS_SLEEPING(td)) {
 1231                                 thread_unlock(td);
 1232                                 breakout = 1;
 1233                                 break;
 1234                         }
 1235                         thread_unlock(td);
 1236                 }
 1237                 if (breakout) {
 1238                         PROC_UNLOCK(p);
 1239                         continue;
 1240                 }
 1241                 /*
 1242                  * get the process size
 1243                  */
 1244                 vm = vmspace_acquire_ref(p);
 1245                 if (vm == NULL) {
 1246                         PROC_UNLOCK(p);
 1247                         continue;
 1248                 }
 1249                 if (!vm_map_trylock_read(&vm->vm_map)) {
 1250                         vmspace_free(vm);
 1251                         PROC_UNLOCK(p);
 1252                         continue;
 1253                 }
 1254                 size = vmspace_swap_count(vm);
 1255                 vm_map_unlock_read(&vm->vm_map);
 1256                 if (shortage == VM_OOM_MEM)
 1257                         size += vmspace_resident_count(vm);
 1258                 vmspace_free(vm);
 1259                 /*
 1260                  * if the this process is bigger than the biggest one
 1261                  * remember it.
 1262                  */
 1263                 if (size > bigsize) {
 1264                         if (bigproc != NULL)
 1265                                 PROC_UNLOCK(bigproc);
 1266                         bigproc = p;
 1267                         bigsize = size;
 1268                 } else
 1269                         PROC_UNLOCK(p);
 1270         }
 1271         sx_sunlock(&allproc_lock);
 1272         if (bigproc != NULL) {
 1273                 killproc(bigproc, "out of swap space");
 1274                 sched_nice(bigproc, PRIO_MIN);
 1275                 PROC_UNLOCK(bigproc);
 1276                 wakeup(&cnt.v_free_count);
 1277         }
 1278 }
 1279 
 1280 /*
 1281  * This routine tries to maintain the pseudo LRU active queue,
 1282  * so that during long periods of time where there is no paging,
 1283  * that some statistic accumulation still occurs.  This code
 1284  * helps the situation where paging just starts to occur.
 1285  */
 1286 static void
 1287 vm_pageout_page_stats()
 1288 {
 1289         vm_object_t object;
 1290         vm_page_t m,next;
 1291         int pcount,tpcount;             /* Number of pages to check */
 1292         static int fullintervalcount = 0;
 1293         int page_shortage;
 1294 
 1295         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
 1296         page_shortage = 
 1297             (cnt.v_inactive_target + cnt.v_cache_max + cnt.v_free_min) -
 1298             (cnt.v_free_count + cnt.v_inactive_count + cnt.v_cache_count);
 1299 
 1300         if (page_shortage <= 0)
 1301                 return;
 1302 
 1303         pcount = cnt.v_active_count;
 1304         fullintervalcount += vm_pageout_stats_interval;
 1305         if (fullintervalcount < vm_pageout_full_stats_interval) {
 1306                 tpcount = (int64_t)vm_pageout_stats_max * cnt.v_active_count /
 1307                     cnt.v_page_count;
 1308                 if (pcount > tpcount)
 1309                         pcount = tpcount;
 1310         } else {
 1311                 fullintervalcount = 0;
 1312         }
 1313 
 1314         m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl);
 1315         while ((m != NULL) && (pcount-- > 0)) {
 1316                 int actcount;
 1317 
 1318                 KASSERT(VM_PAGE_INQUEUE2(m, PQ_ACTIVE),
 1319                     ("vm_pageout_page_stats: page %p isn't active", m));
 1320 
 1321                 next = TAILQ_NEXT(m, pageq);
 1322                 object = m->object;
 1323 
 1324                 if ((m->flags & PG_MARKER) != 0) {
 1325                         m = next;
 1326                         continue;
 1327                 }
 1328                 if (!VM_OBJECT_TRYLOCK(object) &&
 1329                     !vm_pageout_fallback_object_lock(m, &next)) {
 1330                         VM_OBJECT_UNLOCK(object);
 1331                         m = next;
 1332                         continue;
 1333                 }
 1334 
 1335                 /*
 1336                  * Don't deactivate pages that are busy.
 1337                  */
 1338                 if ((m->busy != 0) ||
 1339                     (m->oflags & VPO_BUSY) ||
 1340                     (m->hold_count != 0)) {
 1341                         VM_OBJECT_UNLOCK(object);
 1342                         vm_page_requeue(m);
 1343                         m = next;
 1344                         continue;
 1345                 }
 1346 
 1347                 actcount = 0;
 1348                 if (m->flags & PG_REFERENCED) {
 1349                         vm_page_flag_clear(m, PG_REFERENCED);
 1350                         actcount += 1;
 1351                 }
 1352 
 1353                 actcount += pmap_ts_referenced(m);
 1354                 if (actcount) {
 1355                         m->act_count += ACT_ADVANCE + actcount;
 1356                         if (m->act_count > ACT_MAX)
 1357                                 m->act_count = ACT_MAX;
 1358                         vm_page_requeue(m);
 1359                 } else {
 1360                         if (m->act_count == 0) {
 1361                                 /*
 1362                                  * We turn off page access, so that we have
 1363                                  * more accurate RSS stats.  We don't do this
 1364                                  * in the normal page deactivation when the
 1365                                  * system is loaded VM wise, because the
 1366                                  * cost of the large number of page protect
 1367                                  * operations would be higher than the value
 1368                                  * of doing the operation.
 1369                                  */
 1370                                 pmap_remove_all(m);
 1371                                 vm_page_deactivate(m);
 1372                         } else {
 1373                                 m->act_count -= min(m->act_count, ACT_DECLINE);
 1374                                 vm_page_requeue(m);
 1375                         }
 1376                 }
 1377                 VM_OBJECT_UNLOCK(object);
 1378                 m = next;
 1379         }
 1380 }
 1381 
 1382 /*
 1383  *      vm_pageout is the high level pageout daemon.
 1384  */
 1385 static void
 1386 vm_pageout()
 1387 {
 1388         int error, pass;
 1389 
 1390         /*
 1391          * Initialize some paging parameters.
 1392          */
 1393         cnt.v_interrupt_free_min = 2;
 1394         if (cnt.v_page_count < 2000)
 1395                 vm_pageout_page_count = 8;
 1396 
 1397         /*
 1398          * v_free_reserved needs to include enough for the largest
 1399          * swap pager structures plus enough for any pv_entry structs
 1400          * when paging. 
 1401          */
 1402         if (cnt.v_page_count > 1024)
 1403                 cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
 1404         else
 1405                 cnt.v_free_min = 4;
 1406         cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
 1407             cnt.v_interrupt_free_min;
 1408         cnt.v_free_reserved = vm_pageout_page_count +
 1409             cnt.v_pageout_free_min + (cnt.v_page_count / 768);
 1410         cnt.v_free_severe = cnt.v_free_min / 2;
 1411         cnt.v_free_min += cnt.v_free_reserved;
 1412         cnt.v_free_severe += cnt.v_free_reserved;
 1413 
 1414         /*
 1415          * v_free_target and v_cache_min control pageout hysteresis.  Note
 1416          * that these are more a measure of the VM cache queue hysteresis
 1417          * then the VM free queue.  Specifically, v_free_target is the
 1418          * high water mark (free+cache pages).
 1419          *
 1420          * v_free_reserved + v_cache_min (mostly means v_cache_min) is the
 1421          * low water mark, while v_free_min is the stop.  v_cache_min must
 1422          * be big enough to handle memory needs while the pageout daemon
 1423          * is signalled and run to free more pages.
 1424          */
 1425         if (cnt.v_free_count > 6144)
 1426                 cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved;
 1427         else
 1428                 cnt.v_free_target = 2 * cnt.v_free_min + cnt.v_free_reserved;
 1429 
 1430         if (cnt.v_free_count > 2048) {
 1431                 cnt.v_cache_min = cnt.v_free_target;
 1432                 cnt.v_cache_max = 2 * cnt.v_cache_min;
 1433                 cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
 1434         } else {
 1435                 cnt.v_cache_min = 0;
 1436                 cnt.v_cache_max = 0;
 1437                 cnt.v_inactive_target = cnt.v_free_count / 4;
 1438         }
 1439         if (cnt.v_inactive_target > cnt.v_free_count / 3)
 1440                 cnt.v_inactive_target = cnt.v_free_count / 3;
 1441 
 1442         /* XXX does not really belong here */
 1443         if (vm_page_max_wired == 0)
 1444                 vm_page_max_wired = cnt.v_free_count / 3;
 1445 
 1446         if (vm_pageout_stats_max == 0)
 1447                 vm_pageout_stats_max = cnt.v_free_target;
 1448 
 1449         /*
 1450          * Set interval in seconds for stats scan.
 1451          */
 1452         if (vm_pageout_stats_interval == 0)
 1453                 vm_pageout_stats_interval = 5;
 1454         if (vm_pageout_full_stats_interval == 0)
 1455                 vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4;
 1456 
 1457         swap_pager_swap_init();
 1458         pass = 0;
 1459         /*
 1460          * The pageout daemon is never done, so loop forever.
 1461          */
 1462         while (TRUE) {
 1463                 /*
 1464                  * If we have enough free memory, wakeup waiters.  Do
 1465                  * not clear vm_pages_needed until we reach our target,
 1466                  * otherwise we may be woken up over and over again and
 1467                  * waste a lot of cpu.
 1468                  */
 1469                 mtx_lock(&vm_page_queue_free_mtx);
 1470                 if (vm_pages_needed && !vm_page_count_min()) {
 1471                         if (!vm_paging_needed())
 1472                                 vm_pages_needed = 0;
 1473                         wakeup(&cnt.v_free_count);
 1474                 }
 1475                 if (vm_pages_needed) {
 1476                         /*
 1477                          * Still not done, take a second pass without waiting
 1478                          * (unlimited dirty cleaning), otherwise sleep a bit
 1479                          * and try again.
 1480                          */
 1481                         ++pass;
 1482                         if (pass > 1)
 1483                                 msleep(&vm_pages_needed,
 1484                                     &vm_page_queue_free_mtx, PVM, "psleep",
 1485                                     hz / 2);
 1486                 } else {
 1487                         /*
 1488                          * Good enough, sleep & handle stats.  Prime the pass
 1489                          * for the next run.
 1490                          */
 1491                         if (pass > 1)
 1492                                 pass = 1;
 1493                         else
 1494                                 pass = 0;
 1495                         error = msleep(&vm_pages_needed,
 1496                             &vm_page_queue_free_mtx, PVM, "psleep",
 1497                             vm_pageout_stats_interval * hz);
 1498                         if (error && !vm_pages_needed) {
 1499                                 mtx_unlock(&vm_page_queue_free_mtx);
 1500                                 pass = 0;
 1501                                 vm_page_lock_queues();
 1502                                 vm_pageout_page_stats();
 1503                                 vm_page_unlock_queues();
 1504                                 continue;
 1505                         }
 1506                 }
 1507                 if (vm_pages_needed)
 1508                         cnt.v_pdwakeups++;
 1509                 mtx_unlock(&vm_page_queue_free_mtx);
 1510                 vm_pageout_scan(pass);
 1511         }
 1512 }
 1513 
 1514 /*
 1515  * Unless the free page queue lock is held by the caller, this function
 1516  * should be regarded as advisory.  Specifically, the caller should
 1517  * not msleep() on &cnt.v_free_count following this function unless
 1518  * the free page queue lock is held until the msleep() is performed.
 1519  */
 1520 void
 1521 pagedaemon_wakeup()
 1522 {
 1523 
 1524         if (!vm_pages_needed && curthread->td_proc != pageproc) {
 1525                 vm_pages_needed = 1;
 1526                 wakeup(&vm_pages_needed);
 1527         }
 1528 }
 1529 
 1530 #if !defined(NO_SWAPPING)
 1531 static void
 1532 vm_req_vmdaemon(int req)
 1533 {
 1534         static int lastrun = 0;
 1535 
 1536         mtx_lock(&vm_daemon_mtx);
 1537         vm_pageout_req_swapout |= req;
 1538         if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
 1539                 wakeup(&vm_daemon_needed);
 1540                 lastrun = ticks;
 1541         }
 1542         mtx_unlock(&vm_daemon_mtx);
 1543 }
 1544 
 1545 static void
 1546 vm_daemon()
 1547 {
 1548         struct rlimit rsslim;
 1549         struct proc *p;
 1550         struct thread *td;
 1551         struct vmspace *vm;
 1552         int breakout, swapout_flags;
 1553 
 1554         while (TRUE) {
 1555                 mtx_lock(&vm_daemon_mtx);
 1556                 msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0);
 1557                 swapout_flags = vm_pageout_req_swapout;
 1558                 vm_pageout_req_swapout = 0;
 1559                 mtx_unlock(&vm_daemon_mtx);
 1560                 if (swapout_flags)
 1561                         swapout_procs(swapout_flags);
 1562 
 1563                 /*
 1564                  * scan the processes for exceeding their rlimits or if
 1565                  * process is swapped out -- deactivate pages
 1566                  */
 1567                 sx_slock(&allproc_lock);
 1568                 FOREACH_PROC_IN_SYSTEM(p) {
 1569                         vm_pindex_t limit, size;
 1570 
 1571                         /*
 1572                          * if this is a system process or if we have already
 1573                          * looked at this process, skip it.
 1574                          */
 1575                         PROC_LOCK(p);
 1576                         if (p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) {
 1577                                 PROC_UNLOCK(p);
 1578                                 continue;
 1579                         }
 1580                         /*
 1581                          * if the process is in a non-running type state,
 1582                          * don't touch it.
 1583                          */
 1584                         breakout = 0;
 1585                         FOREACH_THREAD_IN_PROC(p, td) {
 1586                                 thread_lock(td);
 1587                                 if (!TD_ON_RUNQ(td) &&
 1588                                     !TD_IS_RUNNING(td) &&
 1589                                     !TD_IS_SLEEPING(td)) {
 1590                                         thread_unlock(td);
 1591                                         breakout = 1;
 1592                                         break;
 1593                                 }
 1594                                 thread_unlock(td);
 1595                         }
 1596                         if (breakout) {
 1597                                 PROC_UNLOCK(p);
 1598                                 continue;
 1599                         }
 1600                         /*
 1601                          * get a limit
 1602                          */
 1603                         lim_rlimit(p, RLIMIT_RSS, &rsslim);
 1604                         limit = OFF_TO_IDX(
 1605                             qmin(rsslim.rlim_cur, rsslim.rlim_max));
 1606 
 1607                         /*
 1608                          * let processes that are swapped out really be
 1609                          * swapped out set the limit to nothing (will force a
 1610                          * swap-out.)
 1611                          */
 1612                         if ((p->p_flag & P_INMEM) == 0)
 1613                                 limit = 0;      /* XXX */
 1614                         vm = vmspace_acquire_ref(p);
 1615                         PROC_UNLOCK(p);
 1616                         if (vm == NULL)
 1617                                 continue;
 1618 
 1619                         size = vmspace_resident_count(vm);
 1620                         if (limit >= 0 && size >= limit) {
 1621                                 vm_pageout_map_deactivate_pages(
 1622                                     &vm->vm_map, limit);
 1623                         }
 1624                         vmspace_free(vm);
 1625                 }
 1626                 sx_sunlock(&allproc_lock);
 1627         }
 1628 }
 1629 #endif                  /* !defined(NO_SWAPPING) */

Cache object: a406929401de2bb9cc05cb85c23c39d8


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.