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_reserv.c

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

    1 /*-
    2  * Copyright (c) 2002-2006 Rice University
    3  * Copyright (c) 2007-2008 Alan L. Cox <alc@cs.rice.edu>
    4  * All rights reserved.
    5  *
    6  * This software was developed for the FreeBSD Project by Alan L. Cox,
    7  * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro.
    8  *
    9  * Redistribution and use in source and binary forms, with or without
   10  * modification, are permitted provided that the following conditions
   11  * are met:
   12  * 1. Redistributions of source code must retain the above copyright
   13  *    notice, this list of conditions and the following disclaimer.
   14  * 2. Redistributions in binary form must reproduce the above copyright
   15  *    notice, this list of conditions and the following disclaimer in the
   16  *    documentation and/or other materials provided with the distribution.
   17  *
   18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   21  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
   22  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
   25  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
   26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
   28  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   29  * POSSIBILITY OF SUCH DAMAGE.
   30  */
   31 
   32 /*
   33  *      Superpage reservation management module
   34  */
   35 
   36 #include <sys/cdefs.h>
   37 __FBSDID("$FreeBSD: releng/9.2/sys/vm/vm_reserv.c 234771 2012-04-28 20:34:14Z alc $");
   38 
   39 #include "opt_vm.h"
   40 
   41 #include <sys/param.h>
   42 #include <sys/kernel.h>
   43 #include <sys/lock.h>
   44 #include <sys/malloc.h>
   45 #include <sys/mutex.h>
   46 #include <sys/queue.h>
   47 #include <sys/sbuf.h>
   48 #include <sys/sysctl.h>
   49 #include <sys/systm.h>
   50 
   51 #include <vm/vm.h>
   52 #include <vm/vm_param.h>
   53 #include <vm/vm_object.h>
   54 #include <vm/vm_page.h>
   55 #include <vm/vm_phys.h>
   56 #include <vm/vm_reserv.h>
   57 
   58 /*
   59  * The reservation system supports the speculative allocation of large physical
   60  * pages ("superpages").  Speculative allocation enables the fully-automatic
   61  * utilization of superpages by the virtual memory system.  In other words, no
   62  * programmatic directives are required to use superpages.
   63  */
   64 
   65 #if VM_NRESERVLEVEL > 0
   66 
   67 /*
   68  * The number of small pages that are contained in a level 0 reservation
   69  */
   70 #define VM_LEVEL_0_NPAGES       (1 << VM_LEVEL_0_ORDER)
   71 
   72 /*
   73  * The number of bits by which a physical address is shifted to obtain the
   74  * reservation number
   75  */
   76 #define VM_LEVEL_0_SHIFT        (VM_LEVEL_0_ORDER + PAGE_SHIFT)
   77 
   78 /*
   79  * The size of a level 0 reservation in bytes
   80  */
   81 #define VM_LEVEL_0_SIZE         (1 << VM_LEVEL_0_SHIFT)
   82 
   83 /*
   84  * Computes the index of the small page underlying the given (object, pindex)
   85  * within the reservation's array of small pages.
   86  */
   87 #define VM_RESERV_INDEX(object, pindex) \
   88     (((object)->pg_color + (pindex)) & (VM_LEVEL_0_NPAGES - 1))
   89 
   90 /*
   91  * The reservation structure
   92  *
   93  * A reservation structure is constructed whenever a large physical page is
   94  * speculatively allocated to an object.  The reservation provides the small
   95  * physical pages for the range [pindex, pindex + VM_LEVEL_0_NPAGES) of offsets
   96  * within that object.  The reservation's "popcnt" tracks the number of these
   97  * small physical pages that are in use at any given time.  When and if the
   98  * reservation is not fully utilized, it appears in the queue of partially-
   99  * populated reservations.  The reservation always appears on the containing
  100  * object's list of reservations.
  101  *
  102  * A partially-populated reservation can be broken and reclaimed at any time.
  103  */
  104 struct vm_reserv {
  105         TAILQ_ENTRY(vm_reserv) partpopq;
  106         LIST_ENTRY(vm_reserv) objq;
  107         vm_object_t     object;                 /* containing object */
  108         vm_pindex_t     pindex;                 /* offset within object */
  109         vm_page_t       pages;                  /* first page of a superpage */
  110         int             popcnt;                 /* # of pages in use */
  111         char            inpartpopq;
  112 };
  113 
  114 /*
  115  * The reservation array
  116  *
  117  * This array is analoguous in function to vm_page_array.  It differs in the
  118  * respect that it may contain a greater number of useful reservation
  119  * structures than there are (physical) superpages.  These "invalid"
  120  * reservation structures exist to trade-off space for time in the
  121  * implementation of vm_reserv_from_page().  Invalid reservation structures are
  122  * distinguishable from "valid" reservation structures by inspecting the
  123  * reservation's "pages" field.  Invalid reservation structures have a NULL
  124  * "pages" field.
  125  *
  126  * vm_reserv_from_page() maps a small (physical) page to an element of this
  127  * array by computing a physical reservation number from the page's physical
  128  * address.  The physical reservation number is used as the array index.
  129  *
  130  * An "active" reservation is a valid reservation structure that has a non-NULL
  131  * "object" field and a non-zero "popcnt" field.  In other words, every active
  132  * reservation belongs to a particular object.  Moreover, every active
  133  * reservation has an entry in the containing object's list of reservations.  
  134  */
  135 static vm_reserv_t vm_reserv_array;
  136 
  137 /*
  138  * The partially-populated reservation queue
  139  *
  140  * This queue enables the fast recovery of an unused cached or free small page
  141  * from a partially-populated reservation.  The reservation at the head of
  142  * this queue is the least-recently-changed, partially-populated reservation.
  143  *
  144  * Access to this queue is synchronized by the free page queue lock.
  145  */
  146 static TAILQ_HEAD(, vm_reserv) vm_rvq_partpop =
  147                             TAILQ_HEAD_INITIALIZER(vm_rvq_partpop);
  148 
  149 static SYSCTL_NODE(_vm, OID_AUTO, reserv, CTLFLAG_RD, 0, "Reservation Info");
  150 
  151 static long vm_reserv_broken;
  152 SYSCTL_LONG(_vm_reserv, OID_AUTO, broken, CTLFLAG_RD,
  153     &vm_reserv_broken, 0, "Cumulative number of broken reservations");
  154 
  155 static long vm_reserv_freed;
  156 SYSCTL_LONG(_vm_reserv, OID_AUTO, freed, CTLFLAG_RD,
  157     &vm_reserv_freed, 0, "Cumulative number of freed reservations");
  158 
  159 static int sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS);
  160 
  161 SYSCTL_OID(_vm_reserv, OID_AUTO, partpopq, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
  162     sysctl_vm_reserv_partpopq, "A", "Partially-populated reservation queues");
  163 
  164 static long vm_reserv_reclaimed;
  165 SYSCTL_LONG(_vm_reserv, OID_AUTO, reclaimed, CTLFLAG_RD,
  166     &vm_reserv_reclaimed, 0, "Cumulative number of reclaimed reservations");
  167 
  168 static void             vm_reserv_depopulate(vm_reserv_t rv);
  169 static vm_reserv_t      vm_reserv_from_page(vm_page_t m);
  170 static boolean_t        vm_reserv_has_pindex(vm_reserv_t rv,
  171                             vm_pindex_t pindex);
  172 static void             vm_reserv_populate(vm_reserv_t rv);
  173 static void             vm_reserv_reclaim(vm_reserv_t rv);
  174 
  175 /*
  176  * Describes the current state of the partially-populated reservation queue.
  177  */
  178 static int
  179 sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS)
  180 {
  181         struct sbuf sbuf;
  182         vm_reserv_t rv;
  183         int counter, error, level, unused_pages;
  184 
  185         error = sysctl_wire_old_buffer(req, 0);
  186         if (error != 0)
  187                 return (error);
  188         sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
  189         sbuf_printf(&sbuf, "\nLEVEL     SIZE  NUMBER\n\n");
  190         for (level = -1; level <= VM_NRESERVLEVEL - 2; level++) {
  191                 counter = 0;
  192                 unused_pages = 0;
  193                 mtx_lock(&vm_page_queue_free_mtx);
  194                 TAILQ_FOREACH(rv, &vm_rvq_partpop/*[level]*/, partpopq) {
  195                         counter++;
  196                         unused_pages += VM_LEVEL_0_NPAGES - rv->popcnt;
  197                 }
  198                 mtx_unlock(&vm_page_queue_free_mtx);
  199                 sbuf_printf(&sbuf, "%5d: %6dK, %6d\n", level,
  200                     unused_pages * ((int)PAGE_SIZE / 1024), counter);
  201         }
  202         error = sbuf_finish(&sbuf);
  203         sbuf_delete(&sbuf);
  204         return (error);
  205 }
  206 
  207 /*
  208  * Reduces the given reservation's population count.  If the population count
  209  * becomes zero, the reservation is destroyed.  Additionally, moves the
  210  * reservation to the tail of the partially-populated reservations queue if the
  211  * population count is non-zero.
  212  *
  213  * The free page queue lock must be held.
  214  */
  215 static void
  216 vm_reserv_depopulate(vm_reserv_t rv)
  217 {
  218 
  219         mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
  220         KASSERT(rv->object != NULL,
  221             ("vm_reserv_depopulate: reserv %p is free", rv));
  222         KASSERT(rv->popcnt > 0,
  223             ("vm_reserv_depopulate: reserv %p's popcnt is corrupted", rv));
  224         if (rv->inpartpopq) {
  225                 TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
  226                 rv->inpartpopq = FALSE;
  227         }
  228         rv->popcnt--;
  229         if (rv->popcnt == 0) {
  230                 LIST_REMOVE(rv, objq);
  231                 rv->object = NULL;
  232                 vm_phys_free_pages(rv->pages, VM_LEVEL_0_ORDER);
  233                 vm_reserv_freed++;
  234         } else {
  235                 rv->inpartpopq = TRUE;
  236                 TAILQ_INSERT_TAIL(&vm_rvq_partpop, rv, partpopq);
  237         }
  238 }
  239 
  240 /*
  241  * Returns the reservation to which the given page might belong.
  242  */
  243 static __inline vm_reserv_t
  244 vm_reserv_from_page(vm_page_t m)
  245 {
  246 
  247         return (&vm_reserv_array[VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT]);
  248 }
  249 
  250 /*
  251  * Returns TRUE if the given reservation contains the given page index and
  252  * FALSE otherwise.
  253  */
  254 static __inline boolean_t
  255 vm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex)
  256 {
  257 
  258         return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0);
  259 }
  260 
  261 /*
  262  * Increases the given reservation's population count.  Moves the reservation
  263  * to the tail of the partially-populated reservation queue.
  264  *
  265  * The free page queue must be locked.
  266  */
  267 static void
  268 vm_reserv_populate(vm_reserv_t rv)
  269 {
  270 
  271         mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
  272         KASSERT(rv->object != NULL,
  273             ("vm_reserv_populate: reserv %p is free", rv));
  274         KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES,
  275             ("vm_reserv_populate: reserv %p is already full", rv));
  276         if (rv->inpartpopq) {
  277                 TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
  278                 rv->inpartpopq = FALSE;
  279         }
  280         rv->popcnt++;
  281         if (rv->popcnt < VM_LEVEL_0_NPAGES) {
  282                 rv->inpartpopq = TRUE;
  283                 TAILQ_INSERT_TAIL(&vm_rvq_partpop, rv, partpopq);
  284         }
  285 }
  286 
  287 /*
  288  * Allocates a page from an existing or newly-created reservation.
  289  *
  290  * The object and free page queue must be locked.
  291  */
  292 vm_page_t
  293 vm_reserv_alloc_page(vm_object_t object, vm_pindex_t pindex)
  294 {
  295         vm_page_t m, mpred, msucc;
  296         vm_pindex_t first, leftcap, rightcap;
  297         vm_reserv_t rv;
  298 
  299         mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
  300 
  301         /*
  302          * Is a reservation fundamentally not possible?
  303          */
  304         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
  305         if (pindex < VM_RESERV_INDEX(object, pindex) ||
  306             pindex >= object->size)
  307                 return (NULL);
  308 
  309         /*
  310          * Look for an existing reservation.
  311          */
  312         msucc = NULL;
  313         mpred = object->root;
  314         while (mpred != NULL) {
  315                 KASSERT(mpred->pindex != pindex,
  316                     ("vm_reserv_alloc_page: pindex already allocated"));
  317                 rv = vm_reserv_from_page(mpred);
  318                 if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) {
  319                         m = &rv->pages[VM_RESERV_INDEX(object, pindex)];
  320                         /* Handle vm_page_rename(m, new_object, ...). */
  321                         if ((m->flags & (PG_CACHED | PG_FREE)) == 0)
  322                                 return (NULL);
  323                         vm_reserv_populate(rv);
  324                         return (m);
  325                 } else if (mpred->pindex < pindex) {
  326                         if (msucc != NULL ||
  327                             (msucc = TAILQ_NEXT(mpred, listq)) == NULL)
  328                                 break;
  329                         KASSERT(msucc->pindex != pindex,
  330                             ("vm_reserv_alloc_page: pindex already allocated"));
  331                         rv = vm_reserv_from_page(msucc);
  332                         if (rv->object == object &&
  333                             vm_reserv_has_pindex(rv, pindex)) {
  334                                 m = &rv->pages[VM_RESERV_INDEX(object, pindex)];
  335                                 /* Handle vm_page_rename(m, new_object, ...). */
  336                                 if ((m->flags & (PG_CACHED | PG_FREE)) == 0)
  337                                         return (NULL);
  338                                 vm_reserv_populate(rv);
  339                                 return (m);
  340                         } else if (pindex < msucc->pindex)
  341                                 break;
  342                 } else if (msucc == NULL) {
  343                         msucc = mpred;
  344                         mpred = TAILQ_PREV(msucc, pglist, listq);
  345                         continue;
  346                 }
  347                 msucc = NULL;
  348                 mpred = object->root = vm_page_splay(pindex, object->root);
  349         }
  350 
  351         /*
  352          * Determine the first index to the left that can be used.
  353          */
  354         if (mpred == NULL)
  355                 leftcap = 0;
  356         else if ((rv = vm_reserv_from_page(mpred))->object != object)
  357                 leftcap = mpred->pindex + 1;
  358         else
  359                 leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
  360 
  361         /*
  362          * Determine the first index to the right that cannot be used.
  363          */
  364         if (msucc == NULL)
  365                 rightcap = pindex + VM_LEVEL_0_NPAGES;
  366         else if ((rv = vm_reserv_from_page(msucc))->object != object)
  367                 rightcap = msucc->pindex;
  368         else
  369                 rightcap = rv->pindex;
  370 
  371         /*
  372          * Determine if a reservation fits between the first index to
  373          * the left that can be used and the first index to the right
  374          * that cannot be used. 
  375          */
  376         first = pindex - VM_RESERV_INDEX(object, pindex);
  377         if (first < leftcap || first + VM_LEVEL_0_NPAGES > rightcap)
  378                 return (NULL);
  379 
  380         /*
  381          * Would a new reservation extend past the end of the given object? 
  382          */
  383         if (object->size < first + VM_LEVEL_0_NPAGES) {
  384                 /*
  385                  * Don't allocate a new reservation if the object is a vnode or
  386                  * backed by another object that is a vnode. 
  387                  */
  388                 if (object->type == OBJT_VNODE ||
  389                     (object->backing_object != NULL &&
  390                     object->backing_object->type == OBJT_VNODE))
  391                         return (NULL);
  392                 /* Speculate that the object may grow. */
  393         }
  394 
  395         /*
  396          * Allocate a new reservation.
  397          */
  398         m = vm_phys_alloc_pages(VM_FREEPOOL_DEFAULT, VM_LEVEL_0_ORDER);
  399         if (m != NULL) {
  400                 rv = vm_reserv_from_page(m);
  401                 KASSERT(rv->pages == m,
  402                     ("vm_reserv_alloc_page: reserv %p's pages is corrupted",
  403                     rv));
  404                 KASSERT(rv->object == NULL,
  405                     ("vm_reserv_alloc_page: reserv %p isn't free", rv));
  406                 LIST_INSERT_HEAD(&object->rvq, rv, objq);
  407                 rv->object = object;
  408                 rv->pindex = first;
  409                 KASSERT(rv->popcnt == 0,
  410                     ("vm_reserv_alloc_page: reserv %p's popcnt is corrupted",
  411                     rv));
  412                 KASSERT(!rv->inpartpopq,
  413                     ("vm_reserv_alloc_page: reserv %p's inpartpopq is TRUE",
  414                     rv));
  415                 vm_reserv_populate(rv);
  416                 m = &rv->pages[VM_RESERV_INDEX(object, pindex)];
  417         }
  418         return (m);
  419 }
  420 
  421 /*
  422  * Breaks all reservations belonging to the given object.
  423  */
  424 void
  425 vm_reserv_break_all(vm_object_t object)
  426 {
  427         vm_reserv_t rv;
  428         int i;
  429 
  430         mtx_lock(&vm_page_queue_free_mtx);
  431         while ((rv = LIST_FIRST(&object->rvq)) != NULL) {
  432                 KASSERT(rv->object == object,
  433                     ("vm_reserv_break_all: reserv %p is corrupted", rv));
  434                 if (rv->inpartpopq) {
  435                         TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
  436                         rv->inpartpopq = FALSE;
  437                 }
  438                 LIST_REMOVE(rv, objq);
  439                 rv->object = NULL;
  440                 for (i = 0; i < VM_LEVEL_0_NPAGES; i++) {
  441                         if ((rv->pages[i].flags & (PG_CACHED | PG_FREE)) != 0)
  442                                 vm_phys_free_pages(&rv->pages[i], 0);
  443                         else
  444                                 rv->popcnt--;
  445                 }
  446                 KASSERT(rv->popcnt == 0,
  447                     ("vm_reserv_break_all: reserv %p's popcnt is corrupted",
  448                     rv));
  449                 vm_reserv_broken++;
  450         }
  451         mtx_unlock(&vm_page_queue_free_mtx);
  452 }
  453 
  454 /*
  455  * Frees the given page if it belongs to a reservation.  Returns TRUE if the
  456  * page is freed and FALSE otherwise.
  457  *
  458  * The free page queue lock must be held.
  459  */
  460 boolean_t
  461 vm_reserv_free_page(vm_page_t m)
  462 {
  463         vm_reserv_t rv;
  464 
  465         mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
  466         rv = vm_reserv_from_page(m);
  467         if (rv->object == NULL)
  468                 return (FALSE);
  469         if ((m->flags & PG_CACHED) != 0 && m->pool != VM_FREEPOOL_CACHE)
  470                 vm_phys_set_pool(VM_FREEPOOL_CACHE, rv->pages,
  471                     VM_LEVEL_0_ORDER);
  472         vm_reserv_depopulate(rv);
  473         return (TRUE);
  474 }
  475 
  476 /*
  477  * Initializes the reservation management system.  Specifically, initializes
  478  * the reservation array.
  479  *
  480  * Requires that vm_page_array and first_page are initialized!
  481  */
  482 void
  483 vm_reserv_init(void)
  484 {
  485         vm_paddr_t paddr;
  486         int i;
  487 
  488         /*
  489          * Initialize the reservation array.  Specifically, initialize the
  490          * "pages" field for every element that has an underlying superpage.
  491          */
  492         for (i = 0; phys_avail[i + 1] != 0; i += 2) {
  493                 paddr = roundup2(phys_avail[i], VM_LEVEL_0_SIZE);
  494                 while (paddr + VM_LEVEL_0_SIZE <= phys_avail[i + 1]) {
  495                         vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT].pages =
  496                             PHYS_TO_VM_PAGE(paddr);
  497                         paddr += VM_LEVEL_0_SIZE;
  498                 }
  499         }
  500 }
  501 
  502 /*
  503  * Returns a reservation level if the given page belongs to a fully-populated
  504  * reservation and -1 otherwise.
  505  */
  506 int
  507 vm_reserv_level_iffullpop(vm_page_t m)
  508 {
  509         vm_reserv_t rv;
  510 
  511         rv = vm_reserv_from_page(m);
  512         return (rv->popcnt == VM_LEVEL_0_NPAGES ? 0 : -1);
  513 }
  514 
  515 /*
  516  * Prepare for the reactivation of a cached page.
  517  *
  518  * First, suppose that the given page "m" was allocated individually, i.e., not
  519  * as part of a reservation, and cached.  Then, suppose a reservation
  520  * containing "m" is allocated by the same object.  Although "m" and the
  521  * reservation belong to the same object, "m"'s pindex may not match the
  522  * reservation's.
  523  *
  524  * The free page queue must be locked.
  525  */
  526 boolean_t
  527 vm_reserv_reactivate_page(vm_page_t m)
  528 {
  529         vm_reserv_t rv;
  530         int i, m_index;
  531 
  532         mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
  533         rv = vm_reserv_from_page(m);
  534         if (rv->object == NULL)
  535                 return (FALSE);
  536         KASSERT((m->flags & PG_CACHED) != 0,
  537             ("vm_reserv_uncache_page: page %p is not cached", m));
  538         if (m->object == rv->object &&
  539             m->pindex - rv->pindex == VM_RESERV_INDEX(m->object, m->pindex))
  540                 vm_reserv_populate(rv);
  541         else {
  542                 KASSERT(rv->inpartpopq,
  543                     ("vm_reserv_uncache_page: reserv %p's inpartpopq is FALSE",
  544                     rv));
  545                 TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
  546                 rv->inpartpopq = FALSE;
  547                 LIST_REMOVE(rv, objq);
  548                 rv->object = NULL;
  549                 /* Don't vm_phys_free_pages(m, 0). */
  550                 m_index = m - rv->pages;
  551                 for (i = 0; i < m_index; i++) {
  552                         if ((rv->pages[i].flags & (PG_CACHED | PG_FREE)) != 0)
  553                                 vm_phys_free_pages(&rv->pages[i], 0);
  554                         else
  555                                 rv->popcnt--;
  556                 }
  557                 for (i++; i < VM_LEVEL_0_NPAGES; i++) {
  558                         if ((rv->pages[i].flags & (PG_CACHED | PG_FREE)) != 0)
  559                                 vm_phys_free_pages(&rv->pages[i], 0);
  560                         else
  561                                 rv->popcnt--;
  562                 }
  563                 KASSERT(rv->popcnt == 0,
  564                     ("vm_reserv_uncache_page: reserv %p's popcnt is corrupted",
  565                     rv));
  566                 vm_reserv_broken++;
  567         }
  568         return (TRUE);
  569 }
  570 
  571 /*
  572  * Breaks the given partially-populated reservation, releasing its cached and
  573  * free pages to the physical memory allocator.
  574  *
  575  * The free page queue lock must be held.
  576  */
  577 static void
  578 vm_reserv_reclaim(vm_reserv_t rv)
  579 {
  580         int i;
  581 
  582         mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
  583         KASSERT(rv->inpartpopq,
  584             ("vm_reserv_reclaim: reserv %p's inpartpopq is corrupted", rv));
  585         TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
  586         rv->inpartpopq = FALSE;
  587         KASSERT(rv->object != NULL,
  588             ("vm_reserv_reclaim: reserv %p is free", rv));
  589         LIST_REMOVE(rv, objq);
  590         rv->object = NULL;
  591         for (i = 0; i < VM_LEVEL_0_NPAGES; i++) {
  592                 if ((rv->pages[i].flags & (PG_CACHED | PG_FREE)) != 0)
  593                         vm_phys_free_pages(&rv->pages[i], 0);
  594                 else
  595                         rv->popcnt--;
  596         }
  597         KASSERT(rv->popcnt == 0,
  598             ("vm_reserv_reclaim: reserv %p's popcnt is corrupted", rv));
  599         vm_reserv_reclaimed++;
  600 }
  601 
  602 /*
  603  * Breaks the reservation at the head of the partially-populated reservation
  604  * queue, releasing its cached and free pages to the physical memory
  605  * allocator.  Returns TRUE if a reservation is broken and FALSE otherwise.
  606  *
  607  * The free page queue lock must be held.
  608  */
  609 boolean_t
  610 vm_reserv_reclaim_inactive(void)
  611 {
  612         vm_reserv_t rv;
  613 
  614         mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
  615         if ((rv = TAILQ_FIRST(&vm_rvq_partpop)) != NULL) {
  616                 vm_reserv_reclaim(rv);
  617                 return (TRUE);
  618         }
  619         return (FALSE);
  620 }
  621 
  622 /*
  623  * Searches the partially-populated reservation queue for the least recently
  624  * active reservation with unused pages, i.e., cached or free, that satisfy the
  625  * given request for contiguous physical memory.  If a satisfactory reservation
  626  * is found, it is broken.  Returns TRUE if a reservation is broken and FALSE
  627  * otherwise.
  628  *
  629  * The free page queue lock must be held.
  630  */
  631 boolean_t
  632 vm_reserv_reclaim_contig(vm_paddr_t size, vm_paddr_t low, vm_paddr_t high,
  633     unsigned long alignment, unsigned long boundary)
  634 {
  635         vm_paddr_t pa, pa_length;
  636         vm_reserv_t rv;
  637         int i;
  638 
  639         mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
  640         if (size > VM_LEVEL_0_SIZE - PAGE_SIZE)
  641                 return (FALSE);
  642         TAILQ_FOREACH(rv, &vm_rvq_partpop, partpopq) {
  643                 pa = VM_PAGE_TO_PHYS(&rv->pages[VM_LEVEL_0_NPAGES - 1]);
  644                 if (pa + PAGE_SIZE - size < low) {
  645                         /* this entire reservation is too low; go to next */
  646                         continue;
  647                 }
  648                 pa_length = 0;
  649                 for (i = 0; i < VM_LEVEL_0_NPAGES; i++)
  650                         if ((rv->pages[i].flags & (PG_CACHED | PG_FREE)) != 0) {
  651                                 pa_length += PAGE_SIZE;
  652                                 if (pa_length == PAGE_SIZE) {
  653                                         pa = VM_PAGE_TO_PHYS(&rv->pages[i]);
  654                                         if (pa + size > high) {
  655                                                 /* skip to next reservation */
  656                                                 break;
  657                                         } else if (pa < low ||
  658                                             (pa & (alignment - 1)) != 0 ||
  659                                             ((pa ^ (pa + size - 1)) &
  660                                             ~(boundary - 1)) != 0)
  661                                                 pa_length = 0;
  662                                 }
  663                                 if (pa_length >= size) {
  664                                         vm_reserv_reclaim(rv);
  665                                         return (TRUE);
  666                                 }
  667                         } else
  668                                 pa_length = 0;
  669         }
  670         return (FALSE);
  671 }
  672 
  673 /*
  674  * Transfers the reservation underlying the given page to a new object.
  675  *
  676  * The object must be locked.
  677  */
  678 void
  679 vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object,
  680     vm_pindex_t old_object_offset)
  681 {
  682         vm_reserv_t rv;
  683 
  684         VM_OBJECT_LOCK_ASSERT(new_object, MA_OWNED);
  685         rv = vm_reserv_from_page(m);
  686         if (rv->object == old_object) {
  687                 mtx_lock(&vm_page_queue_free_mtx);
  688                 if (rv->object == old_object) {
  689                         LIST_REMOVE(rv, objq);
  690                         LIST_INSERT_HEAD(&new_object->rvq, rv, objq);
  691                         rv->object = new_object;
  692                         rv->pindex -= old_object_offset;
  693                 }
  694                 mtx_unlock(&vm_page_queue_free_mtx);
  695         }
  696 }
  697 
  698 /*
  699  * Allocates the virtual and physical memory required by the reservation
  700  * management system's data structures, in particular, the reservation array.
  701  */
  702 vm_paddr_t
  703 vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t high_water)
  704 {
  705         vm_paddr_t new_end;
  706         size_t size;
  707 
  708         /*
  709          * Calculate the size (in bytes) of the reservation array.  Round up
  710          * from "high_water" because every small page is mapped to an element
  711          * in the reservation array based on its physical address.  Thus, the
  712          * number of elements in the reservation array can be greater than the
  713          * number of superpages. 
  714          */
  715         size = howmany(high_water, VM_LEVEL_0_SIZE) * sizeof(struct vm_reserv);
  716 
  717         /*
  718          * Allocate and map the physical memory for the reservation array.  The
  719          * next available virtual address is returned by reference.
  720          */
  721         new_end = end - round_page(size);
  722         vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end,
  723             VM_PROT_READ | VM_PROT_WRITE);
  724         bzero(vm_reserv_array, size);
  725 
  726         /*
  727          * Return the next available physical address.
  728          */
  729         return (new_end);
  730 }
  731 
  732 #endif  /* VM_NRESERVLEVEL > 0 */

Cache object: 0eeed172d1a02931bad50d230b15fba5


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