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.3/sys/vm/vm_object.c 168345 2007-04-04 03:15:03Z alc $");
   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                 vfslocked = 0;
  445         restart:
  446                 VM_OBJECT_LOCK(object);
  447                 if (object->type == OBJT_VNODE) {
  448                         struct vnode *vp = (struct vnode *) object->handle;
  449 
  450                         /*
  451                          * Conditionally acquire Giant for a vnode-backed
  452                          * object.  We have to be careful since the type of
  453                          * a vnode object can change while the object is
  454                          * unlocked.
  455                          */
  456                         if (VFS_NEEDSGIANT(vp->v_mount) && !vfslocked) {
  457                                 vfslocked = 1;
  458                                 if (!mtx_trylock(&Giant)) {
  459                                         VM_OBJECT_UNLOCK(object);
  460                                         mtx_lock(&Giant);
  461                                         goto restart;
  462                                 }
  463                         }
  464                         vm_object_vndeallocate(object);
  465                         VFS_UNLOCK_GIANT(vfslocked);
  466                         return;
  467                 } else
  468                         /*
  469                          * This is to handle the case that the object
  470                          * changed type while we dropped its lock to
  471                          * obtain Giant.
  472                          */
  473                         VFS_UNLOCK_GIANT(vfslocked);
  474 
  475                 KASSERT(object->ref_count != 0,
  476                         ("vm_object_deallocate: object deallocated too many times: %d", object->type));
  477 
  478                 /*
  479                  * If the reference count goes to 0 we start calling
  480                  * vm_object_terminate() on the object chain.
  481                  * A ref count of 1 may be a special case depending on the
  482                  * shadow count being 0 or 1.
  483                  */
  484                 object->ref_count--;
  485                 if (object->ref_count > 1) {
  486                         VM_OBJECT_UNLOCK(object);
  487                         return;
  488                 } else if (object->ref_count == 1) {
  489                         if (object->shadow_count == 0) {
  490                                 vm_object_set_flag(object, OBJ_ONEMAPPING);
  491                         } else if ((object->shadow_count == 1) &&
  492                             (object->handle == NULL) &&
  493                             (object->type == OBJT_DEFAULT ||
  494                              object->type == OBJT_SWAP)) {
  495                                 vm_object_t robject;
  496 
  497                                 robject = LIST_FIRST(&object->shadow_head);
  498                                 KASSERT(robject != NULL,
  499                                     ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
  500                                          object->ref_count,
  501                                          object->shadow_count));
  502                                 if (!VM_OBJECT_TRYLOCK(robject)) {
  503                                         /*
  504                                          * Avoid a potential deadlock.
  505                                          */
  506                                         object->ref_count++;
  507                                         VM_OBJECT_UNLOCK(object);
  508                                         /*
  509                                          * More likely than not the thread
  510                                          * holding robject's lock has lower
  511                                          * priority than the current thread.
  512                                          * Let the lower priority thread run.
  513                                          */
  514                                         tsleep(&proc0, PVM, "vmo_de", 1);
  515                                         continue;
  516                                 }
  517                                 /*
  518                                  * Collapse object into its shadow unless its
  519                                  * shadow is dead.  In that case, object will
  520                                  * be deallocated by the thread that is
  521                                  * deallocating its shadow.
  522                                  */
  523                                 if ((robject->flags & OBJ_DEAD) == 0 &&
  524                                     (robject->handle == NULL) &&
  525                                     (robject->type == OBJT_DEFAULT ||
  526                                      robject->type == OBJT_SWAP)) {
  527 
  528                                         robject->ref_count++;
  529 retry:
  530                                         if (robject->paging_in_progress) {
  531                                                 VM_OBJECT_UNLOCK(object);
  532                                                 vm_object_pip_wait(robject,
  533                                                     "objde1");
  534                                                 temp = robject->backing_object;
  535                                                 if (object == temp) {
  536                                                         VM_OBJECT_LOCK(object);
  537                                                         goto retry;
  538                                                 }
  539                                         } else if (object->paging_in_progress) {
  540                                                 VM_OBJECT_UNLOCK(robject);
  541                                                 object->flags |= OBJ_PIPWNT;
  542                                                 msleep(object,
  543                                                     VM_OBJECT_MTX(object),
  544                                                     PDROP | PVM, "objde2", 0);
  545                                                 VM_OBJECT_LOCK(robject);
  546                                                 temp = robject->backing_object;
  547                                                 if (object == temp) {
  548                                                         VM_OBJECT_LOCK(object);
  549                                                         goto retry;
  550                                                 }
  551                                         } else
  552                                                 VM_OBJECT_UNLOCK(object);
  553 
  554                                         if (robject->ref_count == 1) {
  555                                                 robject->ref_count--;
  556                                                 object = robject;
  557                                                 goto doterm;
  558                                         }
  559                                         object = robject;
  560                                         vm_object_collapse(object);
  561                                         VM_OBJECT_UNLOCK(object);
  562                                         continue;
  563                                 }
  564                                 VM_OBJECT_UNLOCK(robject);
  565                         }
  566                         VM_OBJECT_UNLOCK(object);
  567                         return;
  568                 }
  569 doterm:
  570                 temp = object->backing_object;
  571                 if (temp != NULL) {
  572                         VM_OBJECT_LOCK(temp);
  573                         LIST_REMOVE(object, shadow_list);
  574                         temp->shadow_count--;
  575                         temp->generation++;
  576                         VM_OBJECT_UNLOCK(temp);
  577                         object->backing_object = NULL;
  578                 }
  579                 /*
  580                  * Don't double-terminate, we could be in a termination
  581                  * recursion due to the terminate having to sync data
  582                  * to disk.
  583                  */
  584                 if ((object->flags & OBJ_DEAD) == 0)
  585                         vm_object_terminate(object);
  586                 else
  587                         VM_OBJECT_UNLOCK(object);
  588                 object = temp;
  589         }
  590 }
  591 
  592 /*
  593  *      vm_object_terminate actually destroys the specified object, freeing
  594  *      up all previously used resources.
  595  *
  596  *      The object must be locked.
  597  *      This routine may block.
  598  */
  599 void
  600 vm_object_terminate(vm_object_t object)
  601 {
  602         vm_page_t p;
  603 
  604         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
  605 
  606         /*
  607          * Make sure no one uses us.
  608          */
  609         vm_object_set_flag(object, OBJ_DEAD);
  610 
  611         /*
  612          * wait for the pageout daemon to be done with the object
  613          */
  614         vm_object_pip_wait(object, "objtrm");
  615 
  616         KASSERT(!object->paging_in_progress,
  617                 ("vm_object_terminate: pageout in progress"));
  618 
  619         /*
  620          * Clean and free the pages, as appropriate. All references to the
  621          * object are gone, so we don't need to lock it.
  622          */
  623         if (object->type == OBJT_VNODE) {
  624                 struct vnode *vp = (struct vnode *)object->handle;
  625 
  626                 /*
  627                  * Clean pages and flush buffers.
  628                  */
  629                 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
  630                 VM_OBJECT_UNLOCK(object);
  631 
  632                 vinvalbuf(vp, V_SAVE, NULL, 0, 0);
  633 
  634                 VM_OBJECT_LOCK(object);
  635         }
  636 
  637         KASSERT(object->ref_count == 0, 
  638                 ("vm_object_terminate: object with references, ref_count=%d",
  639                 object->ref_count));
  640 
  641         /*
  642          * Now free any remaining pages. For internal objects, this also
  643          * removes them from paging queues. Don't free wired pages, just
  644          * remove them from the object. 
  645          */
  646         vm_page_lock_queues();
  647         while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
  648                 KASSERT(!p->busy && (p->flags & PG_BUSY) == 0,
  649                         ("vm_object_terminate: freeing busy page %p "
  650                         "p->busy = %d, p->flags %x\n", p, p->busy, p->flags));
  651                 if (p->wire_count == 0) {
  652                         vm_page_free(p);
  653                         cnt.v_pfree++;
  654                 } else {
  655                         vm_page_remove(p);
  656                 }
  657         }
  658         vm_page_unlock_queues();
  659 
  660         /*
  661          * Let the pager know object is dead.
  662          */
  663         vm_pager_deallocate(object);
  664         VM_OBJECT_UNLOCK(object);
  665 
  666         /*
  667          * Remove the object from the global object list.
  668          */
  669         mtx_lock(&vm_object_list_mtx);
  670         TAILQ_REMOVE(&vm_object_list, object, object_list);
  671         mtx_unlock(&vm_object_list_mtx);
  672 
  673         /*
  674          * Free the space for the object.
  675          */
  676         uma_zfree(obj_zone, object);
  677 }
  678 
  679 /*
  680  *      vm_object_page_clean
  681  *
  682  *      Clean all dirty pages in the specified range of object.  Leaves page 
  683  *      on whatever queue it is currently on.   If NOSYNC is set then do not
  684  *      write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
  685  *      leaving the object dirty.
  686  *
  687  *      When stuffing pages asynchronously, allow clustering.  XXX we need a
  688  *      synchronous clustering mode implementation.
  689  *
  690  *      Odd semantics: if start == end, we clean everything.
  691  *
  692  *      The object must be locked.
  693  */
  694 void
  695 vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags)
  696 {
  697         vm_page_t p, np;
  698         vm_pindex_t tstart, tend;
  699         vm_pindex_t pi;
  700         int clearobjflags;
  701         int pagerflags;
  702         int curgeneration;
  703 
  704         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
  705         if (object->type != OBJT_VNODE ||
  706                 (object->flags & OBJ_MIGHTBEDIRTY) == 0)
  707                 return;
  708 
  709         pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK;
  710         pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
  711 
  712         vm_object_set_flag(object, OBJ_CLEANING);
  713 
  714         tstart = start;
  715         if (end == 0) {
  716                 tend = object->size;
  717         } else {
  718                 tend = end;
  719         }
  720 
  721         vm_page_lock_queues();
  722         /*
  723          * If the caller is smart and only msync()s a range he knows is
  724          * dirty, we may be able to avoid an object scan.  This results in
  725          * a phenominal improvement in performance.  We cannot do this
  726          * as a matter of course because the object may be huge - e.g.
  727          * the size might be in the gigabytes or terrabytes.
  728          */
  729         if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) {
  730                 vm_pindex_t tscan;
  731                 int scanlimit;
  732                 int scanreset;
  733 
  734                 scanreset = object->resident_page_count / EASY_SCAN_FACTOR;
  735                 if (scanreset < 16)
  736                         scanreset = 16;
  737                 pagerflags |= VM_PAGER_IGNORE_CLEANCHK;
  738 
  739                 scanlimit = scanreset;
  740                 tscan = tstart;
  741                 while (tscan < tend) {
  742                         curgeneration = object->generation;
  743                         p = vm_page_lookup(object, tscan);
  744                         if (p == NULL || p->valid == 0 ||
  745                             (p->queue - p->pc) == PQ_CACHE) {
  746                                 if (--scanlimit == 0)
  747                                         break;
  748                                 ++tscan;
  749                                 continue;
  750                         }
  751                         vm_page_test_dirty(p);
  752                         if ((p->dirty & p->valid) == 0) {
  753                                 if (--scanlimit == 0)
  754                                         break;
  755                                 ++tscan;
  756                                 continue;
  757                         }
  758                         /*
  759                          * If we have been asked to skip nosync pages and 
  760                          * this is a nosync page, we can't continue.
  761                          */
  762                         if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
  763                                 if (--scanlimit == 0)
  764                                         break;
  765                                 ++tscan;
  766                                 continue;
  767                         }
  768                         scanlimit = scanreset;
  769 
  770                         /*
  771                          * This returns 0 if it was unable to busy the first
  772                          * page (i.e. had to sleep).
  773                          */
  774                         tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags);
  775                 }
  776 
  777                 /*
  778                  * If everything was dirty and we flushed it successfully,
  779                  * and the requested range is not the entire object, we
  780                  * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can
  781                  * return immediately.
  782                  */
  783                 if (tscan >= tend && (tstart || tend < object->size)) {
  784                         vm_page_unlock_queues();
  785                         vm_object_clear_flag(object, OBJ_CLEANING);
  786                         return;
  787                 }
  788                 pagerflags &= ~VM_PAGER_IGNORE_CLEANCHK;
  789         }
  790 
  791         /*
  792          * Generally set CLEANCHK interlock and make the page read-only so
  793          * we can then clear the object flags.
  794          *
  795          * However, if this is a nosync mmap then the object is likely to 
  796          * stay dirty so do not mess with the page and do not clear the
  797          * object flags.
  798          */
  799         clearobjflags = 1;
  800         TAILQ_FOREACH(p, &object->memq, listq) {
  801                 vm_page_flag_set(p, PG_CLEANCHK);
  802                 if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC))
  803                         clearobjflags = 0;
  804                 else
  805                         pmap_page_protect(p, VM_PROT_READ);
  806         }
  807 
  808         if (clearobjflags && (tstart == 0) && (tend == object->size)) {
  809                 struct vnode *vp;
  810 
  811                 vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
  812                 if (object->type == OBJT_VNODE &&
  813                     (vp = (struct vnode *)object->handle) != NULL) {
  814                         VI_LOCK(vp);
  815                         if (vp->v_iflag & VI_OBJDIRTY)
  816                                 vp->v_iflag &= ~VI_OBJDIRTY;
  817                         VI_UNLOCK(vp);
  818                 }
  819         }
  820 
  821 rescan:
  822         curgeneration = object->generation;
  823 
  824         for (p = TAILQ_FIRST(&object->memq); p; p = np) {
  825                 int n;
  826 
  827                 np = TAILQ_NEXT(p, listq);
  828 
  829 again:
  830                 pi = p->pindex;
  831                 if (((p->flags & PG_CLEANCHK) == 0) ||
  832                         (pi < tstart) || (pi >= tend) ||
  833                         (p->valid == 0) ||
  834                         ((p->queue - p->pc) == PQ_CACHE)) {
  835                         vm_page_flag_clear(p, PG_CLEANCHK);
  836                         continue;
  837                 }
  838 
  839                 vm_page_test_dirty(p);
  840                 if ((p->dirty & p->valid) == 0) {
  841                         vm_page_flag_clear(p, PG_CLEANCHK);
  842                         continue;
  843                 }
  844 
  845                 /*
  846                  * If we have been asked to skip nosync pages and this is a
  847                  * nosync page, skip it.  Note that the object flags were
  848                  * not cleared in this case so we do not have to set them.
  849                  */
  850                 if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
  851                         vm_page_flag_clear(p, PG_CLEANCHK);
  852                         continue;
  853                 }
  854 
  855                 n = vm_object_page_collect_flush(object, p,
  856                         curgeneration, pagerflags);
  857                 if (n == 0)
  858                         goto rescan;
  859 
  860                 if (object->generation != curgeneration)
  861                         goto rescan;
  862 
  863                 /*
  864                  * Try to optimize the next page.  If we can't we pick up
  865                  * our (random) scan where we left off.
  866                  */
  867                 if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) {
  868                         if ((p = vm_page_lookup(object, pi + n)) != NULL)
  869                                 goto again;
  870                 }
  871         }
  872         vm_page_unlock_queues();
  873 #if 0
  874         VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
  875 #endif
  876 
  877         vm_object_clear_flag(object, OBJ_CLEANING);
  878         return;
  879 }
  880 
  881 static int
  882 vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags)
  883 {
  884         int runlen;
  885         int maxf;
  886         int chkb;
  887         int maxb;
  888         int i;
  889         vm_pindex_t pi;
  890         vm_page_t maf[vm_pageout_page_count];
  891         vm_page_t mab[vm_pageout_page_count];
  892         vm_page_t ma[vm_pageout_page_count];
  893 
  894         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
  895         pi = p->pindex;
  896         while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) {
  897                 vm_page_lock_queues();
  898                 if (object->generation != curgeneration) {
  899                         return(0);
  900                 }
  901         }
  902         maxf = 0;
  903         for(i = 1; i < vm_pageout_page_count; i++) {
  904                 vm_page_t tp;
  905 
  906                 if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
  907                         if ((tp->flags & PG_BUSY) ||
  908                                 ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
  909                                  (tp->flags & PG_CLEANCHK) == 0) ||
  910                                 (tp->busy != 0))
  911                                 break;
  912                         if((tp->queue - tp->pc) == PQ_CACHE) {
  913                                 vm_page_flag_clear(tp, PG_CLEANCHK);
  914                                 break;
  915                         }
  916                         vm_page_test_dirty(tp);
  917                         if ((tp->dirty & tp->valid) == 0) {
  918                                 vm_page_flag_clear(tp, PG_CLEANCHK);
  919                                 break;
  920                         }
  921                         maf[ i - 1 ] = tp;
  922                         maxf++;
  923                         continue;
  924                 }
  925                 break;
  926         }
  927 
  928         maxb = 0;
  929         chkb = vm_pageout_page_count -  maxf;
  930         if (chkb) {
  931                 for(i = 1; i < chkb;i++) {
  932                         vm_page_t tp;
  933 
  934                         if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
  935                                 if ((tp->flags & PG_BUSY) ||
  936                                         ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 &&
  937                                          (tp->flags & PG_CLEANCHK) == 0) ||
  938                                         (tp->busy != 0))
  939                                         break;
  940                                 if ((tp->queue - tp->pc) == PQ_CACHE) {
  941                                         vm_page_flag_clear(tp, PG_CLEANCHK);
  942                                         break;
  943                                 }
  944                                 vm_page_test_dirty(tp);
  945                                 if ((tp->dirty & tp->valid) == 0) {
  946                                         vm_page_flag_clear(tp, PG_CLEANCHK);
  947                                         break;
  948                                 }
  949                                 mab[ i - 1 ] = tp;
  950                                 maxb++;
  951                                 continue;
  952                         }
  953                         break;
  954                 }
  955         }
  956 
  957         for(i = 0; i < maxb; i++) {
  958                 int index = (maxb - i) - 1;
  959                 ma[index] = mab[i];
  960                 vm_page_flag_clear(ma[index], PG_CLEANCHK);
  961         }
  962         vm_page_flag_clear(p, PG_CLEANCHK);
  963         ma[maxb] = p;
  964         for(i = 0; i < maxf; i++) {
  965                 int index = (maxb + i) + 1;
  966                 ma[index] = maf[i];
  967                 vm_page_flag_clear(ma[index], PG_CLEANCHK);
  968         }
  969         runlen = maxb + maxf + 1;
  970 
  971         vm_pageout_flush(ma, runlen, pagerflags);
  972         for (i = 0; i < runlen; i++) {
  973                 if (ma[i]->valid & ma[i]->dirty) {
  974                         pmap_page_protect(ma[i], VM_PROT_READ);
  975                         vm_page_flag_set(ma[i], PG_CLEANCHK);
  976 
  977                         /*
  978                          * maxf will end up being the actual number of pages
  979                          * we wrote out contiguously, non-inclusive of the
  980                          * first page.  We do not count look-behind pages.
  981                          */
  982                         if (i >= maxb + 1 && (maxf > i - maxb - 1))
  983                                 maxf = i - maxb - 1;
  984                 }
  985         }
  986         return(maxf + 1);
  987 }
  988 
  989 /*
  990  * Note that there is absolutely no sense in writing out
  991  * anonymous objects, so we track down the vnode object
  992  * to write out.
  993  * We invalidate (remove) all pages from the address space
  994  * for semantic correctness.
  995  *
  996  * Note: certain anonymous maps, such as MAP_NOSYNC maps,
  997  * may start out with a NULL object.
  998  */
  999 void
 1000 vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size,
 1001     boolean_t syncio, boolean_t invalidate)
 1002 {
 1003         vm_object_t backing_object;
 1004         struct vnode *vp;
 1005         struct mount *mp;
 1006         int flags;
 1007 
 1008         if (object == NULL)
 1009                 return;
 1010         VM_OBJECT_LOCK(object);
 1011         while ((backing_object = object->backing_object) != NULL) {
 1012                 VM_OBJECT_LOCK(backing_object);
 1013                 offset += object->backing_object_offset;
 1014                 VM_OBJECT_UNLOCK(object);
 1015                 object = backing_object;
 1016                 if (object->size < OFF_TO_IDX(offset + size))
 1017                         size = IDX_TO_OFF(object->size) - offset;
 1018         }
 1019         /*
 1020          * Flush pages if writing is allowed, invalidate them
 1021          * if invalidation requested.  Pages undergoing I/O
 1022          * will be ignored by vm_object_page_remove().
 1023          *
 1024          * We cannot lock the vnode and then wait for paging
 1025          * to complete without deadlocking against vm_fault.
 1026          * Instead we simply call vm_object_page_remove() and
 1027          * allow it to block internally on a page-by-page
 1028          * basis when it encounters pages undergoing async
 1029          * I/O.
 1030          */
 1031         if (object->type == OBJT_VNODE &&
 1032             (object->flags & OBJ_MIGHTBEDIRTY) != 0) {
 1033                 int vfslocked;
 1034                 vp = object->handle;
 1035                 VM_OBJECT_UNLOCK(object);
 1036                 (void) vn_start_write(vp, &mp, V_WAIT);
 1037                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 1038                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
 1039                 flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
 1040                 flags |= invalidate ? OBJPC_INVAL : 0;
 1041                 VM_OBJECT_LOCK(object);
 1042                 vm_object_page_clean(object,
 1043                     OFF_TO_IDX(offset),
 1044                     OFF_TO_IDX(offset + size + PAGE_MASK),
 1045                     flags);
 1046                 VM_OBJECT_UNLOCK(object);
 1047                 VOP_UNLOCK(vp, 0, curthread);
 1048                 VFS_UNLOCK_GIANT(vfslocked);
 1049                 vn_finished_write(mp);
 1050                 VM_OBJECT_LOCK(object);
 1051         }
 1052         if ((object->type == OBJT_VNODE ||
 1053              object->type == OBJT_DEVICE) && invalidate) {
 1054                 boolean_t purge;
 1055                 purge = old_msync || (object->type == OBJT_DEVICE);
 1056                 vm_object_page_remove(object,
 1057                     OFF_TO_IDX(offset),
 1058                     OFF_TO_IDX(offset + size + PAGE_MASK),
 1059                     purge ? FALSE : TRUE);
 1060         }
 1061         VM_OBJECT_UNLOCK(object);
 1062 }
 1063 
 1064 /*
 1065  *      vm_object_madvise:
 1066  *
 1067  *      Implements the madvise function at the object/page level.
 1068  *
 1069  *      MADV_WILLNEED   (any object)
 1070  *
 1071  *          Activate the specified pages if they are resident.
 1072  *
 1073  *      MADV_DONTNEED   (any object)
 1074  *
 1075  *          Deactivate the specified pages if they are resident.
 1076  *
 1077  *      MADV_FREE       (OBJT_DEFAULT/OBJT_SWAP objects,
 1078  *                       OBJ_ONEMAPPING only)
 1079  *
 1080  *          Deactivate and clean the specified pages if they are
 1081  *          resident.  This permits the process to reuse the pages
 1082  *          without faulting or the kernel to reclaim the pages
 1083  *          without I/O.
 1084  */
 1085 void
 1086 vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise)
 1087 {
 1088         vm_pindex_t end, tpindex;
 1089         vm_object_t backing_object, tobject;
 1090         vm_page_t m;
 1091 
 1092         if (object == NULL)
 1093                 return;
 1094         VM_OBJECT_LOCK(object);
 1095         end = pindex + count;
 1096         /*
 1097          * Locate and adjust resident pages
 1098          */
 1099         for (; pindex < end; pindex += 1) {
 1100 relookup:
 1101                 tobject = object;
 1102                 tpindex = pindex;
 1103 shadowlookup:
 1104                 /*
 1105                  * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
 1106                  * and those pages must be OBJ_ONEMAPPING.
 1107                  */
 1108                 if (advise == MADV_FREE) {
 1109                         if ((tobject->type != OBJT_DEFAULT &&
 1110                              tobject->type != OBJT_SWAP) ||
 1111                             (tobject->flags & OBJ_ONEMAPPING) == 0) {
 1112                                 goto unlock_tobject;
 1113                         }
 1114                 }
 1115                 m = vm_page_lookup(tobject, tpindex);
 1116                 if (m == NULL) {
 1117                         /*
 1118                          * There may be swap even if there is no backing page
 1119                          */
 1120                         if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
 1121                                 swap_pager_freespace(tobject, tpindex, 1);
 1122                         /*
 1123                          * next object
 1124                          */
 1125                         backing_object = tobject->backing_object;
 1126                         if (backing_object == NULL)
 1127                                 goto unlock_tobject;
 1128                         VM_OBJECT_LOCK(backing_object);
 1129                         tpindex += OFF_TO_IDX(tobject->backing_object_offset);
 1130                         if (tobject != object)
 1131                                 VM_OBJECT_UNLOCK(tobject);
 1132                         tobject = backing_object;
 1133                         goto shadowlookup;
 1134                 }
 1135                 /*
 1136                  * If the page is busy or not in a normal active state,
 1137                  * we skip it.  If the page is not managed there are no
 1138                  * page queues to mess with.  Things can break if we mess
 1139                  * with pages in any of the below states.
 1140                  */
 1141                 vm_page_lock_queues();
 1142                 if (m->hold_count ||
 1143                     m->wire_count ||
 1144                     (m->flags & PG_UNMANAGED) ||
 1145                     m->valid != VM_PAGE_BITS_ALL) {
 1146                         vm_page_unlock_queues();
 1147                         goto unlock_tobject;
 1148                 }
 1149                 if ((m->flags & PG_BUSY) || m->busy) {
 1150                         vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
 1151                         if (object != tobject)
 1152                                 VM_OBJECT_UNLOCK(object);
 1153                         VM_OBJECT_UNLOCK(tobject);
 1154                         msleep(m, &vm_page_queue_mtx, PDROP | PVM, "madvpo", 0);
 1155                         VM_OBJECT_LOCK(object);
 1156                         goto relookup;
 1157                 }
 1158                 if (advise == MADV_WILLNEED) {
 1159                         vm_page_activate(m);
 1160                 } else if (advise == MADV_DONTNEED) {
 1161                         vm_page_dontneed(m);
 1162                 } else if (advise == MADV_FREE) {
 1163                         /*
 1164                          * Mark the page clean.  This will allow the page
 1165                          * to be freed up by the system.  However, such pages
 1166                          * are often reused quickly by malloc()/free()
 1167                          * so we do not do anything that would cause
 1168                          * a page fault if we can help it.
 1169                          *
 1170                          * Specifically, we do not try to actually free
 1171                          * the page now nor do we try to put it in the
 1172                          * cache (which would cause a page fault on reuse).
 1173                          *
 1174                          * But we do make the page is freeable as we
 1175                          * can without actually taking the step of unmapping
 1176                          * it.
 1177                          */
 1178                         pmap_clear_modify(m);
 1179                         m->dirty = 0;
 1180                         m->act_count = 0;
 1181                         vm_page_dontneed(m);
 1182                 }
 1183                 vm_page_unlock_queues();
 1184                 if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
 1185                         swap_pager_freespace(tobject, tpindex, 1);
 1186 unlock_tobject:
 1187                 if (tobject != object)
 1188                         VM_OBJECT_UNLOCK(tobject);
 1189         }       
 1190         VM_OBJECT_UNLOCK(object);
 1191 }
 1192 
 1193 /*
 1194  *      vm_object_shadow:
 1195  *
 1196  *      Create a new object which is backed by the
 1197  *      specified existing object range.  The source
 1198  *      object reference is deallocated.
 1199  *
 1200  *      The new object and offset into that object
 1201  *      are returned in the source parameters.
 1202  */
 1203 void
 1204 vm_object_shadow(
 1205         vm_object_t *object,    /* IN/OUT */
 1206         vm_ooffset_t *offset,   /* IN/OUT */
 1207         vm_size_t length)
 1208 {
 1209         vm_object_t source;
 1210         vm_object_t result;
 1211 
 1212         source = *object;
 1213 
 1214         /*
 1215          * Don't create the new object if the old object isn't shared.
 1216          */
 1217         if (source != NULL) {
 1218                 VM_OBJECT_LOCK(source);
 1219                 if (source->ref_count == 1 &&
 1220                     source->handle == NULL &&
 1221                     (source->type == OBJT_DEFAULT ||
 1222                      source->type == OBJT_SWAP)) {
 1223                         VM_OBJECT_UNLOCK(source);
 1224                         return;
 1225                 }
 1226                 VM_OBJECT_UNLOCK(source);
 1227         }
 1228 
 1229         /*
 1230          * Allocate a new object with the given length.
 1231          */
 1232         result = vm_object_allocate(OBJT_DEFAULT, length);
 1233 
 1234         /*
 1235          * The new object shadows the source object, adding a reference to it.
 1236          * Our caller changes his reference to point to the new object,
 1237          * removing a reference to the source object.  Net result: no change
 1238          * of reference count.
 1239          *
 1240          * Try to optimize the result object's page color when shadowing
 1241          * in order to maintain page coloring consistency in the combined 
 1242          * shadowed object.
 1243          */
 1244         result->backing_object = source;
 1245         /*
 1246          * Store the offset into the source object, and fix up the offset into
 1247          * the new object.
 1248          */
 1249         result->backing_object_offset = *offset;
 1250         if (source != NULL) {
 1251                 VM_OBJECT_LOCK(source);
 1252                 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
 1253                 source->shadow_count++;
 1254                 source->generation++;
 1255                 if (length < source->size)
 1256                         length = source->size;
 1257                 if (length > PQ_L2_SIZE / 3 + PQ_PRIME1 ||
 1258                     source->generation > 1)
 1259                         length = PQ_L2_SIZE / 3 + PQ_PRIME1;
 1260                 result->pg_color = (source->pg_color +
 1261                     length * source->generation) & PQ_L2_MASK;
 1262                 result->flags |= source->flags & OBJ_NEEDGIANT;
 1263                 VM_OBJECT_UNLOCK(source);
 1264                 next_index = (result->pg_color + PQ_L2_SIZE / 3 + PQ_PRIME1) &
 1265                     PQ_L2_MASK;
 1266         }
 1267 
 1268 
 1269         /*
 1270          * Return the new things
 1271          */
 1272         *offset = 0;
 1273         *object = result;
 1274 }
 1275 
 1276 /*
 1277  *      vm_object_split:
 1278  *
 1279  * Split the pages in a map entry into a new object.  This affords
 1280  * easier removal of unused pages, and keeps object inheritance from
 1281  * being a negative impact on memory usage.
 1282  */
 1283 void
 1284 vm_object_split(vm_map_entry_t entry)
 1285 {
 1286         vm_page_t m;
 1287         vm_object_t orig_object, new_object, source;
 1288         vm_pindex_t offidxstart, offidxend;
 1289         vm_size_t idx, size;
 1290 
 1291         orig_object = entry->object.vm_object;
 1292         if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
 1293                 return;
 1294         if (orig_object->ref_count <= 1)
 1295                 return;
 1296         VM_OBJECT_UNLOCK(orig_object);
 1297 
 1298         offidxstart = OFF_TO_IDX(entry->offset);
 1299         offidxend = offidxstart + OFF_TO_IDX(entry->end - entry->start);
 1300         size = offidxend - offidxstart;
 1301 
 1302         /*
 1303          * If swap_pager_copy() is later called, it will convert new_object
 1304          * into a swap object.
 1305          */
 1306         new_object = vm_object_allocate(OBJT_DEFAULT, size);
 1307 
 1308         VM_OBJECT_LOCK(new_object);
 1309         VM_OBJECT_LOCK(orig_object);
 1310         source = orig_object->backing_object;
 1311         if (source != NULL) {
 1312                 VM_OBJECT_LOCK(source);
 1313                 if ((source->flags & OBJ_DEAD) != 0) {
 1314                         VM_OBJECT_UNLOCK(source);
 1315                         VM_OBJECT_UNLOCK(orig_object);
 1316                         VM_OBJECT_UNLOCK(new_object);
 1317                         vm_object_deallocate(new_object);
 1318                         VM_OBJECT_LOCK(orig_object);
 1319                         return;
 1320                 }
 1321                 LIST_INSERT_HEAD(&source->shadow_head,
 1322                                   new_object, shadow_list);
 1323                 source->shadow_count++;
 1324                 source->generation++;
 1325                 vm_object_reference_locked(source);     /* for new_object */
 1326                 vm_object_clear_flag(source, OBJ_ONEMAPPING);
 1327                 VM_OBJECT_UNLOCK(source);
 1328                 new_object->backing_object_offset = 
 1329                         orig_object->backing_object_offset + entry->offset;
 1330                 new_object->backing_object = source;
 1331         }
 1332         new_object->flags |= orig_object->flags & OBJ_NEEDGIANT;
 1333         vm_page_lock_queues();
 1334         for (idx = 0; idx < size; idx++) {
 1335         retry:
 1336                 m = vm_page_lookup(orig_object, offidxstart + idx);
 1337                 if (m == NULL)
 1338                         continue;
 1339 
 1340                 /*
 1341                  * We must wait for pending I/O to complete before we can
 1342                  * rename the page.
 1343                  *
 1344                  * We do not have to VM_PROT_NONE the page as mappings should
 1345                  * not be changed by this operation.
 1346                  */
 1347                 if ((m->flags & PG_BUSY) || m->busy) {
 1348                         vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
 1349                         VM_OBJECT_UNLOCK(orig_object);
 1350                         VM_OBJECT_UNLOCK(new_object);
 1351                         msleep(m, &vm_page_queue_mtx, PDROP | PVM, "spltwt", 0);
 1352                         VM_OBJECT_LOCK(new_object);
 1353                         VM_OBJECT_LOCK(orig_object);
 1354                         vm_page_lock_queues();
 1355                         goto retry;
 1356                 }
 1357                 vm_page_rename(m, new_object, idx);
 1358                 /* page automatically made dirty by rename and cache handled */
 1359                 vm_page_busy(m);
 1360         }
 1361         vm_page_unlock_queues();
 1362         if (orig_object->type == OBJT_SWAP) {
 1363                 /*
 1364                  * swap_pager_copy() can sleep, in which case the orig_object's
 1365                  * and new_object's locks are released and reacquired. 
 1366                  */
 1367                 swap_pager_copy(orig_object, new_object, offidxstart, 0);
 1368         }
 1369         VM_OBJECT_UNLOCK(orig_object);
 1370         vm_page_lock_queues();
 1371         TAILQ_FOREACH(m, &new_object->memq, listq)
 1372                 vm_page_wakeup(m);
 1373         vm_page_unlock_queues();
 1374         VM_OBJECT_UNLOCK(new_object);
 1375         entry->object.vm_object = new_object;
 1376         entry->offset = 0LL;
 1377         vm_object_deallocate(orig_object);
 1378         VM_OBJECT_LOCK(new_object);
 1379 }
 1380 
 1381 #define OBSC_TEST_ALL_SHADOWED  0x0001
 1382 #define OBSC_COLLAPSE_NOWAIT    0x0002
 1383 #define OBSC_COLLAPSE_WAIT      0x0004
 1384 
 1385 static int
 1386 vm_object_backing_scan(vm_object_t object, int op)
 1387 {
 1388         int r = 1;
 1389         vm_page_t p;
 1390         vm_object_t backing_object;
 1391         vm_pindex_t backing_offset_index;
 1392 
 1393         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
 1394         VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED);
 1395 
 1396         backing_object = object->backing_object;
 1397         backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
 1398 
 1399         /*
 1400          * Initial conditions
 1401          */
 1402         if (op & OBSC_TEST_ALL_SHADOWED) {
 1403                 /*
 1404                  * We do not want to have to test for the existence of
 1405                  * swap pages in the backing object.  XXX but with the
 1406                  * new swapper this would be pretty easy to do.
 1407                  *
 1408                  * XXX what about anonymous MAP_SHARED memory that hasn't
 1409                  * been ZFOD faulted yet?  If we do not test for this, the
 1410                  * shadow test may succeed! XXX
 1411                  */
 1412                 if (backing_object->type != OBJT_DEFAULT) {
 1413                         return (0);
 1414                 }
 1415         }
 1416         if (op & OBSC_COLLAPSE_WAIT) {
 1417                 vm_object_set_flag(backing_object, OBJ_DEAD);
 1418         }
 1419 
 1420         /*
 1421          * Our scan
 1422          */
 1423         p = TAILQ_FIRST(&backing_object->memq);
 1424         while (p) {
 1425                 vm_page_t next = TAILQ_NEXT(p, listq);
 1426                 vm_pindex_t new_pindex = p->pindex - backing_offset_index;
 1427 
 1428                 if (op & OBSC_TEST_ALL_SHADOWED) {
 1429                         vm_page_t pp;
 1430 
 1431                         /*
 1432                          * Ignore pages outside the parent object's range
 1433                          * and outside the parent object's mapping of the 
 1434                          * backing object.
 1435                          *
 1436                          * note that we do not busy the backing object's
 1437                          * page.
 1438                          */
 1439                         if (
 1440                             p->pindex < backing_offset_index ||
 1441                             new_pindex >= object->size
 1442                         ) {
 1443                                 p = next;
 1444                                 continue;
 1445                         }
 1446 
 1447                         /*
 1448                          * See if the parent has the page or if the parent's
 1449                          * object pager has the page.  If the parent has the
 1450                          * page but the page is not valid, the parent's
 1451                          * object pager must have the page.
 1452                          *
 1453                          * If this fails, the parent does not completely shadow
 1454                          * the object and we might as well give up now.
 1455                          */
 1456 
 1457                         pp = vm_page_lookup(object, new_pindex);
 1458                         if (
 1459                             (pp == NULL || pp->valid == 0) &&
 1460                             !vm_pager_has_page(object, new_pindex, NULL, NULL)
 1461                         ) {
 1462                                 r = 0;
 1463                                 break;
 1464                         }
 1465                 }
 1466 
 1467                 /*
 1468                  * Check for busy page
 1469                  */
 1470                 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
 1471                         vm_page_t pp;
 1472 
 1473                         if (op & OBSC_COLLAPSE_NOWAIT) {
 1474                                 if ((p->flags & PG_BUSY) ||
 1475                                     !p->valid || 
 1476                                     p->busy) {
 1477                                         p = next;
 1478                                         continue;
 1479                                 }
 1480                         } else if (op & OBSC_COLLAPSE_WAIT) {
 1481                                 if ((p->flags & PG_BUSY) || p->busy) {
 1482                                         vm_page_lock_queues();
 1483                                         vm_page_flag_set(p,
 1484                                             PG_WANTED | PG_REFERENCED);
 1485                                         VM_OBJECT_UNLOCK(backing_object);
 1486                                         VM_OBJECT_UNLOCK(object);
 1487                                         msleep(p, &vm_page_queue_mtx,
 1488                                             PDROP | PVM, "vmocol", 0);
 1489                                         VM_OBJECT_LOCK(object);
 1490                                         VM_OBJECT_LOCK(backing_object);
 1491                                         /*
 1492                                          * If we slept, anything could have
 1493                                          * happened.  Since the object is
 1494                                          * marked dead, the backing offset
 1495                                          * should not have changed so we
 1496                                          * just restart our scan.
 1497                                          */
 1498                                         p = TAILQ_FIRST(&backing_object->memq);
 1499                                         continue;
 1500                                 }
 1501                         }
 1502 
 1503                         KASSERT(
 1504                             p->object == backing_object,
 1505                             ("vm_object_backing_scan: object mismatch")
 1506                         );
 1507 
 1508                         /*
 1509                          * Destroy any associated swap
 1510                          */
 1511                         if (backing_object->type == OBJT_SWAP) {
 1512                                 swap_pager_freespace(
 1513                                     backing_object, 
 1514                                     p->pindex,
 1515                                     1
 1516                                 );
 1517                         }
 1518 
 1519                         if (
 1520                             p->pindex < backing_offset_index ||
 1521                             new_pindex >= object->size
 1522                         ) {
 1523                                 /*
 1524                                  * Page is out of the parent object's range, we 
 1525                                  * can simply destroy it. 
 1526                                  */
 1527                                 vm_page_lock_queues();
 1528                                 KASSERT(!pmap_page_is_mapped(p),
 1529                                     ("freeing mapped page %p", p));
 1530                                 if (p->wire_count == 0)
 1531                                         vm_page_free(p);
 1532                                 else
 1533                                         vm_page_remove(p);
 1534                                 vm_page_unlock_queues();
 1535                                 p = next;
 1536                                 continue;
 1537                         }
 1538 
 1539                         pp = vm_page_lookup(object, new_pindex);
 1540                         if (
 1541                             pp != NULL ||
 1542                             vm_pager_has_page(object, new_pindex, NULL, NULL)
 1543                         ) {
 1544                                 /*
 1545                                  * page already exists in parent OR swap exists
 1546                                  * for this location in the parent.  Destroy 
 1547                                  * the original page from the backing object.
 1548                                  *
 1549                                  * Leave the parent's page alone
 1550                                  */
 1551                                 vm_page_lock_queues();
 1552                                 KASSERT(!pmap_page_is_mapped(p),
 1553                                     ("freeing mapped page %p", p));
 1554                                 if (p->wire_count == 0)
 1555                                         vm_page_free(p);
 1556                                 else
 1557                                         vm_page_remove(p);
 1558                                 vm_page_unlock_queues();
 1559                                 p = next;
 1560                                 continue;
 1561                         }
 1562 
 1563                         /*
 1564                          * Page does not exist in parent, rename the
 1565                          * page from the backing object to the main object. 
 1566                          *
 1567                          * If the page was mapped to a process, it can remain 
 1568                          * mapped through the rename.
 1569                          */
 1570                         vm_page_lock_queues();
 1571                         vm_page_rename(p, object, new_pindex);
 1572                         vm_page_unlock_queues();
 1573                         /* page automatically made dirty by rename */
 1574                 }
 1575                 p = next;
 1576         }
 1577         return (r);
 1578 }
 1579 
 1580 
 1581 /*
 1582  * this version of collapse allows the operation to occur earlier and
 1583  * when paging_in_progress is true for an object...  This is not a complete
 1584  * operation, but should plug 99.9% of the rest of the leaks.
 1585  */
 1586 static void
 1587 vm_object_qcollapse(vm_object_t object)
 1588 {
 1589         vm_object_t backing_object = object->backing_object;
 1590 
 1591         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
 1592         VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED);
 1593 
 1594         if (backing_object->ref_count != 1)
 1595                 return;
 1596 
 1597         vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
 1598 }
 1599 
 1600 /*
 1601  *      vm_object_collapse:
 1602  *
 1603  *      Collapse an object with the object backing it.
 1604  *      Pages in the backing object are moved into the
 1605  *      parent, and the backing object is deallocated.
 1606  */
 1607 void
 1608 vm_object_collapse(vm_object_t object)
 1609 {
 1610         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
 1611         
 1612         while (TRUE) {
 1613                 vm_object_t backing_object;
 1614 
 1615                 /*
 1616                  * Verify that the conditions are right for collapse:
 1617                  *
 1618                  * The object exists and the backing object exists.
 1619                  */
 1620                 if ((backing_object = object->backing_object) == NULL)
 1621                         break;
 1622 
 1623                 /*
 1624                  * we check the backing object first, because it is most likely
 1625                  * not collapsable.
 1626                  */
 1627                 VM_OBJECT_LOCK(backing_object);
 1628                 if (backing_object->handle != NULL ||
 1629                     (backing_object->type != OBJT_DEFAULT &&
 1630                      backing_object->type != OBJT_SWAP) ||
 1631                     (backing_object->flags & OBJ_DEAD) ||
 1632                     object->handle != NULL ||
 1633                     (object->type != OBJT_DEFAULT &&
 1634                      object->type != OBJT_SWAP) ||
 1635                     (object->flags & OBJ_DEAD)) {
 1636                         VM_OBJECT_UNLOCK(backing_object);
 1637                         break;
 1638                 }
 1639 
 1640                 if (
 1641                     object->paging_in_progress != 0 ||
 1642                     backing_object->paging_in_progress != 0
 1643                 ) {
 1644                         vm_object_qcollapse(object);
 1645                         VM_OBJECT_UNLOCK(backing_object);
 1646                         break;
 1647                 }
 1648                 /*
 1649                  * We know that we can either collapse the backing object (if
 1650                  * the parent is the only reference to it) or (perhaps) have
 1651                  * the parent bypass the object if the parent happens to shadow
 1652                  * all the resident pages in the entire backing object.
 1653                  *
 1654                  * This is ignoring pager-backed pages such as swap pages.
 1655                  * vm_object_backing_scan fails the shadowing test in this
 1656                  * case.
 1657                  */
 1658                 if (backing_object->ref_count == 1) {
 1659                         /*
 1660                          * If there is exactly one reference to the backing
 1661                          * object, we can collapse it into the parent.  
 1662                          */
 1663                         vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
 1664 
 1665                         /*
 1666                          * Move the pager from backing_object to object.
 1667                          */
 1668                         if (backing_object->type == OBJT_SWAP) {
 1669                                 /*
 1670                                  * swap_pager_copy() can sleep, in which case
 1671                                  * the backing_object's and object's locks are
 1672                                  * released and reacquired.
 1673                                  */
 1674                                 swap_pager_copy(
 1675                                     backing_object,
 1676                                     object,
 1677                                     OFF_TO_IDX(object->backing_object_offset), TRUE);
 1678                         }
 1679                         /*
 1680                          * Object now shadows whatever backing_object did.
 1681                          * Note that the reference to 
 1682                          * backing_object->backing_object moves from within 
 1683                          * backing_object to within object.
 1684                          */
 1685                         LIST_REMOVE(object, shadow_list);
 1686                         backing_object->shadow_count--;
 1687                         backing_object->generation++;
 1688                         if (backing_object->backing_object) {
 1689                                 VM_OBJECT_LOCK(backing_object->backing_object);
 1690                                 LIST_REMOVE(backing_object, shadow_list);
 1691                                 LIST_INSERT_HEAD(
 1692                                     &backing_object->backing_object->shadow_head,
 1693                                     object, shadow_list);
 1694                                 /*
 1695                                  * The shadow_count has not changed.
 1696                                  */
 1697                                 backing_object->backing_object->generation++;
 1698                                 VM_OBJECT_UNLOCK(backing_object->backing_object);
 1699                         }
 1700                         object->backing_object = backing_object->backing_object;
 1701                         object->backing_object_offset +=
 1702                             backing_object->backing_object_offset;
 1703 
 1704                         /*
 1705                          * Discard backing_object.
 1706                          *
 1707                          * Since the backing object has no pages, no pager left,
 1708                          * and no object references within it, all that is
 1709                          * necessary is to dispose of it.
 1710                          */
 1711                         KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object));
 1712                         VM_OBJECT_UNLOCK(backing_object);
 1713 
 1714                         mtx_lock(&vm_object_list_mtx);
 1715                         TAILQ_REMOVE(
 1716                             &vm_object_list, 
 1717                             backing_object,
 1718                             object_list
 1719                         );
 1720                         mtx_unlock(&vm_object_list_mtx);
 1721 
 1722                         uma_zfree(obj_zone, backing_object);
 1723 
 1724                         object_collapses++;
 1725                 } else {
 1726                         vm_object_t new_backing_object;
 1727 
 1728                         /*
 1729                          * If we do not entirely shadow the backing object,
 1730                          * there is nothing we can do so we give up.
 1731                          */
 1732                         if (object->resident_page_count != object->size &&
 1733                             vm_object_backing_scan(object,
 1734                             OBSC_TEST_ALL_SHADOWED) == 0) {
 1735                                 VM_OBJECT_UNLOCK(backing_object);
 1736                                 break;
 1737                         }
 1738 
 1739                         /*
 1740                          * Make the parent shadow the next object in the
 1741                          * chain.  Deallocating backing_object will not remove
 1742                          * it, since its reference count is at least 2.
 1743                          */
 1744                         LIST_REMOVE(object, shadow_list);
 1745                         backing_object->shadow_count--;
 1746                         backing_object->generation++;
 1747 
 1748                         new_backing_object = backing_object->backing_object;
 1749                         if ((object->backing_object = new_backing_object) != NULL) {
 1750                                 VM_OBJECT_LOCK(new_backing_object);
 1751                                 LIST_INSERT_HEAD(
 1752                                     &new_backing_object->shadow_head,
 1753                                     object,
 1754                                     shadow_list
 1755                                 );
 1756                                 new_backing_object->shadow_count++;
 1757                                 new_backing_object->generation++;
 1758                                 vm_object_reference_locked(new_backing_object);
 1759                                 VM_OBJECT_UNLOCK(new_backing_object);
 1760                                 object->backing_object_offset +=
 1761                                         backing_object->backing_object_offset;
 1762                         }
 1763 
 1764                         /*
 1765                          * Drop the reference count on backing_object. Since
 1766                          * its ref_count was at least 2, it will not vanish.
 1767                          */
 1768                         backing_object->ref_count--;
 1769                         VM_OBJECT_UNLOCK(backing_object);
 1770                         object_bypasses++;
 1771                 }
 1772 
 1773                 /*
 1774                  * Try again with this object's new backing object.
 1775                  */
 1776         }
 1777 }
 1778 
 1779 /*
 1780  *      vm_object_page_remove:
 1781  *
 1782  *      Removes all physical pages in the given range from the
 1783  *      object's list of pages.  If the range's end is zero, all
 1784  *      physical pages from the range's start to the end of the object
 1785  *      are deleted.
 1786  *
 1787  *      The object must be locked.
 1788  */
 1789 void
 1790 vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end,
 1791     boolean_t clean_only)
 1792 {
 1793         vm_page_t p, next;
 1794 
 1795         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
 1796         if (object->resident_page_count == 0)
 1797                 return;
 1798 
 1799         /*
 1800          * Since physically-backed objects do not use managed pages, we can't
 1801          * remove pages from the object (we must instead remove the page
 1802          * references, and then destroy the object).
 1803          */
 1804         KASSERT(object->type != OBJT_PHYS,
 1805             ("attempt to remove pages from a physical object"));
 1806 
 1807         vm_object_pip_add(object, 1);
 1808 again:
 1809         vm_page_lock_queues();
 1810         if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
 1811                 if (p->pindex < start) {
 1812                         p = vm_page_splay(start, object->root);
 1813                         if ((object->root = p)->pindex < start)
 1814                                 p = TAILQ_NEXT(p, listq);
 1815                 }
 1816         }
 1817         /*
 1818          * Assert: the variable p is either (1) the page with the
 1819          * least pindex greater than or equal to the parameter pindex
 1820          * or (2) NULL.
 1821          */
 1822         for (;
 1823              p != NULL && (p->pindex < end || end == 0);
 1824              p = next) {
 1825                 next = TAILQ_NEXT(p, listq);
 1826 
 1827                 if (p->wire_count != 0) {
 1828                         pmap_remove_all(p);
 1829                         if (!clean_only)
 1830                                 p->valid = 0;
 1831                         continue;
 1832                 }
 1833                 if (vm_page_sleep_if_busy(p, TRUE, "vmopar"))
 1834                         goto again;
 1835                 if (clean_only && p->valid) {
 1836                         pmap_page_protect(p, VM_PROT_READ | VM_PROT_EXECUTE);
 1837                         if (p->valid & p->dirty)
 1838                                 continue;
 1839                 }
 1840                 pmap_remove_all(p);
 1841                 vm_page_free(p);
 1842         }
 1843         vm_page_unlock_queues();
 1844         vm_object_pip_wakeup(object);
 1845 }
 1846 
 1847 /*
 1848  *      Routine:        vm_object_coalesce
 1849  *      Function:       Coalesces two objects backing up adjoining
 1850  *                      regions of memory into a single object.
 1851  *
 1852  *      returns TRUE if objects were combined.
 1853  *
 1854  *      NOTE:   Only works at the moment if the second object is NULL -
 1855  *              if it's not, which object do we lock first?
 1856  *
 1857  *      Parameters:
 1858  *              prev_object     First object to coalesce
 1859  *              prev_offset     Offset into prev_object
 1860  *              prev_size       Size of reference to prev_object
 1861  *              next_size       Size of reference to the second object
 1862  *
 1863  *      Conditions:
 1864  *      The object must *not* be locked.
 1865  */
 1866 boolean_t
 1867 vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset,
 1868         vm_size_t prev_size, vm_size_t next_size)
 1869 {
 1870         vm_pindex_t next_pindex;
 1871 
 1872         if (prev_object == NULL)
 1873                 return (TRUE);
 1874         VM_OBJECT_LOCK(prev_object);
 1875         if (prev_object->type != OBJT_DEFAULT &&
 1876             prev_object->type != OBJT_SWAP) {
 1877                 VM_OBJECT_UNLOCK(prev_object);
 1878                 return (FALSE);
 1879         }
 1880 
 1881         /*
 1882          * Try to collapse the object first
 1883          */
 1884         vm_object_collapse(prev_object);
 1885 
 1886         /*
 1887          * Can't coalesce if: . more than one reference . paged out . shadows
 1888          * another object . has a copy elsewhere (any of which mean that the
 1889          * pages not mapped to prev_entry may be in use anyway)
 1890          */
 1891         if (prev_object->backing_object != NULL) {
 1892                 VM_OBJECT_UNLOCK(prev_object);
 1893                 return (FALSE);
 1894         }
 1895 
 1896         prev_size >>= PAGE_SHIFT;
 1897         next_size >>= PAGE_SHIFT;
 1898         next_pindex = OFF_TO_IDX(prev_offset) + prev_size;
 1899 
 1900         if ((prev_object->ref_count > 1) &&
 1901             (prev_object->size != next_pindex)) {
 1902                 VM_OBJECT_UNLOCK(prev_object);
 1903                 return (FALSE);
 1904         }
 1905 
 1906         /*
 1907          * Remove any pages that may still be in the object from a previous
 1908          * deallocation.
 1909          */
 1910         if (next_pindex < prev_object->size) {
 1911                 vm_object_page_remove(prev_object,
 1912                                       next_pindex,
 1913                                       next_pindex + next_size, FALSE);
 1914                 if (prev_object->type == OBJT_SWAP)
 1915                         swap_pager_freespace(prev_object,
 1916                                              next_pindex, next_size);
 1917         }
 1918 
 1919         /*
 1920          * Extend the object if necessary.
 1921          */
 1922         if (next_pindex + next_size > prev_object->size)
 1923                 prev_object->size = next_pindex + next_size;
 1924 
 1925         VM_OBJECT_UNLOCK(prev_object);
 1926         return (TRUE);
 1927 }
 1928 
 1929 void
 1930 vm_object_set_writeable_dirty(vm_object_t object)
 1931 {
 1932         struct vnode *vp;
 1933 
 1934         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
 1935         if ((object->flags & (OBJ_MIGHTBEDIRTY|OBJ_WRITEABLE)) ==
 1936             (OBJ_MIGHTBEDIRTY|OBJ_WRITEABLE))
 1937                 return;
 1938         vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
 1939         if (object->type == OBJT_VNODE &&
 1940             (vp = (struct vnode *)object->handle) != NULL) {
 1941                 VI_LOCK(vp);
 1942                 vp->v_iflag |= VI_OBJDIRTY;
 1943                 VI_UNLOCK(vp);
 1944         }
 1945 }
 1946 
 1947 #include "opt_ddb.h"
 1948 #ifdef DDB
 1949 #include <sys/kernel.h>
 1950 
 1951 #include <sys/cons.h>
 1952 
 1953 #include <ddb/ddb.h>
 1954 
 1955 static int
 1956 _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry)
 1957 {
 1958         vm_map_t tmpm;
 1959         vm_map_entry_t tmpe;
 1960         vm_object_t obj;
 1961         int entcount;
 1962 
 1963         if (map == 0)
 1964                 return 0;
 1965 
 1966         if (entry == 0) {
 1967                 tmpe = map->header.next;
 1968                 entcount = map->nentries;
 1969                 while (entcount-- && (tmpe != &map->header)) {
 1970                         if (_vm_object_in_map(map, object, tmpe)) {
 1971                                 return 1;
 1972                         }
 1973                         tmpe = tmpe->next;
 1974                 }
 1975         } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
 1976                 tmpm = entry->object.sub_map;
 1977                 tmpe = tmpm->header.next;
 1978                 entcount = tmpm->nentries;
 1979                 while (entcount-- && tmpe != &tmpm->header) {
 1980                         if (_vm_object_in_map(tmpm, object, tmpe)) {
 1981                                 return 1;
 1982                         }
 1983                         tmpe = tmpe->next;
 1984                 }
 1985         } else if ((obj = entry->object.vm_object) != NULL) {
 1986                 for (; obj; obj = obj->backing_object)
 1987                         if (obj == object) {
 1988                                 return 1;
 1989                         }
 1990         }
 1991         return 0;
 1992 }
 1993 
 1994 static int
 1995 vm_object_in_map(vm_object_t object)
 1996 {
 1997         struct proc *p;
 1998 
 1999         /* sx_slock(&allproc_lock); */
 2000         LIST_FOREACH(p, &allproc, p_list) {
 2001                 if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
 2002                         continue;
 2003                 if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
 2004                         /* sx_sunlock(&allproc_lock); */
 2005                         return 1;
 2006                 }
 2007         }
 2008         /* sx_sunlock(&allproc_lock); */
 2009         if (_vm_object_in_map(kernel_map, object, 0))
 2010                 return 1;
 2011         if (_vm_object_in_map(kmem_map, object, 0))
 2012                 return 1;
 2013         if (_vm_object_in_map(pager_map, object, 0))
 2014                 return 1;
 2015         if (_vm_object_in_map(buffer_map, object, 0))
 2016                 return 1;
 2017         return 0;
 2018 }
 2019 
 2020 DB_SHOW_COMMAND(vmochk, vm_object_check)
 2021 {
 2022         vm_object_t object;
 2023 
 2024         /*
 2025          * make sure that internal objs are in a map somewhere
 2026          * and none have zero ref counts.
 2027          */
 2028         TAILQ_FOREACH(object, &vm_object_list, object_list) {
 2029                 if (object->handle == NULL &&
 2030                     (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
 2031                         if (object->ref_count == 0) {
 2032                                 db_printf("vmochk: internal obj has zero ref count: %ld\n",
 2033                                         (long)object->size);
 2034                         }
 2035                         if (!vm_object_in_map(object)) {
 2036                                 db_printf(
 2037                         "vmochk: internal obj is not in a map: "
 2038                         "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
 2039                                     object->ref_count, (u_long)object->size, 
 2040                                     (u_long)object->size,
 2041                                     (void *)object->backing_object);
 2042                         }
 2043                 }
 2044         }
 2045 }
 2046 
 2047 /*
 2048  *      vm_object_print:        [ debug ]
 2049  */
 2050 DB_SHOW_COMMAND(object, vm_object_print_static)
 2051 {
 2052         /* XXX convert args. */
 2053         vm_object_t object = (vm_object_t)addr;
 2054         boolean_t full = have_addr;
 2055 
 2056         vm_page_t p;
 2057 
 2058         /* XXX count is an (unused) arg.  Avoid shadowing it. */
 2059 #define count   was_count
 2060 
 2061         int count;
 2062 
 2063         if (object == NULL)
 2064                 return;
 2065 
 2066         db_iprintf(
 2067             "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n",
 2068             object, (int)object->type, (uintmax_t)object->size,
 2069             object->resident_page_count, object->ref_count, object->flags);
 2070         db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n",
 2071             object->shadow_count, 
 2072             object->backing_object ? object->backing_object->ref_count : 0,
 2073             object->backing_object, (uintmax_t)object->backing_object_offset);
 2074 
 2075         if (!full)
 2076                 return;
 2077 
 2078         db_indent += 2;
 2079         count = 0;
 2080         TAILQ_FOREACH(p, &object->memq, listq) {
 2081                 if (count == 0)
 2082                         db_iprintf("memory:=");
 2083                 else if (count == 6) {
 2084                         db_printf("\n");
 2085                         db_iprintf(" ...");
 2086                         count = 0;
 2087                 } else
 2088                         db_printf(",");
 2089                 count++;
 2090 
 2091                 db_printf("(off=0x%jx,page=0x%jx)",
 2092                     (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p));
 2093         }
 2094         if (count != 0)
 2095                 db_printf("\n");
 2096         db_indent -= 2;
 2097 }
 2098 
 2099 /* XXX. */
 2100 #undef count
 2101 
 2102 /* XXX need this non-static entry for calling from vm_map_print. */
 2103 void
 2104 vm_object_print(
 2105         /* db_expr_t */ long addr,
 2106         boolean_t have_addr,
 2107         /* db_expr_t */ long count,
 2108         char *modif)
 2109 {
 2110         vm_object_print_static(addr, have_addr, count, modif);
 2111 }
 2112 
 2113 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
 2114 {
 2115         vm_object_t object;
 2116         int nl = 0;
 2117         int c;
 2118 
 2119         TAILQ_FOREACH(object, &vm_object_list, object_list) {
 2120                 vm_pindex_t idx, fidx;
 2121                 vm_pindex_t osize;
 2122                 vm_paddr_t pa = -1, padiff;
 2123                 int rcount;
 2124                 vm_page_t m;
 2125 
 2126                 db_printf("new object: %p\n", (void *)object);
 2127                 if (nl > 18) {
 2128                         c = cngetc();
 2129                         if (c != ' ')
 2130                                 return;
 2131                         nl = 0;
 2132                 }
 2133                 nl++;
 2134                 rcount = 0;
 2135                 fidx = 0;
 2136                 osize = object->size;
 2137                 if (osize > 128)
 2138                         osize = 128;
 2139                 for (idx = 0; idx < osize; idx++) {
 2140                         m = vm_page_lookup(object, idx);
 2141                         if (m == NULL) {
 2142                                 if (rcount) {
 2143                                         db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
 2144                                                 (long)fidx, rcount, (long)pa);
 2145                                         if (nl > 18) {
 2146                                                 c = cngetc();
 2147                                                 if (c != ' ')
 2148                                                         return;
 2149                                                 nl = 0;
 2150                                         }
 2151                                         nl++;
 2152                                         rcount = 0;
 2153                                 }
 2154                                 continue;
 2155                         }
 2156 
 2157                                 
 2158                         if (rcount &&
 2159                                 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
 2160                                 ++rcount;
 2161                                 continue;
 2162                         }
 2163                         if (rcount) {
 2164                                 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
 2165                                 padiff >>= PAGE_SHIFT;
 2166                                 padiff &= PQ_L2_MASK;
 2167                                 if (padiff == 0) {
 2168                                         pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
 2169                                         ++rcount;
 2170                                         continue;
 2171                                 }
 2172                                 db_printf(" index(%ld)run(%d)pa(0x%lx)",
 2173                                         (long)fidx, rcount, (long)pa);
 2174                                 db_printf("pd(%ld)\n", (long)padiff);
 2175                                 if (nl > 18) {
 2176                                         c = cngetc();
 2177                                         if (c != ' ')
 2178                                                 return;
 2179                                         nl = 0;
 2180                                 }
 2181                                 nl++;
 2182                         }
 2183                         fidx = idx;
 2184                         pa = VM_PAGE_TO_PHYS(m);
 2185                         rcount = 1;
 2186                 }
 2187                 if (rcount) {
 2188                         db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
 2189                                 (long)fidx, rcount, (long)pa);
 2190                         if (nl > 18) {
 2191                                 c = cngetc();
 2192                                 if (c != ' ')
 2193                                         return;
 2194                                 nl = 0;
 2195                         }
 2196                         nl++;
 2197                 }
 2198         }
 2199 }
 2200 #endif /* DDB */

Cache object: 09fa5947c03a861aac57d7b25725d1a9


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