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

Cache object: d7ccfc4cc7a2cab8726bca8ee6698894


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