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

Cache object: b291f35ce93751f0acf0b05f7ad37bba


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