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/uvm/uvm_pdaemon.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: uvm_pdaemon.c,v 1.59.2.2 2005/03/16 12:11:02 tron Exp $        */
    2 
    3 /*
    4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
    5  * Copyright (c) 1991, 1993, The Regents of the University of California.
    6  *
    7  * All rights reserved.
    8  *
    9  * This code is derived from software contributed to Berkeley by
   10  * The Mach Operating System project at Carnegie-Mellon University.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. All advertising materials mentioning features or use of this software
   21  *    must display the following acknowledgement:
   22  *      This product includes software developed by Charles D. Cranor,
   23  *      Washington University, the University of California, Berkeley and
   24  *      its contributors.
   25  * 4. Neither the name of the University nor the names of its contributors
   26  *    may be used to endorse or promote products derived from this software
   27  *    without specific prior written permission.
   28  *
   29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   39  * SUCH DAMAGE.
   40  *
   41  *      @(#)vm_pageout.c        8.5 (Berkeley) 2/14/94
   42  * from: Id: uvm_pdaemon.c,v 1.1.2.32 1998/02/06 05:26:30 chs Exp
   43  *
   44  *
   45  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
   46  * All rights reserved.
   47  *
   48  * Permission to use, copy, modify and distribute this software and
   49  * its documentation is hereby granted, provided that both the copyright
   50  * notice and this permission notice appear in all copies of the
   51  * software, derivative works or modified versions, and any portions
   52  * thereof, and that both notices appear in supporting documentation.
   53  *
   54  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
   55  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
   56  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
   57  *
   58  * Carnegie Mellon requests users of this software to return to
   59  *
   60  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
   61  *  School of Computer Science
   62  *  Carnegie Mellon University
   63  *  Pittsburgh PA 15213-3890
   64  *
   65  * any improvements or extensions that they make and grant Carnegie the
   66  * rights to redistribute these changes.
   67  */
   68 
   69 /*
   70  * uvm_pdaemon.c: the page daemon
   71  */
   72 
   73 #include <sys/cdefs.h>
   74 __KERNEL_RCSID(0, "$NetBSD: uvm_pdaemon.c,v 1.59.2.2 2005/03/16 12:11:02 tron Exp $");
   75 
   76 #include "opt_uvmhist.h"
   77 
   78 #include <sys/param.h>
   79 #include <sys/proc.h>
   80 #include <sys/systm.h>
   81 #include <sys/kernel.h>
   82 #include <sys/pool.h>
   83 #include <sys/buf.h>
   84 #include <sys/vnode.h>
   85 
   86 #include <uvm/uvm.h>
   87 
   88 /*
   89  * UVMPD_NUMDIRTYREACTS is how many dirty pages the pagedaemon will reactivate
   90  * in a pass thru the inactive list when swap is full.  the value should be
   91  * "small"... if it's too large we'll cycle the active pages thru the inactive
   92  * queue too quickly to for them to be referenced and avoid being freed.
   93  */
   94 
   95 #define UVMPD_NUMDIRTYREACTS 16
   96 
   97 
   98 /*
   99  * local prototypes
  100  */
  101 
  102 void            uvmpd_scan(void);
  103 void            uvmpd_scan_inactive(struct pglist *);
  104 void            uvmpd_tune(void);
  105 
  106 /*
  107  * XXX hack to avoid hangs when large processes fork.
  108  */
  109 int uvm_extrapages;
  110 
  111 /*
  112  * uvm_wait: wait (sleep) for the page daemon to free some pages
  113  *
  114  * => should be called with all locks released
  115  * => should _not_ be called by the page daemon (to avoid deadlock)
  116  */
  117 
  118 void
  119 uvm_wait(wmsg)
  120         const char *wmsg;
  121 {
  122         int timo = 0;
  123         int s = splbio();
  124 
  125         /*
  126          * check for page daemon going to sleep (waiting for itself)
  127          */
  128 
  129         if (curproc == uvm.pagedaemon_proc && uvmexp.paging == 0) {
  130                 /*
  131                  * now we have a problem: the pagedaemon wants to go to
  132                  * sleep until it frees more memory.   but how can it
  133                  * free more memory if it is asleep?  that is a deadlock.
  134                  * we have two options:
  135                  *  [1] panic now
  136                  *  [2] put a timeout on the sleep, thus causing the
  137                  *      pagedaemon to only pause (rather than sleep forever)
  138                  *
  139                  * note that option [2] will only help us if we get lucky
  140                  * and some other process on the system breaks the deadlock
  141                  * by exiting or freeing memory (thus allowing the pagedaemon
  142                  * to continue).  for now we panic if DEBUG is defined,
  143                  * otherwise we hope for the best with option [2] (better
  144                  * yet, this should never happen in the first place!).
  145                  */
  146 
  147                 printf("pagedaemon: deadlock detected!\n");
  148                 timo = hz >> 3;         /* set timeout */
  149 #if defined(DEBUG)
  150                 /* DEBUG: panic so we can debug it */
  151                 panic("pagedaemon deadlock");
  152 #endif
  153         }
  154 
  155         simple_lock(&uvm.pagedaemon_lock);
  156         wakeup(&uvm.pagedaemon);                /* wake the daemon! */
  157         UVM_UNLOCK_AND_WAIT(&uvmexp.free, &uvm.pagedaemon_lock, FALSE, wmsg,
  158             timo);
  159 
  160         splx(s);
  161 }
  162 
  163 
  164 /*
  165  * uvmpd_tune: tune paging parameters
  166  *
  167  * => called when ever memory is added (or removed?) to the system
  168  * => caller must call with page queues locked
  169  */
  170 
  171 void
  172 uvmpd_tune(void)
  173 {
  174         UVMHIST_FUNC("uvmpd_tune"); UVMHIST_CALLED(pdhist);
  175 
  176         uvmexp.freemin = uvmexp.npages / 20;
  177 
  178         /* between 16k and 256k */
  179         /* XXX:  what are these values good for? */
  180         uvmexp.freemin = MAX(uvmexp.freemin, (16*1024) >> PAGE_SHIFT);
  181         uvmexp.freemin = MIN(uvmexp.freemin, (256*1024) >> PAGE_SHIFT);
  182 
  183         /* Make sure there's always a user page free. */
  184         if (uvmexp.freemin < uvmexp.reserve_kernel + 1)
  185                 uvmexp.freemin = uvmexp.reserve_kernel + 1;
  186 
  187         uvmexp.freetarg = (uvmexp.freemin * 4) / 3;
  188         if (uvmexp.freetarg <= uvmexp.freemin)
  189                 uvmexp.freetarg = uvmexp.freemin + 1;
  190 
  191         uvmexp.freetarg += uvm_extrapages;
  192         uvm_extrapages = 0;
  193 
  194         /* uvmexp.inactarg: computed in main daemon loop */
  195 
  196         uvmexp.wiredmax = uvmexp.npages / 3;
  197         UVMHIST_LOG(pdhist, "<- done, freemin=%d, freetarg=%d, wiredmax=%d",
  198               uvmexp.freemin, uvmexp.freetarg, uvmexp.wiredmax, 0);
  199 }
  200 
  201 /*
  202  * uvm_pageout: the main loop for the pagedaemon
  203  */
  204 
  205 void
  206 uvm_pageout(void *arg)
  207 {
  208         int bufcnt, npages = 0;
  209         int extrapages = 0;
  210         UVMHIST_FUNC("uvm_pageout"); UVMHIST_CALLED(pdhist);
  211 
  212         UVMHIST_LOG(pdhist,"<starting uvm pagedaemon>", 0, 0, 0, 0);
  213 
  214         /*
  215          * ensure correct priority and set paging parameters...
  216          */
  217 
  218         uvm.pagedaemon_proc = curproc;
  219         uvm_lock_pageq();
  220         npages = uvmexp.npages;
  221         uvmpd_tune();
  222         uvm_unlock_pageq();
  223 
  224         /*
  225          * main loop
  226          */
  227 
  228         for (;;) {
  229                 simple_lock(&uvm.pagedaemon_lock);
  230 
  231                 UVMHIST_LOG(pdhist,"  <<SLEEPING>>",0,0,0,0);
  232                 UVM_UNLOCK_AND_WAIT(&uvm.pagedaemon,
  233                     &uvm.pagedaemon_lock, FALSE, "pgdaemon", 0);
  234                 uvmexp.pdwoke++;
  235                 UVMHIST_LOG(pdhist,"  <<WOKE UP>>",0,0,0,0);
  236 
  237                 /*
  238                  * now lock page queues and recompute inactive count
  239                  */
  240 
  241                 uvm_lock_pageq();
  242                 if (npages != uvmexp.npages || extrapages != uvm_extrapages) {
  243                         npages = uvmexp.npages;
  244                         extrapages = uvm_extrapages;
  245                         uvmpd_tune();
  246                 }
  247 
  248                 uvmexp.inactarg = (uvmexp.active + uvmexp.inactive) / 3;
  249                 if (uvmexp.inactarg <= uvmexp.freetarg) {
  250                         uvmexp.inactarg = uvmexp.freetarg + 1;
  251                 }
  252 
  253                 /*
  254                  * Estimate a hint.  Note that bufmem are returned to
  255                  * system only when entire pool page is empty.
  256                  */
  257                 bufcnt = uvmexp.freetarg - uvmexp.free;
  258                 if (bufcnt < 0)
  259                         bufcnt = 0;
  260 
  261                 UVMHIST_LOG(pdhist,"  free/ftarg=%d/%d, inact/itarg=%d/%d",
  262                     uvmexp.free, uvmexp.freetarg, uvmexp.inactive,
  263                     uvmexp.inactarg);
  264 
  265                 /*
  266                  * scan if needed
  267                  */
  268 
  269                 if (uvmexp.free + uvmexp.paging < uvmexp.freetarg ||
  270                     uvmexp.inactive < uvmexp.inactarg) {
  271                         uvmpd_scan();
  272                 }
  273 
  274                 /*
  275                  * if there's any free memory to be had,
  276                  * wake up any waiters.
  277                  */
  278 
  279                 if (uvmexp.free > uvmexp.reserve_kernel ||
  280                     uvmexp.paging == 0) {
  281                         wakeup(&uvmexp.free);
  282                 }
  283 
  284                 /*
  285                  * scan done.  unlock page queues (the only lock we are holding)
  286                  */
  287 
  288                 uvm_unlock_pageq();
  289 
  290                 buf_drain(bufcnt << PAGE_SHIFT);
  291 
  292                 /*
  293                  * drain pool resources now that we're not holding any locks
  294                  */
  295 
  296                 pool_drain(0);
  297 
  298                 /*
  299                  * free any cached u-areas we don't need
  300                  */
  301                 uvm_uarea_drain(TRUE);
  302 
  303         }
  304         /*NOTREACHED*/
  305 }
  306 
  307 
  308 /*
  309  * uvm_aiodone_daemon:  main loop for the aiodone daemon.
  310  */
  311 
  312 void
  313 uvm_aiodone_daemon(void *arg)
  314 {
  315         int s, free;
  316         struct buf *bp, *nbp;
  317         UVMHIST_FUNC("uvm_aiodoned"); UVMHIST_CALLED(pdhist);
  318 
  319         for (;;) {
  320 
  321                 /*
  322                  * carefully attempt to go to sleep (without losing "wakeups"!).
  323                  * we need splbio because we want to make sure the aio_done list
  324                  * is totally empty before we go to sleep.
  325                  */
  326 
  327                 s = splbio();
  328                 simple_lock(&uvm.aiodoned_lock);
  329                 if (TAILQ_FIRST(&uvm.aio_done) == NULL) {
  330                         UVMHIST_LOG(pdhist,"  <<SLEEPING>>",0,0,0,0);
  331                         UVM_UNLOCK_AND_WAIT(&uvm.aiodoned,
  332                             &uvm.aiodoned_lock, FALSE, "aiodoned", 0);
  333                         UVMHIST_LOG(pdhist,"  <<WOKE UP>>",0,0,0,0);
  334 
  335                         /* relock aiodoned_lock, still at splbio */
  336                         simple_lock(&uvm.aiodoned_lock);
  337                 }
  338 
  339                 /*
  340                  * check for done aio structures
  341                  */
  342 
  343                 bp = TAILQ_FIRST(&uvm.aio_done);
  344                 if (bp) {
  345                         TAILQ_INIT(&uvm.aio_done);
  346                 }
  347 
  348                 simple_unlock(&uvm.aiodoned_lock);
  349                 splx(s);
  350 
  351                 /*
  352                  * process each i/o that's done.
  353                  */
  354 
  355                 free = uvmexp.free;
  356                 while (bp != NULL) {
  357                         nbp = TAILQ_NEXT(bp, b_freelist);
  358                         (*bp->b_iodone)(bp);
  359                         bp = nbp;
  360                 }
  361                 if (free <= uvmexp.reserve_kernel) {
  362                         s = uvm_lock_fpageq();
  363                         wakeup(&uvm.pagedaemon);
  364                         uvm_unlock_fpageq(s);
  365                 } else {
  366                         simple_lock(&uvm.pagedaemon_lock);
  367                         wakeup(&uvmexp.free);
  368                         simple_unlock(&uvm.pagedaemon_lock);
  369                 }
  370         }
  371 }
  372 
  373 /*
  374  * uvmpd_scan_inactive: scan an inactive list for pages to clean or free.
  375  *
  376  * => called with page queues locked
  377  * => we work on meeting our free target by converting inactive pages
  378  *    into free pages.
  379  * => we handle the building of swap-backed clusters
  380  * => we return TRUE if we are exiting because we met our target
  381  */
  382 
  383 void
  384 uvmpd_scan_inactive(pglst)
  385         struct pglist *pglst;
  386 {
  387         int error;
  388         struct vm_page *p, *nextpg = NULL; /* Quell compiler warning */
  389         struct uvm_object *uobj;
  390         struct vm_anon *anon;
  391         struct vm_page *swpps[round_page(MAXPHYS) >> PAGE_SHIFT];
  392         struct simplelock *slock;
  393         int swnpages, swcpages;
  394         int swslot;
  395         int dirtyreacts, t, result;
  396         boolean_t anonunder, fileunder, execunder;
  397         boolean_t anonover, fileover, execover;
  398         boolean_t anonreact, filereact, execreact;
  399         UVMHIST_FUNC("uvmpd_scan_inactive"); UVMHIST_CALLED(pdhist);
  400 
  401         /*
  402          * swslot is non-zero if we are building a swap cluster.  we want
  403          * to stay in the loop while we have a page to scan or we have
  404          * a swap-cluster to build.
  405          */
  406 
  407         swslot = 0;
  408         swnpages = swcpages = 0;
  409         dirtyreacts = 0;
  410 
  411         /*
  412          * decide which types of pages we want to reactivate instead of freeing
  413          * to keep usage within the minimum and maximum usage limits.
  414          */
  415 
  416         t = uvmexp.active + uvmexp.inactive + uvmexp.free;
  417         anonunder = (uvmexp.anonpages <= (t * uvmexp.anonmin) >> 8);
  418         fileunder = (uvmexp.filepages <= (t * uvmexp.filemin) >> 8);
  419         execunder = (uvmexp.execpages <= (t * uvmexp.execmin) >> 8);
  420         anonover = uvmexp.anonpages > ((t * uvmexp.anonmax) >> 8);
  421         fileover = uvmexp.filepages > ((t * uvmexp.filemax) >> 8);
  422         execover = uvmexp.execpages > ((t * uvmexp.execmax) >> 8);
  423         anonreact = anonunder || (!anonover && (fileover || execover));
  424         filereact = fileunder || (!fileover && (anonover || execover));
  425         execreact = execunder || (!execover && (anonover || fileover));
  426         for (p = TAILQ_FIRST(pglst); p != NULL || swslot != 0; p = nextpg) {
  427                 uobj = NULL;
  428                 anon = NULL;
  429                 if (p) {
  430 
  431                         /*
  432                          * see if we've met the free target.
  433                          */
  434 
  435                         if (uvmexp.free + uvmexp.paging >=
  436                             uvmexp.freetarg << 2 ||
  437                             dirtyreacts == UVMPD_NUMDIRTYREACTS) {
  438                                 UVMHIST_LOG(pdhist,"  met free target: "
  439                                             "exit loop", 0, 0, 0, 0);
  440 
  441                                 if (swslot == 0) {
  442                                         /* exit now if no swap-i/o pending */
  443                                         break;
  444                                 }
  445 
  446                                 /* set p to null to signal final swap i/o */
  447                                 p = NULL;
  448                                 nextpg = NULL;
  449                         }
  450                 }
  451                 if (p) {        /* if (we have a new page to consider) */
  452 
  453                         /*
  454                          * we are below target and have a new page to consider.
  455                          */
  456 
  457                         uvmexp.pdscans++;
  458                         nextpg = TAILQ_NEXT(p, pageq);
  459 
  460                         /*
  461                          * move referenced pages back to active queue and
  462                          * skip to next page.
  463                          */
  464 
  465                         if (pmap_clear_reference(p)) {
  466                                 uvm_pageactivate(p);
  467                                 uvmexp.pdreact++;
  468                                 continue;
  469                         }
  470                         anon = p->uanon;
  471                         uobj = p->uobject;
  472 
  473                         /*
  474                          * enforce the minimum thresholds on different
  475                          * types of memory usage.  if reusing the current
  476                          * page would reduce that type of usage below its
  477                          * minimum, reactivate the page instead and move
  478                          * on to the next page.
  479                          */
  480 
  481                         if (uobj && UVM_OBJ_IS_VTEXT(uobj) && execreact) {
  482                                 uvm_pageactivate(p);
  483                                 uvmexp.pdreexec++;
  484                                 continue;
  485                         }
  486                         if (uobj && UVM_OBJ_IS_VNODE(uobj) &&
  487                             !UVM_OBJ_IS_VTEXT(uobj) && filereact) {
  488                                 uvm_pageactivate(p);
  489                                 uvmexp.pdrefile++;
  490                                 continue;
  491                         }
  492                         if ((anon || UVM_OBJ_IS_AOBJ(uobj)) && anonreact) {
  493                                 uvm_pageactivate(p);
  494                                 uvmexp.pdreanon++;
  495                                 continue;
  496                         }
  497 
  498                         /*
  499                          * first we attempt to lock the object that this page
  500                          * belongs to.  if our attempt fails we skip on to
  501                          * the next page (no harm done).  it is important to
  502                          * "try" locking the object as we are locking in the
  503                          * wrong order (pageq -> object) and we don't want to
  504                          * deadlock.
  505                          *
  506                          * the only time we expect to see an ownerless page
  507                          * (i.e. a page with no uobject and !PQ_ANON) is if an
  508                          * anon has loaned a page from a uvm_object and the
  509                          * uvm_object has dropped the ownership.  in that
  510                          * case, the anon can "take over" the loaned page
  511                          * and make it its own.
  512                          */
  513 
  514                         /* does the page belong to an object? */
  515                         if (uobj != NULL) {
  516                                 slock = &uobj->vmobjlock;
  517                                 if (!simple_lock_try(slock)) {
  518                                         continue;
  519                                 }
  520                                 if (p->flags & PG_BUSY) {
  521                                         simple_unlock(slock);
  522                                         uvmexp.pdbusy++;
  523                                         continue;
  524                                 }
  525                                 uvmexp.pdobscan++;
  526                         } else {
  527                                 KASSERT(anon != NULL);
  528                                 slock = &anon->an_lock;
  529                                 if (!simple_lock_try(slock)) {
  530                                         continue;
  531                                 }
  532 
  533                                 /*
  534                                  * set PQ_ANON if it isn't set already.
  535                                  */
  536 
  537                                 if ((p->pqflags & PQ_ANON) == 0) {
  538                                         KASSERT(p->loan_count > 0);
  539                                         p->loan_count--;
  540                                         p->pqflags |= PQ_ANON;
  541                                         /* anon now owns it */
  542                                 }
  543                                 if (p->flags & PG_BUSY) {
  544                                         simple_unlock(slock);
  545                                         uvmexp.pdbusy++;
  546                                         continue;
  547                                 }
  548                                 uvmexp.pdanscan++;
  549                         }
  550 
  551 
  552                         /*
  553                          * we now have the object and the page queues locked.
  554                          * if the page is not swap-backed, call the object's
  555                          * pager to flush and free the page.
  556                          */
  557 
  558                         if ((p->pqflags & PQ_SWAPBACKED) == 0) {
  559                                 uvm_unlock_pageq();
  560                                 (void) (uobj->pgops->pgo_put)(uobj, p->offset,
  561                                     p->offset + PAGE_SIZE,
  562                                     PGO_CLEANIT|PGO_FREE);
  563                                 uvm_lock_pageq();
  564                                 if (nextpg &&
  565                                     (nextpg->pqflags & PQ_INACTIVE) == 0) {
  566                                         nextpg = TAILQ_FIRST(pglst);
  567                                 }
  568                                 continue;
  569                         }
  570 
  571                         /*
  572                          * the page is swap-backed.  remove all the permissions
  573                          * from the page so we can sync the modified info
  574                          * without any race conditions.  if the page is clean
  575                          * we can free it now and continue.
  576                          */
  577 
  578                         pmap_page_protect(p, VM_PROT_NONE);
  579                         if ((p->flags & PG_CLEAN) && pmap_clear_modify(p)) {
  580                                 p->flags &= ~(PG_CLEAN);
  581                         }
  582                         if (p->flags & PG_CLEAN) {
  583                                 int slot;
  584                                 int pageidx;
  585 
  586                                 pageidx = p->offset >> PAGE_SHIFT;
  587                                 uvm_pagefree(p);
  588                                 uvmexp.pdfreed++;
  589 
  590                                 /*
  591                                  * for anons, we need to remove the page
  592                                  * from the anon ourselves.  for aobjs,
  593                                  * pagefree did that for us.
  594                                  */
  595 
  596                                 if (anon) {
  597                                         KASSERT(anon->an_swslot != 0);
  598                                         anon->u.an_page = NULL;
  599                                         slot = anon->an_swslot;
  600                                 } else {
  601                                         slot = uao_find_swslot(uobj, pageidx);
  602                                 }
  603                                 simple_unlock(slock);
  604 
  605                                 if (slot > 0) {
  606                                         /* this page is now only in swap. */
  607                                         simple_lock(&uvm.swap_data_lock);
  608                                         KASSERT(uvmexp.swpgonly <
  609                                                 uvmexp.swpginuse);
  610                                         uvmexp.swpgonly++;
  611                                         simple_unlock(&uvm.swap_data_lock);
  612                                 }
  613                                 continue;
  614                         }
  615 
  616                         /*
  617                          * this page is dirty, skip it if we'll have met our
  618                          * free target when all the current pageouts complete.
  619                          */
  620 
  621                         if (uvmexp.free + uvmexp.paging >
  622                             uvmexp.freetarg << 2) {
  623                                 simple_unlock(slock);
  624                                 continue;
  625                         }
  626 
  627                         /*
  628                          * free any swap space allocated to the page since
  629                          * we'll have to write it again with its new data.
  630                          */
  631 
  632                         if ((p->pqflags & PQ_ANON) && anon->an_swslot) {
  633                                 uvm_swap_free(anon->an_swslot, 1);
  634                                 anon->an_swslot = 0;
  635                         } else if (p->pqflags & PQ_AOBJ) {
  636                                 uao_dropswap(uobj, p->offset >> PAGE_SHIFT);
  637                         }
  638 
  639                         /*
  640                          * if all pages in swap are only in swap,
  641                          * the swap space is full and we can't page out
  642                          * any more swap-backed pages.  reactivate this page
  643                          * so that we eventually cycle all pages through
  644                          * the inactive queue.
  645                          */
  646 
  647                         if (uvm_swapisfull()) {
  648                                 dirtyreacts++;
  649                                 uvm_pageactivate(p);
  650                                 simple_unlock(slock);
  651                                 continue;
  652                         }
  653 
  654                         /*
  655                          * start new swap pageout cluster (if necessary).
  656                          */
  657 
  658                         if (swslot == 0) {
  659                                 /* Even with strange MAXPHYS, the shift
  660                                    implicitly rounds down to a page. */
  661                                 swnpages = MAXPHYS >> PAGE_SHIFT;
  662                                 swslot = uvm_swap_alloc(&swnpages, TRUE);
  663                                 if (swslot == 0) {
  664                                         simple_unlock(slock);
  665                                         continue;
  666                                 }
  667                                 swcpages = 0;
  668                         }
  669 
  670                         /*
  671                          * at this point, we're definitely going reuse this
  672                          * page.  mark the page busy and delayed-free.
  673                          * we should remove the page from the page queues
  674                          * so we don't ever look at it again.
  675                          * adjust counters and such.
  676                          */
  677 
  678                         p->flags |= PG_BUSY;
  679                         UVM_PAGE_OWN(p, "scan_inactive");
  680 
  681                         p->flags |= PG_PAGEOUT;
  682                         uvmexp.paging++;
  683                         uvm_pagedequeue(p);
  684 
  685                         uvmexp.pgswapout++;
  686 
  687                         /*
  688                          * add the new page to the cluster.
  689                          */
  690 
  691                         if (anon) {
  692                                 anon->an_swslot = swslot + swcpages;
  693                                 simple_unlock(slock);
  694                         } else {
  695                                 result = uao_set_swslot(uobj,
  696                                     p->offset >> PAGE_SHIFT, swslot + swcpages);
  697                                 if (result == -1) {
  698                                         p->flags &= ~(PG_BUSY|PG_PAGEOUT);
  699                                         UVM_PAGE_OWN(p, NULL);
  700                                         uvmexp.paging--;
  701                                         uvm_pageactivate(p);
  702                                         simple_unlock(slock);
  703                                         continue;
  704                                 }
  705                                 simple_unlock(slock);
  706                         }
  707                         swpps[swcpages] = p;
  708                         swcpages++;
  709 
  710                         /*
  711                          * if the cluster isn't full, look for more pages
  712                          * before starting the i/o.
  713                          */
  714 
  715                         if (swcpages < swnpages) {
  716                                 continue;
  717                         }
  718                 }
  719 
  720                 /*
  721                  * if this is the final pageout we could have a few
  722                  * unused swap blocks.  if so, free them now.
  723                  */
  724 
  725                 if (swcpages < swnpages) {
  726                         uvm_swap_free(swslot + swcpages, (swnpages - swcpages));
  727                 }
  728 
  729                 /*
  730                  * now start the pageout.
  731                  */
  732 
  733                 uvm_unlock_pageq();
  734                 uvmexp.pdpageouts++;
  735                 error = uvm_swap_put(swslot, swpps, swcpages, 0);
  736                 KASSERT(error == 0);
  737                 uvm_lock_pageq();
  738 
  739                 /*
  740                  * zero swslot to indicate that we are
  741                  * no longer building a swap-backed cluster.
  742                  */
  743 
  744                 swslot = 0;
  745 
  746                 /*
  747                  * the pageout is in progress.  bump counters and set up
  748                  * for the next loop.
  749                  */
  750 
  751                 uvmexp.pdpending++;
  752                 if (nextpg && (nextpg->pqflags & PQ_INACTIVE) == 0) {
  753                         nextpg = TAILQ_FIRST(pglst);
  754                 }
  755         }
  756 }
  757 
  758 /*
  759  * uvmpd_scan: scan the page queues and attempt to meet our targets.
  760  *
  761  * => called with pageq's locked
  762  */
  763 
  764 void
  765 uvmpd_scan(void)
  766 {
  767         int inactive_shortage, swap_shortage, pages_freed;
  768         struct vm_page *p, *nextpg;
  769         struct uvm_object *uobj;
  770         struct vm_anon *anon;
  771         struct simplelock *slock;
  772         UVMHIST_FUNC("uvmpd_scan"); UVMHIST_CALLED(pdhist);
  773 
  774         uvmexp.pdrevs++;
  775         uobj = NULL;
  776         anon = NULL;
  777 
  778 #ifndef __SWAP_BROKEN
  779 
  780         /*
  781          * swap out some processes if we are below our free target.
  782          * we need to unlock the page queues for this.
  783          */
  784 
  785         if (uvmexp.free < uvmexp.freetarg && uvmexp.nswapdev != 0) {
  786                 uvmexp.pdswout++;
  787                 UVMHIST_LOG(pdhist,"  free %d < target %d: swapout",
  788                     uvmexp.free, uvmexp.freetarg, 0, 0);
  789                 uvm_unlock_pageq();
  790                 uvm_swapout_threads();
  791                 uvm_lock_pageq();
  792 
  793         }
  794 #endif
  795 
  796         /*
  797          * now we want to work on meeting our targets.   first we work on our
  798          * free target by converting inactive pages into free pages.  then
  799          * we work on meeting our inactive target by converting active pages
  800          * to inactive ones.
  801          */
  802 
  803         UVMHIST_LOG(pdhist, "  starting 'free' loop",0,0,0,0);
  804 
  805         pages_freed = uvmexp.pdfreed;
  806         uvmpd_scan_inactive(&uvm.page_inactive);
  807         pages_freed = uvmexp.pdfreed - pages_freed;
  808 
  809         /*
  810          * we have done the scan to get free pages.   now we work on meeting
  811          * our inactive target.
  812          */
  813 
  814         inactive_shortage = uvmexp.inactarg - uvmexp.inactive;
  815 
  816         /*
  817          * detect if we're not going to be able to page anything out
  818          * until we free some swap resources from active pages.
  819          */
  820 
  821         swap_shortage = 0;
  822         if (uvmexp.free < uvmexp.freetarg &&
  823             uvmexp.swpginuse >= uvmexp.swpgavail &&
  824             !uvm_swapisfull() &&
  825             pages_freed == 0) {
  826                 swap_shortage = uvmexp.freetarg - uvmexp.free;
  827         }
  828 
  829         UVMHIST_LOG(pdhist, "  loop 2: inactive_shortage=%d swap_shortage=%d",
  830                     inactive_shortage, swap_shortage,0,0);
  831         for (p = TAILQ_FIRST(&uvm.page_active);
  832              p != NULL && (inactive_shortage > 0 || swap_shortage > 0);
  833              p = nextpg) {
  834                 nextpg = TAILQ_NEXT(p, pageq);
  835                 if (p->flags & PG_BUSY) {
  836                         continue;
  837                 }
  838 
  839                 /*
  840                  * lock the page's owner.
  841                  */
  842 
  843                 if (p->uobject != NULL) {
  844                         uobj = p->uobject;
  845                         slock = &uobj->vmobjlock;
  846                         if (!simple_lock_try(slock)) {
  847                                 continue;
  848                         }
  849                 } else {
  850                         anon = p->uanon;
  851                         KASSERT(anon != NULL);
  852                         slock = &anon->an_lock;
  853                         if (!simple_lock_try(slock)) {
  854                                 continue;
  855                         }
  856 
  857                         /* take over the page? */
  858                         if ((p->pqflags & PQ_ANON) == 0) {
  859                                 KASSERT(p->loan_count > 0);
  860                                 p->loan_count--;
  861                                 p->pqflags |= PQ_ANON;
  862                         }
  863                 }
  864 
  865                 /*
  866                  * skip this page if it's busy.
  867                  */
  868 
  869                 if ((p->flags & PG_BUSY) != 0) {
  870                         simple_unlock(slock);
  871                         continue;
  872                 }
  873 
  874                 /*
  875                  * if there's a shortage of swap, free any swap allocated
  876                  * to this page so that other pages can be paged out.
  877                  */
  878 
  879                 if (swap_shortage > 0) {
  880                         if ((p->pqflags & PQ_ANON) && anon->an_swslot) {
  881                                 uvm_swap_free(anon->an_swslot, 1);
  882                                 anon->an_swslot = 0;
  883                                 p->flags &= ~PG_CLEAN;
  884                                 swap_shortage--;
  885                         } else if (p->pqflags & PQ_AOBJ) {
  886                                 int slot = uao_set_swslot(uobj,
  887                                         p->offset >> PAGE_SHIFT, 0);
  888                                 if (slot) {
  889                                         uvm_swap_free(slot, 1);
  890                                         p->flags &= ~PG_CLEAN;
  891                                         swap_shortage--;
  892                                 }
  893                         }
  894                 }
  895 
  896                 /*
  897                  * if there's a shortage of inactive pages, deactivate.
  898                  */
  899 
  900                 if (inactive_shortage > 0) {
  901                         /* no need to check wire_count as pg is "active" */
  902                         uvm_pagedeactivate(p);
  903                         uvmexp.pddeact++;
  904                         inactive_shortage--;
  905                 }
  906 
  907                 /*
  908                  * we're done with this page.
  909                  */
  910 
  911                 simple_unlock(slock);
  912         }
  913 }

Cache object: ca6e6726f9bb02495d6e7edb6f33cbbd


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