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


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

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

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

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

Cache object: c0a09bc76e6b383b045392671376fb73


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