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

Cache object: 65975c603ee399a1a76b27933c4d1c14


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