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_object.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, 1993
    3  *      The Regents of the University of California.  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_object.c   8.5 (Berkeley) 3/22/94
   37  *
   38  *
   39  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
   40  * All rights reserved.
   41  *
   42  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
   43  *
   44  * Permission to use, copy, modify and distribute this software and
   45  * its documentation is hereby granted, provided that both the copyright
   46  * notice and this permission notice appear in all copies of the
   47  * software, derivative works or modified versions, and any portions
   48  * thereof, and that both notices appear in supporting documentation.
   49  *
   50  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
   51  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
   52  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
   53  *
   54  * Carnegie Mellon requests users of this software to return to
   55  *
   56  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
   57  *  School of Computer Science
   58  *  Carnegie Mellon University
   59  *  Pittsburgh PA 15213-3890
   60  *
   61  * any improvements or extensions that they make and grant Carnegie the
   62  * rights to redistribute these changes.
   63  *
   64  * $FreeBSD$
   65  */
   66 
   67 /*
   68  *      Virtual memory object module.
   69  */
   70 
   71 #include <sys/param.h>
   72 #include <sys/systm.h>
   73 #include <sys/proc.h>           /* for curproc, pageproc */
   74 #include <sys/vnode.h>
   75 #include <sys/vmmeter.h>
   76 #include <sys/mman.h>
   77 #include <sys/mount.h>
   78 #include <sys/kernel.h>
   79 #include <sys/sysctl.h>
   80 
   81 #include <vm/vm.h>
   82 #include <vm/vm_param.h>
   83 #include <vm/pmap.h>
   84 #include <vm/vm_map.h>
   85 #include <vm/vm_object.h>
   86 #include <vm/vm_page.h>
   87 #include <vm/vm_pageout.h>
   88 #include <vm/vm_pager.h>
   89 #include <vm/swap_pager.h>
   90 #include <vm/vm_kern.h>
   91 #include <vm/vm_extern.h>
   92 #include <vm/vm_zone.h>
   93 
   94 #define EASY_SCAN_FACTOR        8
   95 
   96 #define MSYNC_FLUSH_HARDSEQ     0x01
   97 #define MSYNC_FLUSH_SOFTSEQ     0x02
   98 
   99 static int msync_flush_flags = MSYNC_FLUSH_HARDSEQ | MSYNC_FLUSH_SOFTSEQ;
  100 SYSCTL_INT(_vm, OID_AUTO, msync_flush_flags,
  101         CTLFLAG_RW, &msync_flush_flags, 0, "");
  102 
  103 static void     vm_object_qcollapse (vm_object_t object);
  104 static int      vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags);
  105 
  106 /*
  107  *      Virtual memory objects maintain the actual data
  108  *      associated with allocated virtual memory.  A given
  109  *      page of memory exists within exactly one object.
  110  *
  111  *      An object is only deallocated when all "references"
  112  *      are given up.  Only one "reference" to a given
  113  *      region of an object should be writeable.
  114  *
  115  *      Associated with each object is a list of all resident
  116  *      memory pages belonging to that object; this list is
  117  *      maintained by the "vm_page" module, and locked by the object's
  118  *      lock.
  119  *
  120  *      Each object also records a "pager" routine which is
  121  *      used to retrieve (and store) pages to the proper backing
  122  *      storage.  In addition, objects may be backed by other
  123  *      objects from which they were virtual-copied.
  124  *
  125  *      The only items within the object structure which are
  126  *      modified after time of creation are:
  127  *              reference count         locked by object's lock
  128  *              pager routine           locked by object's lock
  129  *
  130  */
  131 
  132 struct object_q vm_object_list;
  133 #ifndef NULL_SIMPLELOCKS
  134 static struct simplelock vm_object_list_lock;
  135 #endif
  136 static long vm_object_count;            /* count of all objects */
  137 vm_object_t kernel_object;
  138 vm_object_t kmem_object;
  139 static struct vm_object kernel_object_store;
  140 static struct vm_object kmem_object_store;
  141 extern int vm_pageout_page_count;
  142 
  143 static long object_collapses;
  144 static long object_bypasses;
  145 static int next_index;
  146 static vm_zone_t obj_zone;
  147 static struct vm_zone obj_zone_store;
  148 static int object_hash_rand;
  149 #define VM_OBJECTS_INIT 256
  150 static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
  151 
  152 void
  153 _vm_object_allocate(type, size, object)
  154         objtype_t type;
  155         vm_size_t size;
  156         vm_object_t object;
  157 {
  158         int incr;
  159         TAILQ_INIT(&object->memq);
  160         LIST_INIT(&object->shadow_head);
  161 
  162         object->type = type;
  163         object->size = size;
  164         object->ref_count = 1;
  165         object->flags = 0;
  166         if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
  167                 vm_object_set_flag(object, OBJ_ONEMAPPING);
  168         object->paging_in_progress = 0;
  169         object->resident_page_count = 0;
  170         object->shadow_count = 0;
  171         object->pg_color = next_index;
  172         if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
  173                 incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
  174         else
  175                 incr = size;
  176         next_index = (next_index + incr) & PQ_L2_MASK;
  177         object->handle = NULL;
  178         object->backing_object = NULL;
  179         object->backing_object_offset = (vm_ooffset_t) 0;
  180         /*
  181          * Try to generate a number that will spread objects out in the
  182          * hash table.  We 'wipe' new objects across the hash in 128 page
  183          * increments plus 1 more to offset it a little more by the time
  184          * it wraps around.
  185          */
  186         object->hash_rand = object_hash_rand - 129;
  187 
  188         object->generation++;
  189 
  190         TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
  191         vm_object_count++;
  192         object_hash_rand = object->hash_rand;
  193 }
  194 
  195 /*
  196  *      vm_object_init:
  197  *
  198  *      Initialize the VM objects module.
  199  */
  200 void
  201 vm_object_init()
  202 {
  203         TAILQ_INIT(&vm_object_list);
  204         simple_lock_init(&vm_object_list_lock);
  205         vm_object_count = 0;
  206         
  207         kernel_object = &kernel_object_store;
  208         _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
  209             kernel_object);
  210 
  211         kmem_object = &kmem_object_store;
  212         _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
  213             kmem_object);
  214 
  215         obj_zone = &obj_zone_store;
  216         zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
  217                 vm_objects_init, VM_OBJECTS_INIT);
  218 }
  219 
  220 void
  221 vm_object_init2() {
  222         zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1);
  223 }
  224 
  225 /*
  226  *      vm_object_allocate:
  227  *
  228  *      Returns a new object with the given size.
  229  */
  230 
  231 vm_object_t
  232 vm_object_allocate(type, size)
  233         objtype_t type;
  234         vm_size_t size;
  235 {
  236         vm_object_t result;
  237 
  238         result = (vm_object_t) zalloc(obj_zone);
  239 
  240         _vm_object_allocate(type, size, result);
  241 
  242         return (result);
  243 }
  244 
  245 
  246 /*
  247  *      vm_object_reference:
  248  *
  249  *      Gets another reference to the given object.
  250  */
  251 void
  252 vm_object_reference(object)
  253         vm_object_t object;
  254 {
  255         if (object == NULL)
  256                 return;
  257 
  258 #if 0
  259         /* object can be re-referenced during final cleaning */
  260         KASSERT(!(object->flags & OBJ_DEAD),
  261             ("vm_object_reference: attempting to reference dead obj"));
  262 #endif
  263 
  264         object->ref_count++;
  265         if (object->type == OBJT_VNODE) {
  266                 while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) {
  267                         printf("vm_object_reference: delay in getting object\n");
  268                 }
  269         }
  270 }
  271 
  272 void
  273 vm_object_vndeallocate(object)
  274         vm_object_t object;
  275 {
  276         struct vnode *vp = (struct vnode *) object->handle;
  277 
  278         KASSERT(object->type == OBJT_VNODE,
  279             ("vm_object_vndeallocate: not a vnode object"));
  280         KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
  281 #ifdef INVARIANTS
  282         if (object->ref_count == 0) {
  283                 vprint("vm_object_vndeallocate", vp);
  284                 panic("vm_object_vndeallocate: bad object reference count");
  285         }
  286 #endif
  287 
  288         object->ref_count--;
  289         if (object->ref_count == 0) {
  290                 vp->v_flag &= ~VTEXT;
  291                 vm_object_clear_flag(object, OBJ_OPT);
  292         }
  293         vrele(vp);
  294 }
  295 
  296 /*
  297  *      vm_object_deallocate:
  298  *
  299  *      Release a reference to the specified object,
  300  *      gained either through a vm_object_allocate
  301  *      or a vm_object_reference call.  When all references
  302  *      are gone, storage associated with this object
  303  *      may be relinquished.
  304  *
  305  *      No object may be locked.
  306  */
  307 void
  308 vm_object_deallocate(object)
  309         vm_object_t object;
  310 {
  311         vm_object_t temp;
  312 
  313         while (object != NULL) {
  314 
  315                 if (object->type == OBJT_VNODE) {
  316                         vm_object_vndeallocate(object);
  317                         return;
  318                 }
  319 
  320                 if (object->ref_count == 0) {
  321                         panic("vm_object_deallocate: object deallocated too many times: %d", object->type);
  322                 } else if (object->ref_count > 2) {
  323                         object->ref_count--;
  324                         return;
  325                 }
  326 
  327                 /*
  328                  * Here on ref_count of one or two, which are special cases for
  329                  * objects.
  330                  */
  331                 if ((object->ref_count == 2) && (object->shadow_count == 0)) {
  332                         vm_object_set_flag(object, OBJ_ONEMAPPING);
  333                         object->ref_count--;
  334                         return;
  335                 } else if ((object->ref_count == 2) && (object->shadow_count == 1)) {
  336                         object->ref_count--;
  337                         if ((object->handle == NULL) &&
  338                             (object->type == OBJT_DEFAULT ||
  339                              object->type == OBJT_SWAP)) {
  340                                 vm_object_t robject;
  341 
  342                                 robject = LIST_FIRST(&object->shadow_head);
  343                                 KASSERT(robject != NULL,
  344                                     ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
  345                                          object->ref_count,
  346                                          object->shadow_count));
  347                                 if ((robject->handle == NULL) &&
  348                                     (robject->type == OBJT_DEFAULT ||
  349                                      robject->type == OBJT_SWAP)) {
  350 
  351                                         robject->ref_count++;
  352 
  353                                         while (
  354                                                 robject->paging_in_progress ||
  355                                                 object->paging_in_progress
  356                                         ) {
  357                                                 vm_object_pip_sleep(robject, "objde1");
  358                                                 vm_object_pip_sleep(object, "objde2");
  359                                         }
  360 
  361                                         if (robject->ref_count == 1) {
  362                                                 robject->ref_count--;
  363                                                 object = robject;
  364                                                 goto doterm;
  365                                         }
  366 
  367                                         object = robject;
  368                                         vm_object_collapse(object);
  369                                         continue;
  370                                 }
  371                         }
  372 
  373                         return;
  374 
  375                 } else {
  376                         object->ref_count--;
  377                         if (object->ref_count != 0)
  378                                 return;
  379                 }
  380 
  381 doterm:
  382 
  383                 temp = object->backing_object;
  384                 if (temp) {
  385                         LIST_REMOVE(object, shadow_list);
  386                         temp->shadow_count--;
  387                         if (temp->ref_count == 0)
  388                                 vm_object_clear_flag(temp, OBJ_OPT);
  389                         temp->generation++;
  390                         object->backing_object = NULL;
  391                 }
  392 
  393                 /*
  394                  * Don't double-terminate, we could be in a termination
  395                  * recursion due to the terminate having to sync data
  396                  * to disk.
  397                  */
  398                 if ((object->flags & OBJ_DEAD) == 0)
  399                         vm_object_terminate(object);
  400                 object = temp;
  401         }
  402 }
  403 
  404 /*
  405  *      vm_object_terminate actually destroys the specified object, freeing
  406  *      up all previously used resources.
  407  *
  408  *      The object must be locked.
  409  *      This routine may block.
  410  */
  411 void
  412 vm_object_terminate(object)
  413         vm_object_t object;
  414 {
  415         vm_page_t p;
  416         int s;
  417 
  418         /*
  419          * Make sure no one uses us.
  420          */
  421         vm_object_set_flag(object, OBJ_DEAD);
  422 
  423         /*
  424          * wait for the pageout daemon to be done with the object
  425          */
  426         vm_object_pip_wait(object, "objtrm");
  427 
  428         KASSERT(!object->paging_in_progress,
  429                 ("vm_object_terminate: pageout in progress"));
  430 
  431         /*
  432          * Clean and free the pages, as appropriate. All references to the
  433          * object are gone, so we don't need to lock it.
  434          */
  435         if (object->type == OBJT_VNODE) {
  436                 struct vnode *vp;
  437 
  438                 /*
  439                  * Freeze optimized copies.
  440                  */
  441                 vm_freeze_copyopts(object, 0, object->size);
  442 
  443                 /*
  444                  * Clean pages and flush buffers.
  445                  */
  446                 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
  447 
  448                 vp = (struct vnode *) object->handle;
  449                 vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
  450         }
  451 
  452         /*
  453          * Wait for any I/O to complete, after which there had better not
  454          * be any references left on the object.
  455          */
  456         vm_object_pip_wait(object, "objtrm");
  457 
  458         if (object->ref_count != 0)
  459                 panic("vm_object_terminate: object with references, ref_count=%d", object->ref_count);
  460 
  461         /*
  462          * Now free any remaining pages. For internal objects, this also
  463          * removes them from paging queues. Don't free wired pages, just
  464          * remove them from the object. 
  465          */
  466         s = splvm();
  467         while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
  468                 if (p->busy || (p->flags & PG_BUSY))
  469                         panic("vm_object_terminate: freeing busy page %p\n", p);
  470                 if (p->wire_count == 0) {
  471                         vm_page_busy(p);
  472                         vm_page_free(p);
  473                         cnt.v_pfree++;
  474                 } else {
  475                         vm_page_busy(p);
  476                         vm_page_remove(p);
  477                 }
  478         }
  479         splx(s);
  480 
  481         /*
  482          * Let the pager know object is dead.
  483          */
  484         vm_pager_deallocate(object);
  485 
  486         /*
  487          * Remove the object from the global object list.
  488          */
  489         simple_lock(&vm_object_list_lock);
  490         TAILQ_REMOVE(&vm_object_list, object, object_list);
  491         simple_unlock(&vm_object_list_lock);
  492 
  493         wakeup(object);
  494 
  495         /*
  496          * Free the space for the object.
  497          */
  498         zfree(obj_zone, object);
  499 }
  500 
  501 /*
  502  *      vm_object_page_clean
  503  *
  504  *      Clean all dirty pages in the specified range of object.  Leaves page 
  505  *      on whatever queue it is currently on.   If NOSYNC is set then do not
  506  *      write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
  507  *      leaving the object dirty.
  508  *
  509  *      When stuffing pages asynchronously, allow clustering.  XXX we need a
  510  *      synchronous clustering mode implementation.
  511  *
  512  *      Odd semantics: if start == end, we clean everything.
  513  *
  514  *      The object must be locked.
  515  */
  516 
  517 void
  518 vm_object_page_clean(object, start, end, flags)
  519         vm_object_t object;
  520         vm_pindex_t start;
  521         vm_pindex_t end;
  522         int flags;
  523 {
  524         vm_page_t p, np;
  525         vm_offset_t tstart, tend;
  526         vm_pindex_t pi;
  527         struct vnode *vp;
  528         int clearobjflags;
  529         int pagerflags;
  530         int curgeneration;
  531 
  532         if (object->type != OBJT_VNODE ||
  533                 (object->flags & OBJ_MIGHTBEDIRTY) == 0)
  534                 return;
  535 
  536         pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
  537         pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
  538 
  539         vp = object->handle;
  540 
  541         vm_object_set_flag(object, OBJ_CLEANING);
  542 
  543         /*
  544          * Handle 'entire object' case
  545          */
  546         tstart = start;
  547         if (end == 0) {
  548                 tend = object->size;
  549         } else {
  550                 tend = end;
  551         }
  552 
  553         /*
  554          * If the caller is smart and only msync()s a range he knows is
  555          * dirty, we may be able to avoid an object scan.  This results in
  556          * a phenominal improvement in performance.  We cannot do this
  557          * as a matter of course because the object may be huge - e.g.
  558          * the size might be in the gigabytes or terrabytes.
  559          */
  560         if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) {
  561                 vm_offset_t tscan;
  562                 int scanlimit;
  563                 int scanreset;
  564 
  565                 scanreset = object->resident_page_count / EASY_SCAN_FACTOR;
  566                 if (scanreset < 16)
  567                         scanreset = 16;
  568                 pagerflags |= VM_PAGER_IGNORE_CLEANCHK;
  569 
  570                 scanlimit = scanreset;
  571                 tscan = tstart;
  572                 while (tscan < tend) {
  573                         curgeneration = object->generation;
  574                         p = vm_page_lookup(object, tscan);
  575                         if (p == NULL || p->valid == 0 ||
  576                             (p->queue - p->pc) == PQ_CACHE) {
  577                                 if (--scanlimit == 0)
  578                                         break;
  579                                 ++tscan;
  580                                 continue;
  581                         }
  582                         vm_page_test_dirty(p);
  583                         if ((p->dirty & p->valid) == 0) {
  584                                 if (--scanlimit == 0)
  585                                         break;
  586                                 ++tscan;
  587                                 continue;
  588                         }
  589                         /*
  590                          * If we have been asked to skip nosync pages and 
  591                          * this is a nosync page, we can't continue.
  592                          */
  593                         if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
  594                                 if (--scanlimit == 0)
  595                                         break;
  596                                 ++tscan;
  597                                 continue;
  598                         }
  599                         scanlimit = scanreset;
  600 
  601                         /*
  602                          * This returns 0 if it was unable to busy the first
  603                          * page (i.e. had to sleep).
  604                          */
  605                         tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags);
  606                 }
  607 
  608                 /*
  609                  * If everything was dirty and we flushed it successfully,
  610                  * and the requested range is not the entire object, we
  611                  * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can
  612                  * return immediately.
  613                  */
  614                 if (tscan >= tend && (tstart || tend < object->size)) {
  615                         vm_object_clear_flag(object, OBJ_CLEANING);
  616                         return;
  617                 }
  618                 pagerflags &= ~VM_PAGER_IGNORE_CLEANCHK;
  619         }
  620 
  621         /*
  622          * Generally set CLEANCHK interlock and make the page read-only so
  623          * we can then clear the object flags.
  624          *
  625          * However, if this is a nosync mmap then the object is likely to 
  626          * stay dirty so do not mess with the page and do not clear the
  627          * object flags.
  628          */
  629 
  630         clearobjflags = 1;
  631 
  632         for(p = TAILQ_FIRST(&object->memq); p; p = TAILQ_NEXT(p, listq)) {
  633                 vm_page_flag_set(p, PG_CLEANCHK);
  634                 if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC))
  635                         clearobjflags = 0;
  636                 else
  637                         vm_page_protect(p, VM_PROT_READ);
  638         }
  639 
  640         if (clearobjflags && (tstart == 0) && (tend == object->size)) {
  641                 struct vnode *vp;
  642 
  643                 vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
  644                 if (object->type == OBJT_VNODE &&
  645                     (vp = (struct vnode *)object->handle) != NULL) {
  646                         if (vp->v_flag & VOBJDIRTY) {
  647                                 simple_lock(&vp->v_interlock);
  648                                 vp->v_flag &= ~VOBJDIRTY;
  649                                 simple_unlock(&vp->v_interlock);
  650                         }
  651                 }
  652         }
  653 
  654 rescan:
  655         curgeneration = object->generation;
  656 
  657         for(p = TAILQ_FIRST(&object->memq); p; p = np) {
  658                 int n;
  659 
  660                 np = TAILQ_NEXT(p, listq);
  661 
  662 again:
  663                 pi = p->pindex;
  664                 if (((p->flags & PG_CLEANCHK) == 0) ||
  665                         (pi < tstart) || (pi >= tend) ||
  666                         (p->valid == 0) ||
  667                         ((p->queue - p->pc) == PQ_CACHE)) {
  668                         vm_page_flag_clear(p, PG_CLEANCHK);
  669                         continue;
  670                 }
  671 
  672                 vm_page_test_dirty(p);
  673                 if ((p->dirty & p->valid) == 0) {
  674                         vm_page_flag_clear(p, PG_CLEANCHK);
  675                         continue;
  676                 }
  677 
  678                 /*
  679                  * If we have been asked to skip nosync pages and this is a
  680                  * nosync page, skip it.  Note that the object flags were
  681                  * not cleared in this case so we do not have to set them.
  682                  */
  683                 if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
  684                         vm_page_flag_clear(p, PG_CLEANCHK);
  685                         continue;
  686                 }
  687 
  688                 n = vm_object_page_collect_flush(object, p,
  689                         curgeneration, pagerflags);
  690                 if (n == 0)
  691                         goto rescan;
  692                 if (object->generation != curgeneration)
  693                         goto rescan;
  694 
  695                 /*
  696                  * Try to optimize the next page.  If we can't we pick up
  697                  * our (random) scan where we left off.
  698                  */
  699                 if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) {
  700                         if ((p = vm_page_lookup(object, pi + n)) != NULL)
  701                                 goto again;
  702                 }
  703         }
  704 
  705 #if 0
  706         VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
  707 #endif
  708 
  709         vm_object_clear_flag(object, OBJ_CLEANING);
  710         return;
  711 }
  712 
  713 static int
  714 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags)
  715 {
  716         int runlen;
  717         int s;
  718         int maxf;
  719         int chkb;
  720         int maxb;
  721         int i;
  722         vm_pindex_t pi;
  723         vm_page_t maf[vm_pageout_page_count];
  724         vm_page_t mab[vm_pageout_page_count];
  725         vm_page_t ma[vm_pageout_page_count];
  726 
  727         s = splvm();
  728         pi = p->pindex;
  729         while (vm_page_sleep_busy(p, TRUE, "vpcwai")) {
  730                 if (object->generation != curgeneration) {
  731                         splx(s);
  732                         return(0);
  733                 }
  734         }
  735 
  736         maxf = 0;
  737         for(i = 1; i < vm_pageout_page_count; i++) {
  738                 vm_page_t tp;
  739 
  740                 if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
  741                         if ((tp->flags & PG_BUSY) ||
  742                                 ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 && 
  743                                  (tp->flags & PG_CLEANCHK) == 0) ||
  744                                 (tp->busy != 0))
  745                                 break;
  746                         if((tp->queue - tp->pc) == PQ_CACHE) {
  747                                 vm_page_flag_clear(tp, PG_CLEANCHK);
  748                                 break;
  749                         }
  750                         vm_page_test_dirty(tp);
  751                         if ((tp->dirty & tp->valid) == 0) {
  752                                 vm_page_flag_clear(tp, PG_CLEANCHK);
  753                                 break;
  754                         }
  755                         maf[ i - 1 ] = tp;
  756                         maxf++;
  757                         continue;
  758                 }
  759                 break;
  760         }
  761 
  762         maxb = 0;
  763         chkb = vm_pageout_page_count -  maxf;
  764         if (chkb) {
  765                 for(i = 1; i < chkb;i++) {
  766                         vm_page_t tp;
  767 
  768                         if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
  769                                 if ((tp->flags & PG_BUSY) ||
  770                                         ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 && 
  771                                          (tp->flags & PG_CLEANCHK) == 0) ||
  772                                         (tp->busy != 0))
  773                                         break;
  774                                 if((tp->queue - tp->pc) == PQ_CACHE) {
  775                                         vm_page_flag_clear(tp, PG_CLEANCHK);
  776                                         break;
  777                                 }
  778                                 vm_page_test_dirty(tp);
  779                                 if ((tp->dirty & tp->valid) == 0) {
  780                                         vm_page_flag_clear(tp, PG_CLEANCHK);
  781                                         break;
  782                                 }
  783                                 mab[ i - 1 ] = tp;
  784                                 maxb++;
  785                                 continue;
  786                         }
  787                         break;
  788                 }
  789         }
  790 
  791         for(i = 0; i < maxb; i++) {
  792                 int index = (maxb - i) - 1;
  793                 ma[index] = mab[i];
  794                 vm_page_flag_clear(ma[index], PG_CLEANCHK);
  795         }
  796         vm_page_flag_clear(p, PG_CLEANCHK);
  797         ma[maxb] = p;
  798         for(i = 0; i < maxf; i++) {
  799                 int index = (maxb + i) + 1;
  800                 ma[index] = maf[i];
  801                 vm_page_flag_clear(ma[index], PG_CLEANCHK);
  802         }
  803         runlen = maxb + maxf + 1;
  804 
  805         splx(s);
  806         vm_pageout_flush(ma, runlen, pagerflags);
  807         for (i = 0; i < runlen; i++) {
  808                 if (ma[i]->valid & ma[i]->dirty) {
  809                         vm_page_protect(ma[i], VM_PROT_READ);
  810                         vm_page_flag_set(ma[i], PG_CLEANCHK);
  811 
  812                         /*
  813                          * maxf will end up being the actual number of pages
  814                          * we wrote out contiguously, non-inclusive of the
  815                          * first page.  We do not count look-behind pages.
  816                          */
  817                         if (i >= maxb + 1 && (maxf > i - maxb - 1))
  818                                 maxf = i - maxb - 1;
  819                 }
  820         }
  821         return(maxf + 1);
  822 }
  823 
  824 #ifdef not_used
  825 /* XXX I cannot tell if this should be an exported symbol */
  826 /*
  827  *      vm_object_deactivate_pages
  828  *
  829  *      Deactivate all pages in the specified object.  (Keep its pages
  830  *      in memory even though it is no longer referenced.)
  831  *
  832  *      The object must be locked.
  833  */
  834 static void
  835 vm_object_deactivate_pages(object)
  836         vm_object_t object;
  837 {
  838         vm_page_t p, next;
  839 
  840         for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
  841                 next = TAILQ_NEXT(p, listq);
  842                 vm_page_deactivate(p);
  843         }
  844 }
  845 #endif
  846 
  847 /*
  848  * Same as vm_object_pmap_copy, except range checking really
  849  * works, and is meant for small sections of an object.
  850  *
  851  * This code protects resident pages by making them read-only
  852  * and is typically called on a fork or split when a page
  853  * is converted to copy-on-write.  
  854  *
  855  * NOTE: If the page is already at VM_PROT_NONE, calling
  856  * vm_page_protect will have no effect.
  857  */
  858 
  859 void
  860 vm_object_pmap_copy_1(object, start, end)
  861         vm_object_t object;
  862         vm_pindex_t start;
  863         vm_pindex_t end;
  864 {
  865         vm_pindex_t idx;
  866         vm_page_t p;
  867 
  868         if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
  869                 return;
  870 
  871         for (idx = start; idx < end; idx++) {
  872                 p = vm_page_lookup(object, idx);
  873                 if (p == NULL)
  874                         continue;
  875                 vm_page_protect(p, VM_PROT_READ);
  876         }
  877 }
  878 
  879 /*
  880  *      vm_object_pmap_remove:
  881  *
  882  *      Removes all physical pages in the specified
  883  *      object range from all physical maps.
  884  *
  885  *      The object must *not* be locked.
  886  */
  887 void
  888 vm_object_pmap_remove(object, start, end)
  889         vm_object_t object;
  890         vm_pindex_t start;
  891         vm_pindex_t end;
  892 {
  893         vm_page_t p;
  894 
  895         if (object == NULL)
  896                 return;
  897         for (p = TAILQ_FIRST(&object->memq);
  898                 p != NULL;
  899                 p = TAILQ_NEXT(p, listq)) {
  900                 if (p->pindex >= start && p->pindex < end)
  901                         vm_page_protect(p, VM_PROT_NONE);
  902         }
  903         if ((start == 0) && (object->size == end))
  904                 vm_object_clear_flag(object, OBJ_WRITEABLE);
  905 }
  906 
  907 /*
  908  *      vm_object_madvise:
  909  *
  910  *      Implements the madvise function at the object/page level.
  911  *
  912  *      MADV_WILLNEED   (any object)
  913  *
  914  *          Activate the specified pages if they are resident.
  915  *
  916  *      MADV_DONTNEED   (any object)
  917  *
  918  *          Deactivate the specified pages if they are resident.
  919  *
  920  *      MADV_FREE       (OBJT_DEFAULT/OBJT_SWAP objects,
  921  *                       OBJ_ONEMAPPING only)
  922  *
  923  *          Deactivate and clean the specified pages if they are
  924  *          resident.  This permits the process to reuse the pages
  925  *          without faulting or the kernel to reclaim the pages
  926  *          without I/O.
  927  */
  928 void
  929 vm_object_madvise(object, pindex, count, advise)
  930         vm_object_t object;
  931         vm_pindex_t pindex;
  932         int count;
  933         int advise;
  934 {
  935         vm_pindex_t end, tpindex;
  936         vm_object_t tobject;
  937         vm_page_t m;
  938 
  939         if (object == NULL)
  940                 return;
  941 
  942         end = pindex + count;
  943 
  944         /*
  945          * Locate and adjust resident pages
  946          */
  947 
  948         for (; pindex < end; pindex += 1) {
  949 relookup:
  950                 tobject = object;
  951                 tpindex = pindex;
  952 shadowlookup:
  953                 /*
  954                  * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
  955                  * and those pages must be OBJ_ONEMAPPING.
  956                  */
  957                 if (advise == MADV_FREE) {
  958                         if ((tobject->type != OBJT_DEFAULT &&
  959                              tobject->type != OBJT_SWAP) ||
  960                             (tobject->flags & OBJ_ONEMAPPING) == 0) {
  961                                 continue;
  962                         }
  963                 }
  964 
  965                 m = vm_page_lookup(tobject, tpindex);
  966 
  967                 if (m == NULL) {
  968                         /*
  969                          * There may be swap even if there is no backing page
  970                          */
  971                         if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
  972                                 swap_pager_freespace(tobject, tpindex, 1);
  973 
  974                         /*
  975                          * next object
  976                          */
  977                         if (tobject->backing_object == NULL)
  978                                 continue;
  979                         tpindex += OFF_TO_IDX(tobject->backing_object_offset);
  980                         tobject = tobject->backing_object;
  981                         goto shadowlookup;
  982                 }
  983 
  984                 /*
  985                  * If the page is busy or not in a normal active state,
  986                  * we skip it.  If the page is not managed there are no
  987                  * page queues to mess with.  Things can break if we mess
  988                  * with pages in any of the below states.
  989                  */
  990                 if (
  991                     m->hold_count ||
  992                     m->wire_count ||
  993                     (m->flags & PG_UNMANAGED) ||
  994                     m->valid != VM_PAGE_BITS_ALL
  995                 ) {
  996                         continue;
  997                 }
  998 
  999                 if (vm_page_sleep_busy(m, TRUE, "madvpo"))
 1000                         goto relookup;
 1001 
 1002                 if (advise == MADV_WILLNEED) {
 1003                         vm_page_activate(m);
 1004                 } else if (advise == MADV_DONTNEED) {
 1005                         vm_page_dontneed(m);
 1006                 } else if (advise == MADV_FREE) {
 1007                         /*
 1008                          * Mark the page clean.  This will allow the page
 1009                          * to be freed up by the system.  However, such pages
 1010                          * are often reused quickly by malloc()/free()
 1011                          * so we do not do anything that would cause
 1012                          * a page fault if we can help it.
 1013                          *
 1014                          * Specifically, we do not try to actually free
 1015                          * the page now nor do we try to put it in the
 1016                          * cache (which would cause a page fault on reuse).
 1017                          *
 1018                          * But we do make the page is freeable as we
 1019                          * can without actually taking the step of unmapping
 1020                          * it.
 1021                          */
 1022                         pmap_clear_modify(m);
 1023                         m->dirty = 0;
 1024                         m->act_count = 0;
 1025                         vm_page_dontneed(m);
 1026                         if (tobject->type == OBJT_SWAP)
 1027                                 swap_pager_freespace(tobject, tpindex, 1);
 1028                 }
 1029         }       
 1030 }
 1031 
 1032 /*
 1033  *      vm_object_shadow:
 1034  *
 1035  *      Create a new object which is backed by the
 1036  *      specified existing object range.  The source
 1037  *      object reference is deallocated.
 1038  *
 1039  *      The new object and offset into that object
 1040  *      are returned in the source parameters.
 1041  */
 1042 
 1043 void
 1044 vm_object_shadow(object, offset, length)
 1045         vm_object_t *object;    /* IN/OUT */
 1046         vm_ooffset_t *offset;   /* IN/OUT */
 1047         vm_size_t length;
 1048 {
 1049         vm_object_t source;
 1050         vm_object_t result;
 1051 
 1052         source = *object;
 1053 
 1054         /*
 1055          * Don't create the new object if the old object isn't shared.
 1056          */
 1057 
 1058         if (source != NULL &&
 1059             source->ref_count == 1 &&
 1060             source->handle == NULL &&
 1061             (source->type == OBJT_DEFAULT ||
 1062              source->type == OBJT_SWAP))
 1063                 return;
 1064 
 1065         /*
 1066          * Allocate a new object with the given length
 1067          */
 1068 
 1069         if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
 1070                 panic("vm_object_shadow: no object for shadowing");
 1071 
 1072         /*
 1073          * The new object shadows the source object, adding a reference to it.
 1074          * Our caller changes his reference to point to the new object,
 1075          * removing a reference to the source object.  Net result: no change
 1076          * of reference count.
 1077          *
 1078          * Try to optimize the result object's page color when shadowing
 1079          * in order to maintain page coloring consistency in the combined 
 1080          * shadowed object.
 1081          */
 1082         result->backing_object = source;
 1083         if (source) {
 1084                 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
 1085                 source->shadow_count++;
 1086                 source->generation++;
 1087                 result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & PQ_L2_MASK;
 1088         }
 1089 
 1090         /*
 1091          * Store the offset into the source object, and fix up the offset into
 1092          * the new object.
 1093          */
 1094 
 1095         result->backing_object_offset = *offset;
 1096 
 1097         /*
 1098          * Return the new things
 1099          */
 1100 
 1101         *offset = 0;
 1102         *object = result;
 1103 }
 1104 
 1105 #define OBSC_TEST_ALL_SHADOWED  0x0001
 1106 #define OBSC_COLLAPSE_NOWAIT    0x0002
 1107 #define OBSC_COLLAPSE_WAIT      0x0004
 1108 
 1109 static __inline int
 1110 vm_object_backing_scan(vm_object_t object, int op)
 1111 {
 1112         int s;
 1113         int r = 1;
 1114         vm_page_t p;
 1115         vm_object_t backing_object;
 1116         vm_pindex_t backing_offset_index;
 1117 
 1118         s = splvm();
 1119 
 1120         backing_object = object->backing_object;
 1121         backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
 1122 
 1123         /*
 1124          * Initial conditions
 1125          */
 1126 
 1127         if (op & OBSC_TEST_ALL_SHADOWED) {
 1128                 /*
 1129                  * We do not want to have to test for the existence of
 1130                  * swap pages in the backing object.  XXX but with the
 1131                  * new swapper this would be pretty easy to do.
 1132                  *
 1133                  * XXX what about anonymous MAP_SHARED memory that hasn't
 1134                  * been ZFOD faulted yet?  If we do not test for this, the
 1135                  * shadow test may succeed! XXX
 1136                  */
 1137                 if (backing_object->type != OBJT_DEFAULT) {
 1138                         splx(s);
 1139                         return(0);
 1140                 }
 1141         }
 1142         if (op & OBSC_COLLAPSE_WAIT) {
 1143                 vm_object_set_flag(backing_object, OBJ_DEAD);
 1144         }
 1145 
 1146         /*
 1147          * Our scan
 1148          */
 1149 
 1150         p = TAILQ_FIRST(&backing_object->memq);
 1151         while (p) {
 1152                 vm_page_t next = TAILQ_NEXT(p, listq);
 1153                 vm_pindex_t new_pindex = p->pindex - backing_offset_index;
 1154 
 1155                 if (op & OBSC_TEST_ALL_SHADOWED) {
 1156                         vm_page_t pp;
 1157 
 1158                         /*
 1159                          * Ignore pages outside the parent object's range
 1160                          * and outside the parent object's mapping of the 
 1161                          * backing object.
 1162                          *
 1163                          * note that we do not busy the backing object's
 1164                          * page.
 1165                          */
 1166 
 1167                         if (
 1168                             p->pindex < backing_offset_index ||
 1169                             new_pindex >= object->size
 1170                         ) {
 1171                                 p = next;
 1172                                 continue;
 1173                         }
 1174 
 1175                         /*
 1176                          * See if the parent has the page or if the parent's
 1177                          * object pager has the page.  If the parent has the
 1178                          * page but the page is not valid, the parent's
 1179                          * object pager must have the page.
 1180                          *
 1181                          * If this fails, the parent does not completely shadow
 1182                          * the object and we might as well give up now.
 1183                          */
 1184 
 1185                         pp = vm_page_lookup(object, new_pindex);
 1186                         if (
 1187                             (pp == NULL || pp->valid == 0) &&
 1188                             !vm_pager_has_page(object, new_pindex, NULL, NULL)
 1189                         ) {
 1190                                 r = 0;
 1191                                 break;
 1192                         }
 1193                 }
 1194 
 1195                 /*
 1196                  * Check for busy page
 1197                  */
 1198 
 1199                 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
 1200                         vm_page_t pp;
 1201 
 1202                         if (op & OBSC_COLLAPSE_NOWAIT) {
 1203                                 if (
 1204                                     (p->flags & PG_BUSY) ||
 1205                                     !p->valid || 
 1206                                     p->hold_count || 
 1207                                     p->wire_count ||
 1208                                     p->busy
 1209                                 ) {
 1210                                         p = next;
 1211                                         continue;
 1212                                 }
 1213                         } else if (op & OBSC_COLLAPSE_WAIT) {
 1214                                 if (vm_page_sleep_busy(p, TRUE, "vmocol")) {
 1215                                         /*
 1216                                          * If we slept, anything could have
 1217                                          * happened.  Since the object is
 1218                                          * marked dead, the backing offset
 1219                                          * should not have changed so we
 1220                                          * just restart our scan.
 1221                                          */
 1222                                         p = TAILQ_FIRST(&backing_object->memq);
 1223                                         continue;
 1224                                 }
 1225                         }
 1226 
 1227                         /* 
 1228                          * Busy the page
 1229                          */
 1230                         vm_page_busy(p);
 1231 
 1232                         KASSERT(
 1233                             p->object == backing_object,
 1234                             ("vm_object_qcollapse(): object mismatch")
 1235                         );
 1236 
 1237                         /*
 1238                          * Destroy any associated swap
 1239                          */
 1240                         if (backing_object->type == OBJT_SWAP) {
 1241                                 swap_pager_freespace(
 1242                                     backing_object, 
 1243                                     p->pindex,
 1244                                     1
 1245                                 );
 1246                         }
 1247 
 1248                         if (
 1249                             p->pindex < backing_offset_index ||
 1250                             new_pindex >= object->size
 1251                         ) {
 1252                                 /*
 1253                                  * Page is out of the parent object's range, we 
 1254                                  * can simply destroy it. 
 1255                                  */
 1256                                 vm_page_protect(p, VM_PROT_NONE);
 1257                                 vm_page_free(p);
 1258                                 p = next;
 1259                                 continue;
 1260                         }
 1261 
 1262                         pp = vm_page_lookup(object, new_pindex);
 1263                         if (
 1264                             pp != NULL ||
 1265                             vm_pager_has_page(object, new_pindex, NULL, NULL)
 1266                         ) {
 1267                                 /*
 1268                                  * page already exists in parent OR swap exists
 1269                                  * for this location in the parent.  Destroy 
 1270                                  * the original page from the backing object.
 1271                                  *
 1272                                  * Leave the parent's page alone
 1273                                  */
 1274                                 vm_page_protect(p, VM_PROT_NONE);
 1275                                 vm_page_free(p);
 1276                                 p = next;
 1277                                 continue;
 1278                         }
 1279 
 1280                         /*
 1281                          * Page does not exist in parent, rename the
 1282                          * page from the backing object to the main object. 
 1283                          *
 1284                          * If the page was mapped to a process, it can remain 
 1285                          * mapped through the rename.
 1286                          */
 1287                         if ((p->queue - p->pc) == PQ_CACHE)
 1288                                 vm_page_deactivate(p);
 1289 
 1290                         vm_page_rename(p, object, new_pindex);
 1291                         /* page automatically made dirty by rename */
 1292                 }
 1293                 p = next;
 1294         }
 1295         splx(s);
 1296         return(r);
 1297 }
 1298 
 1299 
 1300 /*
 1301  * this version of collapse allows the operation to occur earlier and
 1302  * when paging_in_progress is true for an object...  This is not a complete
 1303  * operation, but should plug 99.9% of the rest of the leaks.
 1304  */
 1305 static void
 1306 vm_object_qcollapse(object)
 1307         vm_object_t object;
 1308 {
 1309         vm_object_t backing_object = object->backing_object;
 1310 
 1311         if (backing_object->ref_count != 1)
 1312                 return;
 1313 
 1314         backing_object->ref_count += 2;
 1315 
 1316         vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
 1317 
 1318         backing_object->ref_count -= 2;
 1319 }
 1320 
 1321 /*
 1322  *      vm_object_collapse:
 1323  *
 1324  *      Collapse an object with the object backing it.
 1325  *      Pages in the backing object are moved into the
 1326  *      parent, and the backing object is deallocated.
 1327  */
 1328 void
 1329 vm_object_collapse(object)
 1330         vm_object_t object;
 1331 {
 1332         while (TRUE) {
 1333                 vm_object_t backing_object;
 1334 
 1335                 /*
 1336                  * Verify that the conditions are right for collapse:
 1337                  *
 1338                  * The object exists and the backing object exists.
 1339                  */
 1340                 if (object == NULL)
 1341                         break;
 1342 
 1343                 if ((backing_object = object->backing_object) == NULL)
 1344                         break;
 1345 
 1346                 /*
 1347                  * we check the backing object first, because it is most likely
 1348                  * not collapsable.
 1349                  */
 1350                 if (backing_object->handle != NULL ||
 1351                     (backing_object->type != OBJT_DEFAULT &&
 1352                      backing_object->type != OBJT_SWAP) ||
 1353                     (backing_object->flags & OBJ_DEAD) ||
 1354                     object->handle != NULL ||
 1355                     (object->type != OBJT_DEFAULT &&
 1356                      object->type != OBJT_SWAP) ||
 1357                     (object->flags & OBJ_DEAD)) {
 1358                         break;
 1359                 }
 1360 
 1361                 if (
 1362                     object->paging_in_progress != 0 ||
 1363                     backing_object->paging_in_progress != 0
 1364                 ) {
 1365                         vm_object_qcollapse(object);
 1366                         break;
 1367                 }
 1368 
 1369                 /*
 1370                  * We know that we can either collapse the backing object (if
 1371                  * the parent is the only reference to it) or (perhaps) have
 1372                  * the parent bypass the object if the parent happens to shadow
 1373                  * all the resident pages in the entire backing object.
 1374                  *
 1375                  * This is ignoring pager-backed pages such as swap pages.
 1376                  * vm_object_backing_scan fails the shadowing test in this
 1377                  * case.
 1378                  */
 1379 
 1380                 if (backing_object->ref_count == 1) {
 1381                         /*
 1382                          * If there is exactly one reference to the backing
 1383                          * object, we can collapse it into the parent.  
 1384                          */
 1385 
 1386                         vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
 1387 
 1388                         /*
 1389                          * Move the pager from backing_object to object.
 1390                          */
 1391 
 1392                         if (backing_object->type == OBJT_SWAP) {
 1393                                 vm_object_pip_add(backing_object, 1);
 1394 
 1395                                 /*
 1396                                  * scrap the paging_offset junk and do a 
 1397                                  * discrete copy.  This also removes major 
 1398                                  * assumptions about how the swap-pager 
 1399                                  * works from where it doesn't belong.  The
 1400                                  * new swapper is able to optimize the
 1401                                  * destroy-source case.
 1402                                  */
 1403 
 1404                                 vm_object_pip_add(object, 1);
 1405                                 swap_pager_copy(
 1406                                     backing_object,
 1407                                     object,
 1408                                     OFF_TO_IDX(object->backing_object_offset), TRUE);
 1409                                 vm_object_pip_wakeup(object);
 1410 
 1411                                 vm_object_pip_wakeup(backing_object);
 1412                         }
 1413                         /*
 1414                          * Object now shadows whatever backing_object did.
 1415                          * Note that the reference to 
 1416                          * backing_object->backing_object moves from within 
 1417                          * backing_object to within object.
 1418                          */
 1419 
 1420                         LIST_REMOVE(object, shadow_list);
 1421                         object->backing_object->shadow_count--;
 1422                         object->backing_object->generation++;
 1423                         if (backing_object->backing_object) {
 1424                                 LIST_REMOVE(backing_object, shadow_list);
 1425                                 backing_object->backing_object->shadow_count--;
 1426                                 backing_object->backing_object->generation++;
 1427                         }
 1428                         object->backing_object = backing_object->backing_object;
 1429                         if (object->backing_object) {
 1430                                 LIST_INSERT_HEAD(
 1431                                     &object->backing_object->shadow_head,
 1432                                     object, 
 1433                                     shadow_list
 1434                                 );
 1435                                 object->backing_object->shadow_count++;
 1436                                 object->backing_object->generation++;
 1437                         }
 1438 
 1439                         object->backing_object_offset +=
 1440                             backing_object->backing_object_offset;
 1441 
 1442                         /*
 1443                          * Discard backing_object.
 1444                          *
 1445                          * Since the backing object has no pages, no pager left,
 1446                          * and no object references within it, all that is
 1447                          * necessary is to dispose of it.
 1448                          */
 1449 
 1450                         KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
 1451                         KASSERT(TAILQ_FIRST(&backing_object->memq) == NULL, ("backing_object %p somehow has left over pages during collapse!", backing_object));
 1452                         TAILQ_REMOVE(
 1453                             &vm_object_list, 
 1454                             backing_object,
 1455                             object_list
 1456                         );
 1457                         vm_object_count--;
 1458 
 1459                         zfree(obj_zone, backing_object);
 1460 
 1461                         object_collapses++;
 1462                 } else {
 1463                         vm_object_t new_backing_object;
 1464 
 1465                         /*
 1466                          * If we do not entirely shadow the backing object,
 1467                          * there is nothing we can do so we give up.
 1468                          */
 1469 
 1470                         if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) {
 1471                                 break;
 1472                         }
 1473 
 1474                         /*
 1475                          * Make the parent shadow the next object in the
 1476                          * chain.  Deallocating backing_object will not remove
 1477                          * it, since its reference count is at least 2.
 1478                          */
 1479 
 1480                         LIST_REMOVE(object, shadow_list);
 1481                         backing_object->shadow_count--;
 1482                         backing_object->generation++;
 1483 
 1484                         new_backing_object = backing_object->backing_object;
 1485                         if ((object->backing_object = new_backing_object) != NULL) {
 1486                                 vm_object_reference(new_backing_object);
 1487                                 LIST_INSERT_HEAD(
 1488                                     &new_backing_object->shadow_head,
 1489                                     object,
 1490                                     shadow_list
 1491                                 );
 1492                                 new_backing_object->shadow_count++;
 1493                                 new_backing_object->generation++;
 1494                                 object->backing_object_offset +=
 1495                                         backing_object->backing_object_offset;
 1496                         }
 1497 
 1498                         /*
 1499                          * Drop the reference count on backing_object. Since
 1500                          * its ref_count was at least 2, it will not vanish;
 1501                          * so we don't need to call vm_object_deallocate, but
 1502                          * we do anyway.
 1503                          */
 1504                         vm_object_deallocate(backing_object);
 1505                         object_bypasses++;
 1506                 }
 1507 
 1508                 /*
 1509                  * Try again with this object's new backing object.
 1510                  */
 1511         }
 1512 }
 1513 
 1514 /*
 1515  *      vm_object_page_remove: [internal]
 1516  *
 1517  *      Removes all physical pages in the specified
 1518  *      object range from the object's list of pages.
 1519  *
 1520  *      The object must be locked.
 1521  */
 1522 void
 1523 vm_object_page_remove(object, start, end, clean_only)
 1524         vm_object_t object;
 1525         vm_pindex_t start;
 1526         vm_pindex_t end;
 1527         boolean_t clean_only;
 1528 {
 1529         vm_page_t p, next;
 1530         unsigned int size;
 1531         int all;
 1532 
 1533         if (object == NULL ||
 1534             object->resident_page_count == 0)
 1535                 return;
 1536 
 1537         all = ((end == 0) && (start == 0));
 1538 
 1539         /*
 1540          * Since physically-backed objects do not use managed pages, we can't
 1541          * remove pages from the object (we must instead remove the page
 1542          * references, and then destroy the object).
 1543          */
 1544         KASSERT(object->type != OBJT_PHYS, ("attempt to remove pages from a physical object"));
 1545 
 1546         vm_object_pip_add(object, 1);
 1547 again:
 1548         size = end - start;
 1549         if (all || size > object->resident_page_count / 4) {
 1550                 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
 1551                         next = TAILQ_NEXT(p, listq);
 1552                         if (all || ((start <= p->pindex) && (p->pindex < end))) {
 1553                                 if (p->wire_count != 0) {
 1554                                         vm_page_protect(p, VM_PROT_NONE);
 1555                                         if (!clean_only)
 1556                                                 p->valid = 0;
 1557                                         continue;
 1558                                 }
 1559 
 1560                                 /*
 1561                                  * The busy flags are only cleared at
 1562                                  * interrupt -- minimize the spl transitions
 1563                                  */
 1564 
 1565                                 if (vm_page_sleep_busy(p, TRUE, "vmopar"))
 1566                                         goto again;
 1567 
 1568                                 if (clean_only && p->valid) {
 1569                                         vm_page_test_dirty(p);
 1570                                         if (p->valid & p->dirty)
 1571                                                 continue;
 1572                                 }
 1573 
 1574                                 vm_page_busy(p);
 1575                                 vm_page_protect(p, VM_PROT_NONE);
 1576                                 vm_page_free(p);
 1577                         }
 1578                 }
 1579         } else {
 1580                 while (size > 0) {
 1581                         if ((p = vm_page_lookup(object, start)) != 0) {
 1582 
 1583                                 if (p->wire_count != 0) {
 1584                                         vm_page_protect(p, VM_PROT_NONE);
 1585                                         if (!clean_only)
 1586                                                 p->valid = 0;
 1587                                         start += 1;
 1588                                         size -= 1;
 1589                                         continue;
 1590                                 }
 1591 
 1592                                 /*
 1593                                  * The busy flags are only cleared at
 1594                                  * interrupt -- minimize the spl transitions
 1595                                  */
 1596                                 if (vm_page_sleep_busy(p, TRUE, "vmopar"))
 1597                                         goto again;
 1598 
 1599                                 if (clean_only && p->valid) {
 1600                                         vm_page_test_dirty(p);
 1601                                         if (p->valid & p->dirty) {
 1602                                                 start += 1;
 1603                                                 size -= 1;
 1604                                                 continue;
 1605                                         }
 1606                                 }
 1607 
 1608                                 vm_page_busy(p);
 1609                                 vm_page_protect(p, VM_PROT_NONE);
 1610                                 vm_page_free(p);
 1611                         }
 1612                         start += 1;
 1613                         size -= 1;
 1614                 }
 1615         }
 1616         vm_object_pip_wakeup(object);
 1617 }
 1618 
 1619 /*
 1620  *      Routine:        vm_object_coalesce
 1621  *      Function:       Coalesces two objects backing up adjoining
 1622  *                      regions of memory into a single object.
 1623  *
 1624  *      returns TRUE if objects were combined.
 1625  *
 1626  *      NOTE:   Only works at the moment if the second object is NULL -
 1627  *              if it's not, which object do we lock first?
 1628  *
 1629  *      Parameters:
 1630  *              prev_object     First object to coalesce
 1631  *              prev_offset     Offset into prev_object
 1632  *              next_object     Second object into coalesce
 1633  *              next_offset     Offset into next_object
 1634  *
 1635  *              prev_size       Size of reference to prev_object
 1636  *              next_size       Size of reference to next_object
 1637  *
 1638  *      Conditions:
 1639  *      The object must *not* be locked.
 1640  */
 1641 boolean_t
 1642 vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
 1643         vm_object_t prev_object;
 1644         vm_pindex_t prev_pindex;
 1645         vm_size_t prev_size, next_size;
 1646 {
 1647         vm_pindex_t next_pindex;
 1648 
 1649         if (prev_object == NULL) {
 1650                 return (TRUE);
 1651         }
 1652 
 1653         if (prev_object->type != OBJT_DEFAULT &&
 1654             prev_object->type != OBJT_SWAP) {
 1655                 return (FALSE);
 1656         }
 1657 
 1658         /*
 1659          * Try to collapse the object first
 1660          */
 1661         vm_object_collapse(prev_object);
 1662 
 1663         /*
 1664          * Can't coalesce if: . more than one reference . paged out . shadows
 1665          * another object . has a copy elsewhere (any of which mean that the
 1666          * pages not mapped to prev_entry may be in use anyway)
 1667          */
 1668 
 1669         if (prev_object->backing_object != NULL) {
 1670                 return (FALSE);
 1671         }
 1672 
 1673         prev_size >>= PAGE_SHIFT;
 1674         next_size >>= PAGE_SHIFT;
 1675         next_pindex = prev_pindex + prev_size;
 1676 
 1677         if ((prev_object->ref_count > 1) &&
 1678             (prev_object->size != next_pindex)) {
 1679                 return (FALSE);
 1680         }
 1681 
 1682         /*
 1683          * Remove any pages that may still be in the object from a previous
 1684          * deallocation.
 1685          */
 1686         if (next_pindex < prev_object->size) {
 1687                 vm_object_page_remove(prev_object,
 1688                                       next_pindex,
 1689                                       next_pindex + next_size, FALSE);
 1690                 if (prev_object->type == OBJT_SWAP)
 1691                         swap_pager_freespace(prev_object,
 1692                                              next_pindex, next_size);
 1693         }
 1694 
 1695         /*
 1696          * Extend the object if necessary.
 1697          */
 1698         if (next_pindex + next_size > prev_object->size)
 1699                 prev_object->size = next_pindex + next_size;
 1700 
 1701         return (TRUE);
 1702 }
 1703 
 1704 void
 1705 vm_object_set_writeable_dirty(vm_object_t object)
 1706 {
 1707         struct vnode *vp;
 1708 
 1709         vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
 1710         if (object->type == OBJT_VNODE &&
 1711             (vp = (struct vnode *)object->handle) != NULL) {
 1712                 if ((vp->v_flag & VOBJDIRTY) == 0) {
 1713                         simple_lock(&vp->v_interlock);
 1714                         vp->v_flag |= VOBJDIRTY;
 1715                         simple_unlock(&vp->v_interlock);
 1716                 }
 1717         }
 1718 }
 1719 
 1720 
 1721 
 1722 #include "opt_ddb.h"
 1723 #ifdef DDB
 1724 #include <sys/kernel.h>
 1725 
 1726 #include <sys/cons.h>
 1727 
 1728 #include <ddb/ddb.h>
 1729 
 1730 static int      _vm_object_in_map __P((vm_map_t map, vm_object_t object,
 1731                                        vm_map_entry_t entry));
 1732 static int      vm_object_in_map __P((vm_object_t object));
 1733 
 1734 static int
 1735 _vm_object_in_map(map, object, entry)
 1736         vm_map_t map;
 1737         vm_object_t object;
 1738         vm_map_entry_t entry;
 1739 {
 1740         vm_map_t tmpm;
 1741         vm_map_entry_t tmpe;
 1742         vm_object_t obj;
 1743         int entcount;
 1744 
 1745         if (map == 0)
 1746                 return 0;
 1747 
 1748         if (entry == 0) {
 1749                 tmpe = map->header.next;
 1750                 entcount = map->nentries;
 1751                 while (entcount-- && (tmpe != &map->header)) {
 1752                         if( _vm_object_in_map(map, object, tmpe)) {
 1753                                 return 1;
 1754                         }
 1755                         tmpe = tmpe->next;
 1756                 }
 1757         } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
 1758                 tmpm = entry->object.sub_map;
 1759                 tmpe = tmpm->header.next;
 1760                 entcount = tmpm->nentries;
 1761                 while (entcount-- && tmpe != &tmpm->header) {
 1762                         if( _vm_object_in_map(tmpm, object, tmpe)) {
 1763                                 return 1;
 1764                         }
 1765                         tmpe = tmpe->next;
 1766                 }
 1767         } else if ((obj = entry->object.vm_object) != NULL) {
 1768                 for(; obj; obj=obj->backing_object)
 1769                         if( obj == object) {
 1770                                 return 1;
 1771                         }
 1772         }
 1773         return 0;
 1774 }
 1775 
 1776 static int
 1777 vm_object_in_map( object)
 1778         vm_object_t object;
 1779 {
 1780         struct proc *p;
 1781         for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
 1782                 if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
 1783                         continue;
 1784                 if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
 1785                         return 1;
 1786         }
 1787         if( _vm_object_in_map( kernel_map, object, 0))
 1788                 return 1;
 1789         if( _vm_object_in_map( kmem_map, object, 0))
 1790                 return 1;
 1791         if( _vm_object_in_map( pager_map, object, 0))
 1792                 return 1;
 1793         if( _vm_object_in_map( buffer_map, object, 0))
 1794                 return 1;
 1795         if( _vm_object_in_map( mb_map, object, 0))
 1796                 return 1;
 1797         return 0;
 1798 }
 1799 
 1800 DB_SHOW_COMMAND(vmochk, vm_object_check)
 1801 {
 1802         vm_object_t object;
 1803 
 1804         /*
 1805          * make sure that internal objs are in a map somewhere
 1806          * and none have zero ref counts.
 1807          */
 1808         for (object = TAILQ_FIRST(&vm_object_list);
 1809                         object != NULL;
 1810                         object = TAILQ_NEXT(object, object_list)) {
 1811                 if (object->handle == NULL &&
 1812                     (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
 1813                         if (object->ref_count == 0) {
 1814                                 db_printf("vmochk: internal obj has zero ref count: %ld\n",
 1815                                         (long)object->size);
 1816                         }
 1817                         if (!vm_object_in_map(object)) {
 1818                                 db_printf(
 1819                         "vmochk: internal obj is not in a map: "
 1820                         "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
 1821                                     object->ref_count, (u_long)object->size, 
 1822                                     (u_long)object->size,
 1823                                     (void *)object->backing_object);
 1824                         }
 1825                 }
 1826         }
 1827 }
 1828 
 1829 /*
 1830  *      vm_object_print:        [ debug ]
 1831  */
 1832 DB_SHOW_COMMAND(object, vm_object_print_static)
 1833 {
 1834         /* XXX convert args. */
 1835         vm_object_t object = (vm_object_t)addr;
 1836         boolean_t full = have_addr;
 1837 
 1838         vm_page_t p;
 1839 
 1840         /* XXX count is an (unused) arg.  Avoid shadowing it. */
 1841 #define count   was_count
 1842 
 1843         int count;
 1844 
 1845         if (object == NULL)
 1846                 return;
 1847 
 1848         db_iprintf(
 1849             "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
 1850             object, (int)object->type, (u_long)object->size,
 1851             object->resident_page_count, object->ref_count, object->flags);
 1852         /*
 1853          * XXX no %qd in kernel.  Truncate object->backing_object_offset.
 1854          */
 1855         db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
 1856             object->shadow_count, 
 1857             object->backing_object ? object->backing_object->ref_count : 0,
 1858             object->backing_object, (long)object->backing_object_offset);
 1859 
 1860         if (!full)
 1861                 return;
 1862 
 1863         db_indent += 2;
 1864         count = 0;
 1865         for (p = TAILQ_FIRST(&object->memq); p != NULL; p = TAILQ_NEXT(p, listq)) {
 1866                 if (count == 0)
 1867                         db_iprintf("memory:=");
 1868                 else if (count == 6) {
 1869                         db_printf("\n");
 1870                         db_iprintf(" ...");
 1871                         count = 0;
 1872                 } else
 1873                         db_printf(",");
 1874                 count++;
 1875 
 1876                 db_printf("(off=0x%lx,page=0x%lx)",
 1877                     (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
 1878         }
 1879         if (count != 0)
 1880                 db_printf("\n");
 1881         db_indent -= 2;
 1882 }
 1883 
 1884 /* XXX. */
 1885 #undef count
 1886 
 1887 /* XXX need this non-static entry for calling from vm_map_print. */
 1888 void
 1889 vm_object_print(addr, have_addr, count, modif)
 1890         /* db_expr_t */ long addr;
 1891         boolean_t have_addr;
 1892         /* db_expr_t */ long count;
 1893         char *modif;
 1894 {
 1895         vm_object_print_static(addr, have_addr, count, modif);
 1896 }
 1897 
 1898 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
 1899 {
 1900         vm_object_t object;
 1901         int nl = 0;
 1902         int c;
 1903         for (object = TAILQ_FIRST(&vm_object_list);
 1904                         object != NULL;
 1905                         object = TAILQ_NEXT(object, object_list)) {
 1906                 vm_pindex_t idx, fidx;
 1907                 vm_pindex_t osize;
 1908                 vm_paddr_t pa = -1, padiff;
 1909                 int rcount;
 1910                 vm_page_t m;
 1911 
 1912                 db_printf("new object: %p\n", (void *)object);
 1913                 if ( nl > 18) {
 1914                         c = cngetc();
 1915                         if (c != ' ')
 1916                                 return;
 1917                         nl = 0;
 1918                 }
 1919                 nl++;
 1920                 rcount = 0;
 1921                 fidx = 0;
 1922                 osize = object->size;
 1923                 if (osize > 128)
 1924                         osize = 128;
 1925                 for(idx=0;idx<osize;idx++) {
 1926                         m = vm_page_lookup(object, idx);
 1927                         if (m == NULL) {
 1928                                 if (rcount) {
 1929                                         db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
 1930                                                 (long)fidx, rcount, (long)pa);
 1931                                         if ( nl > 18) {
 1932                                                 c = cngetc();
 1933                                                 if (c != ' ')
 1934                                                         return;
 1935                                                 nl = 0;
 1936                                         }
 1937                                         nl++;
 1938                                         rcount = 0;
 1939                                 }
 1940                                 continue;
 1941                         }
 1942 
 1943                                 
 1944                         if (rcount &&
 1945                                 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
 1946                                 ++rcount;
 1947                                 continue;
 1948                         }
 1949                         if (rcount) {
 1950                                 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
 1951                                 padiff >>= PAGE_SHIFT;
 1952                                 padiff &= PQ_L2_MASK;
 1953                                 if (padiff == 0) {
 1954                                         pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
 1955                                         ++rcount;
 1956                                         continue;
 1957                                 }
 1958                                 db_printf(" index(%ld)run(%d)pa(0x%lx)",
 1959                                         (long)fidx, rcount, (long)pa);
 1960                                 db_printf("pd(%ld)\n", (long)padiff);
 1961                                 if ( nl > 18) {
 1962                                         c = cngetc();
 1963                                         if (c != ' ')
 1964                                                 return;
 1965                                         nl = 0;
 1966                                 }
 1967                                 nl++;
 1968                         }
 1969                         fidx = idx;
 1970                         pa = VM_PAGE_TO_PHYS(m);
 1971                         rcount = 1;
 1972                 }
 1973                 if (rcount) {
 1974                         db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
 1975                                 (long)fidx, rcount, (long)pa);
 1976                         if ( nl > 18) {
 1977                                 c = cngetc();
 1978                                 if (c != ' ')
 1979                                         return;
 1980                                 nl = 0;
 1981                         }
 1982                         nl++;
 1983                 }
 1984         }
 1985 }
 1986 #endif /* DDB */

Cache object: bcea51bfce781aed25ba19821e24fa88


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