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