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

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

    1 /*
    2  * Copyright (c) 1991 Regents of the University of California.
    3  * All rights reserved.
    4  *
    5  * This code is derived from software contributed to Berkeley by
    6  * The Mach Operating System project at Carnegie-Mellon University.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. All advertising materials mentioning features or use of this software
   17  *    must display the following acknowledgement:
   18  *      This product includes software developed by the University of
   19  *      California, Berkeley and its contributors.
   20  * 4. Neither the name of the University nor the names of its contributors
   21  *    may be used to endorse or promote products derived from this software
   22  *    without specific prior written permission.
   23  *
   24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   34  * SUCH DAMAGE.
   35  *
   36  *      from: @(#)vm_page.c     7.4 (Berkeley) 5/7/91
   37  * $FreeBSD: releng/5.0/sys/vm/vm_page.c 107189 2002-11-23 19:10:31Z alc $
   38  */
   39 
   40 /*
   41  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
   42  * All rights reserved.
   43  *
   44  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
   45  *
   46  * Permission to use, copy, modify and distribute this software and
   47  * its documentation is hereby granted, provided that both the copyright
   48  * notice and this permission notice appear in all copies of the
   49  * software, derivative works or modified versions, and any portions
   50  * thereof, and that both notices appear in supporting documentation.
   51  *
   52  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
   53  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
   54  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
   55  *
   56  * Carnegie Mellon requests users of this software to return to
   57  *
   58  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
   59  *  School of Computer Science
   60  *  Carnegie Mellon University
   61  *  Pittsburgh PA 15213-3890
   62  *
   63  * any improvements or extensions that they make and grant Carnegie the
   64  * rights to redistribute these changes.
   65  */
   66 
   67 /*
   68  *                      GENERAL RULES ON VM_PAGE MANIPULATION
   69  *
   70  *      - a pageq mutex is required when adding or removing a page from a
   71  *        page queue (vm_page_queue[]), regardless of other mutexes or the
   72  *        busy state of a page.
   73  *
   74  *      - a hash chain mutex is required when associating or disassociating
   75  *        a page from the VM PAGE CACHE hash table (vm_page_buckets),
   76  *        regardless of other mutexes or the busy state of a page.
   77  *
   78  *      - either a hash chain mutex OR a busied page is required in order
   79  *        to modify the page flags.  A hash chain mutex must be obtained in
   80  *        order to busy a page.  A page's flags cannot be modified by a
   81  *        hash chain mutex if the page is marked busy.
   82  *
   83  *      - The object memq mutex is held when inserting or removing
   84  *        pages from an object (vm_page_insert() or vm_page_remove()).  This
   85  *        is different from the object's main mutex.
   86  *
   87  *      Generally speaking, you have to be aware of side effects when running
   88  *      vm_page ops.  A vm_page_lookup() will return with the hash chain
   89  *      locked, whether it was able to lookup the page or not.  vm_page_free(),
   90  *      vm_page_cache(), vm_page_activate(), and a number of other routines
   91  *      will release the hash chain mutex for you.  Intermediate manipulation
   92  *      routines such as vm_page_flag_set() expect the hash chain to be held
   93  *      on entry and the hash chain will remain held on return.
   94  *
   95  *      pageq scanning can only occur with the pageq in question locked.
   96  *      We have a known bottleneck with the active queue, but the cache
   97  *      and free queues are actually arrays already. 
   98  */
   99 
  100 /*
  101  *      Resident memory management module.
  102  */
  103 
  104 #include <sys/param.h>
  105 #include <sys/systm.h>
  106 #include <sys/lock.h>
  107 #include <sys/malloc.h>
  108 #include <sys/mutex.h>
  109 #include <sys/proc.h>
  110 #include <sys/vmmeter.h>
  111 #include <sys/vnode.h>
  112 
  113 #include <vm/vm.h>
  114 #include <vm/vm_param.h>
  115 #include <vm/vm_kern.h>
  116 #include <vm/vm_object.h>
  117 #include <vm/vm_page.h>
  118 #include <vm/vm_pageout.h>
  119 #include <vm/vm_pager.h>
  120 #include <vm/vm_extern.h>
  121 #include <vm/uma.h>
  122 #include <vm/uma_int.h>
  123 
  124 /*
  125  *      Associated with page of user-allocatable memory is a
  126  *      page structure.
  127  */
  128 
  129 struct mtx vm_page_queue_mtx;
  130 struct mtx vm_page_queue_free_mtx;
  131 
  132 vm_page_t vm_page_array = 0;
  133 int vm_page_array_size = 0;
  134 long first_page = 0;
  135 int vm_page_zero_count = 0;
  136 
  137 /*
  138  *      vm_set_page_size:
  139  *
  140  *      Sets the page size, perhaps based upon the memory
  141  *      size.  Must be called before any use of page-size
  142  *      dependent functions.
  143  */
  144 void
  145 vm_set_page_size(void)
  146 {
  147         if (cnt.v_page_size == 0)
  148                 cnt.v_page_size = PAGE_SIZE;
  149         if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0)
  150                 panic("vm_set_page_size: page size not a power of two");
  151 }
  152 
  153 /*
  154  *      vm_page_startup:
  155  *
  156  *      Initializes the resident memory module.
  157  *
  158  *      Allocates memory for the page cells, and
  159  *      for the object/offset-to-page hash table headers.
  160  *      Each page cell is initialized and placed on the free list.
  161  */
  162 vm_offset_t
  163 vm_page_startup(vm_offset_t starta, vm_offset_t enda, vm_offset_t vaddr)
  164 {
  165         vm_offset_t mapped;
  166         vm_size_t npages, page_range;
  167         vm_offset_t new_end;
  168         int i;
  169         vm_offset_t pa;
  170         int nblocks;
  171         vm_offset_t last_pa;
  172 
  173         /* the biggest memory array is the second group of pages */
  174         vm_offset_t end;
  175         vm_offset_t biggestone, biggestsize;
  176 
  177         vm_offset_t total;
  178         vm_size_t bootpages;
  179 
  180         total = 0;
  181         biggestsize = 0;
  182         biggestone = 0;
  183         nblocks = 0;
  184         vaddr = round_page(vaddr);
  185 
  186         for (i = 0; phys_avail[i + 1]; i += 2) {
  187                 phys_avail[i] = round_page(phys_avail[i]);
  188                 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
  189         }
  190 
  191         for (i = 0; phys_avail[i + 1]; i += 2) {
  192                 vm_size_t size = phys_avail[i + 1] - phys_avail[i];
  193 
  194                 if (size > biggestsize) {
  195                         biggestone = i;
  196                         biggestsize = size;
  197                 }
  198                 ++nblocks;
  199                 total += size;
  200         }
  201 
  202         end = phys_avail[biggestone+1];
  203 
  204         /*
  205          * Initialize the locks.
  206          */
  207         mtx_init(&vm_page_queue_mtx, "vm page queue mutex", NULL, MTX_DEF);
  208         mtx_init(&vm_page_queue_free_mtx, "vm page queue free mutex", NULL,
  209            MTX_SPIN);
  210 
  211         /*
  212          * Initialize the queue headers for the free queue, the active queue
  213          * and the inactive queue.
  214          */
  215         vm_pageq_init();
  216 
  217         /*
  218          * Allocate memory for use when boot strapping the kernel memory
  219          * allocator.
  220          */
  221         bootpages = UMA_BOOT_PAGES * UMA_SLAB_SIZE;
  222         new_end = end - bootpages;
  223         new_end = trunc_page(new_end);
  224         mapped = pmap_map(&vaddr, new_end, end,
  225             VM_PROT_READ | VM_PROT_WRITE);
  226         bzero((caddr_t) mapped, end - new_end);
  227         uma_startup((caddr_t)mapped);
  228 
  229         /*
  230          * Compute the number of pages of memory that will be available for
  231          * use (taking into account the overhead of a page structure per
  232          * page).
  233          */
  234         first_page = phys_avail[0] / PAGE_SIZE;
  235         page_range = phys_avail[(nblocks - 1) * 2 + 1] / PAGE_SIZE - first_page;
  236         npages = (total - (page_range * sizeof(struct vm_page)) -
  237             (end - new_end)) / PAGE_SIZE;
  238         end = new_end;
  239 
  240         /*
  241          * Initialize the mem entry structures now, and put them in the free
  242          * queue.
  243          */
  244         new_end = trunc_page(end - page_range * sizeof(struct vm_page));
  245         mapped = pmap_map(&vaddr, new_end, end,
  246             VM_PROT_READ | VM_PROT_WRITE);
  247         vm_page_array = (vm_page_t) mapped;
  248 
  249         /*
  250          * Clear all of the page structures
  251          */
  252         bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
  253         vm_page_array_size = page_range;
  254 
  255         /*
  256          * Construct the free queue(s) in descending order (by physical
  257          * address) so that the first 16MB of physical memory is allocated
  258          * last rather than first.  On large-memory machines, this avoids
  259          * the exhaustion of low physical memory before isa_dmainit has run.
  260          */
  261         cnt.v_page_count = 0;
  262         cnt.v_free_count = 0;
  263         for (i = 0; phys_avail[i + 1] && npages > 0; i += 2) {
  264                 pa = phys_avail[i];
  265                 if (i == biggestone)
  266                         last_pa = new_end;
  267                 else
  268                         last_pa = phys_avail[i + 1];
  269                 while (pa < last_pa && npages-- > 0) {
  270                         vm_pageq_add_new_page(pa);
  271                         pa += PAGE_SIZE;
  272                 }
  273         }
  274         return (vaddr);
  275 }
  276 
  277 void
  278 vm_page_flag_set(vm_page_t m, unsigned short bits)
  279 {
  280         GIANT_REQUIRED;
  281         m->flags |= bits;
  282 } 
  283 
  284 void
  285 vm_page_flag_clear(vm_page_t m, unsigned short bits)
  286 {
  287         GIANT_REQUIRED;
  288         m->flags &= ~bits;
  289 }
  290 
  291 void
  292 vm_page_busy(vm_page_t m)
  293 {
  294         KASSERT((m->flags & PG_BUSY) == 0,
  295             ("vm_page_busy: page already busy!!!"));
  296         vm_page_flag_set(m, PG_BUSY);
  297 }
  298 
  299 /*
  300  *      vm_page_flash:
  301  *
  302  *      wakeup anyone waiting for the page.
  303  */
  304 void
  305 vm_page_flash(vm_page_t m)
  306 {
  307         if (m->flags & PG_WANTED) {
  308                 vm_page_flag_clear(m, PG_WANTED);
  309                 wakeup(m);
  310         }
  311 }
  312 
  313 /*
  314  *      vm_page_wakeup:
  315  *
  316  *      clear the PG_BUSY flag and wakeup anyone waiting for the
  317  *      page.
  318  *
  319  */
  320 void
  321 vm_page_wakeup(vm_page_t m)
  322 {
  323         KASSERT(m->flags & PG_BUSY, ("vm_page_wakeup: page not busy!!!"));
  324         vm_page_flag_clear(m, PG_BUSY);
  325         vm_page_flash(m);
  326 }
  327 
  328 /*
  329  *
  330  *
  331  */
  332 void
  333 vm_page_io_start(vm_page_t m)
  334 {
  335 
  336         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
  337         m->busy++;
  338 }
  339 
  340 void
  341 vm_page_io_finish(vm_page_t m)
  342 {
  343 
  344         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
  345         m->busy--;
  346         if (m->busy == 0)
  347                 vm_page_flash(m);
  348 }
  349 
  350 /*
  351  * Keep page from being freed by the page daemon
  352  * much of the same effect as wiring, except much lower
  353  * overhead and should be used only for *very* temporary
  354  * holding ("wiring").
  355  */
  356 void
  357 vm_page_hold(vm_page_t mem)
  358 {
  359         GIANT_REQUIRED;
  360         mem->hold_count++;
  361 }
  362 
  363 void
  364 vm_page_unhold(vm_page_t mem)
  365 {
  366         GIANT_REQUIRED;
  367         --mem->hold_count;
  368         KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!"));
  369         if (mem->hold_count == 0 && mem->queue == PQ_HOLD)
  370                 vm_page_free_toq(mem);
  371 }
  372 
  373 /*
  374  *      vm_page_copy:
  375  *
  376  *      Copy one page to another
  377  */
  378 void
  379 vm_page_copy(vm_page_t src_m, vm_page_t dest_m)
  380 {
  381         pmap_copy_page(src_m, dest_m);
  382         dest_m->valid = VM_PAGE_BITS_ALL;
  383 }
  384 
  385 /*
  386  *      vm_page_free:
  387  *
  388  *      Free a page
  389  *
  390  *      The clearing of PG_ZERO is a temporary safety until the code can be
  391  *      reviewed to determine that PG_ZERO is being properly cleared on
  392  *      write faults or maps.  PG_ZERO was previously cleared in
  393  *      vm_page_alloc().
  394  */
  395 void
  396 vm_page_free(vm_page_t m)
  397 {
  398         vm_page_flag_clear(m, PG_ZERO);
  399         vm_page_free_toq(m);
  400         vm_page_zero_idle_wakeup();
  401 }
  402 
  403 /*
  404  *      vm_page_free_zero:
  405  *
  406  *      Free a page to the zerod-pages queue
  407  */
  408 void
  409 vm_page_free_zero(vm_page_t m)
  410 {
  411         vm_page_flag_set(m, PG_ZERO);
  412         vm_page_free_toq(m);
  413 }
  414 
  415 /*
  416  *      vm_page_sleep_busy:
  417  *
  418  *      Wait until page is no longer PG_BUSY or (if also_m_busy is TRUE)
  419  *      m->busy is zero.  Returns TRUE if it had to sleep ( including if
  420  *      it almost had to sleep and made temporary spl*() mods), FALSE
  421  *      otherwise.
  422  *
  423  *      This routine assumes that interrupts can only remove the busy
  424  *      status from a page, not set the busy status or change it from
  425  *      PG_BUSY to m->busy or vise versa (which would create a timing
  426  *      window).
  427  */
  428 int
  429 vm_page_sleep_busy(vm_page_t m, int also_m_busy, const char *msg)
  430 {
  431         GIANT_REQUIRED;
  432         if ((m->flags & PG_BUSY) || (also_m_busy && m->busy))  {
  433                 int s = splvm();
  434                 if ((m->flags & PG_BUSY) || (also_m_busy && m->busy)) {
  435                         /*
  436                          * Page is busy. Wait and retry.
  437                          */
  438                         vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
  439                         tsleep(m, PVM, msg, 0);
  440                 }
  441                 splx(s);
  442                 return (TRUE);
  443                 /* not reached */
  444         }
  445         return (FALSE);
  446 }
  447 
  448 /*
  449  *      vm_page_sleep_if_busy:
  450  *
  451  *      Sleep and release the page queues lock if PG_BUSY is set or,
  452  *      if also_m_busy is TRUE, busy is non-zero.  Returns TRUE if the
  453  *      thread slept and the page queues lock was released.
  454  *      Otherwise, retains the page queues lock and returns FALSE.
  455  */
  456 int
  457 vm_page_sleep_if_busy(vm_page_t m, int also_m_busy, const char *msg)
  458 {
  459 
  460         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
  461         if ((m->flags & PG_BUSY) || (also_m_busy && m->busy)) {
  462                 vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
  463                 msleep(m, &vm_page_queue_mtx, PDROP | PVM, msg, 0);
  464                 return (TRUE);
  465         }
  466         return (FALSE);
  467 }
  468 
  469 /*
  470  *      vm_page_dirty:
  471  *
  472  *      make page all dirty
  473  */
  474 void
  475 vm_page_dirty(vm_page_t m)
  476 {
  477         KASSERT(m->queue - m->pc != PQ_CACHE,
  478             ("vm_page_dirty: page in cache!"));
  479         m->dirty = VM_PAGE_BITS_ALL;
  480 }
  481 
  482 /*
  483  *      vm_page_splay:
  484  *
  485  *      Implements Sleator and Tarjan's top-down splay algorithm.  Returns
  486  *      the vm_page containing the given pindex.  If, however, that
  487  *      pindex is not found in the vm_object, returns a vm_page that is
  488  *      adjacent to the pindex, coming before or after it.
  489  */
  490 vm_page_t
  491 vm_page_splay(vm_pindex_t pindex, vm_page_t root)
  492 {
  493         struct vm_page dummy;
  494         vm_page_t lefttreemax, righttreemin, y;
  495 
  496         if (root == NULL)
  497                 return (root);
  498         lefttreemax = righttreemin = &dummy;
  499         for (;; root = y) {
  500                 if (pindex < root->pindex) {
  501                         if ((y = root->left) == NULL)
  502                                 break;
  503                         if (pindex < y->pindex) {
  504                                 /* Rotate right. */
  505                                 root->left = y->right;
  506                                 y->right = root;
  507                                 root = y;
  508                                 if ((y = root->left) == NULL)
  509                                         break;
  510                         }
  511                         /* Link into the new root's right tree. */
  512                         righttreemin->left = root;
  513                         righttreemin = root;
  514                 } else if (pindex > root->pindex) {
  515                         if ((y = root->right) == NULL)
  516                                 break;
  517                         if (pindex > y->pindex) {
  518                                 /* Rotate left. */
  519                                 root->right = y->left;
  520                                 y->left = root;
  521                                 root = y;
  522                                 if ((y = root->right) == NULL)
  523                                         break;
  524                         }
  525                         /* Link into the new root's left tree. */
  526                         lefttreemax->right = root;
  527                         lefttreemax = root;
  528                 } else
  529                         break;
  530         }
  531         /* Assemble the new root. */
  532         lefttreemax->right = root->left;
  533         righttreemin->left = root->right;
  534         root->left = dummy.right;
  535         root->right = dummy.left;
  536         return (root);
  537 }
  538 
  539 /*
  540  *      vm_page_insert:         [ internal use only ]
  541  *
  542  *      Inserts the given mem entry into the object and object list.
  543  *
  544  *      The pagetables are not updated but will presumably fault the page
  545  *      in if necessary, or if a kernel page the caller will at some point
  546  *      enter the page into the kernel's pmap.  We are not allowed to block
  547  *      here so we *can't* do this anyway.
  548  *
  549  *      The object and page must be locked, and must be splhigh.
  550  *      This routine may not block.
  551  */
  552 void
  553 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
  554 {
  555         vm_page_t root;
  556 
  557         GIANT_REQUIRED;
  558 
  559         if (m->object != NULL)
  560                 panic("vm_page_insert: already inserted");
  561 
  562         /*
  563          * Record the object/offset pair in this page
  564          */
  565         m->object = object;
  566         m->pindex = pindex;
  567 
  568         /*
  569          * Now link into the object's ordered list of backed pages.
  570          */
  571         root = object->root;
  572         if (root == NULL) {
  573                 m->left = NULL;
  574                 m->right = NULL;
  575                 TAILQ_INSERT_TAIL(&object->memq, m, listq);
  576         } else {
  577                 root = vm_page_splay(pindex, root);
  578                 if (pindex < root->pindex) {
  579                         m->left = root->left;
  580                         m->right = root;
  581                         root->left = NULL;
  582                         TAILQ_INSERT_BEFORE(root, m, listq);
  583                 } else {
  584                         m->right = root->right;
  585                         m->left = root;
  586                         root->right = NULL;
  587                         TAILQ_INSERT_AFTER(&object->memq, root, m, listq);
  588                 }
  589         }
  590         object->root = m;
  591         object->generation++;
  592 
  593         /*
  594          * show that the object has one more resident page.
  595          */
  596         object->resident_page_count++;
  597 
  598         /*
  599          * Since we are inserting a new and possibly dirty page,
  600          * update the object's OBJ_WRITEABLE and OBJ_MIGHTBEDIRTY flags.
  601          */
  602         if (m->flags & PG_WRITEABLE)
  603                 vm_object_set_writeable_dirty(object);
  604 }
  605 
  606 /*
  607  *      vm_page_remove:
  608  *                              NOTE: used by device pager as well -wfj
  609  *
  610  *      Removes the given mem entry from the object/offset-page
  611  *      table and the object page list, but do not invalidate/terminate
  612  *      the backing store.
  613  *
  614  *      The object and page must be locked, and at splhigh.
  615  *      The underlying pmap entry (if any) is NOT removed here.
  616  *      This routine may not block.
  617  */
  618 void
  619 vm_page_remove(vm_page_t m)
  620 {
  621         vm_object_t object;
  622         vm_page_t root;
  623 
  624         GIANT_REQUIRED;
  625 
  626         if (m->object == NULL)
  627                 return;
  628 
  629         if ((m->flags & PG_BUSY) == 0) {
  630                 panic("vm_page_remove: page not busy");
  631         }
  632 
  633         /*
  634          * Basically destroy the page.
  635          */
  636         vm_page_wakeup(m);
  637 
  638         object = m->object;
  639 
  640         /*
  641          * Now remove from the object's list of backed pages.
  642          */
  643         if (m != object->root)
  644                 vm_page_splay(m->pindex, object->root);
  645         if (m->left == NULL)
  646                 root = m->right;
  647         else {
  648                 root = vm_page_splay(m->pindex, m->left);
  649                 root->right = m->right;
  650         }
  651         object->root = root;
  652         TAILQ_REMOVE(&object->memq, m, listq);
  653 
  654         /*
  655          * And show that the object has one fewer resident page.
  656          */
  657         object->resident_page_count--;
  658         object->generation++;
  659 
  660         m->object = NULL;
  661 }
  662 
  663 /*
  664  *      vm_page_lookup:
  665  *
  666  *      Returns the page associated with the object/offset
  667  *      pair specified; if none is found, NULL is returned.
  668  *
  669  *      The object must be locked.
  670  *      This routine may not block.
  671  *      This is a critical path routine
  672  */
  673 vm_page_t
  674 vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
  675 {
  676         vm_page_t m;
  677 
  678         GIANT_REQUIRED;
  679 
  680         m = vm_page_splay(pindex, object->root);
  681         if ((object->root = m) != NULL && m->pindex != pindex)
  682                 m = NULL;
  683         return (m);
  684 }
  685 
  686 /*
  687  *      vm_page_rename:
  688  *
  689  *      Move the given memory entry from its
  690  *      current object to the specified target object/offset.
  691  *
  692  *      The object must be locked.
  693  *      This routine may not block.
  694  *
  695  *      Note: this routine will raise itself to splvm(), the caller need not. 
  696  *
  697  *      Note: swap associated with the page must be invalidated by the move.  We
  698  *            have to do this for several reasons:  (1) we aren't freeing the
  699  *            page, (2) we are dirtying the page, (3) the VM system is probably
  700  *            moving the page from object A to B, and will then later move
  701  *            the backing store from A to B and we can't have a conflict.
  702  *
  703  *      Note: we *always* dirty the page.  It is necessary both for the
  704  *            fact that we moved it, and because we may be invalidating
  705  *            swap.  If the page is on the cache, we have to deactivate it
  706  *            or vm_page_dirty() will panic.  Dirty pages are not allowed
  707  *            on the cache.
  708  */
  709 void
  710 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
  711 {
  712         int s;
  713 
  714         s = splvm();
  715         vm_page_lock_queues();
  716         vm_page_remove(m);
  717         vm_page_insert(m, new_object, new_pindex);
  718         if (m->queue - m->pc == PQ_CACHE)
  719                 vm_page_deactivate(m);
  720         vm_page_dirty(m);
  721         vm_page_unlock_queues();
  722         splx(s);
  723 }
  724 
  725 /*
  726  *      vm_page_select_cache:
  727  *
  728  *      Find a page on the cache queue with color optimization.  As pages
  729  *      might be found, but not applicable, they are deactivated.  This
  730  *      keeps us from using potentially busy cached pages.
  731  *
  732  *      This routine must be called at splvm().
  733  *      This routine may not block.
  734  */
  735 static vm_page_t
  736 vm_page_select_cache(vm_pindex_t color)
  737 {
  738         vm_page_t m;
  739 
  740         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
  741         while (TRUE) {
  742                 m = vm_pageq_find(PQ_CACHE, color & PQ_L2_MASK, FALSE);
  743                 if (m && ((m->flags & (PG_BUSY|PG_UNMANAGED)) || m->busy ||
  744                                m->hold_count || m->wire_count)) {
  745                         vm_page_deactivate(m);
  746                         continue;
  747                 }
  748                 return m;
  749         }
  750 }
  751 
  752 /*
  753  *      vm_page_select_free:
  754  *
  755  *      Find a free or zero page, with specified preference. 
  756  *
  757  *      This routine must be called at splvm().
  758  *      This routine may not block.
  759  */
  760 static __inline vm_page_t
  761 vm_page_select_free(vm_pindex_t color, boolean_t prefer_zero)
  762 {
  763         vm_page_t m;
  764 
  765         m = vm_pageq_find(PQ_FREE, color & PQ_L2_MASK, prefer_zero);
  766         return (m);
  767 }
  768 
  769 /*
  770  *      vm_page_alloc:
  771  *
  772  *      Allocate and return a memory cell associated
  773  *      with this VM object/offset pair.
  774  *
  775  *      page_req classes:
  776  *      VM_ALLOC_NORMAL         normal process request
  777  *      VM_ALLOC_SYSTEM         system *really* needs a page
  778  *      VM_ALLOC_INTERRUPT      interrupt time request
  779  *      VM_ALLOC_ZERO           zero page
  780  *
  781  *      This routine may not block.
  782  *
  783  *      Additional special handling is required when called from an
  784  *      interrupt (VM_ALLOC_INTERRUPT).  We are not allowed to mess with
  785  *      the page cache in this case.
  786  */
  787 vm_page_t
  788 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
  789 {
  790         vm_page_t m = NULL;
  791         vm_pindex_t color;
  792         int page_req, s;
  793 
  794         GIANT_REQUIRED;
  795 
  796 #ifdef INVARIANTS
  797         if ((req & VM_ALLOC_NOOBJ) == 0) {
  798                 KASSERT(object != NULL,
  799                     ("vm_page_alloc: NULL object."));
  800                 KASSERT(!vm_page_lookup(object, pindex),
  801                     ("vm_page_alloc: page already allocated"));
  802         }
  803 #endif
  804 
  805         page_req = req & VM_ALLOC_CLASS_MASK;
  806 
  807         if ((req & VM_ALLOC_NOOBJ) == 0)
  808                 color = pindex + object->pg_color;
  809         else
  810                 color = pindex;
  811 
  812         /*
  813          * The pager is allowed to eat deeper into the free page list.
  814          */
  815         if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT)) {
  816                 page_req = VM_ALLOC_SYSTEM;
  817         };
  818 
  819         s = splvm();
  820 loop:
  821         mtx_lock_spin(&vm_page_queue_free_mtx);
  822         if (cnt.v_free_count > cnt.v_free_reserved) {
  823                 /*
  824                  * Allocate from the free queue if there are plenty of pages
  825                  * in it.
  826                  */
  827                 m = vm_page_select_free(color, (req & VM_ALLOC_ZERO) != 0);
  828                                         
  829         } else if (
  830             (page_req == VM_ALLOC_SYSTEM && 
  831              cnt.v_cache_count == 0 && 
  832              cnt.v_free_count > cnt.v_interrupt_free_min) ||
  833             (page_req == VM_ALLOC_INTERRUPT && cnt.v_free_count > 0)
  834         ) {
  835                 /*
  836                  * Interrupt or system, dig deeper into the free list.
  837                  */
  838                 m = vm_page_select_free(color, FALSE);
  839         } else if (page_req != VM_ALLOC_INTERRUPT) {
  840                 mtx_unlock_spin(&vm_page_queue_free_mtx);
  841                 /*
  842                  * Allocatable from cache (non-interrupt only).  On success,
  843                  * we must free the page and try again, thus ensuring that
  844                  * cnt.v_*_free_min counters are replenished.
  845                  */
  846                 vm_page_lock_queues();
  847                 if ((m = vm_page_select_cache(color)) == NULL) {
  848                         vm_page_unlock_queues();
  849                         splx(s);
  850 #if defined(DIAGNOSTIC)
  851                         if (cnt.v_cache_count > 0)
  852                                 printf("vm_page_alloc(NORMAL): missing pages on cache queue: %d\n", cnt.v_cache_count);
  853 #endif
  854                         vm_pageout_deficit++;
  855                         pagedaemon_wakeup();
  856                         return (NULL);
  857                 }
  858                 KASSERT(m->dirty == 0, ("Found dirty cache page %p", m));
  859                 vm_page_busy(m);
  860                 pmap_remove_all(m);
  861                 vm_page_free(m);
  862                 vm_page_unlock_queues();
  863                 goto loop;
  864         } else {
  865                 /*
  866                  * Not allocatable from cache from interrupt, give up.
  867                  */
  868                 mtx_unlock_spin(&vm_page_queue_free_mtx);
  869                 splx(s);
  870                 vm_pageout_deficit++;
  871                 pagedaemon_wakeup();
  872                 return (NULL);
  873         }
  874 
  875         /*
  876          *  At this point we had better have found a good page.
  877          */
  878 
  879         KASSERT(
  880             m != NULL,
  881             ("vm_page_alloc(): missing page on free queue\n")
  882         );
  883 
  884         /*
  885          * Remove from free queue
  886          */
  887 
  888         vm_pageq_remove_nowakeup(m);
  889 
  890         /*
  891          * Initialize structure.  Only the PG_ZERO flag is inherited.
  892          */
  893         if (m->flags & PG_ZERO) {
  894                 vm_page_zero_count--;
  895                 m->flags = PG_ZERO | PG_BUSY;
  896         } else {
  897                 m->flags = PG_BUSY;
  898         }
  899         if (req & VM_ALLOC_WIRED) {
  900                 cnt.v_wire_count++;
  901                 m->wire_count = 1;
  902         } else
  903                 m->wire_count = 0;
  904         m->hold_count = 0;
  905         m->act_count = 0;
  906         m->busy = 0;
  907         m->valid = 0;
  908         KASSERT(m->dirty == 0, ("vm_page_alloc: free/cache page %p was dirty", m));
  909         mtx_unlock_spin(&vm_page_queue_free_mtx);
  910 
  911         /*
  912          * vm_page_insert() is safe prior to the splx().  Note also that
  913          * inserting a page here does not insert it into the pmap (which
  914          * could cause us to block allocating memory).  We cannot block 
  915          * anywhere.
  916          */
  917         if ((req & VM_ALLOC_NOOBJ) == 0)
  918                 vm_page_insert(m, object, pindex);
  919 
  920         /*
  921          * Don't wakeup too often - wakeup the pageout daemon when
  922          * we would be nearly out of memory.
  923          */
  924         if (vm_paging_needed())
  925                 pagedaemon_wakeup();
  926 
  927         splx(s);
  928         return (m);
  929 }
  930 
  931 /*
  932  *      vm_wait:        (also see VM_WAIT macro)
  933  *
  934  *      Block until free pages are available for allocation
  935  *      - Called in various places before memory allocations.
  936  */
  937 void
  938 vm_wait(void)
  939 {
  940         int s;
  941 
  942         s = splvm();
  943         if (curproc == pageproc) {
  944                 vm_pageout_pages_needed = 1;
  945                 tsleep(&vm_pageout_pages_needed, PSWP, "VMWait", 0);
  946         } else {
  947                 if (!vm_pages_needed) {
  948                         vm_pages_needed = 1;
  949                         wakeup(&vm_pages_needed);
  950                 }
  951                 tsleep(&cnt.v_free_count, PVM, "vmwait", 0);
  952         }
  953         splx(s);
  954 }
  955 
  956 /*
  957  *      vm_waitpfault:  (also see VM_WAITPFAULT macro)
  958  *
  959  *      Block until free pages are available for allocation
  960  *      - Called only in vm_fault so that processes page faulting
  961  *        can be easily tracked.
  962  *      - Sleeps at a lower priority than vm_wait() so that vm_wait()ing
  963  *        processes will be able to grab memory first.  Do not change
  964  *        this balance without careful testing first.
  965  */
  966 void
  967 vm_waitpfault(void)
  968 {
  969         int s;
  970 
  971         s = splvm();
  972         if (!vm_pages_needed) {
  973                 vm_pages_needed = 1;
  974                 wakeup(&vm_pages_needed);
  975         }
  976         tsleep(&cnt.v_free_count, PUSER, "pfault", 0);
  977         splx(s);
  978 }
  979 
  980 /*
  981  *      vm_page_activate:
  982  *
  983  *      Put the specified page on the active list (if appropriate).
  984  *      Ensure that act_count is at least ACT_INIT but do not otherwise
  985  *      mess with it.
  986  *
  987  *      The page queues must be locked.
  988  *      This routine may not block.
  989  */
  990 void
  991 vm_page_activate(vm_page_t m)
  992 {
  993         int s;
  994 
  995         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
  996         s = splvm();
  997         if (m->queue != PQ_ACTIVE) {
  998                 if ((m->queue - m->pc) == PQ_CACHE)
  999                         cnt.v_reactivated++;
 1000                 vm_pageq_remove(m);
 1001                 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
 1002                         if (m->act_count < ACT_INIT)
 1003                                 m->act_count = ACT_INIT;
 1004                         vm_pageq_enqueue(PQ_ACTIVE, m);
 1005                 }
 1006         } else {
 1007                 if (m->act_count < ACT_INIT)
 1008                         m->act_count = ACT_INIT;
 1009         }
 1010         splx(s);
 1011 }
 1012 
 1013 /*
 1014  *      vm_page_free_wakeup:
 1015  *
 1016  *      Helper routine for vm_page_free_toq() and vm_page_cache().  This
 1017  *      routine is called when a page has been added to the cache or free
 1018  *      queues.
 1019  *
 1020  *      This routine may not block.
 1021  *      This routine must be called at splvm()
 1022  */
 1023 static __inline void
 1024 vm_page_free_wakeup(void)
 1025 {
 1026         /*
 1027          * if pageout daemon needs pages, then tell it that there are
 1028          * some free.
 1029          */
 1030         if (vm_pageout_pages_needed &&
 1031             cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) {
 1032                 wakeup(&vm_pageout_pages_needed);
 1033                 vm_pageout_pages_needed = 0;
 1034         }
 1035         /*
 1036          * wakeup processes that are waiting on memory if we hit a
 1037          * high water mark. And wakeup scheduler process if we have
 1038          * lots of memory. this process will swapin processes.
 1039          */
 1040         if (vm_pages_needed && !vm_page_count_min()) {
 1041                 vm_pages_needed = 0;
 1042                 wakeup(&cnt.v_free_count);
 1043         }
 1044 }
 1045 
 1046 /*
 1047  *      vm_page_free_toq:
 1048  *
 1049  *      Returns the given page to the PQ_FREE list,
 1050  *      disassociating it with any VM object.
 1051  *
 1052  *      Object and page must be locked prior to entry.
 1053  *      This routine may not block.
 1054  */
 1055 
 1056 void
 1057 vm_page_free_toq(vm_page_t m)
 1058 {
 1059         int s;
 1060         struct vpgqueues *pq;
 1061         vm_object_t object = m->object;
 1062 
 1063         GIANT_REQUIRED;
 1064         s = splvm();
 1065         cnt.v_tfree++;
 1066 
 1067         if (m->busy || ((m->queue - m->pc) == PQ_FREE)) {
 1068                 printf(
 1069                 "vm_page_free: pindex(%lu), busy(%d), PG_BUSY(%d), hold(%d)\n",
 1070                     (u_long)m->pindex, m->busy, (m->flags & PG_BUSY) ? 1 : 0,
 1071                     m->hold_count);
 1072                 if ((m->queue - m->pc) == PQ_FREE)
 1073                         panic("vm_page_free: freeing free page");
 1074                 else
 1075                         panic("vm_page_free: freeing busy page");
 1076         }
 1077 
 1078         /*
 1079          * unqueue, then remove page.  Note that we cannot destroy
 1080          * the page here because we do not want to call the pager's
 1081          * callback routine until after we've put the page on the
 1082          * appropriate free queue.
 1083          */
 1084         vm_pageq_remove_nowakeup(m);
 1085         vm_page_remove(m);
 1086 
 1087         /*
 1088          * If fictitious remove object association and
 1089          * return, otherwise delay object association removal.
 1090          */
 1091         if ((m->flags & PG_FICTITIOUS) != 0) {
 1092                 splx(s);
 1093                 return;
 1094         }
 1095 
 1096         m->valid = 0;
 1097         vm_page_undirty(m);
 1098 
 1099         if (m->wire_count != 0) {
 1100                 if (m->wire_count > 1) {
 1101                         panic("vm_page_free: invalid wire count (%d), pindex: 0x%lx",
 1102                                 m->wire_count, (long)m->pindex);
 1103                 }
 1104                 panic("vm_page_free: freeing wired page\n");
 1105         }
 1106 
 1107         /*
 1108          * If we've exhausted the object's resident pages we want to free
 1109          * it up.
 1110          */
 1111         if (object && 
 1112             (object->type == OBJT_VNODE) &&
 1113             ((object->flags & OBJ_DEAD) == 0)
 1114         ) {
 1115                 struct vnode *vp = (struct vnode *)object->handle;
 1116 
 1117                 if (vp) {
 1118                         VI_LOCK(vp);
 1119                         if (VSHOULDFREE(vp))
 1120                                 vfree(vp);
 1121                         VI_UNLOCK(vp);
 1122                 }
 1123         }
 1124 
 1125         /*
 1126          * Clear the UNMANAGED flag when freeing an unmanaged page.
 1127          */
 1128         if (m->flags & PG_UNMANAGED) {
 1129                 m->flags &= ~PG_UNMANAGED;
 1130         } else {
 1131 #ifdef __alpha__
 1132                 pmap_page_is_free(m);
 1133 #endif
 1134         }
 1135 
 1136         if (m->hold_count != 0) {
 1137                 m->flags &= ~PG_ZERO;
 1138                 m->queue = PQ_HOLD;
 1139         } else
 1140                 m->queue = PQ_FREE + m->pc;
 1141         pq = &vm_page_queues[m->queue];
 1142         mtx_lock_spin(&vm_page_queue_free_mtx);
 1143         pq->lcnt++;
 1144         ++(*pq->cnt);
 1145 
 1146         /*
 1147          * Put zero'd pages on the end ( where we look for zero'd pages
 1148          * first ) and non-zerod pages at the head.
 1149          */
 1150         if (m->flags & PG_ZERO) {
 1151                 TAILQ_INSERT_TAIL(&pq->pl, m, pageq);
 1152                 ++vm_page_zero_count;
 1153         } else {
 1154                 TAILQ_INSERT_HEAD(&pq->pl, m, pageq);
 1155         }
 1156         mtx_unlock_spin(&vm_page_queue_free_mtx);
 1157         vm_page_free_wakeup();
 1158         splx(s);
 1159 }
 1160 
 1161 /*
 1162  *      vm_page_unmanage:
 1163  *
 1164  *      Prevent PV management from being done on the page.  The page is
 1165  *      removed from the paging queues as if it were wired, and as a 
 1166  *      consequence of no longer being managed the pageout daemon will not
 1167  *      touch it (since there is no way to locate the pte mappings for the
 1168  *      page).  madvise() calls that mess with the pmap will also no longer
 1169  *      operate on the page.
 1170  *
 1171  *      Beyond that the page is still reasonably 'normal'.  Freeing the page
 1172  *      will clear the flag.
 1173  *
 1174  *      This routine is used by OBJT_PHYS objects - objects using unswappable
 1175  *      physical memory as backing store rather then swap-backed memory and
 1176  *      will eventually be extended to support 4MB unmanaged physical 
 1177  *      mappings.
 1178  */
 1179 void
 1180 vm_page_unmanage(vm_page_t m)
 1181 {
 1182         int s;
 1183 
 1184         s = splvm();
 1185         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
 1186         if ((m->flags & PG_UNMANAGED) == 0) {
 1187                 if (m->wire_count == 0)
 1188                         vm_pageq_remove(m);
 1189         }
 1190         vm_page_flag_set(m, PG_UNMANAGED);
 1191         splx(s);
 1192 }
 1193 
 1194 /*
 1195  *      vm_page_wire:
 1196  *
 1197  *      Mark this page as wired down by yet
 1198  *      another map, removing it from paging queues
 1199  *      as necessary.
 1200  *
 1201  *      The page queues must be locked.
 1202  *      This routine may not block.
 1203  */
 1204 void
 1205 vm_page_wire(vm_page_t m)
 1206 {
 1207         int s;
 1208 
 1209         /*
 1210          * Only bump the wire statistics if the page is not already wired,
 1211          * and only unqueue the page if it is on some queue (if it is unmanaged
 1212          * it is already off the queues).
 1213          */
 1214         s = splvm();
 1215         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
 1216         if (m->wire_count == 0) {
 1217                 if ((m->flags & PG_UNMANAGED) == 0)
 1218                         vm_pageq_remove(m);
 1219                 cnt.v_wire_count++;
 1220         }
 1221         m->wire_count++;
 1222         KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
 1223         splx(s);
 1224 }
 1225 
 1226 /*
 1227  *      vm_page_unwire:
 1228  *
 1229  *      Release one wiring of this page, potentially
 1230  *      enabling it to be paged again.
 1231  *
 1232  *      Many pages placed on the inactive queue should actually go
 1233  *      into the cache, but it is difficult to figure out which.  What
 1234  *      we do instead, if the inactive target is well met, is to put
 1235  *      clean pages at the head of the inactive queue instead of the tail.
 1236  *      This will cause them to be moved to the cache more quickly and
 1237  *      if not actively re-referenced, freed more quickly.  If we just
 1238  *      stick these pages at the end of the inactive queue, heavy filesystem
 1239  *      meta-data accesses can cause an unnecessary paging load on memory bound 
 1240  *      processes.  This optimization causes one-time-use metadata to be
 1241  *      reused more quickly.
 1242  *
 1243  *      BUT, if we are in a low-memory situation we have no choice but to
 1244  *      put clean pages on the cache queue.
 1245  *
 1246  *      A number of routines use vm_page_unwire() to guarantee that the page
 1247  *      will go into either the inactive or active queues, and will NEVER
 1248  *      be placed in the cache - for example, just after dirtying a page.
 1249  *      dirty pages in the cache are not allowed.
 1250  *
 1251  *      The page queues must be locked.
 1252  *      This routine may not block.
 1253  */
 1254 void
 1255 vm_page_unwire(vm_page_t m, int activate)
 1256 {
 1257         int s;
 1258 
 1259         s = splvm();
 1260         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
 1261         if (m->wire_count > 0) {
 1262                 m->wire_count--;
 1263                 if (m->wire_count == 0) {
 1264                         cnt.v_wire_count--;
 1265                         if (m->flags & PG_UNMANAGED) {
 1266                                 ;
 1267                         } else if (activate)
 1268                                 vm_pageq_enqueue(PQ_ACTIVE, m);
 1269                         else {
 1270                                 vm_page_flag_clear(m, PG_WINATCFLS);
 1271                                 vm_pageq_enqueue(PQ_INACTIVE, m);
 1272                         }
 1273                 }
 1274         } else {
 1275                 panic("vm_page_unwire: invalid wire count: %d\n", m->wire_count);
 1276         }
 1277         splx(s);
 1278 }
 1279 
 1280 
 1281 /*
 1282  * Move the specified page to the inactive queue.  If the page has
 1283  * any associated swap, the swap is deallocated.
 1284  *
 1285  * Normally athead is 0 resulting in LRU operation.  athead is set
 1286  * to 1 if we want this page to be 'as if it were placed in the cache',
 1287  * except without unmapping it from the process address space.
 1288  *
 1289  * This routine may not block.
 1290  */
 1291 static __inline void
 1292 _vm_page_deactivate(vm_page_t m, int athead)
 1293 {
 1294         int s;
 1295 
 1296         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
 1297         /*
 1298          * Ignore if already inactive.
 1299          */
 1300         if (m->queue == PQ_INACTIVE)
 1301                 return;
 1302 
 1303         s = splvm();
 1304         if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
 1305                 if ((m->queue - m->pc) == PQ_CACHE)
 1306                         cnt.v_reactivated++;
 1307                 vm_page_flag_clear(m, PG_WINATCFLS);
 1308                 vm_pageq_remove(m);
 1309                 if (athead)
 1310                         TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
 1311                 else
 1312                         TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
 1313                 m->queue = PQ_INACTIVE;
 1314                 vm_page_queues[PQ_INACTIVE].lcnt++;
 1315                 cnt.v_inactive_count++;
 1316         }
 1317         splx(s);
 1318 }
 1319 
 1320 void
 1321 vm_page_deactivate(vm_page_t m)
 1322 {
 1323     _vm_page_deactivate(m, 0);
 1324 }
 1325 
 1326 /*
 1327  * vm_page_try_to_cache:
 1328  *
 1329  * Returns 0 on failure, 1 on success
 1330  */
 1331 int
 1332 vm_page_try_to_cache(vm_page_t m)
 1333 {
 1334 
 1335         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
 1336         if (m->dirty || m->hold_count || m->busy || m->wire_count ||
 1337             (m->flags & (PG_BUSY|PG_UNMANAGED))) {
 1338                 return (0);
 1339         }
 1340         vm_page_test_dirty(m);
 1341         if (m->dirty)
 1342                 return (0);
 1343         vm_page_cache(m);
 1344         return (1);
 1345 }
 1346 
 1347 /*
 1348  * vm_page_try_to_free()
 1349  *
 1350  *      Attempt to free the page.  If we cannot free it, we do nothing.
 1351  *      1 is returned on success, 0 on failure.
 1352  */
 1353 int
 1354 vm_page_try_to_free(vm_page_t m)
 1355 {
 1356 
 1357         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
 1358         if (m->dirty || m->hold_count || m->busy || m->wire_count ||
 1359             (m->flags & (PG_BUSY|PG_UNMANAGED))) {
 1360                 return (0);
 1361         }
 1362         vm_page_test_dirty(m);
 1363         if (m->dirty)
 1364                 return (0);
 1365         vm_page_busy(m);
 1366         pmap_remove_all(m);
 1367         vm_page_free(m);
 1368         return (1);
 1369 }
 1370 
 1371 /*
 1372  * vm_page_cache
 1373  *
 1374  * Put the specified page onto the page cache queue (if appropriate).
 1375  *
 1376  * This routine may not block.
 1377  */
 1378 void
 1379 vm_page_cache(vm_page_t m)
 1380 {
 1381         int s;
 1382 
 1383         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
 1384         if ((m->flags & (PG_BUSY|PG_UNMANAGED)) || m->busy || m->wire_count) {
 1385                 printf("vm_page_cache: attempting to cache busy page\n");
 1386                 return;
 1387         }
 1388         if ((m->queue - m->pc) == PQ_CACHE)
 1389                 return;
 1390 
 1391         /*
 1392          * Remove all pmaps and indicate that the page is not
 1393          * writeable or mapped.
 1394          */
 1395         pmap_remove_all(m);
 1396         if (m->dirty != 0) {
 1397                 panic("vm_page_cache: caching a dirty page, pindex: %ld",
 1398                         (long)m->pindex);
 1399         }
 1400         s = splvm();
 1401         vm_pageq_remove_nowakeup(m);
 1402         vm_pageq_enqueue(PQ_CACHE + m->pc, m);
 1403         vm_page_free_wakeup();
 1404         splx(s);
 1405 }
 1406 
 1407 /*
 1408  * vm_page_dontneed
 1409  *
 1410  *      Cache, deactivate, or do nothing as appropriate.  This routine
 1411  *      is typically used by madvise() MADV_DONTNEED.
 1412  *
 1413  *      Generally speaking we want to move the page into the cache so
 1414  *      it gets reused quickly.  However, this can result in a silly syndrome
 1415  *      due to the page recycling too quickly.  Small objects will not be
 1416  *      fully cached.  On the otherhand, if we move the page to the inactive
 1417  *      queue we wind up with a problem whereby very large objects 
 1418  *      unnecessarily blow away our inactive and cache queues.
 1419  *
 1420  *      The solution is to move the pages based on a fixed weighting.  We
 1421  *      either leave them alone, deactivate them, or move them to the cache,
 1422  *      where moving them to the cache has the highest weighting.
 1423  *      By forcing some pages into other queues we eventually force the
 1424  *      system to balance the queues, potentially recovering other unrelated
 1425  *      space from active.  The idea is to not force this to happen too
 1426  *      often.
 1427  */
 1428 void
 1429 vm_page_dontneed(vm_page_t m)
 1430 {
 1431         static int dnweight;
 1432         int dnw;
 1433         int head;
 1434 
 1435         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
 1436         dnw = ++dnweight;
 1437 
 1438         /*
 1439          * occassionally leave the page alone
 1440          */
 1441         if ((dnw & 0x01F0) == 0 ||
 1442             m->queue == PQ_INACTIVE || 
 1443             m->queue - m->pc == PQ_CACHE
 1444         ) {
 1445                 if (m->act_count >= ACT_INIT)
 1446                         --m->act_count;
 1447                 return;
 1448         }
 1449 
 1450         if (m->dirty == 0)
 1451                 vm_page_test_dirty(m);
 1452 
 1453         if (m->dirty || (dnw & 0x0070) == 0) {
 1454                 /*
 1455                  * Deactivate the page 3 times out of 32.
 1456                  */
 1457                 head = 0;
 1458         } else {
 1459                 /*
 1460                  * Cache the page 28 times out of every 32.  Note that
 1461                  * the page is deactivated instead of cached, but placed
 1462                  * at the head of the queue instead of the tail.
 1463                  */
 1464                 head = 1;
 1465         }
 1466         _vm_page_deactivate(m, head);
 1467 }
 1468 
 1469 /*
 1470  * Grab a page, waiting until we are waken up due to the page
 1471  * changing state.  We keep on waiting, if the page continues
 1472  * to be in the object.  If the page doesn't exist, allocate it.
 1473  *
 1474  * This routine may block.
 1475  */
 1476 vm_page_t
 1477 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
 1478 {
 1479         vm_page_t m;
 1480         int s, generation;
 1481 
 1482         GIANT_REQUIRED;
 1483 retrylookup:
 1484         if ((m = vm_page_lookup(object, pindex)) != NULL) {
 1485                 vm_page_lock_queues();
 1486                 if (m->busy || (m->flags & PG_BUSY)) {
 1487                         generation = object->generation;
 1488 
 1489                         s = splvm();
 1490                         while ((object->generation == generation) &&
 1491                                         (m->busy || (m->flags & PG_BUSY))) {
 1492                                 vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
 1493                                 msleep(m, &vm_page_queue_mtx, PVM, "pgrbwt", 0);
 1494                                 if ((allocflags & VM_ALLOC_RETRY) == 0) {
 1495                                         vm_page_unlock_queues();
 1496                                         splx(s);
 1497                                         return NULL;
 1498                                 }
 1499                         }
 1500                         vm_page_unlock_queues();
 1501                         splx(s);
 1502                         goto retrylookup;
 1503                 } else {
 1504                         if (allocflags & VM_ALLOC_WIRED)
 1505                                 vm_page_wire(m);
 1506                         vm_page_busy(m);
 1507                         vm_page_unlock_queues();
 1508                         return m;
 1509                 }
 1510         }
 1511 
 1512         m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_RETRY);
 1513         if (m == NULL) {
 1514                 VM_WAIT;
 1515                 if ((allocflags & VM_ALLOC_RETRY) == 0)
 1516                         return NULL;
 1517                 goto retrylookup;
 1518         }
 1519 
 1520         return m;
 1521 }
 1522 
 1523 /*
 1524  * Mapping function for valid bits or for dirty bits in
 1525  * a page.  May not block.
 1526  *
 1527  * Inputs are required to range within a page.
 1528  */
 1529 __inline int
 1530 vm_page_bits(int base, int size)
 1531 {
 1532         int first_bit;
 1533         int last_bit;
 1534 
 1535         KASSERT(
 1536             base + size <= PAGE_SIZE,
 1537             ("vm_page_bits: illegal base/size %d/%d", base, size)
 1538         );
 1539 
 1540         if (size == 0)          /* handle degenerate case */
 1541                 return (0);
 1542 
 1543         first_bit = base >> DEV_BSHIFT;
 1544         last_bit = (base + size - 1) >> DEV_BSHIFT;
 1545 
 1546         return ((2 << last_bit) - (1 << first_bit));
 1547 }
 1548 
 1549 /*
 1550  *      vm_page_set_validclean:
 1551  *
 1552  *      Sets portions of a page valid and clean.  The arguments are expected
 1553  *      to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
 1554  *      of any partial chunks touched by the range.  The invalid portion of
 1555  *      such chunks will be zero'd.
 1556  *
 1557  *      This routine may not block.
 1558  *
 1559  *      (base + size) must be less then or equal to PAGE_SIZE.
 1560  */
 1561 void
 1562 vm_page_set_validclean(vm_page_t m, int base, int size)
 1563 {
 1564         int pagebits;
 1565         int frag;
 1566         int endoff;
 1567 
 1568         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
 1569         if (size == 0)  /* handle degenerate case */
 1570                 return;
 1571 
 1572         /*
 1573          * If the base is not DEV_BSIZE aligned and the valid
 1574          * bit is clear, we have to zero out a portion of the
 1575          * first block.
 1576          */
 1577         if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
 1578             (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
 1579                 pmap_zero_page_area(m, frag, base - frag);
 1580 
 1581         /*
 1582          * If the ending offset is not DEV_BSIZE aligned and the 
 1583          * valid bit is clear, we have to zero out a portion of
 1584          * the last block.
 1585          */
 1586         endoff = base + size;
 1587         if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
 1588             (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
 1589                 pmap_zero_page_area(m, endoff,
 1590                     DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
 1591 
 1592         /*
 1593          * Set valid, clear dirty bits.  If validating the entire
 1594          * page we can safely clear the pmap modify bit.  We also
 1595          * use this opportunity to clear the PG_NOSYNC flag.  If a process
 1596          * takes a write fault on a MAP_NOSYNC memory area the flag will
 1597          * be set again.
 1598          *
 1599          * We set valid bits inclusive of any overlap, but we can only
 1600          * clear dirty bits for DEV_BSIZE chunks that are fully within
 1601          * the range.
 1602          */
 1603         pagebits = vm_page_bits(base, size);
 1604         m->valid |= pagebits;
 1605 #if 0   /* NOT YET */
 1606         if ((frag = base & (DEV_BSIZE - 1)) != 0) {
 1607                 frag = DEV_BSIZE - frag;
 1608                 base += frag;
 1609                 size -= frag;
 1610                 if (size < 0)
 1611                         size = 0;
 1612         }
 1613         pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
 1614 #endif
 1615         m->dirty &= ~pagebits;
 1616         if (base == 0 && size == PAGE_SIZE) {
 1617                 pmap_clear_modify(m);
 1618                 vm_page_flag_clear(m, PG_NOSYNC);
 1619         }
 1620 }
 1621 
 1622 #if 0
 1623 
 1624 void
 1625 vm_page_set_dirty(vm_page_t m, int base, int size)
 1626 {
 1627         m->dirty |= vm_page_bits(base, size);
 1628 }
 1629 
 1630 #endif
 1631 
 1632 void
 1633 vm_page_clear_dirty(vm_page_t m, int base, int size)
 1634 {
 1635         GIANT_REQUIRED;
 1636         m->dirty &= ~vm_page_bits(base, size);
 1637 }
 1638 
 1639 /*
 1640  *      vm_page_set_invalid:
 1641  *
 1642  *      Invalidates DEV_BSIZE'd chunks within a page.  Both the
 1643  *      valid and dirty bits for the effected areas are cleared.
 1644  *
 1645  *      May not block.
 1646  */
 1647 void
 1648 vm_page_set_invalid(vm_page_t m, int base, int size)
 1649 {
 1650         int bits;
 1651 
 1652         GIANT_REQUIRED;
 1653         bits = vm_page_bits(base, size);
 1654         m->valid &= ~bits;
 1655         m->dirty &= ~bits;
 1656         m->object->generation++;
 1657 }
 1658 
 1659 /*
 1660  * vm_page_zero_invalid()
 1661  *
 1662  *      The kernel assumes that the invalid portions of a page contain 
 1663  *      garbage, but such pages can be mapped into memory by user code.
 1664  *      When this occurs, we must zero out the non-valid portions of the
 1665  *      page so user code sees what it expects.
 1666  *
 1667  *      Pages are most often semi-valid when the end of a file is mapped 
 1668  *      into memory and the file's size is not page aligned.
 1669  */
 1670 void
 1671 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
 1672 {
 1673         int b;
 1674         int i;
 1675 
 1676         /*
 1677          * Scan the valid bits looking for invalid sections that
 1678          * must be zerod.  Invalid sub-DEV_BSIZE'd areas ( where the
 1679          * valid bit may be set ) have already been zerod by
 1680          * vm_page_set_validclean().
 1681          */
 1682         for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
 1683                 if (i == (PAGE_SIZE / DEV_BSIZE) || 
 1684                     (m->valid & (1 << i))
 1685                 ) {
 1686                         if (i > b) {
 1687                                 pmap_zero_page_area(m, 
 1688                                     b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
 1689                         }
 1690                         b = i + 1;
 1691                 }
 1692         }
 1693 
 1694         /*
 1695          * setvalid is TRUE when we can safely set the zero'd areas
 1696          * as being valid.  We can do this if there are no cache consistancy
 1697          * issues.  e.g. it is ok to do with UFS, but not ok to do with NFS.
 1698          */
 1699         if (setvalid)
 1700                 m->valid = VM_PAGE_BITS_ALL;
 1701 }
 1702 
 1703 /*
 1704  *      vm_page_is_valid:
 1705  *
 1706  *      Is (partial) page valid?  Note that the case where size == 0
 1707  *      will return FALSE in the degenerate case where the page is
 1708  *      entirely invalid, and TRUE otherwise.
 1709  *
 1710  *      May not block.
 1711  */
 1712 int
 1713 vm_page_is_valid(vm_page_t m, int base, int size)
 1714 {
 1715         int bits = vm_page_bits(base, size);
 1716 
 1717         if (m->valid && ((m->valid & bits) == bits))
 1718                 return 1;
 1719         else
 1720                 return 0;
 1721 }
 1722 
 1723 /*
 1724  * update dirty bits from pmap/mmu.  May not block.
 1725  */
 1726 void
 1727 vm_page_test_dirty(vm_page_t m)
 1728 {
 1729         if ((m->dirty != VM_PAGE_BITS_ALL) && pmap_is_modified(m)) {
 1730                 vm_page_dirty(m);
 1731         }
 1732 }
 1733 
 1734 int so_zerocp_fullpage = 0;
 1735 
 1736 void
 1737 vm_page_cowfault(vm_page_t m)
 1738 {
 1739         vm_page_t mnew;
 1740         vm_object_t object;
 1741         vm_pindex_t pindex;
 1742 
 1743         object = m->object;
 1744         pindex = m->pindex;
 1745         vm_page_busy(m);
 1746 
 1747  retry_alloc:
 1748         vm_page_remove(m);
 1749         /*
 1750          * An interrupt allocation is requested because the page
 1751          * queues lock is held. 
 1752          */
 1753         mnew = vm_page_alloc(object, pindex, VM_ALLOC_INTERRUPT);
 1754         if (mnew == NULL) {
 1755                 vm_page_insert(m, object, pindex);
 1756                 vm_page_unlock_queues();
 1757                 VM_WAIT;
 1758                 vm_page_lock_queues();
 1759                 goto retry_alloc;
 1760         }
 1761 
 1762         if (m->cow == 0) {
 1763                 /* 
 1764                  * check to see if we raced with an xmit complete when 
 1765                  * waiting to allocate a page.  If so, put things back 
 1766                  * the way they were 
 1767                  */
 1768                 vm_page_busy(mnew);
 1769                 vm_page_free(mnew);
 1770                 vm_page_insert(m, object, pindex);
 1771         } else { /* clear COW & copy page */
 1772                 if (so_zerocp_fullpage) {
 1773                         mnew->valid = VM_PAGE_BITS_ALL;
 1774                 } else {
 1775                         vm_page_copy(m, mnew);
 1776                 }
 1777                 vm_page_dirty(mnew);
 1778                 vm_page_flag_clear(mnew, PG_BUSY);
 1779         }
 1780 }
 1781 
 1782 void 
 1783 vm_page_cowclear(vm_page_t m)
 1784 {
 1785 
 1786         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
 1787         if (m->cow) {
 1788                 m->cow--;
 1789                 /* 
 1790                  * let vm_fault add back write permission  lazily
 1791                  */
 1792         } 
 1793         /*
 1794          *  sf_buf_free() will free the page, so we needn't do it here
 1795          */ 
 1796 }
 1797 
 1798 void
 1799 vm_page_cowsetup(vm_page_t m)
 1800 {
 1801 
 1802         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
 1803         m->cow++;
 1804         pmap_page_protect(m, VM_PROT_READ);
 1805 }
 1806 
 1807 #include "opt_ddb.h"
 1808 #ifdef DDB
 1809 #include <sys/kernel.h>
 1810 
 1811 #include <ddb/ddb.h>
 1812 
 1813 DB_SHOW_COMMAND(page, vm_page_print_page_info)
 1814 {
 1815         db_printf("cnt.v_free_count: %d\n", cnt.v_free_count);
 1816         db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count);
 1817         db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count);
 1818         db_printf("cnt.v_active_count: %d\n", cnt.v_active_count);
 1819         db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count);
 1820         db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved);
 1821         db_printf("cnt.v_free_min: %d\n", cnt.v_free_min);
 1822         db_printf("cnt.v_free_target: %d\n", cnt.v_free_target);
 1823         db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min);
 1824         db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target);
 1825 }
 1826 
 1827 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
 1828 {
 1829         int i;
 1830         db_printf("PQ_FREE:");
 1831         for (i = 0; i < PQ_L2_SIZE; i++) {
 1832                 db_printf(" %d", vm_page_queues[PQ_FREE + i].lcnt);
 1833         }
 1834         db_printf("\n");
 1835                 
 1836         db_printf("PQ_CACHE:");
 1837         for (i = 0; i < PQ_L2_SIZE; i++) {
 1838                 db_printf(" %d", vm_page_queues[PQ_CACHE + i].lcnt);
 1839         }
 1840         db_printf("\n");
 1841 
 1842         db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n",
 1843                 vm_page_queues[PQ_ACTIVE].lcnt,
 1844                 vm_page_queues[PQ_INACTIVE].lcnt);
 1845 }
 1846 #endif /* DDB */

Cache object: 857c32884dd51b2eaf2bdd4b086e0dc7


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