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

Cache object: 16d6f48f4049a85125645e4ead4c1923


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