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

Cache object: a91cdc0c63bd9340a685205e7b1f89bd


[ 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.