[ 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  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  cheribsd  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1  -  FREEBSD-LIBC  -  FREEBSD8-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
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: head/sys/vm/vm_pageout.c 260975 2014-01-21 03:27:47Z jhb $");
   77 
   78 #include "opt_vm.h"
   79 #include <sys/param.h>
   80 #include <sys/systm.h>
   81 #include <sys/kernel.h>
   82 #include <sys/eventhandler.h>
   83 #include <sys/lock.h>
   84 #include <sys/mutex.h>
   85 #include <sys/proc.h>
   86 #include <sys/kthread.h>
   87 #include <sys/ktr.h>
   88 #include <sys/mount.h>
   89 #include <sys/racct.h>
   90 #include <sys/resourcevar.h>
   91 #include <sys/sched.h>
   92 #include <sys/signalvar.h>
   93 #include <sys/smp.h>
   94 #include <sys/vnode.h>
   95 #include <sys/vmmeter.h>
   96 #include <sys/rwlock.h>
   97 #include <sys/sx.h>
   98 #include <sys/sysctl.h>
   99 
  100 #include <vm/vm.h>
  101 #include <vm/vm_param.h>
  102 #include <vm/vm_object.h>
  103 #include <vm/vm_page.h>
  104 #include <vm/vm_map.h>
  105 #include <vm/vm_pageout.h>
  106 #include <vm/vm_pager.h>
  107 #include <vm/vm_phys.h>
  108 #include <vm/swap_pager.h>
  109 #include <vm/vm_extern.h>
  110 #include <vm/uma.h>
  111 
  112 /*
  113  * System initialization
  114  */
  115 
  116 /* the kernel process "vm_pageout"*/
  117 static void vm_pageout(void);
  118 static int vm_pageout_clean(vm_page_t);
  119 static void vm_pageout_scan(struct vm_domain *vmd, int pass);
  120 static void vm_pageout_mightbe_oom(struct vm_domain *vmd, int pass);
  121 
  122 struct proc *pageproc;
  123 
  124 static struct kproc_desc page_kp = {
  125         "pagedaemon",
  126         vm_pageout,
  127         &pageproc
  128 };
  129 SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start,
  130     &page_kp);
  131 
  132 #if !defined(NO_SWAPPING)
  133 /* the kernel process "vm_daemon"*/
  134 static void vm_daemon(void);
  135 static struct   proc *vmproc;
  136 
  137 static struct kproc_desc vm_kp = {
  138         "vmdaemon",
  139         vm_daemon,
  140         &vmproc
  141 };
  142 SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp);
  143 #endif
  144 
  145 
  146 int vm_pages_needed;            /* Event on which pageout daemon sleeps */
  147 int vm_pageout_deficit;         /* Estimated number of pages deficit */
  148 int vm_pageout_pages_needed;    /* flag saying that the pageout daemon needs pages */
  149 int vm_pageout_wakeup_thresh;
  150 
  151 #if !defined(NO_SWAPPING)
  152 static int vm_pageout_req_swapout;      /* XXX */
  153 static int vm_daemon_needed;
  154 static struct mtx vm_daemon_mtx;
  155 /* Allow for use by vm_pageout before vm_daemon is initialized. */
  156 MTX_SYSINIT(vm_daemon, &vm_daemon_mtx, "vm daemon", MTX_DEF);
  157 #endif
  158 static int vm_max_launder = 32;
  159 static int vm_pageout_update_period;
  160 static int defer_swap_pageouts;
  161 static int disable_swap_pageouts;
  162 static int lowmem_period = 10;
  163 static int lowmem_ticks;
  164 
  165 #if defined(NO_SWAPPING)
  166 static int vm_swap_enabled = 0;
  167 static int vm_swap_idle_enabled = 0;
  168 #else
  169 static int vm_swap_enabled = 1;
  170 static int vm_swap_idle_enabled = 0;
  171 #endif
  172 
  173 SYSCTL_INT(_vm, OID_AUTO, pageout_wakeup_thresh,
  174         CTLFLAG_RW, &vm_pageout_wakeup_thresh, 0,
  175         "free page threshold for waking up the pageout daemon");
  176 
  177 SYSCTL_INT(_vm, OID_AUTO, max_launder,
  178         CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout");
  179 
  180 SYSCTL_INT(_vm, OID_AUTO, pageout_update_period,
  181         CTLFLAG_RW, &vm_pageout_update_period, 0,
  182         "Maximum active LRU update period");
  183   
  184 SYSCTL_INT(_vm, OID_AUTO, lowmem_period, CTLFLAG_RW, &lowmem_period, 0,
  185         "Low memory callback period");
  186 
  187 #if defined(NO_SWAPPING)
  188 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
  189         CTLFLAG_RD, &vm_swap_enabled, 0, "Enable entire process swapout");
  190 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
  191         CTLFLAG_RD, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
  192 #else
  193 SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled,
  194         CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout");
  195 SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled,
  196         CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria");
  197 #endif
  198 
  199 SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts,
  200         CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem");
  201 
  202 SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts,
  203         CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages");
  204 
  205 static int pageout_lock_miss;
  206 SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss,
  207         CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout");
  208 
  209 #define VM_PAGEOUT_PAGE_COUNT 16
  210 int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT;
  211 
  212 int vm_page_max_wired;          /* XXX max # of wired pages system-wide */
  213 SYSCTL_INT(_vm, OID_AUTO, max_wired,
  214         CTLFLAG_RW, &vm_page_max_wired, 0, "System-wide limit to wired page count");
  215 
  216 static boolean_t vm_pageout_fallback_object_lock(vm_page_t, vm_page_t *);
  217 static boolean_t vm_pageout_launder(struct vm_pagequeue *pq, int, vm_paddr_t,
  218     vm_paddr_t);
  219 #if !defined(NO_SWAPPING)
  220 static void vm_pageout_map_deactivate_pages(vm_map_t, long);
  221 static void vm_pageout_object_deactivate_pages(pmap_t, vm_object_t, long);
  222 static void vm_req_vmdaemon(int req);
  223 #endif
  224 static boolean_t vm_pageout_page_lock(vm_page_t, vm_page_t *);
  225 
  226 /*
  227  * Initialize a dummy page for marking the caller's place in the specified
  228  * paging queue.  In principle, this function only needs to set the flag
  229  * PG_MARKER.  Nonetheless, it wirte busies and initializes the hold count
  230  * to one as safety precautions.
  231  */ 
  232 static void
  233 vm_pageout_init_marker(vm_page_t marker, u_short queue)
  234 {
  235 
  236         bzero(marker, sizeof(*marker));
  237         marker->flags = PG_MARKER;
  238         marker->busy_lock = VPB_SINGLE_EXCLUSIVER;
  239         marker->queue = queue;
  240         marker->hold_count = 1;
  241 }
  242 
  243 /*
  244  * vm_pageout_fallback_object_lock:
  245  * 
  246  * Lock vm object currently associated with `m'. VM_OBJECT_TRYWLOCK is
  247  * known to have failed and page queue must be either PQ_ACTIVE or
  248  * PQ_INACTIVE.  To avoid lock order violation, unlock the page queues
  249  * while locking the vm object.  Use marker page to detect page queue
  250  * changes and maintain notion of next page on page queue.  Return
  251  * TRUE if no changes were detected, FALSE otherwise.  vm object is
  252  * locked on return.
  253  * 
  254  * This function depends on both the lock portion of struct vm_object
  255  * and normal struct vm_page being type stable.
  256  */
  257 static boolean_t
  258 vm_pageout_fallback_object_lock(vm_page_t m, vm_page_t *next)
  259 {
  260         struct vm_page marker;
  261         struct vm_pagequeue *pq;
  262         boolean_t unchanged;
  263         u_short queue;
  264         vm_object_t object;
  265 
  266         queue = m->queue;
  267         vm_pageout_init_marker(&marker, queue);
  268         pq = vm_page_pagequeue(m);
  269         object = m->object;
  270         
  271         TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, plinks.q);
  272         vm_pagequeue_unlock(pq);
  273         vm_page_unlock(m);
  274         VM_OBJECT_WLOCK(object);
  275         vm_page_lock(m);
  276         vm_pagequeue_lock(pq);
  277 
  278         /* Page queue might have changed. */
  279         *next = TAILQ_NEXT(&marker, plinks.q);
  280         unchanged = (m->queue == queue &&
  281                      m->object == object &&
  282                      &marker == TAILQ_NEXT(m, plinks.q));
  283         TAILQ_REMOVE(&pq->pq_pl, &marker, plinks.q);
  284         return (unchanged);
  285 }
  286 
  287 /*
  288  * Lock the page while holding the page queue lock.  Use marker page
  289  * to detect page queue changes and maintain notion of next page on
  290  * page queue.  Return TRUE if no changes were detected, FALSE
  291  * otherwise.  The page is locked on return. The page queue lock might
  292  * be dropped and reacquired.
  293  *
  294  * This function depends on normal struct vm_page being type stable.
  295  */
  296 static boolean_t
  297 vm_pageout_page_lock(vm_page_t m, vm_page_t *next)
  298 {
  299         struct vm_page marker;
  300         struct vm_pagequeue *pq;
  301         boolean_t unchanged;
  302         u_short queue;
  303 
  304         vm_page_lock_assert(m, MA_NOTOWNED);
  305         if (vm_page_trylock(m))
  306                 return (TRUE);
  307 
  308         queue = m->queue;
  309         vm_pageout_init_marker(&marker, queue);
  310         pq = vm_page_pagequeue(m);
  311 
  312         TAILQ_INSERT_AFTER(&pq->pq_pl, m, &marker, plinks.q);
  313         vm_pagequeue_unlock(pq);
  314         vm_page_lock(m);
  315         vm_pagequeue_lock(pq);
  316 
  317         /* Page queue might have changed. */
  318         *next = TAILQ_NEXT(&marker, plinks.q);
  319         unchanged = (m->queue == queue && &marker == TAILQ_NEXT(m, plinks.q));
  320         TAILQ_REMOVE(&pq->pq_pl, &marker, plinks.q);
  321         return (unchanged);
  322 }
  323 
  324 /*
  325  * vm_pageout_clean:
  326  *
  327  * Clean the page and remove it from the laundry.
  328  * 
  329  * We set the busy bit to cause potential page faults on this page to
  330  * block.  Note the careful timing, however, the busy bit isn't set till
  331  * late and we cannot do anything that will mess with the page.
  332  */
  333 static int
  334 vm_pageout_clean(vm_page_t m)
  335 {
  336         vm_object_t object;
  337         vm_page_t mc[2*vm_pageout_page_count], pb, ps;
  338         int pageout_count;
  339         int ib, is, page_base;
  340         vm_pindex_t pindex = m->pindex;
  341 
  342         vm_page_lock_assert(m, MA_OWNED);
  343         object = m->object;
  344         VM_OBJECT_ASSERT_WLOCKED(object);
  345 
  346         /*
  347          * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP
  348          * with the new swapper, but we could have serious problems paging
  349          * out other object types if there is insufficient memory.  
  350          *
  351          * Unfortunately, checking free memory here is far too late, so the
  352          * check has been moved up a procedural level.
  353          */
  354 
  355         /*
  356          * Can't clean the page if it's busy or held.
  357          */
  358         vm_page_assert_unbusied(m);
  359         KASSERT(m->hold_count == 0, ("vm_pageout_clean: page %p is held", m));
  360         vm_page_unlock(m);
  361 
  362         mc[vm_pageout_page_count] = pb = ps = m;
  363         pageout_count = 1;
  364         page_base = vm_pageout_page_count;
  365         ib = 1;
  366         is = 1;
  367 
  368         /*
  369          * Scan object for clusterable pages.
  370          *
  371          * We can cluster ONLY if: ->> the page is NOT
  372          * clean, wired, busy, held, or mapped into a
  373          * buffer, and one of the following:
  374          * 1) The page is inactive, or a seldom used
  375          *    active page.
  376          * -or-
  377          * 2) we force the issue.
  378          *
  379          * During heavy mmap/modification loads the pageout
  380          * daemon can really fragment the underlying file
  381          * due to flushing pages out of order and not trying
  382          * align the clusters (which leave sporatic out-of-order
  383          * holes).  To solve this problem we do the reverse scan
  384          * first and attempt to align our cluster, then do a 
  385          * forward scan if room remains.
  386          */
  387 more:
  388         while (ib && pageout_count < vm_pageout_page_count) {
  389                 vm_page_t p;
  390 
  391                 if (ib > pindex) {
  392                         ib = 0;
  393                         break;
  394                 }
  395 
  396                 if ((p = vm_page_prev(pb)) == NULL || vm_page_busied(p)) {
  397                         ib = 0;
  398                         break;
  399                 }
  400                 vm_page_lock(p);
  401                 vm_page_test_dirty(p);
  402                 if (p->dirty == 0 ||
  403                     p->queue != PQ_INACTIVE ||
  404                     p->hold_count != 0) {       /* may be undergoing I/O */
  405                         vm_page_unlock(p);
  406                         ib = 0;
  407                         break;
  408                 }
  409                 vm_page_unlock(p);
  410                 mc[--page_base] = pb = p;
  411                 ++pageout_count;
  412                 ++ib;
  413                 /*
  414                  * alignment boundry, stop here and switch directions.  Do
  415                  * not clear ib.
  416                  */
  417                 if ((pindex - (ib - 1)) % vm_pageout_page_count == 0)
  418                         break;
  419         }
  420 
  421         while (pageout_count < vm_pageout_page_count && 
  422             pindex + is < object->size) {
  423                 vm_page_t p;
  424 
  425                 if ((p = vm_page_next(ps)) == NULL || vm_page_busied(p))
  426                         break;
  427                 vm_page_lock(p);
  428                 vm_page_test_dirty(p);
  429                 if (p->dirty == 0 ||
  430                     p->queue != PQ_INACTIVE ||
  431                     p->hold_count != 0) {       /* may be undergoing I/O */
  432                         vm_page_unlock(p);
  433                         break;
  434                 }
  435                 vm_page_unlock(p);
  436                 mc[page_base + pageout_count] = ps = p;
  437                 ++pageout_count;
  438                 ++is;
  439         }
  440 
  441         /*
  442          * If we exhausted our forward scan, continue with the reverse scan
  443          * when possible, even past a page boundry.  This catches boundry
  444          * conditions.
  445          */
  446         if (ib && pageout_count < vm_pageout_page_count)
  447                 goto more;
  448 
  449         /*
  450          * we allow reads during pageouts...
  451          */
  452         return (vm_pageout_flush(&mc[page_base], pageout_count, 0, 0, NULL,
  453             NULL));
  454 }
  455 
  456 /*
  457  * vm_pageout_flush() - launder the given pages
  458  *
  459  *      The given pages are laundered.  Note that we setup for the start of
  460  *      I/O ( i.e. busy the page ), mark it read-only, and bump the object
  461  *      reference count all in here rather then in the parent.  If we want
  462  *      the parent to do more sophisticated things we may have to change
  463  *      the ordering.
  464  *
  465  *      Returned runlen is the count of pages between mreq and first
  466  *      page after mreq with status VM_PAGER_AGAIN.
  467  *      *eio is set to TRUE if pager returned VM_PAGER_ERROR or VM_PAGER_FAIL
  468  *      for any page in runlen set.
  469  */
  470 int
  471 vm_pageout_flush(vm_page_t *mc, int count, int flags, int mreq, int *prunlen,
  472     boolean_t *eio)
  473 {
  474         vm_object_t object = mc[0]->object;
  475         int pageout_status[count];
  476         int numpagedout = 0;
  477         int i, runlen;
  478 
  479         VM_OBJECT_ASSERT_WLOCKED(object);
  480 
  481         /*
  482          * Initiate I/O.  Bump the vm_page_t->busy counter and
  483          * mark the pages read-only.
  484          *
  485          * We do not have to fixup the clean/dirty bits here... we can
  486          * allow the pager to do it after the I/O completes.
  487          *
  488          * NOTE! mc[i]->dirty may be partial or fragmented due to an
  489          * edge case with file fragments.
  490          */
  491         for (i = 0; i < count; i++) {
  492                 KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL,
  493                     ("vm_pageout_flush: partially invalid page %p index %d/%d",
  494                         mc[i], i, count));
  495                 vm_page_sbusy(mc[i]);
  496                 pmap_remove_write(mc[i]);
  497         }
  498         vm_object_pip_add(object, count);
  499 
  500         vm_pager_put_pages(object, mc, count, flags, pageout_status);
  501 
  502         runlen = count - mreq;
  503         if (eio != NULL)
  504                 *eio = FALSE;
  505         for (i = 0; i < count; i++) {
  506                 vm_page_t mt = mc[i];
  507 
  508                 KASSERT(pageout_status[i] == VM_PAGER_PEND ||
  509                     !pmap_page_is_write_mapped(mt),
  510                     ("vm_pageout_flush: page %p is not write protected", mt));
  511                 switch (pageout_status[i]) {
  512                 case VM_PAGER_OK:
  513                 case VM_PAGER_PEND:
  514                         numpagedout++;
  515                         break;
  516                 case VM_PAGER_BAD:
  517                         /*
  518                          * Page outside of range of object. Right now we
  519                          * essentially lose the changes by pretending it
  520                          * worked.
  521                          */
  522                         vm_page_undirty(mt);
  523                         break;
  524                 case VM_PAGER_ERROR:
  525                 case VM_PAGER_FAIL:
  526                         /*
  527                          * If page couldn't be paged out, then reactivate the
  528                          * page so it doesn't clog the inactive list.  (We
  529                          * will try paging out it again later).
  530                          */
  531                         vm_page_lock(mt);
  532                         vm_page_activate(mt);
  533                         vm_page_unlock(mt);
  534                         if (eio != NULL && i >= mreq && i - mreq < runlen)
  535                                 *eio = TRUE;
  536                         break;
  537                 case VM_PAGER_AGAIN:
  538                         if (i >= mreq && i - mreq < runlen)
  539                                 runlen = i - mreq;
  540                         break;
  541                 }
  542 
  543                 /*
  544                  * If the operation is still going, leave the page busy to
  545                  * block all other accesses. Also, leave the paging in
  546                  * progress indicator set so that we don't attempt an object
  547                  * collapse.
  548                  */
  549                 if (pageout_status[i] != VM_PAGER_PEND) {
  550                         vm_object_pip_wakeup(object);
  551                         vm_page_sunbusy(mt);
  552                         if (vm_page_count_severe()) {
  553                                 vm_page_lock(mt);
  554                                 vm_page_try_to_cache(mt);
  555                                 vm_page_unlock(mt);
  556                         }
  557                 }
  558         }
  559         if (prunlen != NULL)
  560                 *prunlen = runlen;
  561         return (numpagedout);
  562 }
  563 
  564 static boolean_t
  565 vm_pageout_launder(struct vm_pagequeue *pq, int tries, vm_paddr_t low,
  566     vm_paddr_t high)
  567 {
  568         struct mount *mp;
  569         struct vnode *vp;
  570         vm_object_t object;
  571         vm_paddr_t pa;
  572         vm_page_t m, m_tmp, next;
  573         int lockmode;
  574 
  575         vm_pagequeue_lock(pq);
  576         TAILQ_FOREACH_SAFE(m, &pq->pq_pl, plinks.q, next) {
  577                 if ((m->flags & PG_MARKER) != 0)
  578                         continue;
  579                 pa = VM_PAGE_TO_PHYS(m);
  580                 if (pa < low || pa + PAGE_SIZE > high)
  581                         continue;
  582                 if (!vm_pageout_page_lock(m, &next) || m->hold_count != 0) {
  583                         vm_page_unlock(m);
  584                         continue;
  585                 }
  586                 object = m->object;
  587                 if ((!VM_OBJECT_TRYWLOCK(object) &&
  588                     (!vm_pageout_fallback_object_lock(m, &next) ||
  589                     m->hold_count != 0)) || vm_page_busied(m)) {
  590                         vm_page_unlock(m);
  591                         VM_OBJECT_WUNLOCK(object);
  592                         continue;
  593                 }
  594                 vm_page_test_dirty(m);
  595                 if (m->dirty == 0 && object->ref_count != 0)
  596                         pmap_remove_all(m);
  597                 if (m->dirty != 0) {
  598                         vm_page_unlock(m);
  599                         if (tries == 0 || (object->flags & OBJ_DEAD) != 0) {
  600                                 VM_OBJECT_WUNLOCK(object);
  601                                 continue;
  602                         }
  603                         if (object->type == OBJT_VNODE) {
  604                                 vm_pagequeue_unlock(pq);
  605                                 vp = object->handle;
  606                                 vm_object_reference_locked(object);
  607                                 VM_OBJECT_WUNLOCK(object);
  608                                 (void)vn_start_write(vp, &mp, V_WAIT);
  609                                 lockmode = MNT_SHARED_WRITES(vp->v_mount) ?
  610                                     LK_SHARED : LK_EXCLUSIVE;
  611                                 vn_lock(vp, lockmode | LK_RETRY);
  612                                 VM_OBJECT_WLOCK(object);
  613                                 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
  614                                 VM_OBJECT_WUNLOCK(object);
  615                                 VOP_UNLOCK(vp, 0);
  616                                 vm_object_deallocate(object);
  617                                 vn_finished_write(mp);
  618                                 return (TRUE);
  619                         } else if (object->type == OBJT_SWAP ||
  620                             object->type == OBJT_DEFAULT) {
  621                                 vm_pagequeue_unlock(pq);
  622                                 m_tmp = m;
  623                                 vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC,
  624                                     0, NULL, NULL);
  625                                 VM_OBJECT_WUNLOCK(object);
  626                                 return (TRUE);
  627                         }
  628                 } else {
  629                         /*
  630                          * Dequeue here to prevent lock recursion in
  631                          * vm_page_cache().
  632                          */
  633                         vm_page_dequeue_locked(m);
  634                         vm_page_cache(m);
  635                         vm_page_unlock(m);
  636                 }
  637                 VM_OBJECT_WUNLOCK(object);
  638         }
  639         vm_pagequeue_unlock(pq);
  640         return (FALSE);
  641 }
  642 
  643 /*
  644  * Increase the number of cached pages.  The specified value, "tries",
  645  * determines which categories of pages are cached:
  646  *
  647  *  0: All clean, inactive pages within the specified physical address range
  648  *     are cached.  Will not sleep.
  649  *  1: The vm_lowmem handlers are called.  All inactive pages within
  650  *     the specified physical address range are cached.  May sleep.
  651  *  2: The vm_lowmem handlers are called.  All inactive and active pages
  652  *     within the specified physical address range are cached.  May sleep.
  653  */
  654 void
  655 vm_pageout_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high)
  656 {
  657         int actl, actmax, inactl, inactmax, dom, initial_dom;
  658         static int start_dom = 0;
  659 
  660         if (tries > 0) {
  661                 /*
  662                  * Decrease registered cache sizes.  The vm_lowmem handlers
  663                  * may acquire locks and/or sleep, so they can only be invoked
  664                  * when "tries" is greater than zero.
  665                  */
  666                 EVENTHANDLER_INVOKE(vm_lowmem, 0);
  667 
  668                 /*
  669                  * We do this explicitly after the caches have been drained
  670                  * above.
  671                  */
  672                 uma_reclaim();
  673         }
  674 
  675         /*
  676          * Make the next scan start on the next domain.
  677          */
  678         initial_dom = atomic_fetchadd_int(&start_dom, 1) % vm_ndomains;
  679 
  680         inactl = 0;
  681         inactmax = cnt.v_inactive_count;
  682         actl = 0;
  683         actmax = tries < 2 ? 0 : cnt.v_active_count;
  684         dom = initial_dom;
  685 
  686         /*
  687          * Scan domains in round-robin order, first inactive queues,
  688          * then active.  Since domain usually owns large physically
  689          * contiguous chunk of memory, it makes sense to completely
  690          * exhaust one domain before switching to next, while growing
  691          * the pool of contiguous physical pages.
  692          *
  693          * Do not even start launder a domain which cannot contain
  694          * the specified address range, as indicated by segments
  695          * constituting the domain.
  696          */
  697 again:
  698         if (inactl < inactmax) {
  699                 if (vm_phys_domain_intersects(vm_dom[dom].vmd_segs,
  700                     low, high) &&
  701                     vm_pageout_launder(&vm_dom[dom].vmd_pagequeues[PQ_INACTIVE],
  702                     tries, low, high)) {
  703                         inactl++;
  704                         goto again;
  705                 }
  706                 if (++dom == vm_ndomains)
  707                         dom = 0;
  708                 if (dom != initial_dom)
  709                         goto again;
  710         }
  711         if (actl < actmax) {
  712                 if (vm_phys_domain_intersects(vm_dom[dom].vmd_segs,
  713                     low, high) &&
  714                     vm_pageout_launder(&vm_dom[dom].vmd_pagequeues[PQ_ACTIVE],
  715                       tries, low, high)) {
  716                         actl++;
  717                         goto again;
  718                 }
  719                 if (++dom == vm_ndomains)
  720                         dom = 0;
  721                 if (dom != initial_dom)
  722                         goto again;
  723         }
  724 }
  725 
  726 #if !defined(NO_SWAPPING)
  727 /*
  728  *      vm_pageout_object_deactivate_pages
  729  *
  730  *      Deactivate enough pages to satisfy the inactive target
  731  *      requirements.
  732  *
  733  *      The object and map must be locked.
  734  */
  735 static void
  736 vm_pageout_object_deactivate_pages(pmap_t pmap, vm_object_t first_object,
  737     long desired)
  738 {
  739         vm_object_t backing_object, object;
  740         vm_page_t p;
  741         int act_delta, remove_mode;
  742 
  743         VM_OBJECT_ASSERT_LOCKED(first_object);
  744         if ((first_object->flags & OBJ_FICTITIOUS) != 0)
  745                 return;
  746         for (object = first_object;; object = backing_object) {
  747                 if (pmap_resident_count(pmap) <= desired)
  748                         goto unlock_return;
  749                 VM_OBJECT_ASSERT_LOCKED(object);
  750                 if ((object->flags & OBJ_UNMANAGED) != 0 ||
  751                     object->paging_in_progress != 0)
  752                         goto unlock_return;
  753 
  754                 remove_mode = 0;
  755                 if (object->shadow_count > 1)
  756                         remove_mode = 1;
  757                 /*
  758                  * Scan the object's entire memory queue.
  759                  */
  760                 TAILQ_FOREACH(p, &object->memq, listq) {
  761                         if (pmap_resident_count(pmap) <= desired)
  762                                 goto unlock_return;
  763                         if (vm_page_busied(p))
  764                                 continue;
  765                         PCPU_INC(cnt.v_pdpages);
  766                         vm_page_lock(p);
  767                         if (p->wire_count != 0 || p->hold_count != 0 ||
  768                             !pmap_page_exists_quick(pmap, p)) {
  769                                 vm_page_unlock(p);
  770                                 continue;
  771                         }
  772                         act_delta = pmap_ts_referenced(p);
  773                         if ((p->aflags & PGA_REFERENCED) != 0) {
  774                                 if (act_delta == 0)
  775                                         act_delta = 1;
  776                                 vm_page_aflag_clear(p, PGA_REFERENCED);
  777                         }
  778                         if (p->queue != PQ_ACTIVE && act_delta != 0) {
  779                                 vm_page_activate(p);
  780                                 p->act_count += act_delta;
  781                         } else if (p->queue == PQ_ACTIVE) {
  782                                 if (act_delta == 0) {
  783                                         p->act_count -= min(p->act_count,
  784                                             ACT_DECLINE);
  785                                         if (!remove_mode && p->act_count == 0) {
  786                                                 pmap_remove_all(p);
  787                                                 vm_page_deactivate(p);
  788                                         } else
  789                                                 vm_page_requeue(p);
  790                                 } else {
  791                                         vm_page_activate(p);
  792                                         if (p->act_count < ACT_MAX -
  793                                             ACT_ADVANCE)
  794                                                 p->act_count += ACT_ADVANCE;
  795                                         vm_page_requeue(p);
  796                                 }
  797                         } else if (p->queue == PQ_INACTIVE)
  798                                 pmap_remove_all(p);
  799                         vm_page_unlock(p);
  800                 }
  801                 if ((backing_object = object->backing_object) == NULL)
  802                         goto unlock_return;
  803                 VM_OBJECT_RLOCK(backing_object);
  804                 if (object != first_object)
  805                         VM_OBJECT_RUNLOCK(object);
  806         }
  807 unlock_return:
  808         if (object != first_object)
  809                 VM_OBJECT_RUNLOCK(object);
  810 }
  811 
  812 /*
  813  * deactivate some number of pages in a map, try to do it fairly, but
  814  * that is really hard to do.
  815  */
  816 static void
  817 vm_pageout_map_deactivate_pages(map, desired)
  818         vm_map_t map;
  819         long desired;
  820 {
  821         vm_map_entry_t tmpe;
  822         vm_object_t obj, bigobj;
  823         int nothingwired;
  824 
  825         if (!vm_map_trylock(map))
  826                 return;
  827 
  828         bigobj = NULL;
  829         nothingwired = TRUE;
  830 
  831         /*
  832          * first, search out the biggest object, and try to free pages from
  833          * that.
  834          */
  835         tmpe = map->header.next;
  836         while (tmpe != &map->header) {
  837                 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
  838                         obj = tmpe->object.vm_object;
  839                         if (obj != NULL && VM_OBJECT_TRYRLOCK(obj)) {
  840                                 if (obj->shadow_count <= 1 &&
  841                                     (bigobj == NULL ||
  842                                      bigobj->resident_page_count < obj->resident_page_count)) {
  843                                         if (bigobj != NULL)
  844                                                 VM_OBJECT_RUNLOCK(bigobj);
  845                                         bigobj = obj;
  846                                 } else
  847                                         VM_OBJECT_RUNLOCK(obj);
  848                         }
  849                 }
  850                 if (tmpe->wired_count > 0)
  851                         nothingwired = FALSE;
  852                 tmpe = tmpe->next;
  853         }
  854 
  855         if (bigobj != NULL) {
  856                 vm_pageout_object_deactivate_pages(map->pmap, bigobj, desired);
  857                 VM_OBJECT_RUNLOCK(bigobj);
  858         }
  859         /*
  860          * Next, hunt around for other pages to deactivate.  We actually
  861          * do this search sort of wrong -- .text first is not the best idea.
  862          */
  863         tmpe = map->header.next;
  864         while (tmpe != &map->header) {
  865                 if (pmap_resident_count(vm_map_pmap(map)) <= desired)
  866                         break;
  867                 if ((tmpe->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
  868                         obj = tmpe->object.vm_object;
  869                         if (obj != NULL) {
  870                                 VM_OBJECT_RLOCK(obj);
  871                                 vm_pageout_object_deactivate_pages(map->pmap, obj, desired);
  872                                 VM_OBJECT_RUNLOCK(obj);
  873                         }
  874                 }
  875                 tmpe = tmpe->next;
  876         }
  877 
  878 #ifdef __ia64__
  879         /*
  880          * Remove all non-wired, managed mappings if a process is swapped out.
  881          * This will free page table pages.
  882          */
  883         if (desired == 0)
  884                 pmap_remove_pages(map->pmap);
  885 #else
  886         /*
  887          * Remove all mappings if a process is swapped out, this will free page
  888          * table pages.
  889          */
  890         if (desired == 0 && nothingwired) {
  891                 pmap_remove(vm_map_pmap(map), vm_map_min(map),
  892                     vm_map_max(map));
  893         }
  894 #endif
  895 
  896         vm_map_unlock(map);
  897 }
  898 #endif          /* !defined(NO_SWAPPING) */
  899 
  900 /*
  901  *      vm_pageout_scan does the dirty work for the pageout daemon.
  902  *
  903  *      pass 0 - Update active LRU/deactivate pages
  904  *      pass 1 - Move inactive to cache or free
  905  *      pass 2 - Launder dirty pages
  906  */
  907 static void
  908 vm_pageout_scan(struct vm_domain *vmd, int pass)
  909 {
  910         vm_page_t m, next;
  911         struct vm_pagequeue *pq;
  912         vm_object_t object;
  913         int act_delta, addl_page_shortage, deficit, maxscan, page_shortage;
  914         int vnodes_skipped = 0;
  915         int maxlaunder;
  916         int lockmode;
  917         boolean_t queues_locked;
  918 
  919         /*
  920          * If we need to reclaim memory ask kernel caches to return
  921          * some.  We rate limit to avoid thrashing.
  922          */
  923         if (vmd == &vm_dom[0] && pass > 0 &&
  924             lowmem_ticks + (lowmem_period * hz) < ticks) {
  925                 /*
  926                  * Decrease registered cache sizes.
  927                  */
  928                 EVENTHANDLER_INVOKE(vm_lowmem, 0);
  929                 /*
  930                  * We do this explicitly after the caches have been
  931                  * drained above.
  932                  */
  933                 uma_reclaim();
  934                 lowmem_ticks = ticks;
  935         }
  936 
  937         /*
  938          * The addl_page_shortage is the number of temporarily
  939          * stuck pages in the inactive queue.  In other words, the
  940          * number of pages from the inactive count that should be
  941          * discounted in setting the target for the active queue scan.
  942          */
  943         addl_page_shortage = 0;
  944 
  945         deficit = atomic_readandclear_int(&vm_pageout_deficit);
  946 
  947         /*
  948          * Calculate the number of pages we want to either free or move
  949          * to the cache.
  950          */
  951         page_shortage = vm_paging_target() + deficit;
  952 
  953         /*
  954          * maxlaunder limits the number of dirty pages we flush per scan.
  955          * For most systems a smaller value (16 or 32) is more robust under
  956          * extreme memory and disk pressure because any unnecessary writes
  957          * to disk can result in extreme performance degredation.  However,
  958          * systems with excessive dirty pages (especially when MAP_NOSYNC is
  959          * used) will die horribly with limited laundering.  If the pageout
  960          * daemon cannot clean enough pages in the first pass, we let it go
  961          * all out in succeeding passes.
  962          */
  963         if ((maxlaunder = vm_max_launder) <= 1)
  964                 maxlaunder = 1;
  965         if (pass > 1)
  966                 maxlaunder = 10000;
  967 
  968         /*
  969          * Start scanning the inactive queue for pages we can move to the
  970          * cache or free.  The scan will stop when the target is reached or
  971          * we have scanned the entire inactive queue.  Note that m->act_count
  972          * is not used to form decisions for the inactive queue, only for the
  973          * active queue.
  974          */
  975         pq = &vmd->vmd_pagequeues[PQ_INACTIVE];
  976         maxscan = pq->pq_cnt;
  977         vm_pagequeue_lock(pq);
  978         queues_locked = TRUE;
  979         for (m = TAILQ_FIRST(&pq->pq_pl);
  980              m != NULL && maxscan-- > 0 && page_shortage > 0;
  981              m = next) {
  982                 vm_pagequeue_assert_locked(pq);
  983                 KASSERT(queues_locked, ("unlocked queues"));
  984                 KASSERT(m->queue == PQ_INACTIVE, ("Inactive queue %p", m));
  985 
  986                 PCPU_INC(cnt.v_pdpages);
  987                 next = TAILQ_NEXT(m, plinks.q);
  988 
  989                 /*
  990                  * skip marker pages
  991                  */
  992                 if (m->flags & PG_MARKER)
  993                         continue;
  994 
  995                 KASSERT((m->flags & PG_FICTITIOUS) == 0,
  996                     ("Fictitious page %p cannot be in inactive queue", m));
  997                 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
  998                     ("Unmanaged page %p cannot be in inactive queue", m));
  999 
 1000                 /*
 1001                  * The page or object lock acquisitions fail if the
 1002                  * page was removed from the queue or moved to a
 1003                  * different position within the queue.  In either
 1004                  * case, addl_page_shortage should not be incremented.
 1005                  */
 1006                 if (!vm_pageout_page_lock(m, &next)) {
 1007                         vm_page_unlock(m);
 1008                         continue;
 1009                 }
 1010                 object = m->object;
 1011                 if (!VM_OBJECT_TRYWLOCK(object) &&
 1012                     !vm_pageout_fallback_object_lock(m, &next)) {
 1013                         vm_page_unlock(m);
 1014                         VM_OBJECT_WUNLOCK(object);
 1015                         continue;
 1016                 }
 1017 
 1018                 /*
 1019                  * Don't mess with busy pages, keep them at at the
 1020                  * front of the queue, most likely they are being
 1021                  * paged out.  Increment addl_page_shortage for busy
 1022                  * pages, because they may leave the inactive queue
 1023                  * shortly after page scan is finished.
 1024                  */
 1025                 if (vm_page_busied(m)) {
 1026                         vm_page_unlock(m);
 1027                         VM_OBJECT_WUNLOCK(object);
 1028                         addl_page_shortage++;
 1029                         continue;
 1030                 }
 1031 
 1032                 /*
 1033                  * We unlock the inactive page queue, invalidating the
 1034                  * 'next' pointer.  Use our marker to remember our
 1035                  * place.
 1036                  */
 1037                 TAILQ_INSERT_AFTER(&pq->pq_pl, m, &vmd->vmd_marker, plinks.q);
 1038                 vm_pagequeue_unlock(pq);
 1039                 queues_locked = FALSE;
 1040 
 1041                 /*
 1042                  * We bump the activation count if the page has been
 1043                  * referenced while in the inactive queue.  This makes
 1044                  * it less likely that the page will be added back to the
 1045                  * inactive queue prematurely again.  Here we check the 
 1046                  * page tables (or emulated bits, if any), given the upper 
 1047                  * level VM system not knowing anything about existing 
 1048                  * references.
 1049                  */
 1050                 if ((m->aflags & PGA_REFERENCED) != 0) {
 1051                         vm_page_aflag_clear(m, PGA_REFERENCED);
 1052                         act_delta = 1;
 1053                 } else
 1054                         act_delta = 0;
 1055                 if (object->ref_count != 0) {
 1056                         act_delta += pmap_ts_referenced(m);
 1057                 } else {
 1058                         KASSERT(!pmap_page_is_mapped(m),
 1059                             ("vm_pageout_scan: page %p is mapped", m));
 1060                 }
 1061 
 1062                 /*
 1063                  * If the upper level VM system knows about any page 
 1064                  * references, we reactivate the page or requeue it.
 1065                  */
 1066                 if (act_delta != 0) {
 1067                         if (object->ref_count != 0) {
 1068                                 vm_page_activate(m);
 1069                                 m->act_count += act_delta + ACT_ADVANCE;
 1070                         } else {
 1071                                 vm_pagequeue_lock(pq);
 1072                                 queues_locked = TRUE;
 1073                                 vm_page_requeue_locked(m);
 1074                         }
 1075                         VM_OBJECT_WUNLOCK(object);
 1076                         vm_page_unlock(m);
 1077                         goto relock_queues;
 1078                 }
 1079 
 1080                 if (m->hold_count != 0) {
 1081                         vm_page_unlock(m);
 1082                         VM_OBJECT_WUNLOCK(object);
 1083 
 1084                         /*
 1085                          * Held pages are essentially stuck in the
 1086                          * queue.  So, they ought to be discounted
 1087                          * from the inactive count.  See the
 1088                          * calculation of the page_shortage for the
 1089                          * loop over the active queue below.
 1090                          */
 1091                         addl_page_shortage++;
 1092                         goto relock_queues;
 1093                 }
 1094 
 1095                 /*
 1096                  * If the page appears to be clean at the machine-independent
 1097                  * layer, then remove all of its mappings from the pmap in
 1098                  * anticipation of placing it onto the cache queue.  If,
 1099                  * however, any of the page's mappings allow write access,
 1100                  * then the page may still be modified until the last of those
 1101                  * mappings are removed.
 1102                  */
 1103                 vm_page_test_dirty(m);
 1104                 if (m->dirty == 0 && object->ref_count != 0)
 1105                         pmap_remove_all(m);
 1106 
 1107                 if (m->valid == 0) {
 1108                         /*
 1109                          * Invalid pages can be easily freed
 1110                          */
 1111                         vm_page_free(m);
 1112                         PCPU_INC(cnt.v_dfree);
 1113                         --page_shortage;
 1114                 } else if (m->dirty == 0) {
 1115                         /*
 1116                          * Clean pages can be placed onto the cache queue.
 1117                          * This effectively frees them.
 1118                          */
 1119                         vm_page_cache(m);
 1120                         --page_shortage;
 1121                 } else if ((m->flags & PG_WINATCFLS) == 0 && pass < 2) {
 1122                         /*
 1123                          * Dirty pages need to be paged out, but flushing
 1124                          * a page is extremely expensive versus freeing
 1125                          * a clean page.  Rather then artificially limiting
 1126                          * the number of pages we can flush, we instead give
 1127                          * dirty pages extra priority on the inactive queue
 1128                          * by forcing them to be cycled through the queue
 1129                          * twice before being flushed, after which the
 1130                          * (now clean) page will cycle through once more
 1131                          * before being freed.  This significantly extends
 1132                          * the thrash point for a heavily loaded machine.
 1133                          */
 1134                         m->flags |= PG_WINATCFLS;
 1135                         vm_pagequeue_lock(pq);
 1136                         queues_locked = TRUE;
 1137                         vm_page_requeue_locked(m);
 1138                 } else if (maxlaunder > 0) {
 1139                         /*
 1140                          * We always want to try to flush some dirty pages if
 1141                          * we encounter them, to keep the system stable.
 1142                          * Normally this number is small, but under extreme
 1143                          * pressure where there are insufficient clean pages
 1144                          * on the inactive queue, we may have to go all out.
 1145                          */
 1146                         int swap_pageouts_ok;
 1147                         struct vnode *vp = NULL;
 1148                         struct mount *mp = NULL;
 1149 
 1150                         if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) {
 1151                                 swap_pageouts_ok = 1;
 1152                         } else {
 1153                                 swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts);
 1154                                 swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts &&
 1155                                 vm_page_count_min());
 1156                                                                                 
 1157                         }
 1158 
 1159                         /*
 1160                          * We don't bother paging objects that are "dead".  
 1161                          * Those objects are in a "rundown" state.
 1162                          */
 1163                         if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) {
 1164                                 vm_pagequeue_lock(pq);
 1165                                 vm_page_unlock(m);
 1166                                 VM_OBJECT_WUNLOCK(object);
 1167                                 queues_locked = TRUE;
 1168                                 vm_page_requeue_locked(m);
 1169                                 goto relock_queues;
 1170                         }
 1171 
 1172                         /*
 1173                          * The object is already known NOT to be dead.   It
 1174                          * is possible for the vget() to block the whole
 1175                          * pageout daemon, but the new low-memory handling
 1176                          * code should prevent it.
 1177                          *
 1178                          * The previous code skipped locked vnodes and, worse,
 1179                          * reordered pages in the queue.  This results in
 1180                          * completely non-deterministic operation and, on a
 1181                          * busy system, can lead to extremely non-optimal
 1182                          * pageouts.  For example, it can cause clean pages
 1183                          * to be freed and dirty pages to be moved to the end
 1184                          * of the queue.  Since dirty pages are also moved to
 1185                          * the end of the queue once-cleaned, this gives
 1186                          * way too large a weighting to deferring the freeing
 1187                          * of dirty pages.
 1188                          *
 1189                          * We can't wait forever for the vnode lock, we might
 1190                          * deadlock due to a vn_read() getting stuck in
 1191                          * vm_wait while holding this vnode.  We skip the 
 1192                          * vnode if we can't get it in a reasonable amount
 1193                          * of time.
 1194                          */
 1195                         if (object->type == OBJT_VNODE) {
 1196                                 vm_page_unlock(m);
 1197                                 vp = object->handle;
 1198                                 if (vp->v_type == VREG &&
 1199                                     vn_start_write(vp, &mp, V_NOWAIT) != 0) {
 1200                                         mp = NULL;
 1201                                         ++pageout_lock_miss;
 1202                                         if (object->flags & OBJ_MIGHTBEDIRTY)
 1203                                                 vnodes_skipped++;
 1204                                         goto unlock_and_continue;
 1205                                 }
 1206                                 KASSERT(mp != NULL,
 1207                                     ("vp %p with NULL v_mount", vp));
 1208                                 vm_object_reference_locked(object);
 1209                                 VM_OBJECT_WUNLOCK(object);
 1210                                 lockmode = MNT_SHARED_WRITES(vp->v_mount) ?
 1211                                     LK_SHARED : LK_EXCLUSIVE;
 1212                                 if (vget(vp, lockmode | LK_TIMELOCK,
 1213                                     curthread)) {
 1214                                         VM_OBJECT_WLOCK(object);
 1215                                         ++pageout_lock_miss;
 1216                                         if (object->flags & OBJ_MIGHTBEDIRTY)
 1217                                                 vnodes_skipped++;
 1218                                         vp = NULL;
 1219                                         goto unlock_and_continue;
 1220                                 }
 1221                                 VM_OBJECT_WLOCK(object);
 1222                                 vm_page_lock(m);
 1223                                 vm_pagequeue_lock(pq);
 1224                                 queues_locked = TRUE;
 1225                                 /*
 1226                                  * The page might have been moved to another
 1227                                  * queue during potential blocking in vget()
 1228                                  * above.  The page might have been freed and
 1229                                  * reused for another vnode.
 1230                                  */
 1231                                 if (m->queue != PQ_INACTIVE ||
 1232                                     m->object != object ||
 1233                                     TAILQ_NEXT(m, plinks.q) != &vmd->vmd_marker) {
 1234                                         vm_page_unlock(m);
 1235                                         if (object->flags & OBJ_MIGHTBEDIRTY)
 1236                                                 vnodes_skipped++;
 1237                                         goto unlock_and_continue;
 1238                                 }
 1239         
 1240                                 /*
 1241                                  * The page may have been busied during the
 1242                                  * blocking in vget().  We don't move the
 1243                                  * page back onto the end of the queue so that
 1244                                  * statistics are more correct if we don't.
 1245                                  */
 1246                                 if (vm_page_busied(m)) {
 1247                                         vm_page_unlock(m);
 1248                                         addl_page_shortage++;
 1249                                         goto unlock_and_continue;
 1250                                 }
 1251 
 1252                                 /*
 1253                                  * If the page has become held it might
 1254                                  * be undergoing I/O, so skip it
 1255                                  */
 1256                                 if (m->hold_count != 0) {
 1257                                         vm_page_unlock(m);
 1258                                         addl_page_shortage++;
 1259                                         if (object->flags & OBJ_MIGHTBEDIRTY)
 1260                                                 vnodes_skipped++;
 1261                                         goto unlock_and_continue;
 1262                                 }
 1263                                 vm_pagequeue_unlock(pq);
 1264                                 queues_locked = FALSE;
 1265                         }
 1266 
 1267                         /*
 1268                          * If a page is dirty, then it is either being washed
 1269                          * (but not yet cleaned) or it is still in the
 1270                          * laundry.  If it is still in the laundry, then we
 1271                          * start the cleaning operation. 
 1272                          *
 1273                          * decrement page_shortage on success to account for
 1274                          * the (future) cleaned page.  Otherwise we could wind
 1275                          * up laundering or cleaning too many pages.
 1276                          */
 1277                         if (vm_pageout_clean(m) != 0) {
 1278                                 --page_shortage;
 1279                                 --maxlaunder;
 1280                         }
 1281 unlock_and_continue:
 1282                         vm_page_lock_assert(m, MA_NOTOWNED);
 1283                         VM_OBJECT_WUNLOCK(object);
 1284                         if (mp != NULL) {
 1285                                 if (queues_locked) {
 1286                                         vm_pagequeue_unlock(pq);
 1287                                         queues_locked = FALSE;
 1288                                 }
 1289                                 if (vp != NULL)
 1290                                         vput(vp);
 1291                                 vm_object_deallocate(object);
 1292                                 vn_finished_write(mp);
 1293                         }
 1294                         vm_page_lock_assert(m, MA_NOTOWNED);
 1295                         goto relock_queues;
 1296                 }
 1297                 vm_page_unlock(m);
 1298                 VM_OBJECT_WUNLOCK(object);
 1299 relock_queues:
 1300                 if (!queues_locked) {
 1301                         vm_pagequeue_lock(pq);
 1302                         queues_locked = TRUE;
 1303                 }
 1304                 next = TAILQ_NEXT(&vmd->vmd_marker, plinks.q);
 1305                 TAILQ_REMOVE(&pq->pq_pl, &vmd->vmd_marker, plinks.q);
 1306         }
 1307         vm_pagequeue_unlock(pq);
 1308 
 1309         /*
 1310          * Compute the number of pages we want to try to move from the
 1311          * active queue to the inactive queue.
 1312          */
 1313         page_shortage = cnt.v_inactive_target - cnt.v_inactive_count +
 1314             vm_paging_target() + deficit + addl_page_shortage;
 1315 
 1316         pq = &vmd->vmd_pagequeues[PQ_ACTIVE];
 1317         vm_pagequeue_lock(pq);
 1318         maxscan = pq->pq_cnt;
 1319 
 1320         /*
 1321          * If we're just idle polling attempt to visit every
 1322          * active page within 'update_period' seconds.
 1323          */
 1324         if (pass == 0 && vm_pageout_update_period != 0) {
 1325                 maxscan /= vm_pageout_update_period;
 1326                 page_shortage = maxscan;
 1327         }
 1328 
 1329         /*
 1330          * Scan the active queue for things we can deactivate. We nominally
 1331          * track the per-page activity counter and use it to locate
 1332          * deactivation candidates.
 1333          */
 1334         m = TAILQ_FIRST(&pq->pq_pl);
 1335         while (m != NULL && maxscan-- > 0 && page_shortage > 0) {
 1336 
 1337                 KASSERT(m->queue == PQ_ACTIVE,
 1338                     ("vm_pageout_scan: page %p isn't active", m));
 1339 
 1340                 next = TAILQ_NEXT(m, plinks.q);
 1341                 if ((m->flags & PG_MARKER) != 0) {
 1342                         m = next;
 1343                         continue;
 1344                 }
 1345                 KASSERT((m->flags & PG_FICTITIOUS) == 0,
 1346                     ("Fictitious page %p cannot be in active queue", m));
 1347                 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
 1348                     ("Unmanaged page %p cannot be in active queue", m));
 1349                 if (!vm_pageout_page_lock(m, &next)) {
 1350                         vm_page_unlock(m);
 1351                         m = next;
 1352                         continue;
 1353                 }
 1354 
 1355                 /*
 1356                  * The count for pagedaemon pages is done after checking the
 1357                  * page for eligibility...
 1358                  */
 1359                 PCPU_INC(cnt.v_pdpages);
 1360 
 1361                 /*
 1362                  * Check to see "how much" the page has been used.
 1363                  */
 1364                 if ((m->aflags & PGA_REFERENCED) != 0) {
 1365                         vm_page_aflag_clear(m, PGA_REFERENCED);
 1366                         act_delta = 1;
 1367                 } else
 1368                         act_delta = 0;
 1369 
 1370                 /*
 1371                  * Unlocked object ref count check.  Two races are possible.
 1372                  * 1) The ref was transitioning to zero and we saw non-zero,
 1373                  *    the pmap bits will be checked unnecessarily.
 1374                  * 2) The ref was transitioning to one and we saw zero. 
 1375                  *    The page lock prevents a new reference to this page so
 1376                  *    we need not check the reference bits.
 1377                  */
 1378                 if (m->object->ref_count != 0)
 1379                         act_delta += pmap_ts_referenced(m);
 1380 
 1381                 /*
 1382                  * Advance or decay the act_count based on recent usage.
 1383                  */
 1384                 if (act_delta != 0) {
 1385                         m->act_count += ACT_ADVANCE + act_delta;
 1386                         if (m->act_count > ACT_MAX)
 1387                                 m->act_count = ACT_MAX;
 1388                 } else
 1389                         m->act_count -= min(m->act_count, ACT_DECLINE);
 1390 
 1391                 /*
 1392                  * Move this page to the tail of the active or inactive
 1393                  * queue depending on usage.
 1394                  */
 1395                 if (m->act_count == 0) {
 1396                         /* Dequeue to avoid later lock recursion. */
 1397                         vm_page_dequeue_locked(m);
 1398                         vm_page_deactivate(m);
 1399                         page_shortage--;
 1400                 } else
 1401                         vm_page_requeue_locked(m);
 1402                 vm_page_unlock(m);
 1403                 m = next;
 1404         }
 1405         vm_pagequeue_unlock(pq);
 1406 #if !defined(NO_SWAPPING)
 1407         /*
 1408          * Idle process swapout -- run once per second.
 1409          */
 1410         if (vm_swap_idle_enabled) {
 1411                 static long lsec;
 1412                 if (time_second != lsec) {
 1413                         vm_req_vmdaemon(VM_SWAP_IDLE);
 1414                         lsec = time_second;
 1415                 }
 1416         }
 1417 #endif
 1418                 
 1419         /*
 1420          * If we didn't get enough free pages, and we have skipped a vnode
 1421          * in a writeable object, wakeup the sync daemon.  And kick swapout
 1422          * if we did not get enough free pages.
 1423          */
 1424         if (vm_paging_target() > 0) {
 1425                 if (vnodes_skipped && vm_page_count_min())
 1426                         (void) speedup_syncer();
 1427 #if !defined(NO_SWAPPING)
 1428                 if (vm_swap_enabled && vm_page_count_target())
 1429                         vm_req_vmdaemon(VM_SWAP_NORMAL);
 1430 #endif
 1431         }
 1432 
 1433         /*
 1434          * If we are critically low on one of RAM or swap and low on
 1435          * the other, kill the largest process.  However, we avoid
 1436          * doing this on the first pass in order to give ourselves a
 1437          * chance to flush out dirty vnode-backed pages and to allow
 1438          * active pages to be moved to the inactive queue and reclaimed.
 1439          */
 1440         vm_pageout_mightbe_oom(vmd, pass);
 1441 }
 1442 
 1443 static int vm_pageout_oom_vote;
 1444 
 1445 /*
 1446  * The pagedaemon threads randlomly select one to perform the
 1447  * OOM.  Trying to kill processes before all pagedaemons
 1448  * failed to reach free target is premature.
 1449  */
 1450 static void
 1451 vm_pageout_mightbe_oom(struct vm_domain *vmd, int pass)
 1452 {
 1453         int old_vote;
 1454 
 1455         if (pass <= 1 || !((swap_pager_avail < 64 && vm_page_count_min()) ||
 1456             (swap_pager_full && vm_paging_target() > 0))) {
 1457                 if (vmd->vmd_oom) {
 1458                         vmd->vmd_oom = FALSE;
 1459                         atomic_subtract_int(&vm_pageout_oom_vote, 1);
 1460                 }
 1461                 return;
 1462         }
 1463 
 1464         if (vmd->vmd_oom)
 1465                 return;
 1466 
 1467         vmd->vmd_oom = TRUE;
 1468         old_vote = atomic_fetchadd_int(&vm_pageout_oom_vote, 1);
 1469         if (old_vote != vm_ndomains - 1)
 1470                 return;
 1471 
 1472         /*
 1473          * The current pagedaemon thread is the last in the quorum to
 1474          * start OOM.  Initiate the selection and signaling of the
 1475          * victim.
 1476          */
 1477         vm_pageout_oom(VM_OOM_MEM);
 1478 
 1479         /*
 1480          * After one round of OOM terror, recall our vote.  On the
 1481          * next pass, current pagedaemon would vote again if the low
 1482          * memory condition is still there, due to vmd_oom being
 1483          * false.
 1484          */
 1485         vmd->vmd_oom = FALSE;
 1486         atomic_subtract_int(&vm_pageout_oom_vote, 1);
 1487 }
 1488 
 1489 void
 1490 vm_pageout_oom(int shortage)
 1491 {
 1492         struct proc *p, *bigproc;
 1493         vm_offset_t size, bigsize;
 1494         struct thread *td;
 1495         struct vmspace *vm;
 1496 
 1497         /*
 1498          * We keep the process bigproc locked once we find it to keep anyone
 1499          * from messing with it; however, there is a possibility of
 1500          * deadlock if process B is bigproc and one of it's child processes
 1501          * attempts to propagate a signal to B while we are waiting for A's
 1502          * lock while walking this list.  To avoid this, we don't block on
 1503          * the process lock but just skip a process if it is already locked.
 1504          */
 1505         bigproc = NULL;
 1506         bigsize = 0;
 1507         sx_slock(&allproc_lock);
 1508         FOREACH_PROC_IN_SYSTEM(p) {
 1509                 int breakout;
 1510 
 1511                 if (PROC_TRYLOCK(p) == 0)
 1512                         continue;
 1513                 /*
 1514                  * If this is a system, protected or killed process, skip it.
 1515                  */
 1516                 if (p->p_state != PRS_NORMAL ||
 1517                     (p->p_flag & (P_INEXEC | P_PROTECTED | P_SYSTEM)) ||
 1518                     (p->p_pid == 1) || P_KILLED(p) ||
 1519                     ((p->p_pid < 48) && (swap_pager_avail != 0))) {
 1520                         PROC_UNLOCK(p);
 1521                         continue;
 1522                 }
 1523                 /*
 1524                  * If the process is in a non-running type state,
 1525                  * don't touch it.  Check all the threads individually.
 1526                  */
 1527                 breakout = 0;
 1528                 FOREACH_THREAD_IN_PROC(p, td) {
 1529                         thread_lock(td);
 1530                         if (!TD_ON_RUNQ(td) &&
 1531                             !TD_IS_RUNNING(td) &&
 1532                             !TD_IS_SLEEPING(td) &&
 1533                             !TD_IS_SUSPENDED(td)) {
 1534                                 thread_unlock(td);
 1535                                 breakout = 1;
 1536                                 break;
 1537                         }
 1538                         thread_unlock(td);
 1539                 }
 1540                 if (breakout) {
 1541                         PROC_UNLOCK(p);
 1542                         continue;
 1543                 }
 1544                 /*
 1545                  * get the process size
 1546                  */
 1547                 vm = vmspace_acquire_ref(p);
 1548                 if (vm == NULL) {
 1549                         PROC_UNLOCK(p);
 1550                         continue;
 1551                 }
 1552                 if (!vm_map_trylock_read(&vm->vm_map)) {
 1553                         vmspace_free(vm);
 1554                         PROC_UNLOCK(p);
 1555                         continue;
 1556                 }
 1557                 size = vmspace_swap_count(vm);
 1558                 vm_map_unlock_read(&vm->vm_map);
 1559                 if (shortage == VM_OOM_MEM)
 1560                         size += vmspace_resident_count(vm);
 1561                 vmspace_free(vm);
 1562                 /*
 1563                  * if the this process is bigger than the biggest one
 1564                  * remember it.
 1565                  */
 1566                 if (size > bigsize) {
 1567                         if (bigproc != NULL)
 1568                                 PROC_UNLOCK(bigproc);
 1569                         bigproc = p;
 1570                         bigsize = size;
 1571                 } else
 1572                         PROC_UNLOCK(p);
 1573         }
 1574         sx_sunlock(&allproc_lock);
 1575         if (bigproc != NULL) {
 1576                 killproc(bigproc, "out of swap space");
 1577                 sched_nice(bigproc, PRIO_MIN);
 1578                 PROC_UNLOCK(bigproc);
 1579                 wakeup(&cnt.v_free_count);
 1580         }
 1581 }
 1582 
 1583 static void
 1584 vm_pageout_worker(void *arg)
 1585 {
 1586         struct vm_domain *domain;
 1587         int domidx;
 1588 
 1589         domidx = (uintptr_t)arg;
 1590         domain = &vm_dom[domidx];
 1591 
 1592         /*
 1593          * XXXKIB It could be useful to bind pageout daemon threads to
 1594          * the cores belonging to the domain, from which vm_page_array
 1595          * is allocated.
 1596          */
 1597 
 1598         KASSERT(domain->vmd_segs != 0, ("domain without segments"));
 1599         vm_pageout_init_marker(&domain->vmd_marker, PQ_INACTIVE);
 1600 
 1601         /*
 1602          * The pageout daemon worker is never done, so loop forever.
 1603          */
 1604         while (TRUE) {
 1605                 /*
 1606                  * If we have enough free memory, wakeup waiters.  Do
 1607                  * not clear vm_pages_needed until we reach our target,
 1608                  * otherwise we may be woken up over and over again and
 1609                  * waste a lot of cpu.
 1610                  */
 1611                 mtx_lock(&vm_page_queue_free_mtx);
 1612                 if (vm_pages_needed && !vm_page_count_min()) {
 1613                         if (!vm_paging_needed())
 1614                                 vm_pages_needed = 0;
 1615                         wakeup(&cnt.v_free_count);
 1616                 }
 1617                 if (vm_pages_needed) {
 1618                         /*
 1619                          * Still not done, take a second pass without waiting
 1620                          * (unlimited dirty cleaning), otherwise sleep a bit
 1621                          * and try again.
 1622                          */
 1623                         if (domain->vmd_pass > 1)
 1624                                 msleep(&vm_pages_needed,
 1625                                     &vm_page_queue_free_mtx, PVM, "psleep",
 1626                                     hz / 2);
 1627                 } else {
 1628                         /*
 1629                          * Good enough, sleep until required to refresh
 1630                          * stats.
 1631                          */
 1632                         domain->vmd_pass = 0;
 1633                         msleep(&vm_pages_needed, &vm_page_queue_free_mtx,
 1634                             PVM, "psleep", hz);
 1635 
 1636                 }
 1637                 if (vm_pages_needed) {
 1638                         cnt.v_pdwakeups++;
 1639                         domain->vmd_pass++;
 1640                 }
 1641                 mtx_unlock(&vm_page_queue_free_mtx);
 1642                 vm_pageout_scan(domain, domain->vmd_pass);
 1643         }
 1644 }
 1645 
 1646 /*
 1647  *      vm_pageout is the high level pageout daemon.
 1648  */
 1649 static void
 1650 vm_pageout(void)
 1651 {
 1652 #if MAXMEMDOM > 1
 1653         int error, i;
 1654 #endif
 1655 
 1656         /*
 1657          * Initialize some paging parameters.
 1658          */
 1659         cnt.v_interrupt_free_min = 2;
 1660         if (cnt.v_page_count < 2000)
 1661                 vm_pageout_page_count = 8;
 1662 
 1663         /*
 1664          * v_free_reserved needs to include enough for the largest
 1665          * swap pager structures plus enough for any pv_entry structs
 1666          * when paging. 
 1667          */
 1668         if (cnt.v_page_count > 1024)
 1669                 cnt.v_free_min = 4 + (cnt.v_page_count - 1024) / 200;
 1670         else
 1671                 cnt.v_free_min = 4;
 1672         cnt.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE +
 1673             cnt.v_interrupt_free_min;
 1674         cnt.v_free_reserved = vm_pageout_page_count +
 1675             cnt.v_pageout_free_min + (cnt.v_page_count / 768);
 1676         cnt.v_free_severe = cnt.v_free_min / 2;
 1677         cnt.v_free_target = 4 * cnt.v_free_min + cnt.v_free_reserved;
 1678         cnt.v_free_min += cnt.v_free_reserved;
 1679         cnt.v_free_severe += cnt.v_free_reserved;
 1680         cnt.v_inactive_target = (3 * cnt.v_free_target) / 2;
 1681         if (cnt.v_inactive_target > cnt.v_free_count / 3)
 1682                 cnt.v_inactive_target = cnt.v_free_count / 3;
 1683 
 1684         /*
 1685          * Set the default wakeup threshold to be 10% above the minimum
 1686          * page limit.  This keeps the steady state out of shortfall.
 1687          */
 1688         vm_pageout_wakeup_thresh = (cnt.v_free_min / 10) * 11;
 1689 
 1690         /*
 1691          * Set interval in seconds for active scan.  We want to visit each
 1692          * page at least once every ten minutes.  This is to prevent worst
 1693          * case paging behaviors with stale active LRU.
 1694          */
 1695         if (vm_pageout_update_period == 0)
 1696                 vm_pageout_update_period = 600;
 1697 
 1698         /* XXX does not really belong here */
 1699         if (vm_page_max_wired == 0)
 1700                 vm_page_max_wired = cnt.v_free_count / 3;
 1701 
 1702         swap_pager_swap_init();
 1703 #if MAXMEMDOM > 1
 1704         for (i = 1; i < vm_ndomains; i++) {
 1705                 error = kthread_add(vm_pageout_worker, (void *)(uintptr_t)i,
 1706                     curproc, NULL, 0, 0, "dom%d", i);
 1707                 if (error != 0) {
 1708                         panic("starting pageout for domain %d, error %d\n",
 1709                             i, error);
 1710                 }
 1711         }
 1712 #endif
 1713         vm_pageout_worker((void *)(uintptr_t)0);
 1714 }
 1715 
 1716 /*
 1717  * Unless the free page queue lock is held by the caller, this function
 1718  * should be regarded as advisory.  Specifically, the caller should
 1719  * not msleep() on &cnt.v_free_count following this function unless
 1720  * the free page queue lock is held until the msleep() is performed.
 1721  */
 1722 void
 1723 pagedaemon_wakeup(void)
 1724 {
 1725 
 1726         if (!vm_pages_needed && curthread->td_proc != pageproc) {
 1727                 vm_pages_needed = 1;
 1728                 wakeup(&vm_pages_needed);
 1729         }
 1730 }
 1731 
 1732 #if !defined(NO_SWAPPING)
 1733 static void
 1734 vm_req_vmdaemon(int req)
 1735 {
 1736         static int lastrun = 0;
 1737 
 1738         mtx_lock(&vm_daemon_mtx);
 1739         vm_pageout_req_swapout |= req;
 1740         if ((ticks > (lastrun + hz)) || (ticks < lastrun)) {
 1741                 wakeup(&vm_daemon_needed);
 1742                 lastrun = ticks;
 1743         }
 1744         mtx_unlock(&vm_daemon_mtx);
 1745 }
 1746 
 1747 static void
 1748 vm_daemon(void)
 1749 {
 1750         struct rlimit rsslim;
 1751         struct proc *p;
 1752         struct thread *td;
 1753         struct vmspace *vm;
 1754         int breakout, swapout_flags, tryagain, attempts;
 1755 #ifdef RACCT
 1756         uint64_t rsize, ravailable;
 1757 #endif
 1758 
 1759         while (TRUE) {
 1760                 mtx_lock(&vm_daemon_mtx);
 1761 #ifdef RACCT
 1762                 msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", hz);
 1763 #else
 1764                 msleep(&vm_daemon_needed, &vm_daemon_mtx, PPAUSE, "psleep", 0);
 1765 #endif
 1766                 swapout_flags = vm_pageout_req_swapout;
 1767                 vm_pageout_req_swapout = 0;
 1768                 mtx_unlock(&vm_daemon_mtx);
 1769                 if (swapout_flags)
 1770                         swapout_procs(swapout_flags);
 1771 
 1772                 /*
 1773                  * scan the processes for exceeding their rlimits or if
 1774                  * process is swapped out -- deactivate pages
 1775                  */
 1776                 tryagain = 0;
 1777                 attempts = 0;
 1778 again:
 1779                 attempts++;
 1780                 sx_slock(&allproc_lock);
 1781                 FOREACH_PROC_IN_SYSTEM(p) {
 1782                         vm_pindex_t limit, size;
 1783 
 1784                         /*
 1785                          * if this is a system process or if we have already
 1786                          * looked at this process, skip it.
 1787                          */
 1788                         PROC_LOCK(p);
 1789                         if (p->p_state != PRS_NORMAL ||
 1790                             p->p_flag & (P_INEXEC | P_SYSTEM | P_WEXIT)) {
 1791                                 PROC_UNLOCK(p);
 1792                                 continue;
 1793                         }
 1794                         /*
 1795                          * if the process is in a non-running type state,
 1796                          * don't touch it.
 1797                          */
 1798                         breakout = 0;
 1799                         FOREACH_THREAD_IN_PROC(p, td) {
 1800                                 thread_lock(td);
 1801                                 if (!TD_ON_RUNQ(td) &&
 1802                                     !TD_IS_RUNNING(td) &&
 1803                                     !TD_IS_SLEEPING(td) &&
 1804                                     !TD_IS_SUSPENDED(td)) {
 1805                                         thread_unlock(td);
 1806                                         breakout = 1;
 1807                                         break;
 1808                                 }
 1809                                 thread_unlock(td);
 1810                         }
 1811                         if (breakout) {
 1812                                 PROC_UNLOCK(p);
 1813                                 continue;
 1814                         }
 1815                         /*
 1816                          * get a limit
 1817                          */
 1818                         lim_rlimit(p, RLIMIT_RSS, &rsslim);
 1819                         limit = OFF_TO_IDX(
 1820                             qmin(rsslim.rlim_cur, rsslim.rlim_max));
 1821 
 1822                         /*
 1823                          * let processes that are swapped out really be
 1824                          * swapped out set the limit to nothing (will force a
 1825                          * swap-out.)
 1826                          */
 1827                         if ((p->p_flag & P_INMEM) == 0)
 1828                                 limit = 0;      /* XXX */
 1829                         vm = vmspace_acquire_ref(p);
 1830                         PROC_UNLOCK(p);
 1831                         if (vm == NULL)
 1832                                 continue;
 1833 
 1834                         size = vmspace_resident_count(vm);
 1835                         if (size >= limit) {
 1836                                 vm_pageout_map_deactivate_pages(
 1837                                     &vm->vm_map, limit);
 1838                         }
 1839 #ifdef RACCT
 1840                         rsize = IDX_TO_OFF(size);
 1841                         PROC_LOCK(p);
 1842                         racct_set(p, RACCT_RSS, rsize);
 1843                         ravailable = racct_get_available(p, RACCT_RSS);
 1844                         PROC_UNLOCK(p);
 1845                         if (rsize > ravailable) {
 1846                                 /*
 1847                                  * Don't be overly aggressive; this might be
 1848                                  * an innocent process, and the limit could've
 1849                                  * been exceeded by some memory hog.  Don't
 1850                                  * try to deactivate more than 1/4th of process'
 1851                                  * resident set size.
 1852                                  */
 1853                                 if (attempts <= 8) {
 1854                                         if (ravailable < rsize - (rsize / 4))
 1855                                                 ravailable = rsize - (rsize / 4);
 1856                                 }
 1857                                 vm_pageout_map_deactivate_pages(
 1858                                     &vm->vm_map, OFF_TO_IDX(ravailable));
 1859                                 /* Update RSS usage after paging out. */
 1860                                 size = vmspace_resident_count(vm);
 1861                                 rsize = IDX_TO_OFF(size);
 1862                                 PROC_LOCK(p);
 1863                                 racct_set(p, RACCT_RSS, rsize);
 1864                                 PROC_UNLOCK(p);
 1865                                 if (rsize > ravailable)
 1866                                         tryagain = 1;
 1867                         }
 1868 #endif
 1869                         vmspace_free(vm);
 1870                 }
 1871                 sx_sunlock(&allproc_lock);
 1872                 if (tryagain != 0 && attempts <= 10)
 1873                         goto again;
 1874         }
 1875 }
 1876 #endif                  /* !defined(NO_SWAPPING) */

Cache object: 71e89ae4bfd9a3ac5416b2e45811cfac


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