The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/vm/vm_object.c

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

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

Cache object: 621884dd3acd3e770ea103eedce7be3e


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