vm_object.c revision 118537
1139826Simp/* 253541Sshin * Copyright (c) 1991, 1993 353541Sshin * The Regents of the University of California. All rights reserved. 453541Sshin * 553541Sshin * This code is derived from software contributed to Berkeley by 653541Sshin * The Mach Operating System project at Carnegie-Mellon University. 753541Sshin * 853541Sshin * Redistribution and use in source and binary forms, with or without 953541Sshin * modification, are permitted provided that the following conditions 1053541Sshin * are met: 1153541Sshin * 1. Redistributions of source code must retain the above copyright 1253541Sshin * notice, this list of conditions and the following disclaimer. 1353541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1453541Sshin * notice, this list of conditions and the following disclaimer in the 1553541Sshin * documentation and/or other materials provided with the distribution. 1653541Sshin * 3. All advertising materials mentioning features or use of this software 1753541Sshin * must display the following acknowledgement: 1853541Sshin * This product includes software developed by the University of 1953541Sshin * California, Berkeley and its contributors. 2053541Sshin * 4. Neither the name of the University nor the names of its contributors 2153541Sshin * may be used to endorse or promote products derived from this software 2253541Sshin * without specific prior written permission. 2353541Sshin * 2453541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2553541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2653541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2753541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28174510Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29174510Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3053541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3153541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32174510Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33174510Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34174510Sobrien * SUCH DAMAGE. 3553541Sshin * 3653541Sshin * from: @(#)vm_object.c 8.5 (Berkeley) 3/22/94 3753541Sshin * 3853541Sshin * 3953541Sshin * Copyright (c) 1987, 1990 Carnegie-Mellon University. 4053541Sshin * All rights reserved. 4153541Sshin * 4253541Sshin * Authors: Avadis Tevanian, Jr., Michael Wayne Young 4353541Sshin * 4453541Sshin * Permission to use, copy, modify and distribute this software and 4553541Sshin * its documentation is hereby granted, provided that both the copyright 4653541Sshin * notice and this permission notice appear in all copies of the 4753541Sshin * software, derivative works or modified versions, and any portions 4853541Sshin * thereof, and that both notices appear in supporting documentation. 49195699Srwatson * 5053541Sshin * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 5153541Sshin * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 5253541Sshin * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 5362587Sitojun * 5453541Sshin * Carnegie Mellon requests users of this software to return to 5562587Sitojun * 56121684Sume * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 57121684Sume * School of Computer Science 5853541Sshin * Carnegie Mellon University 59184307Srwatson * Pittsburgh PA 15213-3890 60184307Srwatson * 6153541Sshin * any improvements or extensions that they make and grant Carnegie the 6253541Sshin * rights to redistribute these changes. 6353541Sshin */ 6453541Sshin 6553541Sshin/* 6662587Sitojun * Virtual memory object module. 6753541Sshin */ 68175162Sobrien 69175162Sobrien#include <sys/cdefs.h> 70175162Sobrien__FBSDID("$FreeBSD: head/sys/vm/vm_object.c 118537 2003-08-06 12:09:34Z phk $"); 71175162Sobrien 72175162Sobrien#include <sys/param.h> 7353541Sshin#include <sys/systm.h> 74121346Sume#include <sys/lock.h> 75121346Sume#include <sys/mman.h> 76121346Sume#include <sys/mount.h> 77121346Sume#include <sys/kernel.h> 78195699Srwatson#include <sys/sysctl.h> 79195699Srwatson#include <sys/mutex.h> 80195699Srwatson#include <sys/proc.h> /* for curproc, pageproc */ 8153541Sshin#include <sys/socket.h> 82195727Srwatson#include <sys/vnode.h> 83195727Srwatson#include <sys/vmmeter.h> 84195727Srwatson#include <sys/sx.h> 85195699Srwatson 86121346Sume#include <vm/vm.h> 87121346Sume#include <vm/vm_param.h> 88121346Sume#include <vm/pmap.h> 89121355Sume#include <vm/vm_map.h> 90121346Sume#include <vm/vm_object.h> 91121345Sume#include <vm/vm_page.h> 9269774Sphk#include <vm/vm_pageout.h> 9362587Sitojun#include <vm/vm_pager.h> 9453541Sshin#include <vm/swap_pager.h> 9553541Sshin#include <vm/vm_kern.h> 9653541Sshin#include <vm/vm_extern.h> 97157927Sps#include <vm/uma.h> 98157927Sps 99157927Sps#define EASY_SCAN_FACTOR 8 100157927Sps 101181803Sbz#define MSYNC_FLUSH_HARDSEQ 0x01 102181803Sbz#define MSYNC_FLUSH_SOFTSEQ 0x02 103157927Sps 104157927Sps/* 10553541Sshin * msync / VM object flushing optimizations 106171259Sdelphij */ 10753541Sshinstatic int msync_flush_flags = MSYNC_FLUSH_HARDSEQ | MSYNC_FLUSH_SOFTSEQ; 10853541SshinSYSCTL_INT(_vm, OID_AUTO, msync_flush_flags, 109181803Sbz CTLFLAG_RW, &msync_flush_flags, 0, ""); 110181803Sbz 111207369Sbzstatic void vm_object_qcollapse(vm_object_t object); 112190787Szecstatic int vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags); 113190787Szec 114190787Szec/* 115190787Szec * Virtual memory objects maintain the actual data 116157927Sps * associated with allocated virtual memory. A given 117157927Sps * page of memory exists within exactly one object. 118207369Sbz * 119207369Sbz * An object is only deallocated when all "references" 12053541Sshin * are given up. Only one "reference" to a given 12153541Sshin * region of an object should be writeable. 12253541Sshin * 12362587Sitojun * Associated with each object is a list of all resident 12462587Sitojun * memory pages belonging to that object; this list is 12562587Sitojun * maintained by the "vm_page" module, and locked by the object's 12662587Sitojun * lock. 12762587Sitojun * 12862587Sitojun * Each object also records a "pager" routine which is 12962587Sitojun * used to retrieve (and store) pages to the proper backing 13062587Sitojun * storage. In addition, objects may be backed by other 13162587Sitojun * objects from which they were virtual-copied. 13262587Sitojun * 13362587Sitojun * The only items within the object structure which are 13462587Sitojun * modified after time of creation are: 13562587Sitojun * reference count locked by object's lock 13662587Sitojun * pager routine locked by object's lock 13762587Sitojun * 13862587Sitojun */ 13962587Sitojun 14062587Sitojunstruct object_q vm_object_list; 14162587Sitojunstruct mtx vm_object_list_mtx; /* lock for object list and count */ 14262587Sitojun 14362587Sitojunstruct vm_object kernel_object_store; 14462587Sitojunstruct vm_object kmem_object_store; 14562587Sitojun 14662587Sitojunstatic long object_collapses; 14762587Sitojunstatic long object_bypasses; 14862587Sitojunstatic int next_index; 14962587Sitojunstatic uma_zone_t obj_zone; 15062587Sitojun#define VM_OBJECTS_INIT 256 15162587Sitojun 15253541Sshinstatic void vm_object_zinit(void *mem, int size); 15353541Sshin 15453541Sshin#ifdef INVARIANTS 155171259Sdelphijstatic void vm_object_zdtor(void *mem, int size, void *arg); 15653541Sshin 15753541Sshinstatic void 15853541Sshinvm_object_zdtor(void *mem, int size, void *arg) 15953541Sshin{ 16053541Sshin vm_object_t object; 16162587Sitojun 162121630Sume object = (vm_object_t)mem; 163121630Sume KASSERT(object->paging_in_progress == 0, 164121630Sume ("object %p paging_in_progress = %d", 16553541Sshin object, object->paging_in_progress)); 16653541Sshin KASSERT(object->resident_page_count == 0, 16762587Sitojun ("object %p resident_page_count = %d", 16853541Sshin object, object->resident_page_count)); 169121684Sume KASSERT(object->shadow_count == 0, 170165118Sbz ("object %p shadow_count = %d", 171165118Sbz object, object->shadow_count)); 172165118Sbz} 17353541Sshin#endif 17462587Sitojun 17562587Sitojunstatic void 17653541Sshinvm_object_zinit(void *mem, int size) 17753541Sshin{ 17862587Sitojun vm_object_t object; 17962587Sitojun 18062587Sitojun object = (vm_object_t)mem; 181120856Sume bzero(&object->mtx, sizeof(object->mtx)); 18262587Sitojun VM_OBJECT_LOCK_INIT(object); 18353541Sshin 18453541Sshin /* These are true for any object that has been freed */ 18553541Sshin object->paging_in_progress = 0; 18653541Sshin object->resident_page_count = 0; 187194760Srwatson object->shadow_count = 0; 188121630Sume} 189194760Srwatson 190194760Srwatsonvoid 19153541Sshin_vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object) 19253541Sshin{ 19353541Sshin int incr; 19453541Sshin 19553541Sshin TAILQ_INIT(&object->memq); 19653541Sshin LIST_INIT(&object->shadow_head); 19753541Sshin 19853541Sshin object->root = NULL; 19953541Sshin object->type = type; 20053541Sshin object->size = size; 20153541Sshin object->ref_count = 1; 20253541Sshin object->flags = 0; 20353541Sshin if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP)) 20453541Sshin object->flags = OBJ_ONEMAPPING; 20553541Sshin if (size > (PQ_L2_SIZE / 3 + PQ_PRIME1)) 20653541Sshin incr = PQ_L2_SIZE / 3 + PQ_PRIME1; 20753541Sshin else 20853541Sshin incr = size; 20953541Sshin do 21053541Sshin object->pg_color = next_index; 21153541Sshin while (!atomic_cmpset_int(&next_index, object->pg_color, 212120891Sume (object->pg_color + incr) & PQ_L2_MASK)); 213120891Sume object->handle = NULL; 21453541Sshin object->backing_object = NULL; 21553541Sshin object->backing_object_offset = (vm_ooffset_t) 0; 21653541Sshin 21753541Sshin atomic_add_int(&object->generation, 1); 218181803Sbz 21953541Sshin mtx_lock(&vm_object_list_mtx); 220120891Sume TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 22162587Sitojun mtx_unlock(&vm_object_list_mtx); 22253541Sshin} 22353541Sshin 224121345Sume/* 22578064Sume * vm_object_init: 226121345Sume * 227121345Sume * Initialize the VM objects module. 228121345Sume */ 229121345Sumevoid 230121345Sumevm_object_init(void) 231181803Sbz{ 232121345Sume TAILQ_INIT(&vm_object_list); 233181803Sbz mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF); 234121345Sume 235121345Sume VM_OBJECT_LOCK_INIT(&kernel_object_store); 236181803Sbz _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 23753541Sshin kernel_object); 23853541Sshin 239184307Srwatson /* 240184307Srwatson * The kmem object's mutex is given a unique name, instead of 241184307Srwatson * "vm object", to avoid false reports of lock-order reversal 242184307Srwatson * with a system map mutex. 243184307Srwatson */ 24453541Sshin mtx_init(VM_OBJECT_MTX(kmem_object), "kmem object", NULL, MTX_DEF); 24553541Sshin _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 246181803Sbz kmem_object); 24753541Sshin 24853541Sshin obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL, 24953541Sshin#ifdef INVARIANTS 25053541Sshin vm_object_zdtor, 25153541Sshin#else 25253541Sshin NULL, 25353541Sshin#endif 25453541Sshin vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 255121345Sume uma_prealloc(obj_zone, VM_OBJECTS_INIT); 256121345Sume} 257121345Sume 25853541Sshinvoid 259181803Sbzvm_object_set_flag(vm_object_t object, u_short bits) 26078064Sume{ 261181803Sbz object->flags |= bits; 26278064Sume} 263181803Sbz 26453541Sshinvoid 265121607Sumevm_object_clear_flag(vm_object_t object, u_short bits) 26653541Sshin{ 26753541Sshin 26862587Sitojun VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 269184307Srwatson object->flags &= ~bits; 270184307Srwatson} 271184307Srwatson 272184307Srwatsonvoid 273184307Srwatsonvm_object_pip_add(vm_object_t object, short i) 274184307Srwatson{ 275184307Srwatson 276181803Sbz VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 27753541Sshin object->paging_in_progress += i; 27862587Sitojun} 27953541Sshin 28062587Sitojunvoid 28162587Sitojunvm_object_pip_subtract(vm_object_t object, short i) 28262587Sitojun{ 28353541Sshin 284171260Sdelphij VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 28553541Sshin object->paging_in_progress -= i; 28653541Sshin} 287170275Sjinmei 288170275Sjinmeivoid 28953541Sshinvm_object_pip_wakeup(vm_object_t object) 290121345Sume{ 291121345Sume 29253541Sshin VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 29353541Sshin object->paging_in_progress--; 29453541Sshin if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) { 29553541Sshin vm_object_clear_flag(object, OBJ_PIPWNT); 29653541Sshin wakeup(object); 29753541Sshin } 29853541Sshin} 29953541Sshin 300120891Sumevoid 301120891Sumevm_object_pip_wakeupn(vm_object_t object, short i) 30253541Sshin{ 30353541Sshin 30453541Sshin VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 30553541Sshin if (i) 30653541Sshin object->paging_in_progress -= i; 30753541Sshin if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) { 30853541Sshin vm_object_clear_flag(object, OBJ_PIPWNT); 30953541Sshin wakeup(object); 31062587Sitojun } 31153541Sshin} 31253541Sshin 31353541Sshinvoid 31453541Sshinvm_object_pip_wait(vm_object_t object, char *waitid) 315120891Sume{ 316120891Sume 317121345Sume VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 318120856Sume while (object->paging_in_progress) { 31953541Sshin object->flags |= OBJ_PIPWNT; 320120891Sume msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0); 32153541Sshin } 322120891Sume} 323120891Sume 324121345Sume/* 325120856Sume * vm_object_allocate_wait 32653541Sshin * 32753541Sshin * Return a new object with the given size, and give the user the 32853541Sshin * option of waiting for it to complete or failing if the needed 32953541Sshin * memory isn't available. 33053541Sshin */ 33153541Sshinvm_object_t 33253541Sshinvm_object_allocate_wait(objtype_t type, vm_pindex_t size, int flags) 33353541Sshin{ 33453541Sshin vm_object_t result; 33553541Sshin 33653541Sshin result = (vm_object_t) uma_zalloc(obj_zone, flags); 33753541Sshin 33853541Sshin if (result != NULL) 33953541Sshin _vm_object_allocate(type, size, result); 34053541Sshin 34153541Sshin return (result); 34253541Sshin} 34353541Sshin 34462587Sitojun/* 34553541Sshin * vm_object_allocate: 34653541Sshin * 34753541Sshin * Returns a new object with the given size. 34853541Sshin */ 34953541Sshinvm_object_t 35053541Sshinvm_object_allocate(objtype_t type, vm_pindex_t size) 35153541Sshin{ 35253541Sshin return(vm_object_allocate_wait(type, size, M_WAITOK)); 35353541Sshin} 35453541Sshin 35553541Sshin 35653541Sshin/* 357120891Sume * vm_object_reference: 358120891Sume * 359120891Sume * Gets another reference to the given object. Note: OBJ_DEAD 36053541Sshin * objects can be referenced during final cleaning. 36153541Sshin */ 36253541Sshinvoid 36353541Sshinvm_object_reference(vm_object_t object) 36462587Sitojun{ 365121607Sume if (object == NULL) 36662587Sitojun return; 36762587Sitojun if (object != kernel_object && 36862587Sitojun object != kmem_object) 36953541Sshin mtx_lock(&Giant); 37053541Sshin VM_OBJECT_LOCK(object); 37153541Sshin object->ref_count++; 37253541Sshin VM_OBJECT_UNLOCK(object); 37353541Sshin if (object->type == OBJT_VNODE) { 37453541Sshin while (vget((struct vnode *) object->handle, LK_RETRY, curthread)) { 37553541Sshin printf("vm_object_reference: delay in getting object\n"); 37653541Sshin } 37753541Sshin } 37853541Sshin if (object != kernel_object && 37953541Sshin object != kmem_object) 38053541Sshin mtx_unlock(&Giant); 381121684Sume} 382121684Sume 383121684Sume/* 384121684Sume * Handle deallocating an object of type OBJT_VNODE. 385121684Sume */ 386170275Sjinmeivoid 387121684Sumevm_object_vndeallocate(vm_object_t object) 388121684Sume{ 389121684Sume struct vnode *vp = (struct vnode *) object->handle; 390121684Sume 391121684Sume GIANT_REQUIRED; 392121684Sume VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 393170275Sjinmei KASSERT(object->type == OBJT_VNODE, 394121684Sume ("vm_object_vndeallocate: not a vnode object")); 395121684Sume KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp")); 396121684Sume#ifdef INVARIANTS 397121684Sume if (object->ref_count == 0) { 398121684Sume vprint("vm_object_vndeallocate", vp); 399121684Sume panic("vm_object_vndeallocate: bad object reference count"); 400121684Sume } 40153541Sshin#endif 40253541Sshin 40353541Sshin object->ref_count--; 40453541Sshin if (object->ref_count == 0) { 40553541Sshin mp_fixme("Unlocked vflag access."); 40653541Sshin vp->v_vflag &= ~VV_TEXT; 40753541Sshin } 40862587Sitojun VM_OBJECT_UNLOCK(object); 40953541Sshin /* 41062587Sitojun * vrele may need a vop lock 41162587Sitojun */ 41262587Sitojun vrele(vp); 41362587Sitojun} 41462587Sitojun 41562587Sitojun/* 41662587Sitojun * vm_object_deallocate: 41762587Sitojun * 41862587Sitojun * Release a reference to the specified object, 41962587Sitojun * gained either through a vm_object_allocate 42062587Sitojun * or a vm_object_reference call. When all references 42162587Sitojun * are gone, storage associated with this object 42262587Sitojun * may be relinquished. 42362587Sitojun * 42462587Sitojun * No object may be locked. 42562587Sitojun */ 42662587Sitojunvoid 42762587Sitojunvm_object_deallocate(vm_object_t object) 42862587Sitojun{ 42962587Sitojun vm_object_t temp; 43062587Sitojun 43162587Sitojun if (object != kmem_object) 43262587Sitojun mtx_lock(&Giant); 43362587Sitojun while (object != NULL) { 43462587Sitojun VM_OBJECT_LOCK(object); 43562587Sitojun if (object->type == OBJT_VNODE) { 43662587Sitojun vm_object_vndeallocate(object); 43762587Sitojun goto done; 43862587Sitojun } 43962587Sitojun 44062587Sitojun KASSERT(object->ref_count != 0, 44162587Sitojun ("vm_object_deallocate: object deallocated too many times: %d", object->type)); 44262587Sitojun 44362587Sitojun /* 44462587Sitojun * If the reference count goes to 0 we start calling 44553541Sshin * vm_object_terminate() on the object chain. 44653541Sshin * A ref count of 1 may be a special case depending on the 44753541Sshin * shadow count being 0 or 1. 448121345Sume */ 449121345Sume object->ref_count--; 450170275Sjinmei if (object->ref_count > 1) { 451170275Sjinmei VM_OBJECT_UNLOCK(object); 452170275Sjinmei goto done; 45353541Sshin } else if (object->ref_count == 1) { 45453541Sshin if (object->shadow_count == 0) { 45553541Sshin vm_object_set_flag(object, OBJ_ONEMAPPING); 45653541Sshin } else if ((object->shadow_count == 1) && 45753541Sshin (object->handle == NULL) && 45876899Ssumikawa (object->type == OBJT_DEFAULT || 45953541Sshin object->type == OBJT_SWAP)) { 46053541Sshin vm_object_t robject; 461165118Sbz 46276899Ssumikawa robject = LIST_FIRST(&object->shadow_head); 46376899Ssumikawa KASSERT(robject != NULL, 46453541Sshin ("vm_object_deallocate: ref_count: %d, shadow_count: %d", 46553541Sshin object->ref_count, 46653541Sshin object->shadow_count)); 46753541Sshin if (!VM_OBJECT_TRYLOCK(robject)) { 46853541Sshin /* 46953541Sshin * Avoid a potential deadlock. 47076899Ssumikawa */ 47153541Sshin object->ref_count++; 47253541Sshin VM_OBJECT_UNLOCK(object); 473165118Sbz continue; 47476899Ssumikawa } 47576899Ssumikawa if ((robject->handle == NULL) && 47653541Sshin (robject->type == OBJT_DEFAULT || 47753541Sshin robject->type == OBJT_SWAP)) { 47853541Sshin 47962587Sitojun robject->ref_count++; 48053541Sshinretry: 48153541Sshin if (robject->paging_in_progress) { 482184307Srwatson VM_OBJECT_UNLOCK(object); 483184307Srwatson vm_object_pip_wait(robject, 484184307Srwatson "objde1"); 485184307Srwatson VM_OBJECT_LOCK(object); 48653541Sshin goto retry; 48753541Sshin } else if (object->paging_in_progress) { 48853541Sshin VM_OBJECT_UNLOCK(robject); 48953541Sshin object->flags |= OBJ_PIPWNT; 49053541Sshin msleep(object, 49153541Sshin VM_OBJECT_MTX(object), 49253541Sshin PDROP | PVM, "objde2", 0); 49353541Sshin VM_OBJECT_LOCK(robject); 494181803Sbz VM_OBJECT_LOCK(object); 495121345Sume goto retry; 49662587Sitojun } 497181803Sbz VM_OBJECT_UNLOCK(object); 49862587Sitojun if (robject->ref_count == 1) { 499181803Sbz robject->ref_count--; 50062587Sitojun object = robject; 50162587Sitojun goto doterm; 50253541Sshin } 50353541Sshin object = robject; 50453541Sshin vm_object_collapse(object); 50553541Sshin VM_OBJECT_UNLOCK(object); 506121345Sume continue; 50753541Sshin } 50853541Sshin VM_OBJECT_UNLOCK(robject); 50953541Sshin } 51053541Sshin VM_OBJECT_UNLOCK(object); 51153541Sshin goto done; 512121345Sume } 51353541Sshindoterm: 51453541Sshin temp = object->backing_object; 51553541Sshin if (temp != NULL) { 51653541Sshin VM_OBJECT_LOCK(temp); 51753541Sshin LIST_REMOVE(object, shadow_list); 51853541Sshin temp->shadow_count--; 51953541Sshin temp->generation++; 52053541Sshin VM_OBJECT_UNLOCK(temp); 52153541Sshin object->backing_object = NULL; 52262587Sitojun } 52353541Sshin /* 52462587Sitojun * Don't double-terminate, we could be in a termination 52562587Sitojun * recursion due to the terminate having to sync data 52653541Sshin * to disk. 52753541Sshin */ 52853541Sshin if ((object->flags & OBJ_DEAD) == 0) 52962587Sitojun vm_object_terminate(object); 53062587Sitojun else 53162587Sitojun VM_OBJECT_UNLOCK(object); 53253541Sshin object = temp; 53353541Sshin } 53453541Sshindone: 53553541Sshin if (object != kmem_object) 53662587Sitojun mtx_unlock(&Giant); 53762587Sitojun} 53853541Sshin 539170275Sjinmei/* 540170275Sjinmei * vm_object_terminate actually destroys the specified object, freeing 54153541Sshin * up all previously used resources. 54262587Sitojun * 54362587Sitojun * The object must be locked. 54462587Sitojun * This routine may block. 54553541Sshin */ 546170275Sjinmeivoid 547170275Sjinmeivm_object_terminate(vm_object_t object) 548170275Sjinmei{ 54953541Sshin vm_page_t p; 550170275Sjinmei int s; 55162587Sitojun 55262587Sitojun VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 55362587Sitojun 55462587Sitojun /* 555111119Simp * Make sure no one uses us. 55662587Sitojun */ 557181803Sbz vm_object_set_flag(object, OBJ_DEAD); 558184307Srwatson 559184307Srwatson /* 560184307Srwatson * wait for the pageout daemon to be done with the object 56162587Sitojun */ 562181803Sbz vm_object_pip_wait(object, "objtrm"); 56362587Sitojun 56462587Sitojun KASSERT(!object->paging_in_progress, 56562587Sitojun ("vm_object_terminate: pageout in progress")); 56662587Sitojun 56753541Sshin /* 56853541Sshin * Clean and free the pages, as appropriate. All references to the 56953541Sshin * object are gone, so we don't need to lock it. 57053541Sshin */ 57153541Sshin if (object->type == OBJT_VNODE) { 57253541Sshin struct vnode *vp = (struct vnode *)object->handle; 57353541Sshin 57453541Sshin /* 57553541Sshin * Clean pages and flush buffers. 57653541Sshin */ 57753541Sshin vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 578181803Sbz VM_OBJECT_UNLOCK(object); 579184307Srwatson 580184307Srwatson vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0); 581184307Srwatson 582184307Srwatson VM_OBJECT_LOCK(object); 58353541Sshin } 584181803Sbz 58553541Sshin KASSERT(object->ref_count == 0, 58653541Sshin ("vm_object_terminate: object with references, ref_count=%d", 58753541Sshin object->ref_count)); 58853541Sshin 58953541Sshin /* 59053541Sshin * Now free any remaining pages. For internal objects, this also 59153541Sshin * removes them from paging queues. Don't free wired pages, just 592120891Sume * remove them from the object. 593181803Sbz */ 59453541Sshin s = splvm(); 59553541Sshin vm_page_lock_queues(); 59653541Sshin while ((p = TAILQ_FIRST(&object->memq)) != NULL) { 59753541Sshin KASSERT(!p->busy && (p->flags & PG_BUSY) == 0, 59853541Sshin ("vm_object_terminate: freeing busy page %p " 59953541Sshin "p->busy = %d, p->flags %x\n", p, p->busy, p->flags)); 60053541Sshin if (p->wire_count == 0) { 60153541Sshin vm_page_busy(p); 60253541Sshin vm_page_free(p); 603121345Sume cnt.v_pfree++; 60453541Sshin } else { 60553541Sshin vm_page_busy(p); 60653541Sshin vm_page_remove(p); 607121346Sume } 60853541Sshin } 609181803Sbz vm_page_unlock_queues(); 61053541Sshin splx(s); 61153541Sshin 61253541Sshin /* 61353541Sshin * Let the pager know object is dead. 61453541Sshin */ 61553541Sshin vm_pager_deallocate(object); 61653541Sshin VM_OBJECT_UNLOCK(object); 61753541Sshin 61853541Sshin /* 619171259Sdelphij * Remove the object from the global object list. 62053541Sshin */ 62153541Sshin mtx_lock(&vm_object_list_mtx); 62253541Sshin TAILQ_REMOVE(&vm_object_list, object, object_list); 623121355Sume mtx_unlock(&vm_object_list_mtx); 624121345Sume 62553541Sshin wakeup(object); 62653541Sshin 62753541Sshin /* 62853541Sshin * Free the space for the object. 62953541Sshin */ 63053541Sshin uma_zfree(obj_zone, object); 63153541Sshin} 63253541Sshin 63353541Sshin/* 63453541Sshin * vm_object_page_clean 63553541Sshin * 63653541Sshin * Clean all dirty pages in the specified range of object. Leaves page 63753541Sshin * on whatever queue it is currently on. If NOSYNC is set then do not 63853541Sshin * write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC), 63953541Sshin * leaving the object dirty. 64053541Sshin * 64153541Sshin * When stuffing pages asynchronously, allow clustering. XXX we need a 642120891Sume * synchronous clustering mode implementation. 64353541Sshin * 64453541Sshin * Odd semantics: if start == end, we clean everything. 64553541Sshin * 64653541Sshin * The object must be locked. 64753541Sshin */ 64862587Sitojunvoid 64953541Sshinvm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags) 65062587Sitojun{ 65153541Sshin vm_page_t p, np; 65253541Sshin vm_pindex_t tstart, tend; 653181803Sbz vm_pindex_t pi; 654184307Srwatson int clearobjflags; 655184307Srwatson int pagerflags; 656184307Srwatson int curgeneration; 65753541Sshin 658181803Sbz GIANT_REQUIRED; 65953541Sshin VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 66053541Sshin if (object->type != OBJT_VNODE || 66153541Sshin (object->flags & OBJ_MIGHTBEDIRTY) == 0) 66253541Sshin return; 66353541Sshin 66453541Sshin pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK; 66553541Sshin pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0; 666171259Sdelphij 66753541Sshin vm_object_set_flag(object, OBJ_CLEANING); 668121345Sume 669121355Sume tstart = start; 670121345Sume if (end == 0) { 67153541Sshin tend = object->size; 67253541Sshin } else { 67353541Sshin tend = end; 67453541Sshin } 67553541Sshin 67653541Sshin vm_page_lock_queues(); 67753541Sshin /* 67853541Sshin * If the caller is smart and only msync()s a range he knows is 67953541Sshin * dirty, we may be able to avoid an object scan. This results in 68053541Sshin * a phenominal improvement in performance. We cannot do this 681171259Sdelphij * as a matter of course because the object may be huge - e.g. 68253541Sshin * the size might be in the gigabytes or terrabytes. 683121345Sume */ 684121355Sume if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) { 685121345Sume vm_pindex_t tscan; 68653541Sshin int scanlimit; 68753541Sshin int scanreset; 68853541Sshin 68953541Sshin scanreset = object->resident_page_count / EASY_SCAN_FACTOR; 69053541Sshin if (scanreset < 16) 691171259Sdelphij scanreset = 16; 69253541Sshin pagerflags |= VM_PAGER_IGNORE_CLEANCHK; 693121345Sume 694121355Sume scanlimit = scanreset; 695121345Sume tscan = tstart; 69653541Sshin while (tscan < tend) { 69753541Sshin curgeneration = object->generation; 69853541Sshin p = vm_page_lookup(object, tscan); 69953541Sshin if (p == NULL || p->valid == 0 || 70053541Sshin (p->queue - p->pc) == PQ_CACHE) { 70153541Sshin if (--scanlimit == 0) 70253541Sshin break; 703171259Sdelphij ++tscan; 70453541Sshin continue; 705121345Sume } 706121355Sume vm_page_test_dirty(p); 707121345Sume if ((p->dirty & p->valid) == 0) { 70853541Sshin if (--scanlimit == 0) 70953541Sshin break; 71053541Sshin ++tscan; 71153541Sshin continue; 71253541Sshin } 71378064Sume /* 71453541Sshin * If we have been asked to skip nosync pages and 71553541Sshin * this is a nosync page, we can't continue. 71653541Sshin */ 71753541Sshin if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) { 718171259Sdelphij if (--scanlimit == 0) 71953541Sshin break; 720183550Szec ++tscan; 72153541Sshin continue; 72253541Sshin } 723195760Srwatson scanlimit = scanreset; 724121345Sume 725183550Szec /* 726183550Szec * This returns 0 if it was unable to busy the first 727183550Szec * page (i.e. had to sleep). 728183550Szec */ 729183550Szec tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags); 730183550Szec } 731183550Szec 732183550Szec /* 733183550Szec * If everything was dirty and we flushed it successfully, 734183550Szec * and the requested range is not the entire object, we 735183550Szec * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can 736183550Szec * return immediately. 73753541Sshin */ 738183550Szec if (tscan >= tend && (tstart || tend < object->size)) { 739183550Szec vm_page_unlock_queues(); 740183550Szec vm_object_clear_flag(object, OBJ_CLEANING); 741183550Szec return; 742183550Szec } 743183550Szec pagerflags &= ~VM_PAGER_IGNORE_CLEANCHK; 744183550Szec } 745183550Szec 746183550Szec /* 747183550Szec * Generally set CLEANCHK interlock and make the page read-only so 74853541Sshin * we can then clear the object flags. 749183550Szec * 75053541Sshin * However, if this is a nosync mmap then the object is likely to 751121345Sume * stay dirty so do not mess with the page and do not clear the 752195760Srwatson * object flags. 75353541Sshin */ 75453541Sshin clearobjflags = 1; 75553541Sshin TAILQ_FOREACH(p, &object->memq, listq) { 75653541Sshin vm_page_flag_set(p, PG_CLEANCHK); 75753541Sshin if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) 75853541Sshin clearobjflags = 0; 759171259Sdelphij else 76053541Sshin pmap_page_protect(p, VM_PROT_READ); 761183550Szec } 762121345Sume 763195760Srwatson if (clearobjflags && (tstart == 0) && (tend == object->size)) { 764195760Srwatson struct vnode *vp; 765195760Srwatson 76653541Sshin vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY); 767195760Srwatson if (object->type == OBJT_VNODE && 768183550Szec (vp = (struct vnode *)object->handle) != NULL) { 769183550Szec VI_LOCK(vp); 770183550Szec if (vp->v_iflag & VI_OBJDIRTY) 771183550Szec vp->v_iflag &= ~VI_OBJDIRTY; 772183550Szec VI_UNLOCK(vp); 773183550Szec } 774183550Szec } 775183550Szec 77653541Sshinrescan: 777121345Sume curgeneration = object->generation; 778195760Srwatson 77953541Sshin for (p = TAILQ_FIRST(&object->memq); p; p = np) { 780 int n; 781 782 np = TAILQ_NEXT(p, listq); 783 784again: 785 pi = p->pindex; 786 if (((p->flags & PG_CLEANCHK) == 0) || 787 (pi < tstart) || (pi >= tend) || 788 (p->valid == 0) || 789 ((p->queue - p->pc) == PQ_CACHE)) { 790 vm_page_flag_clear(p, PG_CLEANCHK); 791 continue; 792 } 793 794 vm_page_test_dirty(p); 795 if ((p->dirty & p->valid) == 0) { 796 vm_page_flag_clear(p, PG_CLEANCHK); 797 continue; 798 } 799 800 /* 801 * If we have been asked to skip nosync pages and this is a 802 * nosync page, skip it. Note that the object flags were 803 * not cleared in this case so we do not have to set them. 804 */ 805 if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) { 806 vm_page_flag_clear(p, PG_CLEANCHK); 807 continue; 808 } 809 810 n = vm_object_page_collect_flush(object, p, 811 curgeneration, pagerflags); 812 if (n == 0) 813 goto rescan; 814 815 if (object->generation != curgeneration) 816 goto rescan; 817 818 /* 819 * Try to optimize the next page. If we can't we pick up 820 * our (random) scan where we left off. 821 */ 822 if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) { 823 if ((p = vm_page_lookup(object, pi + n)) != NULL) 824 goto again; 825 } 826 } 827 vm_page_unlock_queues(); 828#if 0 829 VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc); 830#endif 831 832 vm_object_clear_flag(object, OBJ_CLEANING); 833 return; 834} 835 836static int 837vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags) 838{ 839 int runlen; 840 int s; 841 int maxf; 842 int chkb; 843 int maxb; 844 int i; 845 vm_pindex_t pi; 846 vm_page_t maf[vm_pageout_page_count]; 847 vm_page_t mab[vm_pageout_page_count]; 848 vm_page_t ma[vm_pageout_page_count]; 849 850 s = splvm(); 851 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 852 pi = p->pindex; 853 while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) { 854 vm_page_lock_queues(); 855 if (object->generation != curgeneration) { 856 splx(s); 857 return(0); 858 } 859 } 860 maxf = 0; 861 for(i = 1; i < vm_pageout_page_count; i++) { 862 vm_page_t tp; 863 864 if ((tp = vm_page_lookup(object, pi + i)) != NULL) { 865 if ((tp->flags & PG_BUSY) || 866 ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 && 867 (tp->flags & PG_CLEANCHK) == 0) || 868 (tp->busy != 0)) 869 break; 870 if((tp->queue - tp->pc) == PQ_CACHE) { 871 vm_page_flag_clear(tp, PG_CLEANCHK); 872 break; 873 } 874 vm_page_test_dirty(tp); 875 if ((tp->dirty & tp->valid) == 0) { 876 vm_page_flag_clear(tp, PG_CLEANCHK); 877 break; 878 } 879 maf[ i - 1 ] = tp; 880 maxf++; 881 continue; 882 } 883 break; 884 } 885 886 maxb = 0; 887 chkb = vm_pageout_page_count - maxf; 888 if (chkb) { 889 for(i = 1; i < chkb;i++) { 890 vm_page_t tp; 891 892 if ((tp = vm_page_lookup(object, pi - i)) != NULL) { 893 if ((tp->flags & PG_BUSY) || 894 ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 && 895 (tp->flags & PG_CLEANCHK) == 0) || 896 (tp->busy != 0)) 897 break; 898 if ((tp->queue - tp->pc) == PQ_CACHE) { 899 vm_page_flag_clear(tp, PG_CLEANCHK); 900 break; 901 } 902 vm_page_test_dirty(tp); 903 if ((tp->dirty & tp->valid) == 0) { 904 vm_page_flag_clear(tp, PG_CLEANCHK); 905 break; 906 } 907 mab[ i - 1 ] = tp; 908 maxb++; 909 continue; 910 } 911 break; 912 } 913 } 914 915 for(i = 0; i < maxb; i++) { 916 int index = (maxb - i) - 1; 917 ma[index] = mab[i]; 918 vm_page_flag_clear(ma[index], PG_CLEANCHK); 919 } 920 vm_page_flag_clear(p, PG_CLEANCHK); 921 ma[maxb] = p; 922 for(i = 0; i < maxf; i++) { 923 int index = (maxb + i) + 1; 924 ma[index] = maf[i]; 925 vm_page_flag_clear(ma[index], PG_CLEANCHK); 926 } 927 runlen = maxb + maxf + 1; 928 929 splx(s); 930 vm_pageout_flush(ma, runlen, pagerflags, TRUE); 931 for (i = 0; i < runlen; i++) { 932 if (ma[i]->valid & ma[i]->dirty) { 933 pmap_page_protect(ma[i], VM_PROT_READ); 934 vm_page_flag_set(ma[i], PG_CLEANCHK); 935 936 /* 937 * maxf will end up being the actual number of pages 938 * we wrote out contiguously, non-inclusive of the 939 * first page. We do not count look-behind pages. 940 */ 941 if (i >= maxb + 1 && (maxf > i - maxb - 1)) 942 maxf = i - maxb - 1; 943 } 944 } 945 return(maxf + 1); 946} 947 948/* 949 * vm_object_madvise: 950 * 951 * Implements the madvise function at the object/page level. 952 * 953 * MADV_WILLNEED (any object) 954 * 955 * Activate the specified pages if they are resident. 956 * 957 * MADV_DONTNEED (any object) 958 * 959 * Deactivate the specified pages if they are resident. 960 * 961 * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, 962 * OBJ_ONEMAPPING only) 963 * 964 * Deactivate and clean the specified pages if they are 965 * resident. This permits the process to reuse the pages 966 * without faulting or the kernel to reclaim the pages 967 * without I/O. 968 */ 969void 970vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise) 971{ 972 vm_pindex_t end, tpindex; 973 vm_object_t backing_object, tobject; 974 vm_page_t m; 975 976 if (object == NULL) 977 return; 978 979 mtx_lock(&Giant); 980 981 end = pindex + count; 982 983 /* 984 * Locate and adjust resident pages 985 */ 986 for (; pindex < end; pindex += 1) { 987relookup: 988 tobject = object; 989 tpindex = pindex; 990 VM_OBJECT_LOCK(tobject); 991shadowlookup: 992 /* 993 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages 994 * and those pages must be OBJ_ONEMAPPING. 995 */ 996 if (advise == MADV_FREE) { 997 if ((tobject->type != OBJT_DEFAULT && 998 tobject->type != OBJT_SWAP) || 999 (tobject->flags & OBJ_ONEMAPPING) == 0) { 1000 goto unlock_tobject; 1001 } 1002 } 1003 1004 m = vm_page_lookup(tobject, tpindex); 1005 1006 if (m == NULL) { 1007 /* 1008 * There may be swap even if there is no backing page 1009 */ 1010 if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 1011 swap_pager_freespace(tobject, tpindex, 1); 1012 1013 /* 1014 * next object 1015 */ 1016 backing_object = tobject->backing_object; 1017 if (backing_object == NULL) 1018 goto unlock_tobject; 1019 VM_OBJECT_LOCK(backing_object); 1020 VM_OBJECT_UNLOCK(tobject); 1021 tobject = backing_object; 1022 tpindex += OFF_TO_IDX(tobject->backing_object_offset); 1023 goto shadowlookup; 1024 } 1025 1026 /* 1027 * If the page is busy or not in a normal active state, 1028 * we skip it. If the page is not managed there are no 1029 * page queues to mess with. Things can break if we mess 1030 * with pages in any of the below states. 1031 */ 1032 vm_page_lock_queues(); 1033 if (m->hold_count || 1034 m->wire_count || 1035 (m->flags & PG_UNMANAGED) || 1036 m->valid != VM_PAGE_BITS_ALL) { 1037 vm_page_unlock_queues(); 1038 goto unlock_tobject; 1039 } 1040 if (vm_page_sleep_if_busy(m, TRUE, "madvpo")) { 1041 VM_OBJECT_UNLOCK(tobject); 1042 goto relookup; 1043 } 1044 if (advise == MADV_WILLNEED) { 1045 vm_page_activate(m); 1046 } else if (advise == MADV_DONTNEED) { 1047 vm_page_dontneed(m); 1048 } else if (advise == MADV_FREE) { 1049 /* 1050 * Mark the page clean. This will allow the page 1051 * to be freed up by the system. However, such pages 1052 * are often reused quickly by malloc()/free() 1053 * so we do not do anything that would cause 1054 * a page fault if we can help it. 1055 * 1056 * Specifically, we do not try to actually free 1057 * the page now nor do we try to put it in the 1058 * cache (which would cause a page fault on reuse). 1059 * 1060 * But we do make the page is freeable as we 1061 * can without actually taking the step of unmapping 1062 * it. 1063 */ 1064 pmap_clear_modify(m); 1065 m->dirty = 0; 1066 m->act_count = 0; 1067 vm_page_dontneed(m); 1068 } 1069 vm_page_unlock_queues(); 1070 if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 1071 swap_pager_freespace(tobject, tpindex, 1); 1072unlock_tobject: 1073 VM_OBJECT_UNLOCK(tobject); 1074 } 1075 mtx_unlock(&Giant); 1076} 1077 1078/* 1079 * vm_object_shadow: 1080 * 1081 * Create a new object which is backed by the 1082 * specified existing object range. The source 1083 * object reference is deallocated. 1084 * 1085 * The new object and offset into that object 1086 * are returned in the source parameters. 1087 */ 1088void 1089vm_object_shadow( 1090 vm_object_t *object, /* IN/OUT */ 1091 vm_ooffset_t *offset, /* IN/OUT */ 1092 vm_size_t length) 1093{ 1094 vm_object_t source; 1095 vm_object_t result; 1096 1097 GIANT_REQUIRED; 1098 1099 source = *object; 1100 1101 /* 1102 * Don't create the new object if the old object isn't shared. 1103 */ 1104 if (source != NULL) { 1105 VM_OBJECT_LOCK(source); 1106 if (source->ref_count == 1 && 1107 source->handle == NULL && 1108 (source->type == OBJT_DEFAULT || 1109 source->type == OBJT_SWAP)) { 1110 VM_OBJECT_UNLOCK(source); 1111 return; 1112 } 1113 VM_OBJECT_UNLOCK(source); 1114 } 1115 1116 /* 1117 * Allocate a new object with the given length. 1118 */ 1119 result = vm_object_allocate(OBJT_DEFAULT, length); 1120 1121 /* 1122 * The new object shadows the source object, adding a reference to it. 1123 * Our caller changes his reference to point to the new object, 1124 * removing a reference to the source object. Net result: no change 1125 * of reference count. 1126 * 1127 * Try to optimize the result object's page color when shadowing 1128 * in order to maintain page coloring consistency in the combined 1129 * shadowed object. 1130 */ 1131 result->backing_object = source; 1132 if (source != NULL) { 1133 VM_OBJECT_LOCK(source); 1134 LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list); 1135 source->shadow_count++; 1136 source->generation++; 1137 if (length < source->size) 1138 length = source->size; 1139 if (length > PQ_L2_SIZE / 3 + PQ_PRIME1 || 1140 source->generation > 1) 1141 length = PQ_L2_SIZE / 3 + PQ_PRIME1; 1142 result->pg_color = (source->pg_color + 1143 length * source->generation) & PQ_L2_MASK; 1144 VM_OBJECT_UNLOCK(source); 1145 next_index = (result->pg_color + PQ_L2_SIZE / 3 + PQ_PRIME1) & 1146 PQ_L2_MASK; 1147 } 1148 1149 /* 1150 * Store the offset into the source object, and fix up the offset into 1151 * the new object. 1152 */ 1153 result->backing_object_offset = *offset; 1154 1155 /* 1156 * Return the new things 1157 */ 1158 *offset = 0; 1159 *object = result; 1160} 1161 1162/* 1163 * vm_object_split: 1164 * 1165 * Split the pages in a map entry into a new object. This affords 1166 * easier removal of unused pages, and keeps object inheritance from 1167 * being a negative impact on memory usage. 1168 */ 1169void 1170vm_object_split(vm_map_entry_t entry) 1171{ 1172 vm_page_t m; 1173 vm_object_t orig_object, new_object, source; 1174 vm_offset_t s, e; 1175 vm_pindex_t offidxstart, offidxend; 1176 vm_size_t idx, size; 1177 vm_ooffset_t offset; 1178 1179 GIANT_REQUIRED; 1180 1181 orig_object = entry->object.vm_object; 1182 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP) 1183 return; 1184 if (orig_object->ref_count <= 1) 1185 return; 1186 1187 offset = entry->offset; 1188 s = entry->start; 1189 e = entry->end; 1190 1191 offidxstart = OFF_TO_IDX(offset); 1192 offidxend = offidxstart + OFF_TO_IDX(e - s); 1193 size = offidxend - offidxstart; 1194 1195 new_object = vm_pager_allocate(orig_object->type, 1196 NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL); 1197 if (new_object == NULL) 1198 return; 1199 1200 source = orig_object->backing_object; 1201 if (source != NULL) { 1202 vm_object_reference(source); /* Referenced by new_object */ 1203 VM_OBJECT_LOCK(source); 1204 LIST_INSERT_HEAD(&source->shadow_head, 1205 new_object, shadow_list); 1206 source->shadow_count++; 1207 source->generation++; 1208 vm_object_clear_flag(source, OBJ_ONEMAPPING); 1209 VM_OBJECT_UNLOCK(source); 1210 new_object->backing_object_offset = 1211 orig_object->backing_object_offset + offset; 1212 new_object->backing_object = source; 1213 } 1214 VM_OBJECT_LOCK(orig_object); 1215 for (idx = 0; idx < size; idx++) { 1216 retry: 1217 m = vm_page_lookup(orig_object, offidxstart + idx); 1218 if (m == NULL) 1219 continue; 1220 1221 /* 1222 * We must wait for pending I/O to complete before we can 1223 * rename the page. 1224 * 1225 * We do not have to VM_PROT_NONE the page as mappings should 1226 * not be changed by this operation. 1227 */ 1228 vm_page_lock_queues(); 1229 if (vm_page_sleep_if_busy(m, TRUE, "spltwt")) 1230 goto retry; 1231 1232 vm_page_busy(m); 1233 vm_page_rename(m, new_object, idx); 1234 /* page automatically made dirty by rename and cache handled */ 1235 vm_page_busy(m); 1236 vm_page_unlock_queues(); 1237 } 1238 if (orig_object->type == OBJT_SWAP) { 1239 vm_object_pip_add(orig_object, 1); 1240 VM_OBJECT_UNLOCK(orig_object); 1241 /* 1242 * copy orig_object pages into new_object 1243 * and destroy unneeded pages in 1244 * shadow object. 1245 */ 1246 swap_pager_copy(orig_object, new_object, offidxstart, 0); 1247 VM_OBJECT_LOCK(orig_object); 1248 vm_object_pip_wakeup(orig_object); 1249 } 1250 VM_OBJECT_UNLOCK(orig_object); 1251 vm_page_lock_queues(); 1252 TAILQ_FOREACH(m, &new_object->memq, listq) 1253 vm_page_wakeup(m); 1254 vm_page_unlock_queues(); 1255 entry->object.vm_object = new_object; 1256 entry->offset = 0LL; 1257 vm_object_deallocate(orig_object); 1258} 1259 1260#define OBSC_TEST_ALL_SHADOWED 0x0001 1261#define OBSC_COLLAPSE_NOWAIT 0x0002 1262#define OBSC_COLLAPSE_WAIT 0x0004 1263 1264static int 1265vm_object_backing_scan(vm_object_t object, int op) 1266{ 1267 int s; 1268 int r = 1; 1269 vm_page_t p; 1270 vm_object_t backing_object; 1271 vm_pindex_t backing_offset_index; 1272 1273 s = splvm(); 1274 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1275 VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED); 1276 1277 backing_object = object->backing_object; 1278 backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 1279 1280 /* 1281 * Initial conditions 1282 */ 1283 if (op & OBSC_TEST_ALL_SHADOWED) { 1284 /* 1285 * We do not want to have to test for the existence of 1286 * swap pages in the backing object. XXX but with the 1287 * new swapper this would be pretty easy to do. 1288 * 1289 * XXX what about anonymous MAP_SHARED memory that hasn't 1290 * been ZFOD faulted yet? If we do not test for this, the 1291 * shadow test may succeed! XXX 1292 */ 1293 if (backing_object->type != OBJT_DEFAULT) { 1294 splx(s); 1295 return (0); 1296 } 1297 } 1298 if (op & OBSC_COLLAPSE_WAIT) { 1299 vm_object_set_flag(backing_object, OBJ_DEAD); 1300 } 1301 1302 /* 1303 * Our scan 1304 */ 1305 p = TAILQ_FIRST(&backing_object->memq); 1306 while (p) { 1307 vm_page_t next = TAILQ_NEXT(p, listq); 1308 vm_pindex_t new_pindex = p->pindex - backing_offset_index; 1309 1310 if (op & OBSC_TEST_ALL_SHADOWED) { 1311 vm_page_t pp; 1312 1313 /* 1314 * Ignore pages outside the parent object's range 1315 * and outside the parent object's mapping of the 1316 * backing object. 1317 * 1318 * note that we do not busy the backing object's 1319 * page. 1320 */ 1321 if ( 1322 p->pindex < backing_offset_index || 1323 new_pindex >= object->size 1324 ) { 1325 p = next; 1326 continue; 1327 } 1328 1329 /* 1330 * See if the parent has the page or if the parent's 1331 * object pager has the page. If the parent has the 1332 * page but the page is not valid, the parent's 1333 * object pager must have the page. 1334 * 1335 * If this fails, the parent does not completely shadow 1336 * the object and we might as well give up now. 1337 */ 1338 1339 pp = vm_page_lookup(object, new_pindex); 1340 if ( 1341 (pp == NULL || pp->valid == 0) && 1342 !vm_pager_has_page(object, new_pindex, NULL, NULL) 1343 ) { 1344 r = 0; 1345 break; 1346 } 1347 } 1348 1349 /* 1350 * Check for busy page 1351 */ 1352 if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) { 1353 vm_page_t pp; 1354 1355 vm_page_lock_queues(); 1356 if (op & OBSC_COLLAPSE_NOWAIT) { 1357 if ((p->flags & PG_BUSY) || 1358 !p->valid || 1359 p->hold_count || 1360 p->wire_count || 1361 p->busy) { 1362 vm_page_unlock_queues(); 1363 p = next; 1364 continue; 1365 } 1366 } else if (op & OBSC_COLLAPSE_WAIT) { 1367 if ((p->flags & PG_BUSY) || p->busy) { 1368 vm_page_flag_set(p, 1369 PG_WANTED | PG_REFERENCED); 1370 VM_OBJECT_UNLOCK(backing_object); 1371 VM_OBJECT_UNLOCK(object); 1372 msleep(p, &vm_page_queue_mtx, 1373 PDROP | PVM, "vmocol", 0); 1374 VM_OBJECT_LOCK(object); 1375 VM_OBJECT_LOCK(backing_object); 1376 /* 1377 * If we slept, anything could have 1378 * happened. Since the object is 1379 * marked dead, the backing offset 1380 * should not have changed so we 1381 * just restart our scan. 1382 */ 1383 p = TAILQ_FIRST(&backing_object->memq); 1384 continue; 1385 } 1386 } 1387 1388 /* 1389 * Busy the page 1390 */ 1391 vm_page_busy(p); 1392 vm_page_unlock_queues(); 1393 1394 KASSERT( 1395 p->object == backing_object, 1396 ("vm_object_qcollapse(): object mismatch") 1397 ); 1398 1399 /* 1400 * Destroy any associated swap 1401 */ 1402 if (backing_object->type == OBJT_SWAP) { 1403 swap_pager_freespace( 1404 backing_object, 1405 p->pindex, 1406 1 1407 ); 1408 } 1409 1410 if ( 1411 p->pindex < backing_offset_index || 1412 new_pindex >= object->size 1413 ) { 1414 /* 1415 * Page is out of the parent object's range, we 1416 * can simply destroy it. 1417 */ 1418 vm_page_lock_queues(); 1419 pmap_remove_all(p); 1420 vm_page_free(p); 1421 vm_page_unlock_queues(); 1422 p = next; 1423 continue; 1424 } 1425 1426 pp = vm_page_lookup(object, new_pindex); 1427 if ( 1428 pp != NULL || 1429 vm_pager_has_page(object, new_pindex, NULL, NULL) 1430 ) { 1431 /* 1432 * page already exists in parent OR swap exists 1433 * for this location in the parent. Destroy 1434 * the original page from the backing object. 1435 * 1436 * Leave the parent's page alone 1437 */ 1438 vm_page_lock_queues(); 1439 pmap_remove_all(p); 1440 vm_page_free(p); 1441 vm_page_unlock_queues(); 1442 p = next; 1443 continue; 1444 } 1445 1446 /* 1447 * Page does not exist in parent, rename the 1448 * page from the backing object to the main object. 1449 * 1450 * If the page was mapped to a process, it can remain 1451 * mapped through the rename. 1452 */ 1453 vm_page_lock_queues(); 1454 vm_page_rename(p, object, new_pindex); 1455 vm_page_unlock_queues(); 1456 /* page automatically made dirty by rename */ 1457 } 1458 p = next; 1459 } 1460 splx(s); 1461 return (r); 1462} 1463 1464 1465/* 1466 * this version of collapse allows the operation to occur earlier and 1467 * when paging_in_progress is true for an object... This is not a complete 1468 * operation, but should plug 99.9% of the rest of the leaks. 1469 */ 1470static void 1471vm_object_qcollapse(vm_object_t object) 1472{ 1473 vm_object_t backing_object = object->backing_object; 1474 1475 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1476 VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED); 1477 1478 if (backing_object->ref_count != 1) 1479 return; 1480 1481 backing_object->ref_count += 2; 1482 1483 vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT); 1484 1485 backing_object->ref_count -= 2; 1486} 1487 1488/* 1489 * vm_object_collapse: 1490 * 1491 * Collapse an object with the object backing it. 1492 * Pages in the backing object are moved into the 1493 * parent, and the backing object is deallocated. 1494 */ 1495void 1496vm_object_collapse(vm_object_t object) 1497{ 1498 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1499 1500 while (TRUE) { 1501 vm_object_t backing_object; 1502 1503 /* 1504 * Verify that the conditions are right for collapse: 1505 * 1506 * The object exists and the backing object exists. 1507 */ 1508 if ((backing_object = object->backing_object) == NULL) 1509 break; 1510 1511 /* 1512 * we check the backing object first, because it is most likely 1513 * not collapsable. 1514 */ 1515 VM_OBJECT_LOCK(backing_object); 1516 if (backing_object->handle != NULL || 1517 (backing_object->type != OBJT_DEFAULT && 1518 backing_object->type != OBJT_SWAP) || 1519 (backing_object->flags & OBJ_DEAD) || 1520 object->handle != NULL || 1521 (object->type != OBJT_DEFAULT && 1522 object->type != OBJT_SWAP) || 1523 (object->flags & OBJ_DEAD)) { 1524 VM_OBJECT_UNLOCK(backing_object); 1525 break; 1526 } 1527 1528 if ( 1529 object->paging_in_progress != 0 || 1530 backing_object->paging_in_progress != 0 1531 ) { 1532 vm_object_qcollapse(object); 1533 VM_OBJECT_UNLOCK(backing_object); 1534 break; 1535 } 1536 /* 1537 * We know that we can either collapse the backing object (if 1538 * the parent is the only reference to it) or (perhaps) have 1539 * the parent bypass the object if the parent happens to shadow 1540 * all the resident pages in the entire backing object. 1541 * 1542 * This is ignoring pager-backed pages such as swap pages. 1543 * vm_object_backing_scan fails the shadowing test in this 1544 * case. 1545 */ 1546 if (backing_object->ref_count == 1) { 1547 /* 1548 * If there is exactly one reference to the backing 1549 * object, we can collapse it into the parent. 1550 */ 1551 vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT); 1552 1553 /* 1554 * Move the pager from backing_object to object. 1555 */ 1556 if (backing_object->type == OBJT_SWAP) { 1557 vm_object_pip_add(backing_object, 1); 1558 VM_OBJECT_UNLOCK(backing_object); 1559 /* 1560 * scrap the paging_offset junk and do a 1561 * discrete copy. This also removes major 1562 * assumptions about how the swap-pager 1563 * works from where it doesn't belong. The 1564 * new swapper is able to optimize the 1565 * destroy-source case. 1566 */ 1567 vm_object_pip_add(object, 1); 1568 VM_OBJECT_UNLOCK(object); 1569 swap_pager_copy( 1570 backing_object, 1571 object, 1572 OFF_TO_IDX(object->backing_object_offset), TRUE); 1573 VM_OBJECT_LOCK(object); 1574 vm_object_pip_wakeup(object); 1575 1576 VM_OBJECT_LOCK(backing_object); 1577 vm_object_pip_wakeup(backing_object); 1578 } 1579 /* 1580 * Object now shadows whatever backing_object did. 1581 * Note that the reference to 1582 * backing_object->backing_object moves from within 1583 * backing_object to within object. 1584 */ 1585 LIST_REMOVE(object, shadow_list); 1586 backing_object->shadow_count--; 1587 backing_object->generation++; 1588 if (backing_object->backing_object) { 1589 VM_OBJECT_LOCK(backing_object->backing_object); 1590 LIST_REMOVE(backing_object, shadow_list); 1591 backing_object->backing_object->shadow_count--; 1592 backing_object->backing_object->generation++; 1593 VM_OBJECT_UNLOCK(backing_object->backing_object); 1594 } 1595 object->backing_object = backing_object->backing_object; 1596 if (object->backing_object) { 1597 VM_OBJECT_LOCK(object->backing_object); 1598 LIST_INSERT_HEAD( 1599 &object->backing_object->shadow_head, 1600 object, 1601 shadow_list 1602 ); 1603 object->backing_object->shadow_count++; 1604 object->backing_object->generation++; 1605 VM_OBJECT_UNLOCK(object->backing_object); 1606 } 1607 1608 object->backing_object_offset += 1609 backing_object->backing_object_offset; 1610/* XXX */ VM_OBJECT_UNLOCK(object); 1611 1612 /* 1613 * Discard backing_object. 1614 * 1615 * Since the backing object has no pages, no pager left, 1616 * and no object references within it, all that is 1617 * necessary is to dispose of it. 1618 */ 1619 KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object)); 1620 KASSERT(TAILQ_FIRST(&backing_object->memq) == NULL, ("backing_object %p somehow has left over pages during collapse!", backing_object)); 1621 VM_OBJECT_UNLOCK(backing_object); 1622 1623 mtx_lock(&vm_object_list_mtx); 1624 TAILQ_REMOVE( 1625 &vm_object_list, 1626 backing_object, 1627 object_list 1628 ); 1629 mtx_unlock(&vm_object_list_mtx); 1630 1631 uma_zfree(obj_zone, backing_object); 1632 1633 object_collapses++; 1634 } else { 1635 vm_object_t new_backing_object; 1636 1637 /* 1638 * If we do not entirely shadow the backing object, 1639 * there is nothing we can do so we give up. 1640 */ 1641 if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) { 1642 VM_OBJECT_UNLOCK(backing_object); 1643 break; 1644 } 1645 1646 /* 1647 * Make the parent shadow the next object in the 1648 * chain. Deallocating backing_object will not remove 1649 * it, since its reference count is at least 2. 1650 */ 1651 LIST_REMOVE(object, shadow_list); 1652 backing_object->shadow_count--; 1653 backing_object->generation++; 1654 VM_OBJECT_UNLOCK(backing_object); 1655/* XXX */ VM_OBJECT_UNLOCK(object); 1656 1657 new_backing_object = backing_object->backing_object; 1658 if ((object->backing_object = new_backing_object) != NULL) { 1659 vm_object_reference(new_backing_object); 1660 VM_OBJECT_LOCK(new_backing_object); 1661 LIST_INSERT_HEAD( 1662 &new_backing_object->shadow_head, 1663 object, 1664 shadow_list 1665 ); 1666 new_backing_object->shadow_count++; 1667 new_backing_object->generation++; 1668 VM_OBJECT_UNLOCK(new_backing_object); 1669 object->backing_object_offset += 1670 backing_object->backing_object_offset; 1671 } 1672 1673 /* 1674 * Drop the reference count on backing_object. Since 1675 * its ref_count was at least 2, it will not vanish; 1676 * so we don't need to call vm_object_deallocate, but 1677 * we do anyway. 1678 */ 1679 vm_object_deallocate(backing_object); 1680 object_bypasses++; 1681 } 1682 1683 /* 1684 * Try again with this object's new backing object. 1685 */ 1686/* XXX */ VM_OBJECT_LOCK(object); 1687 } 1688} 1689 1690/* 1691 * vm_object_page_remove: 1692 * 1693 * Removes all physical pages in the given range from the 1694 * object's list of pages. If the range's end is zero, all 1695 * physical pages from the range's start to the end of the object 1696 * are deleted. 1697 * 1698 * The object must be locked. 1699 */ 1700void 1701vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, 1702 boolean_t clean_only) 1703{ 1704 vm_page_t p, next; 1705 1706 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1707 if (object->resident_page_count == 0) 1708 return; 1709 1710 /* 1711 * Since physically-backed objects do not use managed pages, we can't 1712 * remove pages from the object (we must instead remove the page 1713 * references, and then destroy the object). 1714 */ 1715 KASSERT(object->type != OBJT_PHYS, 1716 ("attempt to remove pages from a physical object")); 1717 1718 vm_object_pip_add(object, 1); 1719again: 1720 vm_page_lock_queues(); 1721 if ((p = TAILQ_FIRST(&object->memq)) != NULL) { 1722 if (p->pindex < start) { 1723 p = vm_page_splay(start, object->root); 1724 if ((object->root = p)->pindex < start) 1725 p = TAILQ_NEXT(p, listq); 1726 } 1727 } 1728 /* 1729 * Assert: the variable p is either (1) the page with the 1730 * least pindex greater than or equal to the parameter pindex 1731 * or (2) NULL. 1732 */ 1733 for (; 1734 p != NULL && (p->pindex < end || end == 0); 1735 p = next) { 1736 next = TAILQ_NEXT(p, listq); 1737 1738 if (p->wire_count != 0) { 1739 pmap_remove_all(p); 1740 if (!clean_only) 1741 p->valid = 0; 1742 continue; 1743 } 1744 if (vm_page_sleep_if_busy(p, TRUE, "vmopar")) 1745 goto again; 1746 if (clean_only && p->valid) { 1747 vm_page_test_dirty(p); 1748 if (p->valid & p->dirty) 1749 continue; 1750 } 1751 vm_page_busy(p); 1752 pmap_remove_all(p); 1753 vm_page_free(p); 1754 } 1755 vm_page_unlock_queues(); 1756 vm_object_pip_wakeup(object); 1757} 1758 1759/* 1760 * Routine: vm_object_coalesce 1761 * Function: Coalesces two objects backing up adjoining 1762 * regions of memory into a single object. 1763 * 1764 * returns TRUE if objects were combined. 1765 * 1766 * NOTE: Only works at the moment if the second object is NULL - 1767 * if it's not, which object do we lock first? 1768 * 1769 * Parameters: 1770 * prev_object First object to coalesce 1771 * prev_offset Offset into prev_object 1772 * next_object Second object into coalesce 1773 * next_offset Offset into next_object 1774 * 1775 * prev_size Size of reference to prev_object 1776 * next_size Size of reference to next_object 1777 * 1778 * Conditions: 1779 * The object must *not* be locked. 1780 */ 1781boolean_t 1782vm_object_coalesce(vm_object_t prev_object, vm_pindex_t prev_pindex, 1783 vm_size_t prev_size, vm_size_t next_size) 1784{ 1785 vm_pindex_t next_pindex; 1786 1787 if (prev_object == NULL) 1788 return (TRUE); 1789 mtx_lock(&Giant); 1790 VM_OBJECT_LOCK(prev_object); 1791 if (prev_object->type != OBJT_DEFAULT && 1792 prev_object->type != OBJT_SWAP) { 1793 VM_OBJECT_UNLOCK(prev_object); 1794 mtx_unlock(&Giant); 1795 return (FALSE); 1796 } 1797 1798 /* 1799 * Try to collapse the object first 1800 */ 1801 vm_object_collapse(prev_object); 1802 1803 /* 1804 * Can't coalesce if: . more than one reference . paged out . shadows 1805 * another object . has a copy elsewhere (any of which mean that the 1806 * pages not mapped to prev_entry may be in use anyway) 1807 */ 1808 if (prev_object->backing_object != NULL) { 1809 VM_OBJECT_UNLOCK(prev_object); 1810 mtx_unlock(&Giant); 1811 return (FALSE); 1812 } 1813 1814 prev_size >>= PAGE_SHIFT; 1815 next_size >>= PAGE_SHIFT; 1816 next_pindex = prev_pindex + prev_size; 1817 1818 if ((prev_object->ref_count > 1) && 1819 (prev_object->size != next_pindex)) { 1820 VM_OBJECT_UNLOCK(prev_object); 1821 mtx_unlock(&Giant); 1822 return (FALSE); 1823 } 1824 1825 /* 1826 * Remove any pages that may still be in the object from a previous 1827 * deallocation. 1828 */ 1829 if (next_pindex < prev_object->size) { 1830 vm_object_page_remove(prev_object, 1831 next_pindex, 1832 next_pindex + next_size, FALSE); 1833 if (prev_object->type == OBJT_SWAP) 1834 swap_pager_freespace(prev_object, 1835 next_pindex, next_size); 1836 } 1837 1838 /* 1839 * Extend the object if necessary. 1840 */ 1841 if (next_pindex + next_size > prev_object->size) 1842 prev_object->size = next_pindex + next_size; 1843 1844 VM_OBJECT_UNLOCK(prev_object); 1845 mtx_unlock(&Giant); 1846 return (TRUE); 1847} 1848 1849void 1850vm_object_set_writeable_dirty(vm_object_t object) 1851{ 1852 struct vnode *vp; 1853 1854 vm_object_set_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY); 1855 if (object->type == OBJT_VNODE && 1856 (vp = (struct vnode *)object->handle) != NULL) { 1857 VI_LOCK(vp); 1858 if ((vp->v_iflag & VI_OBJDIRTY) == 0) 1859 vp->v_iflag |= VI_OBJDIRTY; 1860 VI_UNLOCK(vp); 1861 } 1862} 1863 1864#include "opt_ddb.h" 1865#ifdef DDB 1866#include <sys/kernel.h> 1867 1868#include <sys/cons.h> 1869 1870#include <ddb/ddb.h> 1871 1872static int 1873_vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry) 1874{ 1875 vm_map_t tmpm; 1876 vm_map_entry_t tmpe; 1877 vm_object_t obj; 1878 int entcount; 1879 1880 if (map == 0) 1881 return 0; 1882 1883 if (entry == 0) { 1884 tmpe = map->header.next; 1885 entcount = map->nentries; 1886 while (entcount-- && (tmpe != &map->header)) { 1887 if (_vm_object_in_map(map, object, tmpe)) { 1888 return 1; 1889 } 1890 tmpe = tmpe->next; 1891 } 1892 } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 1893 tmpm = entry->object.sub_map; 1894 tmpe = tmpm->header.next; 1895 entcount = tmpm->nentries; 1896 while (entcount-- && tmpe != &tmpm->header) { 1897 if (_vm_object_in_map(tmpm, object, tmpe)) { 1898 return 1; 1899 } 1900 tmpe = tmpe->next; 1901 } 1902 } else if ((obj = entry->object.vm_object) != NULL) { 1903 for (; obj; obj = obj->backing_object) 1904 if (obj == object) { 1905 return 1; 1906 } 1907 } 1908 return 0; 1909} 1910 1911static int 1912vm_object_in_map(vm_object_t object) 1913{ 1914 struct proc *p; 1915 1916 /* sx_slock(&allproc_lock); */ 1917 LIST_FOREACH(p, &allproc, p_list) { 1918 if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 1919 continue; 1920 if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) { 1921 /* sx_sunlock(&allproc_lock); */ 1922 return 1; 1923 } 1924 } 1925 /* sx_sunlock(&allproc_lock); */ 1926 if (_vm_object_in_map(kernel_map, object, 0)) 1927 return 1; 1928 if (_vm_object_in_map(kmem_map, object, 0)) 1929 return 1; 1930 if (_vm_object_in_map(pager_map, object, 0)) 1931 return 1; 1932 if (_vm_object_in_map(buffer_map, object, 0)) 1933 return 1; 1934 return 0; 1935} 1936 1937DB_SHOW_COMMAND(vmochk, vm_object_check) 1938{ 1939 vm_object_t object; 1940 1941 /* 1942 * make sure that internal objs are in a map somewhere 1943 * and none have zero ref counts. 1944 */ 1945 TAILQ_FOREACH(object, &vm_object_list, object_list) { 1946 if (object->handle == NULL && 1947 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 1948 if (object->ref_count == 0) { 1949 db_printf("vmochk: internal obj has zero ref count: %ld\n", 1950 (long)object->size); 1951 } 1952 if (!vm_object_in_map(object)) { 1953 db_printf( 1954 "vmochk: internal obj is not in a map: " 1955 "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 1956 object->ref_count, (u_long)object->size, 1957 (u_long)object->size, 1958 (void *)object->backing_object); 1959 } 1960 } 1961 } 1962} 1963 1964/* 1965 * vm_object_print: [ debug ] 1966 */ 1967DB_SHOW_COMMAND(object, vm_object_print_static) 1968{ 1969 /* XXX convert args. */ 1970 vm_object_t object = (vm_object_t)addr; 1971 boolean_t full = have_addr; 1972 1973 vm_page_t p; 1974 1975 /* XXX count is an (unused) arg. Avoid shadowing it. */ 1976#define count was_count 1977 1978 int count; 1979 1980 if (object == NULL) 1981 return; 1982 1983 db_iprintf( 1984 "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n", 1985 object, (int)object->type, (uintmax_t)object->size, 1986 object->resident_page_count, object->ref_count, object->flags); 1987 db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n", 1988 object->shadow_count, 1989 object->backing_object ? object->backing_object->ref_count : 0, 1990 object->backing_object, (uintmax_t)object->backing_object_offset); 1991 1992 if (!full) 1993 return; 1994 1995 db_indent += 2; 1996 count = 0; 1997 TAILQ_FOREACH(p, &object->memq, listq) { 1998 if (count == 0) 1999 db_iprintf("memory:="); 2000 else if (count == 6) { 2001 db_printf("\n"); 2002 db_iprintf(" ..."); 2003 count = 0; 2004 } else 2005 db_printf(","); 2006 count++; 2007 2008 db_printf("(off=0x%jx,page=0x%jx)", 2009 (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p)); 2010 } 2011 if (count != 0) 2012 db_printf("\n"); 2013 db_indent -= 2; 2014} 2015 2016/* XXX. */ 2017#undef count 2018 2019/* XXX need this non-static entry for calling from vm_map_print. */ 2020void 2021vm_object_print( 2022 /* db_expr_t */ long addr, 2023 boolean_t have_addr, 2024 /* db_expr_t */ long count, 2025 char *modif) 2026{ 2027 vm_object_print_static(addr, have_addr, count, modif); 2028} 2029 2030DB_SHOW_COMMAND(vmopag, vm_object_print_pages) 2031{ 2032 vm_object_t object; 2033 int nl = 0; 2034 int c; 2035 2036 TAILQ_FOREACH(object, &vm_object_list, object_list) { 2037 vm_pindex_t idx, fidx; 2038 vm_pindex_t osize; 2039 vm_paddr_t pa = -1, padiff; 2040 int rcount; 2041 vm_page_t m; 2042 2043 db_printf("new object: %p\n", (void *)object); 2044 if (nl > 18) { 2045 c = cngetc(); 2046 if (c != ' ') 2047 return; 2048 nl = 0; 2049 } 2050 nl++; 2051 rcount = 0; 2052 fidx = 0; 2053 osize = object->size; 2054 if (osize > 128) 2055 osize = 128; 2056 for (idx = 0; idx < osize; idx++) { 2057 m = vm_page_lookup(object, idx); 2058 if (m == NULL) { 2059 if (rcount) { 2060 db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 2061 (long)fidx, rcount, (long)pa); 2062 if (nl > 18) { 2063 c = cngetc(); 2064 if (c != ' ') 2065 return; 2066 nl = 0; 2067 } 2068 nl++; 2069 rcount = 0; 2070 } 2071 continue; 2072 } 2073 2074 2075 if (rcount && 2076 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) { 2077 ++rcount; 2078 continue; 2079 } 2080 if (rcount) { 2081 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m); 2082 padiff >>= PAGE_SHIFT; 2083 padiff &= PQ_L2_MASK; 2084 if (padiff == 0) { 2085 pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE; 2086 ++rcount; 2087 continue; 2088 } 2089 db_printf(" index(%ld)run(%d)pa(0x%lx)", 2090 (long)fidx, rcount, (long)pa); 2091 db_printf("pd(%ld)\n", (long)padiff); 2092 if (nl > 18) { 2093 c = cngetc(); 2094 if (c != ' ') 2095 return; 2096 nl = 0; 2097 } 2098 nl++; 2099 } 2100 fidx = idx; 2101 pa = VM_PAGE_TO_PHYS(m); 2102 rcount = 1; 2103 } 2104 if (rcount) { 2105 db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 2106 (long)fidx, rcount, (long)pa); 2107 if (nl > 18) { 2108 c = cngetc(); 2109 if (c != ' ') 2110 return; 2111 nl = 0; 2112 } 2113 nl++; 2114 } 2115 } 2116} 2117#endif /* DDB */ 2118