1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 6df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 7df8bae1dSRodney W. Grimes * 8df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 9df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 10df8bae1dSRodney W. Grimes * are met: 11df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 12df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 13df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 15df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 16df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 17df8bae1dSRodney W. Grimes * must display the following acknowledgement: 18df8bae1dSRodney W. Grimes * This product includes software developed by the University of 19df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 20df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 21df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 22df8bae1dSRodney W. Grimes * without specific prior written permission. 23df8bae1dSRodney W. Grimes * 24df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34df8bae1dSRodney W. Grimes * SUCH DAMAGE. 35df8bae1dSRodney W. Grimes * 363c4dd356SDavid Greenman * from: @(#)vm_object.c 8.5 (Berkeley) 3/22/94 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * 39df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 40df8bae1dSRodney W. Grimes * All rights reserved. 41df8bae1dSRodney W. Grimes * 42df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 43df8bae1dSRodney W. Grimes * 44df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 45df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 46df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 47df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 48df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 51df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 52df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 53df8bae1dSRodney W. Grimes * 54df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 55df8bae1dSRodney W. Grimes * 56df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 57df8bae1dSRodney W. Grimes * School of Computer Science 58df8bae1dSRodney W. Grimes * Carnegie Mellon University 59df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 60df8bae1dSRodney W. Grimes * 61df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 62df8bae1dSRodney W. Grimes * rights to redistribute these changes. 633c4dd356SDavid Greenman * 643efc015bSPeter Wemm * $Id: vm_object.c,v 1.157 1999/06/20 21:47:00 alc Exp $ 65df8bae1dSRodney W. Grimes */ 66df8bae1dSRodney W. Grimes 67df8bae1dSRodney W. Grimes /* 68df8bae1dSRodney W. Grimes * Virtual memory object module. 69df8bae1dSRodney W. Grimes */ 70df8bae1dSRodney W. Grimes 71df8bae1dSRodney W. Grimes #include <sys/param.h> 72df8bae1dSRodney W. Grimes #include <sys/systm.h> 73f23b4c91SGarrett Wollman #include <sys/proc.h> /* for curproc, pageproc */ 740d94caffSDavid Greenman #include <sys/vnode.h> 75efeaf95aSDavid Greenman #include <sys/vmmeter.h> 76867a482dSJohn Dyson #include <sys/mman.h> 77cf2819ccSJohn Dyson #include <sys/mount.h> 78df8bae1dSRodney W. Grimes 79df8bae1dSRodney W. Grimes #include <vm/vm.h> 80efeaf95aSDavid Greenman #include <vm/vm_param.h> 81efeaf95aSDavid Greenman #include <vm/vm_prot.h> 82efeaf95aSDavid Greenman #include <vm/pmap.h> 83efeaf95aSDavid Greenman #include <vm/vm_map.h> 84efeaf95aSDavid Greenman #include <vm/vm_object.h> 85df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 8626f9a767SRodney W. Grimes #include <vm/vm_pageout.h> 870d94caffSDavid Greenman #include <vm/vm_pager.h> 8805f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 89a1f6d91cSDavid Greenman #include <vm/vm_kern.h> 90efeaf95aSDavid Greenman #include <vm/vm_extern.h> 9199448ed1SJohn Dyson #include <vm/vm_zone.h> 9226f9a767SRodney W. Grimes 93cac597e4SBruce Evans static void vm_object_qcollapse __P((vm_object_t object)); 94f6b04d2bSDavid Greenman 95df8bae1dSRodney W. Grimes /* 96df8bae1dSRodney W. Grimes * Virtual memory objects maintain the actual data 97df8bae1dSRodney W. Grimes * associated with allocated virtual memory. A given 98df8bae1dSRodney W. Grimes * page of memory exists within exactly one object. 99df8bae1dSRodney W. Grimes * 100df8bae1dSRodney W. Grimes * An object is only deallocated when all "references" 101df8bae1dSRodney W. Grimes * are given up. Only one "reference" to a given 102df8bae1dSRodney W. Grimes * region of an object should be writeable. 103df8bae1dSRodney W. Grimes * 104df8bae1dSRodney W. Grimes * Associated with each object is a list of all resident 105df8bae1dSRodney W. Grimes * memory pages belonging to that object; this list is 106df8bae1dSRodney W. Grimes * maintained by the "vm_page" module, and locked by the object's 107df8bae1dSRodney W. Grimes * lock. 108df8bae1dSRodney W. Grimes * 109df8bae1dSRodney W. Grimes * Each object also records a "pager" routine which is 110df8bae1dSRodney W. Grimes * used to retrieve (and store) pages to the proper backing 111df8bae1dSRodney W. Grimes * storage. In addition, objects may be backed by other 112df8bae1dSRodney W. Grimes * objects from which they were virtual-copied. 113df8bae1dSRodney W. Grimes * 114df8bae1dSRodney W. Grimes * The only items within the object structure which are 115df8bae1dSRodney W. Grimes * modified after time of creation are: 116df8bae1dSRodney W. Grimes * reference count locked by object's lock 117df8bae1dSRodney W. Grimes * pager routine locked by object's lock 118df8bae1dSRodney W. Grimes * 119df8bae1dSRodney W. Grimes */ 120df8bae1dSRodney W. Grimes 12128f8db14SBruce Evans struct object_q vm_object_list; 122289bdf33SBruce Evans #ifndef NULL_SIMPLELOCKS 123303b270bSEivind Eklund static struct simplelock vm_object_list_lock; 124289bdf33SBruce Evans #endif 1254de628deSBruce Evans static long vm_object_count; /* count of all objects */ 12628f8db14SBruce Evans vm_object_t kernel_object; 12728f8db14SBruce Evans vm_object_t kmem_object; 128f708ef1bSPoul-Henning Kamp static struct vm_object kernel_object_store; 129f708ef1bSPoul-Henning Kamp static struct vm_object kmem_object_store; 130aef922f5SJohn Dyson extern int vm_pageout_page_count; 131df8bae1dSRodney W. Grimes 132f708ef1bSPoul-Henning Kamp static long object_collapses; 133f708ef1bSPoul-Henning Kamp static long object_bypasses; 1345070c7f8SJohn Dyson static int next_index; 13599448ed1SJohn Dyson static vm_zone_t obj_zone; 13699448ed1SJohn Dyson static struct vm_zone obj_zone_store; 1371c7c3c6aSMatthew Dillon static int object_hash_rand; 13899448ed1SJohn Dyson #define VM_OBJECTS_INIT 256 139303b270bSEivind Eklund static struct vm_object vm_objects_init[VM_OBJECTS_INIT]; 140df8bae1dSRodney W. Grimes 1413075778bSJohn Dyson void 14224a1cce3SDavid Greenman _vm_object_allocate(type, size, object) 14324a1cce3SDavid Greenman objtype_t type; 144df8bae1dSRodney W. Grimes vm_size_t size; 145d031cff1SMatthew Dillon vm_object_t object; 146df8bae1dSRodney W. Grimes { 14799448ed1SJohn Dyson int incr; 148df8bae1dSRodney W. Grimes TAILQ_INIT(&object->memq); 14924a1cce3SDavid Greenman TAILQ_INIT(&object->shadow_head); 150a1f6d91cSDavid Greenman 15124a1cce3SDavid Greenman object->type = type; 152df8bae1dSRodney W. Grimes object->size = size; 153a1f6d91cSDavid Greenman object->ref_count = 1; 15424a1cce3SDavid Greenman object->flags = 0; 155c0877f10SJohn Dyson if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP)) 156069e9bc1SDoug Rabson vm_object_set_flag(object, OBJ_ONEMAPPING); 157867a482dSJohn Dyson object->behavior = OBJ_NORMAL; 158df8bae1dSRodney W. Grimes object->paging_in_progress = 0; 159a1f6d91cSDavid Greenman object->resident_page_count = 0; 160de5f6a77SJohn Dyson object->shadow_count = 0; 1615070c7f8SJohn Dyson object->pg_color = next_index; 16299448ed1SJohn Dyson if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1)) 16399448ed1SJohn Dyson incr = PQ_L2_SIZE / 3 + PQ_PRIME1; 16499448ed1SJohn Dyson else 16599448ed1SJohn Dyson incr = size; 16699448ed1SJohn Dyson next_index = (next_index + incr) & PQ_L2_MASK; 16724a1cce3SDavid Greenman object->handle = NULL; 16824a1cce3SDavid Greenman object->backing_object = NULL; 169a316d390SJohn Dyson object->backing_object_offset = (vm_ooffset_t) 0; 1701c7c3c6aSMatthew Dillon /* 1711c7c3c6aSMatthew Dillon * Try to generate a number that will spread objects out in the 1721c7c3c6aSMatthew Dillon * hash table. We 'wipe' new objects across the hash in 128 page 1731c7c3c6aSMatthew Dillon * increments plus 1 more to offset it a little more by the time 1741c7c3c6aSMatthew Dillon * it wraps around. 1751c7c3c6aSMatthew Dillon */ 1761c7c3c6aSMatthew Dillon object->hash_rand = object_hash_rand - 129; 177a1f6d91cSDavid Greenman 178a1f6d91cSDavid Greenman object->last_read = 0; 1792d8acc0fSJohn Dyson object->generation++; 180df8bae1dSRodney W. Grimes 181df8bae1dSRodney W. Grimes TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 182df8bae1dSRodney W. Grimes vm_object_count++; 1831c7c3c6aSMatthew Dillon object_hash_rand = object->hash_rand; 184df8bae1dSRodney W. Grimes } 185df8bae1dSRodney W. Grimes 186df8bae1dSRodney W. Grimes /* 18726f9a767SRodney W. Grimes * vm_object_init: 18826f9a767SRodney W. Grimes * 18926f9a767SRodney W. Grimes * Initialize the VM objects module. 19026f9a767SRodney W. Grimes */ 19126f9a767SRodney W. Grimes void 192a316d390SJohn Dyson vm_object_init() 19326f9a767SRodney W. Grimes { 19426f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_list); 195996c772fSJohn Dyson simple_lock_init(&vm_object_list_lock); 19626f9a767SRodney W. Grimes vm_object_count = 0; 1970217125fSDavid Greenman 19826f9a767SRodney W. Grimes kernel_object = &kernel_object_store; 199a316d390SJohn Dyson _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 20026f9a767SRodney W. Grimes kernel_object); 20126f9a767SRodney W. Grimes 20226f9a767SRodney W. Grimes kmem_object = &kmem_object_store; 203a316d390SJohn Dyson _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 20426f9a767SRodney W. Grimes kmem_object); 20599448ed1SJohn Dyson 20699448ed1SJohn Dyson obj_zone = &obj_zone_store; 20799448ed1SJohn Dyson zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object), 20899448ed1SJohn Dyson vm_objects_init, VM_OBJECTS_INIT); 20999448ed1SJohn Dyson } 21099448ed1SJohn Dyson 21199448ed1SJohn Dyson void 21299448ed1SJohn Dyson vm_object_init2() { 2130a80f406SJohn Dyson zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1); 21426f9a767SRodney W. Grimes } 21526f9a767SRodney W. Grimes 21626f9a767SRodney W. Grimes /* 21726f9a767SRodney W. Grimes * vm_object_allocate: 21826f9a767SRodney W. Grimes * 21926f9a767SRodney W. Grimes * Returns a new object with the given size. 22026f9a767SRodney W. Grimes */ 22126f9a767SRodney W. Grimes 22226f9a767SRodney W. Grimes vm_object_t 22324a1cce3SDavid Greenman vm_object_allocate(type, size) 22424a1cce3SDavid Greenman objtype_t type; 22526f9a767SRodney W. Grimes vm_size_t size; 22626f9a767SRodney W. Grimes { 227d031cff1SMatthew Dillon vm_object_t result; 228d031cff1SMatthew Dillon 22999448ed1SJohn Dyson result = (vm_object_t) zalloc(obj_zone); 23067bf6868SJohn Dyson 23124a1cce3SDavid Greenman _vm_object_allocate(type, size, result); 23226f9a767SRodney W. Grimes 23326f9a767SRodney W. Grimes return (result); 23426f9a767SRodney W. Grimes } 23526f9a767SRodney W. Grimes 23626f9a767SRodney W. Grimes 23726f9a767SRodney W. Grimes /* 238df8bae1dSRodney W. Grimes * vm_object_reference: 239df8bae1dSRodney W. Grimes * 240df8bae1dSRodney W. Grimes * Gets another reference to the given object. 241df8bae1dSRodney W. Grimes */ 2426476c0d2SJohn Dyson void 24326f9a767SRodney W. Grimes vm_object_reference(object) 244d031cff1SMatthew Dillon vm_object_t object; 245df8bae1dSRodney W. Grimes { 246df8bae1dSRodney W. Grimes if (object == NULL) 247df8bae1dSRodney W. Grimes return; 24895e5e988SJohn Dyson 2495526d2d9SEivind Eklund KASSERT(!(object->flags & OBJ_DEAD), 2505526d2d9SEivind Eklund ("vm_object_reference: attempting to reference dead obj")); 25195e5e988SJohn Dyson 252df8bae1dSRodney W. Grimes object->ref_count++; 25347221757SJohn Dyson if (object->type == OBJT_VNODE) { 25447221757SJohn Dyson while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) { 25595461b45SJohn Dyson #if !defined(MAX_PERF) 25647221757SJohn Dyson printf("vm_object_reference: delay in getting object\n"); 25795461b45SJohn Dyson #endif 25847221757SJohn Dyson } 25947221757SJohn Dyson } 26095e5e988SJohn Dyson } 26195e5e988SJohn Dyson 262bf27292bSJohn Dyson void 26395e5e988SJohn Dyson vm_object_vndeallocate(object) 26495e5e988SJohn Dyson vm_object_t object; 26595e5e988SJohn Dyson { 26695e5e988SJohn Dyson struct vnode *vp = (struct vnode *) object->handle; 267219cbf59SEivind Eklund 2685526d2d9SEivind Eklund KASSERT(object->type == OBJT_VNODE, 2695526d2d9SEivind Eklund ("vm_object_vndeallocate: not a vnode object")); 270219cbf59SEivind Eklund KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp")); 271219cbf59SEivind Eklund #ifdef INVARIANTS 27295e5e988SJohn Dyson if (object->ref_count == 0) { 27395e5e988SJohn Dyson vprint("vm_object_vndeallocate", vp); 27495e5e988SJohn Dyson panic("vm_object_vndeallocate: bad object reference count"); 27595e5e988SJohn Dyson } 27695e5e988SJohn Dyson #endif 27795e5e988SJohn Dyson 27895e5e988SJohn Dyson object->ref_count--; 27947221757SJohn Dyson if (object->ref_count == 0) { 280bf27292bSJohn Dyson vp->v_flag &= ~VTEXT; 281069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_OPT); 2822be70f79SJohn Dyson } 28347221757SJohn Dyson vrele(vp); 284df8bae1dSRodney W. Grimes } 285df8bae1dSRodney W. Grimes 286df8bae1dSRodney W. Grimes /* 287df8bae1dSRodney W. Grimes * vm_object_deallocate: 288df8bae1dSRodney W. Grimes * 289df8bae1dSRodney W. Grimes * Release a reference to the specified object, 290df8bae1dSRodney W. Grimes * gained either through a vm_object_allocate 291df8bae1dSRodney W. Grimes * or a vm_object_reference call. When all references 292df8bae1dSRodney W. Grimes * are gone, storage associated with this object 293df8bae1dSRodney W. Grimes * may be relinquished. 294df8bae1dSRodney W. Grimes * 295df8bae1dSRodney W. Grimes * No object may be locked. 296df8bae1dSRodney W. Grimes */ 29726f9a767SRodney W. Grimes void 29826f9a767SRodney W. Grimes vm_object_deallocate(object) 29926f9a767SRodney W. Grimes vm_object_t object; 300df8bae1dSRodney W. Grimes { 301df8bae1dSRodney W. Grimes vm_object_t temp; 302df8bae1dSRodney W. Grimes 303df8bae1dSRodney W. Grimes while (object != NULL) { 304df8bae1dSRodney W. Grimes 30595e5e988SJohn Dyson if (object->type == OBJT_VNODE) { 30695e5e988SJohn Dyson vm_object_vndeallocate(object); 30795e5e988SJohn Dyson return; 30895e5e988SJohn Dyson } 30995e5e988SJohn Dyson 3102be70f79SJohn Dyson if (object->ref_count == 0) { 31147221757SJohn Dyson panic("vm_object_deallocate: object deallocated too many times: %d", object->type); 3122be70f79SJohn Dyson } else if (object->ref_count > 2) { 3132be70f79SJohn Dyson object->ref_count--; 3142be70f79SJohn Dyson return; 3152be70f79SJohn Dyson } 3162be70f79SJohn Dyson 3172be70f79SJohn Dyson /* 3182be70f79SJohn Dyson * Here on ref_count of one or two, which are special cases for 3192be70f79SJohn Dyson * objects. 3202be70f79SJohn Dyson */ 321c0877f10SJohn Dyson if ((object->ref_count == 2) && (object->shadow_count == 0)) { 322069e9bc1SDoug Rabson vm_object_set_flag(object, OBJ_ONEMAPPING); 323c0877f10SJohn Dyson object->ref_count--; 324c0877f10SJohn Dyson return; 325c0877f10SJohn Dyson } else if ((object->ref_count == 2) && (object->shadow_count == 1)) { 326be6d5bfaSDavid Greenman object->ref_count--; 3272be70f79SJohn Dyson if ((object->handle == NULL) && 32824a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || 32924a1cce3SDavid Greenman object->type == OBJT_SWAP)) { 330a1f6d91cSDavid Greenman vm_object_t robject; 33195e5e988SJohn Dyson 332b18bfc3dSJohn Dyson robject = TAILQ_FIRST(&object->shadow_head); 3335526d2d9SEivind Eklund KASSERT(robject != NULL, 334219cbf59SEivind Eklund ("vm_object_deallocate: ref_count: %d, shadow_count: %d", 3355526d2d9SEivind Eklund object->ref_count, 3365526d2d9SEivind Eklund object->shadow_count)); 33795e5e988SJohn Dyson if ((robject->handle == NULL) && 33824a1cce3SDavid Greenman (robject->type == OBJT_DEFAULT || 33924a1cce3SDavid Greenman robject->type == OBJT_SWAP)) { 340a1f6d91cSDavid Greenman 34195e5e988SJohn Dyson robject->ref_count++; 34295e5e988SJohn Dyson 3431c7c3c6aSMatthew Dillon while ( 3441c7c3c6aSMatthew Dillon robject->paging_in_progress || 3451c7c3c6aSMatthew Dillon object->paging_in_progress 3461c7c3c6aSMatthew Dillon ) { 34766095752SJohn Dyson vm_object_pip_sleep(robject, "objde1"); 34866095752SJohn Dyson vm_object_pip_sleep(object, "objde2"); 349a1f6d91cSDavid Greenman } 350a1f6d91cSDavid Greenman 35195e5e988SJohn Dyson if (robject->ref_count == 1) { 35295e5e988SJohn Dyson robject->ref_count--; 353ba8da839SDavid Greenman object = robject; 35495e5e988SJohn Dyson goto doterm; 35595e5e988SJohn Dyson } 35695e5e988SJohn Dyson 35795e5e988SJohn Dyson object = robject; 35895e5e988SJohn Dyson vm_object_collapse(object); 359ba8da839SDavid Greenman continue; 360a1f6d91cSDavid Greenman } 36195e5e988SJohn Dyson } 36295e5e988SJohn Dyson 363a1f6d91cSDavid Greenman return; 364df8bae1dSRodney W. Grimes 36595e5e988SJohn Dyson } else { 3661efb74fbSJohn Dyson object->ref_count--; 36795e5e988SJohn Dyson if (object->ref_count != 0) 36895e5e988SJohn Dyson return; 36995e5e988SJohn Dyson } 37095e5e988SJohn Dyson 37195e5e988SJohn Dyson doterm: 3721efb74fbSJohn Dyson 37324a1cce3SDavid Greenman temp = object->backing_object; 374de5f6a77SJohn Dyson if (temp) { 37524a1cce3SDavid Greenman TAILQ_REMOVE(&temp->shadow_head, object, shadow_list); 37695e5e988SJohn Dyson temp->shadow_count--; 3772d8acc0fSJohn Dyson if (temp->ref_count == 0) 378069e9bc1SDoug Rabson vm_object_clear_flag(temp, OBJ_OPT); 379eaf13dd7SJohn Dyson temp->generation++; 38095461b45SJohn Dyson object->backing_object = NULL; 381de5f6a77SJohn Dyson } 382df8bae1dSRodney W. Grimes vm_object_terminate(object); 383df8bae1dSRodney W. Grimes /* unlocks and deallocates object */ 384df8bae1dSRodney W. Grimes object = temp; 385df8bae1dSRodney W. Grimes } 386df8bae1dSRodney W. Grimes } 387df8bae1dSRodney W. Grimes 388df8bae1dSRodney W. Grimes /* 389df8bae1dSRodney W. Grimes * vm_object_terminate actually destroys the specified object, freeing 390df8bae1dSRodney W. Grimes * up all previously used resources. 391df8bae1dSRodney W. Grimes * 392df8bae1dSRodney W. Grimes * The object must be locked. 3931c7c3c6aSMatthew Dillon * This routine may block. 394df8bae1dSRodney W. Grimes */ 39595e5e988SJohn Dyson void 39626f9a767SRodney W. Grimes vm_object_terminate(object) 397d031cff1SMatthew Dillon vm_object_t object; 398df8bae1dSRodney W. Grimes { 399d031cff1SMatthew Dillon vm_page_t p; 400e4b7635dSDavid Greenman int s; 401df8bae1dSRodney W. Grimes 40295e5e988SJohn Dyson /* 40395e5e988SJohn Dyson * Make sure no one uses us. 40495e5e988SJohn Dyson */ 405069e9bc1SDoug Rabson vm_object_set_flag(object, OBJ_DEAD); 4063c631446SJohn Dyson 407df8bae1dSRodney W. Grimes /* 408f6b04d2bSDavid Greenman * wait for the pageout daemon to be done with the object 409df8bae1dSRodney W. Grimes */ 41066095752SJohn Dyson vm_object_pip_wait(object, "objtrm"); 411df8bae1dSRodney W. Grimes 4125526d2d9SEivind Eklund KASSERT(!object->paging_in_progress, 4135526d2d9SEivind Eklund ("vm_object_terminate: pageout in progress")); 41426f9a767SRodney W. Grimes 41526f9a767SRodney W. Grimes /* 4160d94caffSDavid Greenman * Clean and free the pages, as appropriate. All references to the 4170d94caffSDavid Greenman * object are gone, so we don't need to lock it. 41826f9a767SRodney W. Grimes */ 41924a1cce3SDavid Greenman if (object->type == OBJT_VNODE) { 42095e5e988SJohn Dyson struct vnode *vp; 42195e5e988SJohn Dyson 42295e5e988SJohn Dyson /* 42395e5e988SJohn Dyson * Freeze optimized copies. 42495e5e988SJohn Dyson */ 42595e5e988SJohn Dyson vm_freeze_copyopts(object, 0, object->size); 42695e5e988SJohn Dyson 42795e5e988SJohn Dyson /* 42895e5e988SJohn Dyson * Clean pages and flush buffers. 42995e5e988SJohn Dyson */ 4308f9110f6SJohn Dyson vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 43195e5e988SJohn Dyson 43295e5e988SJohn Dyson vp = (struct vnode *) object->handle; 433f6b04d2bSDavid Greenman vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0); 434bef608bdSJohn Dyson } 435bef608bdSJohn Dyson 4360b10ba98SDavid Greenman if (object->ref_count != 0) 4370b10ba98SDavid Greenman panic("vm_object_terminate: object with references, ref_count=%d", object->ref_count); 438996c772fSJohn Dyson 4390d94caffSDavid Greenman /* 440356863ebSDavid Greenman * Now free any remaining pages. For internal objects, this also 441356863ebSDavid Greenman * removes them from paging queues. Don't free wired pages, just 442356863ebSDavid Greenman * remove them from the object. 443df8bae1dSRodney W. Grimes */ 444e4b7635dSDavid Greenman s = splvm(); 445b18bfc3dSJohn Dyson while ((p = TAILQ_FIRST(&object->memq)) != NULL) { 44695461b45SJohn Dyson #if !defined(MAX_PERF) 447a2f4a846SJohn Dyson if (p->busy || (p->flags & PG_BUSY)) 4481c7c3c6aSMatthew Dillon panic("vm_object_terminate: freeing busy page %p\n", p); 44995461b45SJohn Dyson #endif 4500b10ba98SDavid Greenman if (p->wire_count == 0) { 451e69763a3SDoug Rabson vm_page_busy(p); 452df8bae1dSRodney W. Grimes vm_page_free(p); 453df8bae1dSRodney W. Grimes cnt.v_pfree++; 4540b10ba98SDavid Greenman } else { 455356863ebSDavid Greenman vm_page_busy(p); 4560b10ba98SDavid Greenman vm_page_remove(p); 4570b10ba98SDavid Greenman } 458df8bae1dSRodney W. Grimes } 459e4b7635dSDavid Greenman splx(s); 460bef608bdSJohn Dyson 4612d8acc0fSJohn Dyson /* 4629fcfb650SDavid Greenman * Let the pager know object is dead. 4639fcfb650SDavid Greenman */ 4649fcfb650SDavid Greenman vm_pager_deallocate(object); 4659fcfb650SDavid Greenman 4669fcfb650SDavid Greenman /* 4670b10ba98SDavid Greenman * Remove the object from the global object list. 4682d8acc0fSJohn Dyson */ 469996c772fSJohn Dyson simple_lock(&vm_object_list_lock); 470df8bae1dSRodney W. Grimes TAILQ_REMOVE(&vm_object_list, object, object_list); 471996c772fSJohn Dyson simple_unlock(&vm_object_list_lock); 4720b10ba98SDavid Greenman 4730b10ba98SDavid Greenman wakeup(object); 4740b10ba98SDavid Greenman 475df8bae1dSRodney W. Grimes /* 476df8bae1dSRodney W. Grimes * Free the space for the object. 477df8bae1dSRodney W. Grimes */ 47899448ed1SJohn Dyson zfree(obj_zone, object); 47947221757SJohn Dyson } 480df8bae1dSRodney W. Grimes 481df8bae1dSRodney W. Grimes /* 482df8bae1dSRodney W. Grimes * vm_object_page_clean 483df8bae1dSRodney W. Grimes * 484df8bae1dSRodney W. Grimes * Clean all dirty pages in the specified range of object. 48526f9a767SRodney W. Grimes * Leaves page on whatever queue it is currently on. 48626f9a767SRodney W. Grimes * 48726f9a767SRodney W. Grimes * Odd semantics: if start == end, we clean everything. 48826f9a767SRodney W. Grimes * 48926f9a767SRodney W. Grimes * The object must be locked. 49026f9a767SRodney W. Grimes */ 491f6b04d2bSDavid Greenman 492f6b04d2bSDavid Greenman void 4938f9110f6SJohn Dyson vm_object_page_clean(object, start, end, flags) 494f6b04d2bSDavid Greenman vm_object_t object; 495a316d390SJohn Dyson vm_pindex_t start; 496a316d390SJohn Dyson vm_pindex_t end; 4978f9110f6SJohn Dyson int flags; 498f6b04d2bSDavid Greenman { 499d031cff1SMatthew Dillon vm_page_t p, np, tp; 500d031cff1SMatthew Dillon vm_offset_t tstart, tend; 501bd7e5f99SJohn Dyson vm_pindex_t pi; 502aef922f5SJohn Dyson int s; 50324a1cce3SDavid Greenman struct vnode *vp; 504aef922f5SJohn Dyson int runlen; 505bd7e5f99SJohn Dyson int maxf; 506bd7e5f99SJohn Dyson int chkb; 507bd7e5f99SJohn Dyson int maxb; 508bd7e5f99SJohn Dyson int i; 5098f9110f6SJohn Dyson int pagerflags; 510bd7e5f99SJohn Dyson vm_page_t maf[vm_pageout_page_count]; 511bd7e5f99SJohn Dyson vm_page_t mab[vm_pageout_page_count]; 512aef922f5SJohn Dyson vm_page_t ma[vm_pageout_page_count]; 5132d8acc0fSJohn Dyson int curgeneration; 514f6b04d2bSDavid Greenman 515aef922f5SJohn Dyson if (object->type != OBJT_VNODE || 516aef922f5SJohn Dyson (object->flags & OBJ_MIGHTBEDIRTY) == 0) 517f6b04d2bSDavid Greenman return; 518f6b04d2bSDavid Greenman 5198f9110f6SJohn Dyson pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0; 5208f9110f6SJohn Dyson pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0; 5218f9110f6SJohn Dyson 52224a1cce3SDavid Greenman vp = object->handle; 52324a1cce3SDavid Greenman 524069e9bc1SDoug Rabson vm_object_set_flag(object, OBJ_CLEANING); 52524a1cce3SDavid Greenman 526f6b04d2bSDavid Greenman tstart = start; 527f6b04d2bSDavid Greenman if (end == 0) { 528f6b04d2bSDavid Greenman tend = object->size; 529f6b04d2bSDavid Greenman } else { 530f6b04d2bSDavid Greenman tend = end; 531f6b04d2bSDavid Greenman } 532eaf13dd7SJohn Dyson 533eaf13dd7SJohn Dyson for(p = TAILQ_FIRST(&object->memq); p; p = TAILQ_NEXT(p, listq)) { 534e69763a3SDoug Rabson vm_page_flag_set(p, PG_CLEANCHK); 535eaf13dd7SJohn Dyson vm_page_protect(p, VM_PROT_READ); 536eaf13dd7SJohn Dyson } 537eaf13dd7SJohn Dyson 538a316d390SJohn Dyson if ((tstart == 0) && (tend == object->size)) { 539069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY); 540ec4f9fb0SDavid Greenman } 541f6b04d2bSDavid Greenman 542bd7e5f99SJohn Dyson rescan: 5432d8acc0fSJohn Dyson curgeneration = object->generation; 5442d8acc0fSJohn Dyson 545b18bfc3dSJohn Dyson for(p = TAILQ_FIRST(&object->memq); p; p = np) { 546b18bfc3dSJohn Dyson np = TAILQ_NEXT(p, listq); 547bd7e5f99SJohn Dyson 548bd7e5f99SJohn Dyson pi = p->pindex; 549bd7e5f99SJohn Dyson if (((p->flags & PG_CLEANCHK) == 0) || 550bd7e5f99SJohn Dyson (pi < tstart) || (pi >= tend) || 5515070c7f8SJohn Dyson (p->valid == 0) || 5525070c7f8SJohn Dyson ((p->queue - p->pc) == PQ_CACHE)) { 553e69763a3SDoug Rabson vm_page_flag_clear(p, PG_CLEANCHK); 554aef922f5SJohn Dyson continue; 555f6b04d2bSDavid Greenman } 556f6b04d2bSDavid Greenman 557bd7e5f99SJohn Dyson vm_page_test_dirty(p); 558bd7e5f99SJohn Dyson if ((p->dirty & p->valid) == 0) { 559e69763a3SDoug Rabson vm_page_flag_clear(p, PG_CLEANCHK); 560bd7e5f99SJohn Dyson continue; 561bd7e5f99SJohn Dyson } 562ec4f9fb0SDavid Greenman 563b18bfc3dSJohn Dyson s = splvm(); 5641c7c3c6aSMatthew Dillon while (vm_page_sleep_busy(p, TRUE, "vpcwai")) { 5652d8acc0fSJohn Dyson if (object->generation != curgeneration) { 566f6b04d2bSDavid Greenman splx(s); 567bd7e5f99SJohn Dyson goto rescan; 568f6b04d2bSDavid Greenman } 5692d8acc0fSJohn Dyson } 570f6b04d2bSDavid Greenman 571bd7e5f99SJohn Dyson maxf = 0; 572bd7e5f99SJohn Dyson for(i=1;i<vm_pageout_page_count;i++) { 5738aef1712SMatthew Dillon if ((tp = vm_page_lookup(object, pi + i)) != NULL) { 574bd7e5f99SJohn Dyson if ((tp->flags & PG_BUSY) || 575ffc82b0aSJohn Dyson (tp->flags & PG_CLEANCHK) == 0 || 576ffc82b0aSJohn Dyson (tp->busy != 0)) 577bd7e5f99SJohn Dyson break; 5785070c7f8SJohn Dyson if((tp->queue - tp->pc) == PQ_CACHE) { 579e69763a3SDoug Rabson vm_page_flag_clear(tp, PG_CLEANCHK); 5803077a9c2SJohn Dyson break; 5813077a9c2SJohn Dyson } 582bd7e5f99SJohn Dyson vm_page_test_dirty(tp); 583bd7e5f99SJohn Dyson if ((tp->dirty & tp->valid) == 0) { 584e69763a3SDoug Rabson vm_page_flag_clear(tp, PG_CLEANCHK); 585bd7e5f99SJohn Dyson break; 586bd7e5f99SJohn Dyson } 587bd7e5f99SJohn Dyson maf[ i - 1 ] = tp; 588bd7e5f99SJohn Dyson maxf++; 589bd7e5f99SJohn Dyson continue; 590bd7e5f99SJohn Dyson } 591bd7e5f99SJohn Dyson break; 592bd7e5f99SJohn Dyson } 593aef922f5SJohn Dyson 594bd7e5f99SJohn Dyson maxb = 0; 595bd7e5f99SJohn Dyson chkb = vm_pageout_page_count - maxf; 596bd7e5f99SJohn Dyson if (chkb) { 597bd7e5f99SJohn Dyson for(i = 1; i < chkb;i++) { 5988aef1712SMatthew Dillon if ((tp = vm_page_lookup(object, pi - i)) != NULL) { 599bd7e5f99SJohn Dyson if ((tp->flags & PG_BUSY) || 600ffc82b0aSJohn Dyson (tp->flags & PG_CLEANCHK) == 0 || 601ffc82b0aSJohn Dyson (tp->busy != 0)) 602bd7e5f99SJohn Dyson break; 6035070c7f8SJohn Dyson if((tp->queue - tp->pc) == PQ_CACHE) { 604e69763a3SDoug Rabson vm_page_flag_clear(tp, PG_CLEANCHK); 6053077a9c2SJohn Dyson break; 6063077a9c2SJohn Dyson } 607bd7e5f99SJohn Dyson vm_page_test_dirty(tp); 608bd7e5f99SJohn Dyson if ((tp->dirty & tp->valid) == 0) { 609e69763a3SDoug Rabson vm_page_flag_clear(tp, PG_CLEANCHK); 610bd7e5f99SJohn Dyson break; 611bd7e5f99SJohn Dyson } 612bd7e5f99SJohn Dyson mab[ i - 1 ] = tp; 613bd7e5f99SJohn Dyson maxb++; 614bd7e5f99SJohn Dyson continue; 615bd7e5f99SJohn Dyson } 616bd7e5f99SJohn Dyson break; 617bd7e5f99SJohn Dyson } 618bd7e5f99SJohn Dyson } 619bd7e5f99SJohn Dyson 620bd7e5f99SJohn Dyson for(i=0;i<maxb;i++) { 621bd7e5f99SJohn Dyson int index = (maxb - i) - 1; 622bd7e5f99SJohn Dyson ma[index] = mab[i]; 623e69763a3SDoug Rabson vm_page_flag_clear(ma[index], PG_CLEANCHK); 624bd7e5f99SJohn Dyson } 625e69763a3SDoug Rabson vm_page_flag_clear(p, PG_CLEANCHK); 626bd7e5f99SJohn Dyson ma[maxb] = p; 627bd7e5f99SJohn Dyson for(i=0;i<maxf;i++) { 628bd7e5f99SJohn Dyson int index = (maxb + i) + 1; 629bd7e5f99SJohn Dyson ma[index] = maf[i]; 630e69763a3SDoug Rabson vm_page_flag_clear(ma[index], PG_CLEANCHK); 631f6b04d2bSDavid Greenman } 632bd7e5f99SJohn Dyson runlen = maxb + maxf + 1; 633cf2819ccSJohn Dyson 634f35329acSJohn Dyson splx(s); 6358f9110f6SJohn Dyson vm_pageout_flush(ma, runlen, pagerflags); 636cf2819ccSJohn Dyson for (i = 0; i<runlen; i++) { 637cf2819ccSJohn Dyson if (ma[i]->valid & ma[i]->dirty) { 638cf2819ccSJohn Dyson vm_page_protect(ma[i], VM_PROT_READ); 639e69763a3SDoug Rabson vm_page_flag_set(ma[i], PG_CLEANCHK); 640cf2819ccSJohn Dyson } 641cf2819ccSJohn Dyson } 6422d8acc0fSJohn Dyson if (object->generation != curgeneration) 643bd7e5f99SJohn Dyson goto rescan; 644f6b04d2bSDavid Greenman } 645aef922f5SJohn Dyson 646cf2819ccSJohn Dyson VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc); 647aef922f5SJohn Dyson 648069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_CLEANING); 649f5cf85d4SDavid Greenman return; 65026f9a767SRodney W. Grimes } 651df8bae1dSRodney W. Grimes 652f708ef1bSPoul-Henning Kamp #ifdef not_used 653f708ef1bSPoul-Henning Kamp /* XXX I cannot tell if this should be an exported symbol */ 654df8bae1dSRodney W. Grimes /* 655df8bae1dSRodney W. Grimes * vm_object_deactivate_pages 656df8bae1dSRodney W. Grimes * 657df8bae1dSRodney W. Grimes * Deactivate all pages in the specified object. (Keep its pages 658df8bae1dSRodney W. Grimes * in memory even though it is no longer referenced.) 659df8bae1dSRodney W. Grimes * 660df8bae1dSRodney W. Grimes * The object must be locked. 661df8bae1dSRodney W. Grimes */ 662f708ef1bSPoul-Henning Kamp static void 663df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object) 664d031cff1SMatthew Dillon vm_object_t object; 665df8bae1dSRodney W. Grimes { 666d031cff1SMatthew Dillon vm_page_t p, next; 667df8bae1dSRodney W. Grimes 668b18bfc3dSJohn Dyson for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) { 669b18bfc3dSJohn Dyson next = TAILQ_NEXT(p, listq); 670df8bae1dSRodney W. Grimes vm_page_deactivate(p); 671df8bae1dSRodney W. Grimes } 672df8bae1dSRodney W. Grimes } 673f708ef1bSPoul-Henning Kamp #endif 674df8bae1dSRodney W. Grimes 675df8bae1dSRodney W. Grimes /* 6768e3ad7c9SMatthew Dillon * Same as vm_object_pmap_copy, except range checking really 6771efb74fbSJohn Dyson * works, and is meant for small sections of an object. 6788e3ad7c9SMatthew Dillon * 6798e3ad7c9SMatthew Dillon * This code protects resident pages by making them read-only 6808e3ad7c9SMatthew Dillon * and is typically called on a fork or split when a page 6818e3ad7c9SMatthew Dillon * is converted to copy-on-write. 6828e3ad7c9SMatthew Dillon * 6838e3ad7c9SMatthew Dillon * NOTE: If the page is already at VM_PROT_NONE, calling 6848e3ad7c9SMatthew Dillon * vm_page_protect will have no effect. 6851efb74fbSJohn Dyson */ 6868e3ad7c9SMatthew Dillon 6871efb74fbSJohn Dyson void 6881efb74fbSJohn Dyson vm_object_pmap_copy_1(object, start, end) 689d031cff1SMatthew Dillon vm_object_t object; 690d031cff1SMatthew Dillon vm_pindex_t start; 691d031cff1SMatthew Dillon vm_pindex_t end; 6921efb74fbSJohn Dyson { 6931efb74fbSJohn Dyson vm_pindex_t idx; 694d031cff1SMatthew Dillon vm_page_t p; 6951efb74fbSJohn Dyson 6961efb74fbSJohn Dyson if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0) 6971efb74fbSJohn Dyson return; 6981efb74fbSJohn Dyson 6991efb74fbSJohn Dyson for (idx = start; idx < end; idx++) { 7001efb74fbSJohn Dyson p = vm_page_lookup(object, idx); 7011efb74fbSJohn Dyson if (p == NULL) 7021efb74fbSJohn Dyson continue; 7031efb74fbSJohn Dyson vm_page_protect(p, VM_PROT_READ); 7041efb74fbSJohn Dyson } 7051efb74fbSJohn Dyson } 7061efb74fbSJohn Dyson 7071efb74fbSJohn Dyson /* 708df8bae1dSRodney W. Grimes * vm_object_pmap_remove: 709df8bae1dSRodney W. Grimes * 710df8bae1dSRodney W. Grimes * Removes all physical pages in the specified 711df8bae1dSRodney W. Grimes * object range from all physical maps. 712df8bae1dSRodney W. Grimes * 713df8bae1dSRodney W. Grimes * The object must *not* be locked. 714df8bae1dSRodney W. Grimes */ 71526f9a767SRodney W. Grimes void 71626f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end) 717d031cff1SMatthew Dillon vm_object_t object; 718d031cff1SMatthew Dillon vm_pindex_t start; 719d031cff1SMatthew Dillon vm_pindex_t end; 720df8bae1dSRodney W. Grimes { 721d031cff1SMatthew Dillon vm_page_t p; 722d031cff1SMatthew Dillon 723df8bae1dSRodney W. Grimes if (object == NULL) 724df8bae1dSRodney W. Grimes return; 725b5b40fa6SJohn Dyson for (p = TAILQ_FIRST(&object->memq); 726b5b40fa6SJohn Dyson p != NULL; 727b5b40fa6SJohn Dyson p = TAILQ_NEXT(p, listq)) { 728bd7e5f99SJohn Dyson if (p->pindex >= start && p->pindex < end) 729f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 73026f9a767SRodney W. Grimes } 7316e20a165SJohn Dyson if ((start == 0) && (object->size == end)) 732069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_WRITEABLE); 73326f9a767SRodney W. Grimes } 734df8bae1dSRodney W. Grimes 735df8bae1dSRodney W. Grimes /* 736867a482dSJohn Dyson * vm_object_madvise: 737867a482dSJohn Dyson * 738867a482dSJohn Dyson * Implements the madvise function at the object/page level. 7391c7c3c6aSMatthew Dillon * 7401c7c3c6aSMatthew Dillon * Currently, madvise() functions are limited to the default and 7411c7c3c6aSMatthew Dillon * swap object types only, and also limited to only the unshared portions 7421c7c3c6aSMatthew Dillon * of a process's address space. MADV_FREE, certainly, could never be 7431c7c3c6aSMatthew Dillon * run on anything else. The others are more flexible and the code could 7441c7c3c6aSMatthew Dillon * be adjusted in the future to handle expanded cases for them. 745867a482dSJohn Dyson */ 746867a482dSJohn Dyson void 747867a482dSJohn Dyson vm_object_madvise(object, pindex, count, advise) 748867a482dSJohn Dyson vm_object_t object; 749867a482dSJohn Dyson vm_pindex_t pindex; 750867a482dSJohn Dyson int count; 751867a482dSJohn Dyson int advise; 752867a482dSJohn Dyson { 7536e20a165SJohn Dyson vm_pindex_t end, tpindex; 7546e20a165SJohn Dyson vm_object_t tobject; 755867a482dSJohn Dyson vm_page_t m; 756867a482dSJohn Dyson 757867a482dSJohn Dyson if (object == NULL) 758867a482dSJohn Dyson return; 759867a482dSJohn Dyson 760867a482dSJohn Dyson end = pindex + count; 761867a482dSJohn Dyson 7621c7c3c6aSMatthew Dillon /* 7631c7c3c6aSMatthew Dillon * Locate and adjust resident pages 7641c7c3c6aSMatthew Dillon */ 7651c7c3c6aSMatthew Dillon 7661c7c3c6aSMatthew Dillon for (; pindex < end; pindex += 1) { 7676e20a165SJohn Dyson relookup: 7686e20a165SJohn Dyson tobject = object; 7696e20a165SJohn Dyson tpindex = pindex; 7706e20a165SJohn Dyson shadowlookup: 7711c7c3c6aSMatthew Dillon 7721c7c3c6aSMatthew Dillon if (tobject->type != OBJT_DEFAULT && 7731c7c3c6aSMatthew Dillon tobject->type != OBJT_SWAP 7741c7c3c6aSMatthew Dillon ) { 7756e20a165SJohn Dyson continue; 7766e20a165SJohn Dyson } 7776e20a165SJohn Dyson 7781c7c3c6aSMatthew Dillon if ((tobject->flags & OBJ_ONEMAPPING) == 0) 7791c7c3c6aSMatthew Dillon continue; 7801c7c3c6aSMatthew Dillon 7811c7c3c6aSMatthew Dillon m = vm_page_lookup(tobject, tpindex); 7821c7c3c6aSMatthew Dillon 7831c7c3c6aSMatthew Dillon if (m == NULL) { 7841ce137beSMatthew Dillon /* 7851ce137beSMatthew Dillon * There may be swap even if there is no backing page 7861ce137beSMatthew Dillon */ 7871ce137beSMatthew Dillon if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 7881ce137beSMatthew Dillon swap_pager_freespace(tobject, tpindex, 1); 7891ce137beSMatthew Dillon 7901ce137beSMatthew Dillon /* 7911ce137beSMatthew Dillon * next object 7921ce137beSMatthew Dillon */ 7936e20a165SJohn Dyson tobject = tobject->backing_object; 7941c7c3c6aSMatthew Dillon if (tobject == NULL) 7951c7c3c6aSMatthew Dillon continue; 7966e20a165SJohn Dyson tpindex += OFF_TO_IDX(tobject->backing_object_offset); 7976e20a165SJohn Dyson goto shadowlookup; 7986e20a165SJohn Dyson } 799867a482dSJohn Dyson 800867a482dSJohn Dyson /* 801867a482dSJohn Dyson * If the page is busy or not in a normal active state, 802867a482dSJohn Dyson * we skip it. Things can break if we mess with pages 803867a482dSJohn Dyson * in any of the below states. 804867a482dSJohn Dyson */ 8051c7c3c6aSMatthew Dillon if ( 8061c7c3c6aSMatthew Dillon m->hold_count || 8071c7c3c6aSMatthew Dillon m->wire_count || 8081c7c3c6aSMatthew Dillon m->valid != VM_PAGE_BITS_ALL 8091c7c3c6aSMatthew Dillon ) { 810867a482dSJohn Dyson continue; 8116e20a165SJohn Dyson } 8126e20a165SJohn Dyson 8131c7c3c6aSMatthew Dillon if (vm_page_sleep_busy(m, TRUE, "madvpo")) 8146e20a165SJohn Dyson goto relookup; 815867a482dSJohn Dyson 816867a482dSJohn Dyson if (advise == MADV_WILLNEED) { 817867a482dSJohn Dyson vm_page_activate(m); 8186e20a165SJohn Dyson } else if (advise == MADV_DONTNEED) { 819867a482dSJohn Dyson vm_page_deactivate(m); 8200a47b48bSJohn Dyson } else if (advise == MADV_FREE) { 8211c7c3c6aSMatthew Dillon /* 8222aaeadf8SMatthew Dillon * Mark the page clean. This will allow the page 8232aaeadf8SMatthew Dillon * to be freed up by the system. However, such pages 8242aaeadf8SMatthew Dillon * are often reused quickly by malloc()/free() 8252aaeadf8SMatthew Dillon * so we do not do anything that would cause 8262aaeadf8SMatthew Dillon * a page fault if we can help it. 8272aaeadf8SMatthew Dillon * 8282aaeadf8SMatthew Dillon * Specifically, we do not try to actually free 8292aaeadf8SMatthew Dillon * the page now nor do we try to put it in the 8302aaeadf8SMatthew Dillon * cache (which would cause a page fault on reuse). 83141c67e12SMatthew Dillon * 83241c67e12SMatthew Dillon * But we do make the page is freeable as we 83341c67e12SMatthew Dillon * can without actually taking the step of unmapping 83441c67e12SMatthew Dillon * it. 8351c7c3c6aSMatthew Dillon */ 8366e20a165SJohn Dyson pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 8376e20a165SJohn Dyson m->dirty = 0; 83841c67e12SMatthew Dillon m->act_count = 0; 83941c67e12SMatthew Dillon vm_page_deactivate(m); 8401ce137beSMatthew Dillon if (tobject->type == OBJT_SWAP) 8411ce137beSMatthew Dillon swap_pager_freespace(tobject, tpindex, 1); 842867a482dSJohn Dyson } 843867a482dSJohn Dyson } 844867a482dSJohn Dyson } 845867a482dSJohn Dyson 846867a482dSJohn Dyson /* 847df8bae1dSRodney W. Grimes * vm_object_shadow: 848df8bae1dSRodney W. Grimes * 849df8bae1dSRodney W. Grimes * Create a new object which is backed by the 850df8bae1dSRodney W. Grimes * specified existing object range. The source 851df8bae1dSRodney W. Grimes * object reference is deallocated. 852df8bae1dSRodney W. Grimes * 853df8bae1dSRodney W. Grimes * The new object and offset into that object 854df8bae1dSRodney W. Grimes * are returned in the source parameters. 855df8bae1dSRodney W. Grimes */ 856df8bae1dSRodney W. Grimes 85726f9a767SRodney W. Grimes void 85826f9a767SRodney W. Grimes vm_object_shadow(object, offset, length) 859df8bae1dSRodney W. Grimes vm_object_t *object; /* IN/OUT */ 860a316d390SJohn Dyson vm_ooffset_t *offset; /* IN/OUT */ 861df8bae1dSRodney W. Grimes vm_size_t length; 862df8bae1dSRodney W. Grimes { 863d031cff1SMatthew Dillon vm_object_t source; 864d031cff1SMatthew Dillon vm_object_t result; 865df8bae1dSRodney W. Grimes 866df8bae1dSRodney W. Grimes source = *object; 867df8bae1dSRodney W. Grimes 868df8bae1dSRodney W. Grimes /* 8699a2f6362SAlan Cox * Don't create the new object if the old object isn't shared. 8709a2f6362SAlan Cox */ 8719a2f6362SAlan Cox 872c7997d57SAlan Cox if (source != NULL && 873c7997d57SAlan Cox source->ref_count == 1 && 8749a2f6362SAlan Cox source->handle == NULL && 8759a2f6362SAlan Cox (source->type == OBJT_DEFAULT || 8769a2f6362SAlan Cox source->type == OBJT_SWAP)) 8779a2f6362SAlan Cox return; 8789a2f6362SAlan Cox 8799a2f6362SAlan Cox /* 880df8bae1dSRodney W. Grimes * Allocate a new object with the given length 881df8bae1dSRodney W. Grimes */ 882df8bae1dSRodney W. Grimes 88324a1cce3SDavid Greenman if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL) 884df8bae1dSRodney W. Grimes panic("vm_object_shadow: no object for shadowing"); 885df8bae1dSRodney W. Grimes 886df8bae1dSRodney W. Grimes /* 8870d94caffSDavid Greenman * The new object shadows the source object, adding a reference to it. 8880d94caffSDavid Greenman * Our caller changes his reference to point to the new object, 8890d94caffSDavid Greenman * removing a reference to the source object. Net result: no change 8900d94caffSDavid Greenman * of reference count. 8919b09fe24SMatthew Dillon * 8929b09fe24SMatthew Dillon * Try to optimize the result object's page color when shadowing 8939b09fe24SMatthew Dillon * in order to maintain page coloring consistancy in the combined 8949b09fe24SMatthew Dillon * shadowed object. 895df8bae1dSRodney W. Grimes */ 89624a1cce3SDavid Greenman result->backing_object = source; 897de5f6a77SJohn Dyson if (source) { 898de5f6a77SJohn Dyson TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list); 899069e9bc1SDoug Rabson vm_object_clear_flag(source, OBJ_ONEMAPPING); 900eaf13dd7SJohn Dyson source->shadow_count++; 901eaf13dd7SJohn Dyson source->generation++; 9029b09fe24SMatthew Dillon result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & PQ_L2_MASK; 903de5f6a77SJohn Dyson } 904df8bae1dSRodney W. Grimes 905df8bae1dSRodney W. Grimes /* 9060d94caffSDavid Greenman * Store the offset into the source object, and fix up the offset into 9070d94caffSDavid Greenman * the new object. 908df8bae1dSRodney W. Grimes */ 909df8bae1dSRodney W. Grimes 91024a1cce3SDavid Greenman result->backing_object_offset = *offset; 911df8bae1dSRodney W. Grimes 912df8bae1dSRodney W. Grimes /* 913df8bae1dSRodney W. Grimes * Return the new things 914df8bae1dSRodney W. Grimes */ 915df8bae1dSRodney W. Grimes 916df8bae1dSRodney W. Grimes *offset = 0; 917df8bae1dSRodney W. Grimes *object = result; 918df8bae1dSRodney W. Grimes } 919df8bae1dSRodney W. Grimes 9202ad1a3f7SMatthew Dillon #define OBSC_TEST_ALL_SHADOWED 0x0001 9212ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_NOWAIT 0x0002 9222ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_WAIT 0x0004 9232ad1a3f7SMatthew Dillon 9242ad1a3f7SMatthew Dillon static __inline int 9252ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op) 9262ad1a3f7SMatthew Dillon { 9272ad1a3f7SMatthew Dillon int s; 9282ad1a3f7SMatthew Dillon int r = 1; 9292ad1a3f7SMatthew Dillon vm_page_t p; 9302ad1a3f7SMatthew Dillon vm_object_t backing_object; 9312ad1a3f7SMatthew Dillon vm_pindex_t backing_offset_index; 9322ad1a3f7SMatthew Dillon 9332ad1a3f7SMatthew Dillon s = splvm(); 9342ad1a3f7SMatthew Dillon 9352ad1a3f7SMatthew Dillon backing_object = object->backing_object; 9362ad1a3f7SMatthew Dillon backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 9372ad1a3f7SMatthew Dillon 9382ad1a3f7SMatthew Dillon /* 9392ad1a3f7SMatthew Dillon * Initial conditions 9402ad1a3f7SMatthew Dillon */ 9412ad1a3f7SMatthew Dillon 9422ad1a3f7SMatthew Dillon if (op & OBSC_TEST_ALL_SHADOWED) { 9432ad1a3f7SMatthew Dillon /* 9442ad1a3f7SMatthew Dillon * We do not want to have to test for the existance of 9452ad1a3f7SMatthew Dillon * swap pages in the backing object. XXX but with the 9462ad1a3f7SMatthew Dillon * new swapper this would be pretty easy to do. 9472ad1a3f7SMatthew Dillon * 9482ad1a3f7SMatthew Dillon * XXX what about anonymous MAP_SHARED memory that hasn't 9492ad1a3f7SMatthew Dillon * been ZFOD faulted yet? If we do not test for this, the 9502ad1a3f7SMatthew Dillon * shadow test may succeed! XXX 9512ad1a3f7SMatthew Dillon */ 9522ad1a3f7SMatthew Dillon if (backing_object->type != OBJT_DEFAULT) { 9532ad1a3f7SMatthew Dillon splx(s); 9542ad1a3f7SMatthew Dillon return(0); 9552ad1a3f7SMatthew Dillon } 9562ad1a3f7SMatthew Dillon } 9572ad1a3f7SMatthew Dillon if (op & OBSC_COLLAPSE_WAIT) { 9582ad1a3f7SMatthew Dillon vm_object_set_flag(backing_object, OBJ_DEAD); 9592ad1a3f7SMatthew Dillon } 9602ad1a3f7SMatthew Dillon 9612ad1a3f7SMatthew Dillon /* 9622ad1a3f7SMatthew Dillon * Our scan 9632ad1a3f7SMatthew Dillon */ 9642ad1a3f7SMatthew Dillon 9652ad1a3f7SMatthew Dillon p = TAILQ_FIRST(&backing_object->memq); 9662ad1a3f7SMatthew Dillon while (p) { 9672ad1a3f7SMatthew Dillon vm_page_t next = TAILQ_NEXT(p, listq); 9682ad1a3f7SMatthew Dillon vm_pindex_t new_pindex = p->pindex - backing_offset_index; 9692ad1a3f7SMatthew Dillon 9702ad1a3f7SMatthew Dillon if (op & OBSC_TEST_ALL_SHADOWED) { 9712ad1a3f7SMatthew Dillon vm_page_t pp; 9722ad1a3f7SMatthew Dillon 9732ad1a3f7SMatthew Dillon /* 9742ad1a3f7SMatthew Dillon * Ignore pages outside the parent object's range 9752ad1a3f7SMatthew Dillon * and outside the parent object's mapping of the 9762ad1a3f7SMatthew Dillon * backing object. 9772ad1a3f7SMatthew Dillon * 9782ad1a3f7SMatthew Dillon * note that we do not busy the backing object's 9792ad1a3f7SMatthew Dillon * page. 9802ad1a3f7SMatthew Dillon */ 9812ad1a3f7SMatthew Dillon 9822ad1a3f7SMatthew Dillon if ( 9832ad1a3f7SMatthew Dillon p->pindex < backing_offset_index || 9842ad1a3f7SMatthew Dillon new_pindex >= object->size 9852ad1a3f7SMatthew Dillon ) { 9862ad1a3f7SMatthew Dillon p = next; 9872ad1a3f7SMatthew Dillon continue; 9882ad1a3f7SMatthew Dillon } 9892ad1a3f7SMatthew Dillon 9902ad1a3f7SMatthew Dillon /* 9912ad1a3f7SMatthew Dillon * See if the parent has the page or if the parent's 9922ad1a3f7SMatthew Dillon * object pager has the page. If the parent has the 9932ad1a3f7SMatthew Dillon * page but the page is not valid, the parent's 9942ad1a3f7SMatthew Dillon * object pager must have the page. 9952ad1a3f7SMatthew Dillon * 9962ad1a3f7SMatthew Dillon * If this fails, the parent does not completely shadow 9972ad1a3f7SMatthew Dillon * the object and we might as well give up now. 9982ad1a3f7SMatthew Dillon */ 9992ad1a3f7SMatthew Dillon 10002ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 10012ad1a3f7SMatthew Dillon if ( 10022ad1a3f7SMatthew Dillon (pp == NULL || pp->valid == 0) && 10032ad1a3f7SMatthew Dillon !vm_pager_has_page(object, new_pindex, NULL, NULL) 10042ad1a3f7SMatthew Dillon ) { 10052ad1a3f7SMatthew Dillon r = 0; 10062ad1a3f7SMatthew Dillon break; 10072ad1a3f7SMatthew Dillon } 10082ad1a3f7SMatthew Dillon } 10092ad1a3f7SMatthew Dillon 10102ad1a3f7SMatthew Dillon /* 10112ad1a3f7SMatthew Dillon * Check for busy page 10122ad1a3f7SMatthew Dillon */ 10132ad1a3f7SMatthew Dillon 10142ad1a3f7SMatthew Dillon if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) { 10152ad1a3f7SMatthew Dillon vm_page_t pp; 10162ad1a3f7SMatthew Dillon 10172ad1a3f7SMatthew Dillon if (op & OBSC_COLLAPSE_NOWAIT) { 10182ad1a3f7SMatthew Dillon if ( 10192ad1a3f7SMatthew Dillon (p->flags & PG_BUSY) || 10202ad1a3f7SMatthew Dillon !p->valid || 10212ad1a3f7SMatthew Dillon p->hold_count || 10222ad1a3f7SMatthew Dillon p->wire_count || 10232ad1a3f7SMatthew Dillon p->busy 10242ad1a3f7SMatthew Dillon ) { 10252ad1a3f7SMatthew Dillon p = next; 10262ad1a3f7SMatthew Dillon continue; 10272ad1a3f7SMatthew Dillon } 10282ad1a3f7SMatthew Dillon } else if (op & OBSC_COLLAPSE_WAIT) { 10292ad1a3f7SMatthew Dillon if (vm_page_sleep_busy(p, TRUE, "vmocol")) { 10302ad1a3f7SMatthew Dillon /* 10312ad1a3f7SMatthew Dillon * If we slept, anything could have 10322ad1a3f7SMatthew Dillon * happened. Since the object is 10332ad1a3f7SMatthew Dillon * marked dead, the backing offset 10342ad1a3f7SMatthew Dillon * should not have changed so we 10352ad1a3f7SMatthew Dillon * just restart our scan. 10362ad1a3f7SMatthew Dillon */ 10372ad1a3f7SMatthew Dillon p = TAILQ_FIRST(&backing_object->memq); 10382ad1a3f7SMatthew Dillon continue; 10392ad1a3f7SMatthew Dillon } 10402ad1a3f7SMatthew Dillon } 10412ad1a3f7SMatthew Dillon 10422ad1a3f7SMatthew Dillon /* 10432ad1a3f7SMatthew Dillon * Busy the page 10442ad1a3f7SMatthew Dillon */ 10452ad1a3f7SMatthew Dillon vm_page_busy(p); 10462ad1a3f7SMatthew Dillon 10472ad1a3f7SMatthew Dillon KASSERT( 10482ad1a3f7SMatthew Dillon p->object == backing_object, 10492ad1a3f7SMatthew Dillon ("vm_object_qcollapse(): object mismatch") 10502ad1a3f7SMatthew Dillon ); 10512ad1a3f7SMatthew Dillon 10522ad1a3f7SMatthew Dillon /* 10532ad1a3f7SMatthew Dillon * Destroy any associated swap 10542ad1a3f7SMatthew Dillon */ 10552ad1a3f7SMatthew Dillon if (backing_object->type == OBJT_SWAP) { 10562ad1a3f7SMatthew Dillon swap_pager_freespace( 10572ad1a3f7SMatthew Dillon backing_object, 10582ad1a3f7SMatthew Dillon p->pindex, 10592ad1a3f7SMatthew Dillon 1 10602ad1a3f7SMatthew Dillon ); 10612ad1a3f7SMatthew Dillon } 10622ad1a3f7SMatthew Dillon 10632ad1a3f7SMatthew Dillon if ( 10642ad1a3f7SMatthew Dillon p->pindex < backing_offset_index || 10652ad1a3f7SMatthew Dillon new_pindex >= object->size 10662ad1a3f7SMatthew Dillon ) { 10672ad1a3f7SMatthew Dillon /* 10682ad1a3f7SMatthew Dillon * Page is out of the parent object's range, we 10692ad1a3f7SMatthew Dillon * can simply destroy it. 10702ad1a3f7SMatthew Dillon */ 10712ad1a3f7SMatthew Dillon vm_page_protect(p, VM_PROT_NONE); 10722ad1a3f7SMatthew Dillon vm_page_free(p); 10732ad1a3f7SMatthew Dillon p = next; 10742ad1a3f7SMatthew Dillon continue; 10752ad1a3f7SMatthew Dillon } 10762ad1a3f7SMatthew Dillon 10772ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 10782ad1a3f7SMatthew Dillon if ( 10792ad1a3f7SMatthew Dillon pp != NULL || 10802ad1a3f7SMatthew Dillon vm_pager_has_page(object, new_pindex, NULL, NULL) 10812ad1a3f7SMatthew Dillon ) { 10822ad1a3f7SMatthew Dillon /* 10832ad1a3f7SMatthew Dillon * page already exists in parent OR swap exists 10842ad1a3f7SMatthew Dillon * for this location in the parent. Destroy 10852ad1a3f7SMatthew Dillon * the original page from the backing object. 10862ad1a3f7SMatthew Dillon * 10872ad1a3f7SMatthew Dillon * Leave the parent's page alone 10882ad1a3f7SMatthew Dillon */ 10892ad1a3f7SMatthew Dillon vm_page_protect(p, VM_PROT_NONE); 10902ad1a3f7SMatthew Dillon vm_page_free(p); 10912ad1a3f7SMatthew Dillon p = next; 10922ad1a3f7SMatthew Dillon continue; 10932ad1a3f7SMatthew Dillon } 10942ad1a3f7SMatthew Dillon 10952ad1a3f7SMatthew Dillon /* 10962ad1a3f7SMatthew Dillon * Page does not exist in parent, rename the 10972ad1a3f7SMatthew Dillon * page from the backing object to the main object. 1098d1bf5d56SMatthew Dillon * 1099d1bf5d56SMatthew Dillon * If the page was mapped to a process, it can remain 1100d1bf5d56SMatthew Dillon * mapped through the rename. 11012ad1a3f7SMatthew Dillon */ 11022ad1a3f7SMatthew Dillon if ((p->queue - p->pc) == PQ_CACHE) 11032ad1a3f7SMatthew Dillon vm_page_deactivate(p); 11042ad1a3f7SMatthew Dillon 11052ad1a3f7SMatthew Dillon vm_page_rename(p, object, new_pindex); 11062ad1a3f7SMatthew Dillon /* page automatically made dirty by rename */ 11072ad1a3f7SMatthew Dillon } 11082ad1a3f7SMatthew Dillon p = next; 11092ad1a3f7SMatthew Dillon } 11102ad1a3f7SMatthew Dillon splx(s); 11112ad1a3f7SMatthew Dillon return(r); 11122ad1a3f7SMatthew Dillon } 11132ad1a3f7SMatthew Dillon 1114df8bae1dSRodney W. Grimes 1115df8bae1dSRodney W. Grimes /* 11162fe6e4d7SDavid Greenman * this version of collapse allows the operation to occur earlier and 11172fe6e4d7SDavid Greenman * when paging_in_progress is true for an object... This is not a complete 11182fe6e4d7SDavid Greenman * operation, but should plug 99.9% of the rest of the leaks. 11192fe6e4d7SDavid Greenman */ 11202fe6e4d7SDavid Greenman static void 11212fe6e4d7SDavid Greenman vm_object_qcollapse(object) 1122d031cff1SMatthew Dillon vm_object_t object; 11232fe6e4d7SDavid Greenman { 11242ad1a3f7SMatthew Dillon vm_object_t backing_object = object->backing_object; 11252fe6e4d7SDavid Greenman 11262fe6e4d7SDavid Greenman if (backing_object->ref_count != 1) 11272fe6e4d7SDavid Greenman return; 11282fe6e4d7SDavid Greenman 1129010cf3b9SDavid Greenman backing_object->ref_count += 2; 1130010cf3b9SDavid Greenman 11312ad1a3f7SMatthew Dillon vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT); 11321c7c3c6aSMatthew Dillon 1133010cf3b9SDavid Greenman backing_object->ref_count -= 2; 11342fe6e4d7SDavid Greenman } 11352fe6e4d7SDavid Greenman 1136df8bae1dSRodney W. Grimes /* 1137df8bae1dSRodney W. Grimes * vm_object_collapse: 1138df8bae1dSRodney W. Grimes * 1139df8bae1dSRodney W. Grimes * Collapse an object with the object backing it. 1140df8bae1dSRodney W. Grimes * Pages in the backing object are moved into the 1141df8bae1dSRodney W. Grimes * parent, and the backing object is deallocated. 1142df8bae1dSRodney W. Grimes */ 114326f9a767SRodney W. Grimes void 114426f9a767SRodney W. Grimes vm_object_collapse(object) 114524a1cce3SDavid Greenman vm_object_t object; 1146df8bae1dSRodney W. Grimes { 1147df8bae1dSRodney W. Grimes while (TRUE) { 11482ad1a3f7SMatthew Dillon vm_object_t backing_object; 11492ad1a3f7SMatthew Dillon 1150df8bae1dSRodney W. Grimes /* 1151df8bae1dSRodney W. Grimes * Verify that the conditions are right for collapse: 1152df8bae1dSRodney W. Grimes * 11532ad1a3f7SMatthew Dillon * The object exists and the backing object exists. 1154df8bae1dSRodney W. Grimes */ 11552fe6e4d7SDavid Greenman if (object == NULL) 11562ad1a3f7SMatthew Dillon break; 1157df8bae1dSRodney W. Grimes 115824a1cce3SDavid Greenman if ((backing_object = object->backing_object) == NULL) 11592ad1a3f7SMatthew Dillon break; 1160df8bae1dSRodney W. Grimes 1161f919ebdeSDavid Greenman /* 1162f919ebdeSDavid Greenman * we check the backing object first, because it is most likely 116324a1cce3SDavid Greenman * not collapsable. 1164f919ebdeSDavid Greenman */ 116524a1cce3SDavid Greenman if (backing_object->handle != NULL || 116624a1cce3SDavid Greenman (backing_object->type != OBJT_DEFAULT && 116724a1cce3SDavid Greenman backing_object->type != OBJT_SWAP) || 1168f919ebdeSDavid Greenman (backing_object->flags & OBJ_DEAD) || 116924a1cce3SDavid Greenman object->handle != NULL || 117024a1cce3SDavid Greenman (object->type != OBJT_DEFAULT && 117124a1cce3SDavid Greenman object->type != OBJT_SWAP) || 117224a1cce3SDavid Greenman (object->flags & OBJ_DEAD)) { 11732ad1a3f7SMatthew Dillon break; 117424a1cce3SDavid Greenman } 11759b4814bbSDavid Greenman 11762ad1a3f7SMatthew Dillon if ( 11772ad1a3f7SMatthew Dillon object->paging_in_progress != 0 || 11782ad1a3f7SMatthew Dillon backing_object->paging_in_progress != 0 11792ad1a3f7SMatthew Dillon ) { 1180b9921222SDavid Greenman vm_object_qcollapse(object); 11812ad1a3f7SMatthew Dillon break; 1182df8bae1dSRodney W. Grimes } 1183f919ebdeSDavid Greenman 118426f9a767SRodney W. Grimes /* 11850d94caffSDavid Greenman * We know that we can either collapse the backing object (if 11862ad1a3f7SMatthew Dillon * the parent is the only reference to it) or (perhaps) have 11872ad1a3f7SMatthew Dillon * the parent bypass the object if the parent happens to shadow 11882ad1a3f7SMatthew Dillon * all the resident pages in the entire backing object. 11892ad1a3f7SMatthew Dillon * 11902ad1a3f7SMatthew Dillon * This is ignoring pager-backed pages such as swap pages. 11912ad1a3f7SMatthew Dillon * vm_object_backing_scan fails the shadowing test in this 11922ad1a3f7SMatthew Dillon * case. 1193df8bae1dSRodney W. Grimes */ 1194df8bae1dSRodney W. Grimes 1195df8bae1dSRodney W. Grimes if (backing_object->ref_count == 1) { 1196df8bae1dSRodney W. Grimes /* 11972ad1a3f7SMatthew Dillon * If there is exactly one reference to the backing 11982ad1a3f7SMatthew Dillon * object, we can collapse it into the parent. 1199df8bae1dSRodney W. Grimes */ 1200df8bae1dSRodney W. Grimes 12012ad1a3f7SMatthew Dillon vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT); 1202df8bae1dSRodney W. Grimes 1203df8bae1dSRodney W. Grimes /* 1204df8bae1dSRodney W. Grimes * Move the pager from backing_object to object. 1205df8bae1dSRodney W. Grimes */ 1206df8bae1dSRodney W. Grimes 120724a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) { 1208d474eaaaSDoug Rabson vm_object_pip_add(backing_object, 1); 120924a1cce3SDavid Greenman 121024a1cce3SDavid Greenman /* 12111c7c3c6aSMatthew Dillon * scrap the paging_offset junk and do a 12121c7c3c6aSMatthew Dillon * discrete copy. This also removes major 12131c7c3c6aSMatthew Dillon * assumptions about how the swap-pager 12141c7c3c6aSMatthew Dillon * works from where it doesn't belong. The 12151c7c3c6aSMatthew Dillon * new swapper is able to optimize the 12161c7c3c6aSMatthew Dillon * destroy-source case. 121724a1cce3SDavid Greenman */ 12181c7c3c6aSMatthew Dillon 12191c7c3c6aSMatthew Dillon vm_object_pip_add(object, 1); 12201c7c3c6aSMatthew Dillon swap_pager_copy( 12211c7c3c6aSMatthew Dillon backing_object, 12221c7c3c6aSMatthew Dillon object, 12231c7c3c6aSMatthew Dillon OFF_TO_IDX(object->backing_object_offset), TRUE); 1224f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 1225c0503609SDavid Greenman 1226f919ebdeSDavid Greenman vm_object_pip_wakeup(backing_object); 1227c0503609SDavid Greenman } 1228df8bae1dSRodney W. Grimes /* 1229df8bae1dSRodney W. Grimes * Object now shadows whatever backing_object did. 12302ad1a3f7SMatthew Dillon * Note that the reference to 12312ad1a3f7SMatthew Dillon * backing_object->backing_object moves from within 12322ad1a3f7SMatthew Dillon * backing_object to within object. 1233df8bae1dSRodney W. Grimes */ 1234df8bae1dSRodney W. Grimes 12352ad1a3f7SMatthew Dillon TAILQ_REMOVE( 12362ad1a3f7SMatthew Dillon &object->backing_object->shadow_head, 12372ad1a3f7SMatthew Dillon object, 12382ad1a3f7SMatthew Dillon shadow_list 12392ad1a3f7SMatthew Dillon ); 1240eaf13dd7SJohn Dyson object->backing_object->shadow_count--; 1241eaf13dd7SJohn Dyson object->backing_object->generation++; 1242de5f6a77SJohn Dyson if (backing_object->backing_object) { 12432ad1a3f7SMatthew Dillon TAILQ_REMOVE( 12442ad1a3f7SMatthew Dillon &backing_object->backing_object->shadow_head, 12452ad1a3f7SMatthew Dillon backing_object, 12462ad1a3f7SMatthew Dillon shadow_list 12472ad1a3f7SMatthew Dillon ); 1248eaf13dd7SJohn Dyson backing_object->backing_object->shadow_count--; 1249eaf13dd7SJohn Dyson backing_object->backing_object->generation++; 1250de5f6a77SJohn Dyson } 125124a1cce3SDavid Greenman object->backing_object = backing_object->backing_object; 1252de5f6a77SJohn Dyson if (object->backing_object) { 12532ad1a3f7SMatthew Dillon TAILQ_INSERT_TAIL( 12542ad1a3f7SMatthew Dillon &object->backing_object->shadow_head, 12552ad1a3f7SMatthew Dillon object, 12562ad1a3f7SMatthew Dillon shadow_list 12572ad1a3f7SMatthew Dillon ); 1258eaf13dd7SJohn Dyson object->backing_object->shadow_count++; 1259eaf13dd7SJohn Dyson object->backing_object->generation++; 1260de5f6a77SJohn Dyson } 12612fe6e4d7SDavid Greenman 12622ad1a3f7SMatthew Dillon object->backing_object_offset += 12632ad1a3f7SMatthew Dillon backing_object->backing_object_offset; 12642ad1a3f7SMatthew Dillon 1265df8bae1dSRodney W. Grimes /* 1266df8bae1dSRodney W. Grimes * Discard backing_object. 1267df8bae1dSRodney W. Grimes * 12680d94caffSDavid Greenman * Since the backing object has no pages, no pager left, 12690d94caffSDavid Greenman * and no object references within it, all that is 12700d94caffSDavid Greenman * necessary is to dispose of it. 1271df8bae1dSRodney W. Grimes */ 1272df8bae1dSRodney W. Grimes 12732ad1a3f7SMatthew Dillon TAILQ_REMOVE( 12742ad1a3f7SMatthew Dillon &vm_object_list, 12752ad1a3f7SMatthew Dillon backing_object, 12762ad1a3f7SMatthew Dillon object_list 12772ad1a3f7SMatthew Dillon ); 1278df8bae1dSRodney W. Grimes vm_object_count--; 1279df8bae1dSRodney W. Grimes 128099448ed1SJohn Dyson zfree(obj_zone, backing_object); 1281df8bae1dSRodney W. Grimes 1282df8bae1dSRodney W. Grimes object_collapses++; 12830d94caffSDavid Greenman } else { 128495e5e988SJohn Dyson vm_object_t new_backing_object; 1285df8bae1dSRodney W. Grimes 1286df8bae1dSRodney W. Grimes /* 12872ad1a3f7SMatthew Dillon * If we do not entirely shadow the backing object, 12882ad1a3f7SMatthew Dillon * there is nothing we can do so we give up. 1289df8bae1dSRodney W. Grimes */ 1290df8bae1dSRodney W. Grimes 12912ad1a3f7SMatthew Dillon if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) { 12922ad1a3f7SMatthew Dillon break; 129324a1cce3SDavid Greenman } 1294df8bae1dSRodney W. Grimes 1295df8bae1dSRodney W. Grimes /* 12960d94caffSDavid Greenman * Make the parent shadow the next object in the 12970d94caffSDavid Greenman * chain. Deallocating backing_object will not remove 12980d94caffSDavid Greenman * it, since its reference count is at least 2. 1299df8bae1dSRodney W. Grimes */ 1300df8bae1dSRodney W. Grimes 13012ad1a3f7SMatthew Dillon TAILQ_REMOVE( 13022ad1a3f7SMatthew Dillon &backing_object->shadow_head, 13032ad1a3f7SMatthew Dillon object, 13042ad1a3f7SMatthew Dillon shadow_list 13052ad1a3f7SMatthew Dillon ); 1306eaf13dd7SJohn Dyson backing_object->shadow_count--; 1307eaf13dd7SJohn Dyson backing_object->generation++; 130895e5e988SJohn Dyson 130995e5e988SJohn Dyson new_backing_object = backing_object->backing_object; 13108aef1712SMatthew Dillon if ((object->backing_object = new_backing_object) != NULL) { 131195e5e988SJohn Dyson vm_object_reference(new_backing_object); 13122ad1a3f7SMatthew Dillon TAILQ_INSERT_TAIL( 13132ad1a3f7SMatthew Dillon &new_backing_object->shadow_head, 13142ad1a3f7SMatthew Dillon object, 13152ad1a3f7SMatthew Dillon shadow_list 13162ad1a3f7SMatthew Dillon ); 1317eaf13dd7SJohn Dyson new_backing_object->shadow_count++; 1318eaf13dd7SJohn Dyson new_backing_object->generation++; 131995e5e988SJohn Dyson object->backing_object_offset += 132095e5e988SJohn Dyson backing_object->backing_object_offset; 1321de5f6a77SJohn Dyson } 1322df8bae1dSRodney W. Grimes 1323df8bae1dSRodney W. Grimes /* 13240d94caffSDavid Greenman * Drop the reference count on backing_object. Since 13250d94caffSDavid Greenman * its ref_count was at least 2, it will not vanish; 1326eaf13dd7SJohn Dyson * so we don't need to call vm_object_deallocate, but 1327eaf13dd7SJohn Dyson * we do anyway. 1328df8bae1dSRodney W. Grimes */ 132995e5e988SJohn Dyson vm_object_deallocate(backing_object); 1330df8bae1dSRodney W. Grimes object_bypasses++; 1331df8bae1dSRodney W. Grimes } 1332df8bae1dSRodney W. Grimes 1333df8bae1dSRodney W. Grimes /* 1334df8bae1dSRodney W. Grimes * Try again with this object's new backing object. 1335df8bae1dSRodney W. Grimes */ 1336df8bae1dSRodney W. Grimes } 1337df8bae1dSRodney W. Grimes } 1338df8bae1dSRodney W. Grimes 1339df8bae1dSRodney W. Grimes /* 1340df8bae1dSRodney W. Grimes * vm_object_page_remove: [internal] 1341df8bae1dSRodney W. Grimes * 1342df8bae1dSRodney W. Grimes * Removes all physical pages in the specified 1343df8bae1dSRodney W. Grimes * object range from the object's list of pages. 1344df8bae1dSRodney W. Grimes * 1345df8bae1dSRodney W. Grimes * The object must be locked. 1346df8bae1dSRodney W. Grimes */ 134726f9a767SRodney W. Grimes void 13487c1f6cedSDavid Greenman vm_object_page_remove(object, start, end, clean_only) 1349d031cff1SMatthew Dillon vm_object_t object; 1350d031cff1SMatthew Dillon vm_pindex_t start; 1351d031cff1SMatthew Dillon vm_pindex_t end; 13527c1f6cedSDavid Greenman boolean_t clean_only; 1353df8bae1dSRodney W. Grimes { 1354d031cff1SMatthew Dillon vm_page_t p, next; 1355a316d390SJohn Dyson unsigned int size; 1356f5ef029eSPoul-Henning Kamp int all; 1357df8bae1dSRodney W. Grimes 1358a1a54e9fSAlan Cox if (object == NULL || 1359a1a54e9fSAlan Cox object->resident_page_count == 0) 1360df8bae1dSRodney W. Grimes return; 1361df8bae1dSRodney W. Grimes 136295e5e988SJohn Dyson all = ((end == 0) && (start == 0)); 136395e5e988SJohn Dyson 1364d474eaaaSDoug Rabson vm_object_pip_add(object, 1); 136526f9a767SRodney W. Grimes again: 136626f9a767SRodney W. Grimes size = end - start; 1367a1a54e9fSAlan Cox if (all || size > object->resident_page_count / 4) { 1368b18bfc3dSJohn Dyson for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) { 1369b18bfc3dSJohn Dyson next = TAILQ_NEXT(p, listq); 137095e5e988SJohn Dyson if (all || ((start <= p->pindex) && (p->pindex < end))) { 1371bd7e5f99SJohn Dyson if (p->wire_count != 0) { 1372bd7e5f99SJohn Dyson vm_page_protect(p, VM_PROT_NONE); 1373ce65e68cSDavid Greenman if (!clean_only) 1374bd7e5f99SJohn Dyson p->valid = 0; 13750d94caffSDavid Greenman continue; 13760d94caffSDavid Greenman } 13770891ef4cSJohn Dyson 1378b18bfc3dSJohn Dyson /* 1379b18bfc3dSJohn Dyson * The busy flags are only cleared at 1380b18bfc3dSJohn Dyson * interrupt -- minimize the spl transitions 1381b18bfc3dSJohn Dyson */ 1382ffc82b0aSJohn Dyson 13831c7c3c6aSMatthew Dillon if (vm_page_sleep_busy(p, TRUE, "vmopar")) 138426f9a767SRodney W. Grimes goto again; 13850891ef4cSJohn Dyson 13868f9110f6SJohn Dyson if (clean_only && p->valid) { 13877c1f6cedSDavid Greenman vm_page_test_dirty(p); 13887c1f6cedSDavid Greenman if (p->valid & p->dirty) 13897c1f6cedSDavid Greenman continue; 13907c1f6cedSDavid Greenman } 1391eaf13dd7SJohn Dyson 1392e69763a3SDoug Rabson vm_page_busy(p); 1393f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 1394df8bae1dSRodney W. Grimes vm_page_free(p); 139526f9a767SRodney W. Grimes } 139626f9a767SRodney W. Grimes } 139726f9a767SRodney W. Grimes } else { 139826f9a767SRodney W. Grimes while (size > 0) { 1399bd7e5f99SJohn Dyson if ((p = vm_page_lookup(object, start)) != 0) { 1400eaf13dd7SJohn Dyson 1401bd7e5f99SJohn Dyson if (p->wire_count != 0) { 1402bd7e5f99SJohn Dyson vm_page_protect(p, VM_PROT_NONE); 1403ce65e68cSDavid Greenman if (!clean_only) 1404ce65e68cSDavid Greenman p->valid = 0; 1405bd7e5f99SJohn Dyson start += 1; 1406bd7e5f99SJohn Dyson size -= 1; 1407bd7e5f99SJohn Dyson continue; 14080d94caffSDavid Greenman } 1409eaf13dd7SJohn Dyson 1410b18bfc3dSJohn Dyson /* 1411b18bfc3dSJohn Dyson * The busy flags are only cleared at 1412b18bfc3dSJohn Dyson * interrupt -- minimize the spl transitions 1413b18bfc3dSJohn Dyson */ 14141c7c3c6aSMatthew Dillon if (vm_page_sleep_busy(p, TRUE, "vmopar")) 141526f9a767SRodney W. Grimes goto again; 1416eaf13dd7SJohn Dyson 14178f9110f6SJohn Dyson if (clean_only && p->valid) { 14187c1f6cedSDavid Greenman vm_page_test_dirty(p); 1419bd7e5f99SJohn Dyson if (p->valid & p->dirty) { 1420bd7e5f99SJohn Dyson start += 1; 1421bd7e5f99SJohn Dyson size -= 1; 14227c1f6cedSDavid Greenman continue; 14237c1f6cedSDavid Greenman } 1424bd7e5f99SJohn Dyson } 1425eaf13dd7SJohn Dyson 1426e69763a3SDoug Rabson vm_page_busy(p); 1427f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 142826f9a767SRodney W. Grimes vm_page_free(p); 142926f9a767SRodney W. Grimes } 1430a316d390SJohn Dyson start += 1; 1431a316d390SJohn Dyson size -= 1; 1432df8bae1dSRodney W. Grimes } 1433df8bae1dSRodney W. Grimes } 1434f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 1435c0503609SDavid Greenman } 1436df8bae1dSRodney W. Grimes 1437df8bae1dSRodney W. Grimes /* 1438df8bae1dSRodney W. Grimes * Routine: vm_object_coalesce 1439df8bae1dSRodney W. Grimes * Function: Coalesces two objects backing up adjoining 1440df8bae1dSRodney W. Grimes * regions of memory into a single object. 1441df8bae1dSRodney W. Grimes * 1442df8bae1dSRodney W. Grimes * returns TRUE if objects were combined. 1443df8bae1dSRodney W. Grimes * 1444df8bae1dSRodney W. Grimes * NOTE: Only works at the moment if the second object is NULL - 1445df8bae1dSRodney W. Grimes * if it's not, which object do we lock first? 1446df8bae1dSRodney W. Grimes * 1447df8bae1dSRodney W. Grimes * Parameters: 1448df8bae1dSRodney W. Grimes * prev_object First object to coalesce 1449df8bae1dSRodney W. Grimes * prev_offset Offset into prev_object 1450df8bae1dSRodney W. Grimes * next_object Second object into coalesce 1451df8bae1dSRodney W. Grimes * next_offset Offset into next_object 1452df8bae1dSRodney W. Grimes * 1453df8bae1dSRodney W. Grimes * prev_size Size of reference to prev_object 1454df8bae1dSRodney W. Grimes * next_size Size of reference to next_object 1455df8bae1dSRodney W. Grimes * 1456df8bae1dSRodney W. Grimes * Conditions: 1457df8bae1dSRodney W. Grimes * The object must *not* be locked. 1458df8bae1dSRodney W. Grimes */ 14590d94caffSDavid Greenman boolean_t 1460a316d390SJohn Dyson vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size) 1461d031cff1SMatthew Dillon vm_object_t prev_object; 1462a316d390SJohn Dyson vm_pindex_t prev_pindex; 1463df8bae1dSRodney W. Grimes vm_size_t prev_size, next_size; 1464df8bae1dSRodney W. Grimes { 1465ea41812fSAlan Cox vm_pindex_t next_pindex; 1466df8bae1dSRodney W. Grimes 1467df8bae1dSRodney W. Grimes if (prev_object == NULL) { 1468df8bae1dSRodney W. Grimes return (TRUE); 1469df8bae1dSRodney W. Grimes } 1470df8bae1dSRodney W. Grimes 14714112823fSMatthew Dillon if (prev_object->type != OBJT_DEFAULT && 14724112823fSMatthew Dillon prev_object->type != OBJT_SWAP) { 147330dcfc09SJohn Dyson return (FALSE); 147430dcfc09SJohn Dyson } 147530dcfc09SJohn Dyson 1476df8bae1dSRodney W. Grimes /* 1477df8bae1dSRodney W. Grimes * Try to collapse the object first 1478df8bae1dSRodney W. Grimes */ 1479df8bae1dSRodney W. Grimes vm_object_collapse(prev_object); 1480df8bae1dSRodney W. Grimes 1481df8bae1dSRodney W. Grimes /* 14820d94caffSDavid Greenman * Can't coalesce if: . more than one reference . paged out . shadows 14830d94caffSDavid Greenman * another object . has a copy elsewhere (any of which mean that the 14840d94caffSDavid Greenman * pages not mapped to prev_entry may be in use anyway) 1485df8bae1dSRodney W. Grimes */ 1486df8bae1dSRodney W. Grimes 14878cc7e047SJohn Dyson if (prev_object->backing_object != NULL) { 1488df8bae1dSRodney W. Grimes return (FALSE); 1489df8bae1dSRodney W. Grimes } 1490a316d390SJohn Dyson 1491a316d390SJohn Dyson prev_size >>= PAGE_SHIFT; 1492a316d390SJohn Dyson next_size >>= PAGE_SHIFT; 1493ea41812fSAlan Cox next_pindex = prev_pindex + prev_size; 14948cc7e047SJohn Dyson 14958cc7e047SJohn Dyson if ((prev_object->ref_count > 1) && 1496ea41812fSAlan Cox (prev_object->size != next_pindex)) { 14978cc7e047SJohn Dyson return (FALSE); 14988cc7e047SJohn Dyson } 14998cc7e047SJohn Dyson 1500df8bae1dSRodney W. Grimes /* 15010d94caffSDavid Greenman * Remove any pages that may still be in the object from a previous 15020d94caffSDavid Greenman * deallocation. 1503df8bae1dSRodney W. Grimes */ 1504ea41812fSAlan Cox if (next_pindex < prev_object->size) { 1505df8bae1dSRodney W. Grimes vm_object_page_remove(prev_object, 1506ea41812fSAlan Cox next_pindex, 1507ea41812fSAlan Cox next_pindex + next_size, FALSE); 1508ea41812fSAlan Cox if (prev_object->type == OBJT_SWAP) 1509ea41812fSAlan Cox swap_pager_freespace(prev_object, 1510ea41812fSAlan Cox next_pindex, next_size); 1511ea41812fSAlan Cox } 1512df8bae1dSRodney W. Grimes 1513df8bae1dSRodney W. Grimes /* 1514df8bae1dSRodney W. Grimes * Extend the object if necessary. 1515df8bae1dSRodney W. Grimes */ 1516ea41812fSAlan Cox if (next_pindex + next_size > prev_object->size) 1517ea41812fSAlan Cox prev_object->size = next_pindex + next_size; 1518df8bae1dSRodney W. Grimes 1519df8bae1dSRodney W. Grimes return (TRUE); 1520df8bae1dSRodney W. Grimes } 1521df8bae1dSRodney W. Grimes 1522c7c34a24SBruce Evans #include "opt_ddb.h" 1523c3cb3e12SDavid Greenman #ifdef DDB 1524c7c34a24SBruce Evans #include <sys/kernel.h> 1525c7c34a24SBruce Evans 1526c7c34a24SBruce Evans #include <machine/cons.h> 1527c7c34a24SBruce Evans 1528c7c34a24SBruce Evans #include <ddb/ddb.h> 1529c7c34a24SBruce Evans 1530c7c34a24SBruce Evans static int _vm_object_in_map __P((vm_map_t map, vm_object_t object, 1531c7c34a24SBruce Evans vm_map_entry_t entry)); 1532c7c34a24SBruce Evans static int vm_object_in_map __P((vm_object_t object)); 1533c3cb3e12SDavid Greenman 1534cac597e4SBruce Evans static int 1535a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry) 1536a1f6d91cSDavid Greenman vm_map_t map; 1537a1f6d91cSDavid Greenman vm_object_t object; 1538a1f6d91cSDavid Greenman vm_map_entry_t entry; 1539a1f6d91cSDavid Greenman { 1540a1f6d91cSDavid Greenman vm_map_t tmpm; 1541a1f6d91cSDavid Greenman vm_map_entry_t tmpe; 1542a1f6d91cSDavid Greenman vm_object_t obj; 1543a1f6d91cSDavid Greenman int entcount; 1544a1f6d91cSDavid Greenman 1545a1f6d91cSDavid Greenman if (map == 0) 1546a1f6d91cSDavid Greenman return 0; 1547a1f6d91cSDavid Greenman 1548a1f6d91cSDavid Greenman if (entry == 0) { 1549a1f6d91cSDavid Greenman tmpe = map->header.next; 1550a1f6d91cSDavid Greenman entcount = map->nentries; 1551a1f6d91cSDavid Greenman while (entcount-- && (tmpe != &map->header)) { 1552a1f6d91cSDavid Greenman if( _vm_object_in_map(map, object, tmpe)) { 1553a1f6d91cSDavid Greenman return 1; 1554a1f6d91cSDavid Greenman } 1555a1f6d91cSDavid Greenman tmpe = tmpe->next; 1556a1f6d91cSDavid Greenman } 15579fdfe602SMatthew Dillon } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 15589fdfe602SMatthew Dillon tmpm = entry->object.sub_map; 1559a1f6d91cSDavid Greenman tmpe = tmpm->header.next; 1560a1f6d91cSDavid Greenman entcount = tmpm->nentries; 1561a1f6d91cSDavid Greenman while (entcount-- && tmpe != &tmpm->header) { 1562a1f6d91cSDavid Greenman if( _vm_object_in_map(tmpm, object, tmpe)) { 1563a1f6d91cSDavid Greenman return 1; 1564a1f6d91cSDavid Greenman } 1565a1f6d91cSDavid Greenman tmpe = tmpe->next; 1566a1f6d91cSDavid Greenman } 15678aef1712SMatthew Dillon } else if ((obj = entry->object.vm_object) != NULL) { 156824a1cce3SDavid Greenman for(; obj; obj=obj->backing_object) 1569a1f6d91cSDavid Greenman if( obj == object) { 1570a1f6d91cSDavid Greenman return 1; 1571a1f6d91cSDavid Greenman } 1572a1f6d91cSDavid Greenman } 1573a1f6d91cSDavid Greenman return 0; 1574a1f6d91cSDavid Greenman } 1575a1f6d91cSDavid Greenman 1576cac597e4SBruce Evans static int 1577a1f6d91cSDavid Greenman vm_object_in_map( object) 1578a1f6d91cSDavid Greenman vm_object_t object; 1579a1f6d91cSDavid Greenman { 1580a1f6d91cSDavid Greenman struct proc *p; 15811b67ec6dSJeffrey Hsu for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 1582a1f6d91cSDavid Greenman if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 1583a1f6d91cSDavid Greenman continue; 1584a1f6d91cSDavid Greenman if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) 1585a1f6d91cSDavid Greenman return 1; 1586a1f6d91cSDavid Greenman } 1587a1f6d91cSDavid Greenman if( _vm_object_in_map( kernel_map, object, 0)) 1588a1f6d91cSDavid Greenman return 1; 1589a1f6d91cSDavid Greenman if( _vm_object_in_map( kmem_map, object, 0)) 1590a1f6d91cSDavid Greenman return 1; 1591a1f6d91cSDavid Greenman if( _vm_object_in_map( pager_map, object, 0)) 1592a1f6d91cSDavid Greenman return 1; 1593a1f6d91cSDavid Greenman if( _vm_object_in_map( buffer_map, object, 0)) 1594a1f6d91cSDavid Greenman return 1; 1595a1f6d91cSDavid Greenman if( _vm_object_in_map( io_map, object, 0)) 1596a1f6d91cSDavid Greenman return 1; 1597a1f6d91cSDavid Greenman if( _vm_object_in_map( phys_map, object, 0)) 1598a1f6d91cSDavid Greenman return 1; 1599a1f6d91cSDavid Greenman if( _vm_object_in_map( mb_map, object, 0)) 1600a1f6d91cSDavid Greenman return 1; 1601a1f6d91cSDavid Greenman if( _vm_object_in_map( u_map, object, 0)) 1602a1f6d91cSDavid Greenman return 1; 1603a1f6d91cSDavid Greenman return 0; 1604a1f6d91cSDavid Greenman } 1605a1f6d91cSDavid Greenman 1606c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check) 1607f708ef1bSPoul-Henning Kamp { 1608a1f6d91cSDavid Greenman vm_object_t object; 1609a1f6d91cSDavid Greenman 1610a1f6d91cSDavid Greenman /* 1611a1f6d91cSDavid Greenman * make sure that internal objs are in a map somewhere 1612a1f6d91cSDavid Greenman * and none have zero ref counts. 1613a1f6d91cSDavid Greenman */ 1614b18bfc3dSJohn Dyson for (object = TAILQ_FIRST(&vm_object_list); 1615a1f6d91cSDavid Greenman object != NULL; 1616b18bfc3dSJohn Dyson object = TAILQ_NEXT(object, object_list)) { 161724a1cce3SDavid Greenman if (object->handle == NULL && 161824a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 1619a1f6d91cSDavid Greenman if (object->ref_count == 0) { 16203efc015bSPeter Wemm db_printf("vmochk: internal obj has zero ref count: %ld\n", 16213efc015bSPeter Wemm (long)object->size); 1622a1f6d91cSDavid Greenman } 1623a1f6d91cSDavid Greenman if (!vm_object_in_map(object)) { 1624fc62ef1fSBruce Evans db_printf( 1625fc62ef1fSBruce Evans "vmochk: internal obj is not in a map: " 1626fc62ef1fSBruce Evans "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 1627fc62ef1fSBruce Evans object->ref_count, (u_long)object->size, 1628fc62ef1fSBruce Evans (u_long)object->size, 1629fc62ef1fSBruce Evans (void *)object->backing_object); 1630a1f6d91cSDavid Greenman } 1631a1f6d91cSDavid Greenman } 1632a1f6d91cSDavid Greenman } 1633a1f6d91cSDavid Greenman } 1634a1f6d91cSDavid Greenman 163526f9a767SRodney W. Grimes /* 1636df8bae1dSRodney W. Grimes * vm_object_print: [ debug ] 1637df8bae1dSRodney W. Grimes */ 1638c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static) 1639df8bae1dSRodney W. Grimes { 1640c7c34a24SBruce Evans /* XXX convert args. */ 1641c7c34a24SBruce Evans vm_object_t object = (vm_object_t)addr; 1642c7c34a24SBruce Evans boolean_t full = have_addr; 1643c7c34a24SBruce Evans 1644d031cff1SMatthew Dillon vm_page_t p; 1645df8bae1dSRodney W. Grimes 1646c7c34a24SBruce Evans /* XXX count is an (unused) arg. Avoid shadowing it. */ 1647c7c34a24SBruce Evans #define count was_count 1648c7c34a24SBruce Evans 1649d031cff1SMatthew Dillon int count; 1650df8bae1dSRodney W. Grimes 1651df8bae1dSRodney W. Grimes if (object == NULL) 1652df8bae1dSRodney W. Grimes return; 1653df8bae1dSRodney W. Grimes 1654eb95adefSBruce Evans db_iprintf( 1655eb95adefSBruce Evans "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n", 1656eb95adefSBruce Evans object, (int)object->type, (u_long)object->size, 1657eb95adefSBruce Evans object->resident_page_count, object->ref_count, object->flags); 1658eb95adefSBruce Evans /* 16591c7c3c6aSMatthew Dillon * XXX no %qd in kernel. Truncate object->backing_object_offset. 1660eb95adefSBruce Evans */ 16611c7c3c6aSMatthew Dillon db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n", 16621c7c3c6aSMatthew Dillon object->shadow_count, 1663eb95adefSBruce Evans object->backing_object ? object->backing_object->ref_count : 0, 1664eb95adefSBruce Evans object->backing_object, (long)object->backing_object_offset); 1665df8bae1dSRodney W. Grimes 1666df8bae1dSRodney W. Grimes if (!full) 1667df8bae1dSRodney W. Grimes return; 1668df8bae1dSRodney W. Grimes 1669c7c34a24SBruce Evans db_indent += 2; 1670df8bae1dSRodney W. Grimes count = 0; 1671b18bfc3dSJohn Dyson for (p = TAILQ_FIRST(&object->memq); p != NULL; p = TAILQ_NEXT(p, listq)) { 1672df8bae1dSRodney W. Grimes if (count == 0) 1673c7c34a24SBruce Evans db_iprintf("memory:="); 1674df8bae1dSRodney W. Grimes else if (count == 6) { 1675c7c34a24SBruce Evans db_printf("\n"); 1676c7c34a24SBruce Evans db_iprintf(" ..."); 1677df8bae1dSRodney W. Grimes count = 0; 1678df8bae1dSRodney W. Grimes } else 1679c7c34a24SBruce Evans db_printf(","); 1680df8bae1dSRodney W. Grimes count++; 1681df8bae1dSRodney W. Grimes 1682c7c34a24SBruce Evans db_printf("(off=0x%lx,page=0x%lx)", 1683a316d390SJohn Dyson (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p)); 1684df8bae1dSRodney W. Grimes } 1685df8bae1dSRodney W. Grimes if (count != 0) 1686c7c34a24SBruce Evans db_printf("\n"); 1687c7c34a24SBruce Evans db_indent -= 2; 1688df8bae1dSRodney W. Grimes } 16895070c7f8SJohn Dyson 1690c7c34a24SBruce Evans /* XXX. */ 1691c7c34a24SBruce Evans #undef count 1692c7c34a24SBruce Evans 1693c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */ 16945070c7f8SJohn Dyson void 1695c7c34a24SBruce Evans vm_object_print(addr, have_addr, count, modif) 1696ecbb00a2SDoug Rabson /* db_expr_t */ long addr; 1697c7c34a24SBruce Evans boolean_t have_addr; 1698ecbb00a2SDoug Rabson /* db_expr_t */ long count; 1699c7c34a24SBruce Evans char *modif; 1700c7c34a24SBruce Evans { 1701c7c34a24SBruce Evans vm_object_print_static(addr, have_addr, count, modif); 1702c7c34a24SBruce Evans } 1703c7c34a24SBruce Evans 1704c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages) 17055070c7f8SJohn Dyson { 17065070c7f8SJohn Dyson vm_object_t object; 17075070c7f8SJohn Dyson int nl = 0; 17085070c7f8SJohn Dyson int c; 17095070c7f8SJohn Dyson for (object = TAILQ_FIRST(&vm_object_list); 17105070c7f8SJohn Dyson object != NULL; 17115070c7f8SJohn Dyson object = TAILQ_NEXT(object, object_list)) { 17125070c7f8SJohn Dyson vm_pindex_t idx, fidx; 17135070c7f8SJohn Dyson vm_pindex_t osize; 17145070c7f8SJohn Dyson vm_offset_t pa = -1, padiff; 17155070c7f8SJohn Dyson int rcount; 17165070c7f8SJohn Dyson vm_page_t m; 17175070c7f8SJohn Dyson 1718fc62ef1fSBruce Evans db_printf("new object: %p\n", (void *)object); 17195070c7f8SJohn Dyson if ( nl > 18) { 17205070c7f8SJohn Dyson c = cngetc(); 17215070c7f8SJohn Dyson if (c != ' ') 17225070c7f8SJohn Dyson return; 17235070c7f8SJohn Dyson nl = 0; 17245070c7f8SJohn Dyson } 17255070c7f8SJohn Dyson nl++; 17265070c7f8SJohn Dyson rcount = 0; 17275070c7f8SJohn Dyson fidx = 0; 17285070c7f8SJohn Dyson osize = object->size; 17295070c7f8SJohn Dyson if (osize > 128) 17305070c7f8SJohn Dyson osize = 128; 17315070c7f8SJohn Dyson for(idx=0;idx<osize;idx++) { 17325070c7f8SJohn Dyson m = vm_page_lookup(object, idx); 17335070c7f8SJohn Dyson if (m == NULL) { 17345070c7f8SJohn Dyson if (rcount) { 17353efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 17363efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 17375070c7f8SJohn Dyson if ( nl > 18) { 17385070c7f8SJohn Dyson c = cngetc(); 17395070c7f8SJohn Dyson if (c != ' ') 17405070c7f8SJohn Dyson return; 17415070c7f8SJohn Dyson nl = 0; 17425070c7f8SJohn Dyson } 17435070c7f8SJohn Dyson nl++; 17445070c7f8SJohn Dyson rcount = 0; 17455070c7f8SJohn Dyson } 17465070c7f8SJohn Dyson continue; 17475070c7f8SJohn Dyson } 17485070c7f8SJohn Dyson 17495070c7f8SJohn Dyson 17505070c7f8SJohn Dyson if (rcount && 17515070c7f8SJohn Dyson (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) { 17525070c7f8SJohn Dyson ++rcount; 17535070c7f8SJohn Dyson continue; 17545070c7f8SJohn Dyson } 17555070c7f8SJohn Dyson if (rcount) { 17565070c7f8SJohn Dyson padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m); 17575070c7f8SJohn Dyson padiff >>= PAGE_SHIFT; 17585070c7f8SJohn Dyson padiff &= PQ_L2_MASK; 17595070c7f8SJohn Dyson if (padiff == 0) { 17605070c7f8SJohn Dyson pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE; 17615070c7f8SJohn Dyson ++rcount; 17625070c7f8SJohn Dyson continue; 17635070c7f8SJohn Dyson } 17643efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)", 17653efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 17663efc015bSPeter Wemm db_printf("pd(%ld)\n", (long)padiff); 17675070c7f8SJohn Dyson if ( nl > 18) { 17685070c7f8SJohn Dyson c = cngetc(); 17695070c7f8SJohn Dyson if (c != ' ') 17705070c7f8SJohn Dyson return; 17715070c7f8SJohn Dyson nl = 0; 17725070c7f8SJohn Dyson } 17735070c7f8SJohn Dyson nl++; 17745070c7f8SJohn Dyson } 17755070c7f8SJohn Dyson fidx = idx; 17765070c7f8SJohn Dyson pa = VM_PAGE_TO_PHYS(m); 17775070c7f8SJohn Dyson rcount = 1; 17785070c7f8SJohn Dyson } 17795070c7f8SJohn Dyson if (rcount) { 17803efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 17813efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 17825070c7f8SJohn Dyson if ( nl > 18) { 17835070c7f8SJohn Dyson c = cngetc(); 17845070c7f8SJohn Dyson if (c != ' ') 17855070c7f8SJohn Dyson return; 17865070c7f8SJohn Dyson nl = 0; 17875070c7f8SJohn Dyson } 17885070c7f8SJohn Dyson nl++; 17895070c7f8SJohn Dyson } 17905070c7f8SJohn Dyson } 17915070c7f8SJohn Dyson } 1792c3cb3e12SDavid Greenman #endif /* DDB */ 1793