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 * 6428f8db14SBruce Evans * $Id: vm_object.c,v 1.50 1995/07/16 13:28:36 davidg 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/kernel.h> 74f23b4c91SGarrett Wollman #include <sys/proc.h> /* for curproc, pageproc */ 75df8bae1dSRodney W. Grimes #include <sys/malloc.h> 760d94caffSDavid Greenman #include <sys/vnode.h> 770d94caffSDavid Greenman #include <sys/mount.h> 78df8bae1dSRodney W. Grimes 79df8bae1dSRodney W. Grimes #include <vm/vm.h> 80df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 8126f9a767SRodney W. Grimes #include <vm/vm_pageout.h> 820d94caffSDavid Greenman #include <vm/vm_pager.h> 8305f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 84a1f6d91cSDavid Greenman #include <vm/vm_kern.h> 8526f9a767SRodney W. Grimes 8624a1cce3SDavid Greenman static void _vm_object_allocate(objtype_t, vm_size_t, vm_object_t); 87df8bae1dSRodney W. Grimes 88f6b04d2bSDavid Greenman 89df8bae1dSRodney W. Grimes /* 90df8bae1dSRodney W. Grimes * Virtual memory objects maintain the actual data 91df8bae1dSRodney W. Grimes * associated with allocated virtual memory. A given 92df8bae1dSRodney W. Grimes * page of memory exists within exactly one object. 93df8bae1dSRodney W. Grimes * 94df8bae1dSRodney W. Grimes * An object is only deallocated when all "references" 95df8bae1dSRodney W. Grimes * are given up. Only one "reference" to a given 96df8bae1dSRodney W. Grimes * region of an object should be writeable. 97df8bae1dSRodney W. Grimes * 98df8bae1dSRodney W. Grimes * Associated with each object is a list of all resident 99df8bae1dSRodney W. Grimes * memory pages belonging to that object; this list is 100df8bae1dSRodney W. Grimes * maintained by the "vm_page" module, and locked by the object's 101df8bae1dSRodney W. Grimes * lock. 102df8bae1dSRodney W. Grimes * 103df8bae1dSRodney W. Grimes * Each object also records a "pager" routine which is 104df8bae1dSRodney W. Grimes * used to retrieve (and store) pages to the proper backing 105df8bae1dSRodney W. Grimes * storage. In addition, objects may be backed by other 106df8bae1dSRodney W. Grimes * objects from which they were virtual-copied. 107df8bae1dSRodney W. Grimes * 108df8bae1dSRodney W. Grimes * The only items within the object structure which are 109df8bae1dSRodney W. Grimes * modified after time of creation are: 110df8bae1dSRodney W. Grimes * reference count locked by object's lock 111df8bae1dSRodney W. Grimes * pager routine locked by object's lock 112df8bae1dSRodney W. Grimes * 113df8bae1dSRodney W. Grimes */ 114df8bae1dSRodney W. Grimes 11528f8db14SBruce Evans int vm_object_cache_max; 11628f8db14SBruce Evans struct object_q vm_object_cached_list; 11728f8db14SBruce Evans int vm_object_cached; 11828f8db14SBruce Evans struct object_q vm_object_list; 11928f8db14SBruce Evans long vm_object_count; 12028f8db14SBruce Evans vm_object_t kernel_object; 12128f8db14SBruce Evans vm_object_t kmem_object; 122df8bae1dSRodney W. Grimes struct vm_object kernel_object_store; 123df8bae1dSRodney W. Grimes struct vm_object kmem_object_store; 124df8bae1dSRodney W. Grimes 125c3cb3e12SDavid Greenman long object_collapses; 126c3cb3e12SDavid Greenman long object_bypasses; 127df8bae1dSRodney W. Grimes 128df8bae1dSRodney W. Grimes static void 12924a1cce3SDavid Greenman _vm_object_allocate(type, size, object) 13024a1cce3SDavid Greenman objtype_t type; 131df8bae1dSRodney W. Grimes vm_size_t size; 132df8bae1dSRodney W. Grimes register vm_object_t object; 133df8bae1dSRodney W. Grimes { 134df8bae1dSRodney W. Grimes TAILQ_INIT(&object->memq); 13524a1cce3SDavid Greenman TAILQ_INIT(&object->shadow_head); 136a1f6d91cSDavid Greenman 13724a1cce3SDavid Greenman object->type = type; 138df8bae1dSRodney W. Grimes object->size = size; 139a1f6d91cSDavid Greenman object->ref_count = 1; 14024a1cce3SDavid Greenman object->flags = 0; 141df8bae1dSRodney W. Grimes object->paging_in_progress = 0; 142a1f6d91cSDavid Greenman object->resident_page_count = 0; 14324a1cce3SDavid Greenman object->handle = NULL; 144df8bae1dSRodney W. Grimes object->paging_offset = 0; 14524a1cce3SDavid Greenman object->backing_object = NULL; 14624a1cce3SDavid Greenman object->backing_object_offset = (vm_offset_t) 0; 147a1f6d91cSDavid Greenman 148a1f6d91cSDavid Greenman object->last_read = 0; 149df8bae1dSRodney W. Grimes 150df8bae1dSRodney W. Grimes TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 151df8bae1dSRodney W. Grimes vm_object_count++; 152df8bae1dSRodney W. Grimes } 153df8bae1dSRodney W. Grimes 154df8bae1dSRodney W. Grimes /* 15526f9a767SRodney W. Grimes * vm_object_init: 15626f9a767SRodney W. Grimes * 15726f9a767SRodney W. Grimes * Initialize the VM objects module. 15826f9a767SRodney W. Grimes */ 15926f9a767SRodney W. Grimes void 16026f9a767SRodney W. Grimes vm_object_init(vm_offset_t nothing) 16126f9a767SRodney W. Grimes { 16226f9a767SRodney W. Grimes register int i; 16326f9a767SRodney W. Grimes 16426f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_cached_list); 16526f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_list); 16626f9a767SRodney W. Grimes vm_object_count = 0; 1670217125fSDavid Greenman 1680217125fSDavid Greenman vm_object_cache_max = 84; 1690217125fSDavid Greenman if (cnt.v_page_count > 1000) 1700217125fSDavid Greenman vm_object_cache_max += (cnt.v_page_count - 1000) / 4; 17126f9a767SRodney W. Grimes 17226f9a767SRodney W. Grimes kernel_object = &kernel_object_store; 17324a1cce3SDavid Greenman _vm_object_allocate(OBJT_DEFAULT, VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS, 17426f9a767SRodney W. Grimes kernel_object); 17526f9a767SRodney W. Grimes 17626f9a767SRodney W. Grimes kmem_object = &kmem_object_store; 17724a1cce3SDavid Greenman _vm_object_allocate(OBJT_DEFAULT, VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS, 17826f9a767SRodney W. Grimes kmem_object); 17926f9a767SRodney W. Grimes } 18026f9a767SRodney W. Grimes 18126f9a767SRodney W. Grimes /* 18226f9a767SRodney W. Grimes * vm_object_allocate: 18326f9a767SRodney W. Grimes * 18426f9a767SRodney W. Grimes * Returns a new object with the given size. 18526f9a767SRodney W. Grimes */ 18626f9a767SRodney W. Grimes 18726f9a767SRodney W. Grimes vm_object_t 18824a1cce3SDavid Greenman vm_object_allocate(type, size) 18924a1cce3SDavid Greenman objtype_t type; 19026f9a767SRodney W. Grimes vm_size_t size; 19126f9a767SRodney W. Grimes { 19226f9a767SRodney W. Grimes register vm_object_t result; 19326f9a767SRodney W. Grimes 19426f9a767SRodney W. Grimes result = (vm_object_t) 19526f9a767SRodney W. Grimes malloc((u_long) sizeof *result, M_VMOBJ, M_WAITOK); 19626f9a767SRodney W. Grimes 19726f9a767SRodney W. Grimes 19824a1cce3SDavid Greenman _vm_object_allocate(type, size, result); 19926f9a767SRodney W. Grimes 20026f9a767SRodney W. Grimes return (result); 20126f9a767SRodney W. Grimes } 20226f9a767SRodney W. Grimes 20326f9a767SRodney W. Grimes 20426f9a767SRodney W. Grimes /* 205df8bae1dSRodney W. Grimes * vm_object_reference: 206df8bae1dSRodney W. Grimes * 207df8bae1dSRodney W. Grimes * Gets another reference to the given object. 208df8bae1dSRodney W. Grimes */ 20926f9a767SRodney W. Grimes inline void 21026f9a767SRodney W. Grimes vm_object_reference(object) 211df8bae1dSRodney W. Grimes register vm_object_t object; 212df8bae1dSRodney W. Grimes { 213df8bae1dSRodney W. Grimes if (object == NULL) 214df8bae1dSRodney W. Grimes return; 215df8bae1dSRodney W. Grimes 21624a1cce3SDavid Greenman if (object->ref_count == 0) { 21724a1cce3SDavid Greenman if ((object->flags & OBJ_CANPERSIST) == 0) 21824a1cce3SDavid Greenman panic("vm_object_reference: non-persistent object with 0 ref_count"); 21924a1cce3SDavid Greenman TAILQ_REMOVE(&vm_object_cached_list, object, cached_list); 22024a1cce3SDavid Greenman vm_object_cached--; 22124a1cce3SDavid Greenman } 222df8bae1dSRodney W. Grimes object->ref_count++; 223df8bae1dSRodney W. Grimes } 224df8bae1dSRodney W. Grimes 225df8bae1dSRodney W. Grimes /* 226df8bae1dSRodney W. Grimes * vm_object_deallocate: 227df8bae1dSRodney W. Grimes * 228df8bae1dSRodney W. Grimes * Release a reference to the specified object, 229df8bae1dSRodney W. Grimes * gained either through a vm_object_allocate 230df8bae1dSRodney W. Grimes * or a vm_object_reference call. When all references 231df8bae1dSRodney W. Grimes * are gone, storage associated with this object 232df8bae1dSRodney W. Grimes * may be relinquished. 233df8bae1dSRodney W. Grimes * 234df8bae1dSRodney W. Grimes * No object may be locked. 235df8bae1dSRodney W. Grimes */ 23626f9a767SRodney W. Grimes void 23726f9a767SRodney W. Grimes vm_object_deallocate(object) 23826f9a767SRodney W. Grimes vm_object_t object; 239df8bae1dSRodney W. Grimes { 240df8bae1dSRodney W. Grimes vm_object_t temp; 241df8bae1dSRodney W. Grimes 242df8bae1dSRodney W. Grimes while (object != NULL) { 243df8bae1dSRodney W. Grimes 244ba8da839SDavid Greenman if (object->ref_count == 0) 245ba8da839SDavid Greenman panic("vm_object_deallocate: object deallocated too many times"); 246ba8da839SDavid Greenman 247df8bae1dSRodney W. Grimes /* 248df8bae1dSRodney W. Grimes * Lose the reference 249df8bae1dSRodney W. Grimes */ 250be6d5bfaSDavid Greenman object->ref_count--; 251be6d5bfaSDavid Greenman 252be6d5bfaSDavid Greenman if (object->ref_count != 0) { 253be6d5bfaSDavid Greenman if ((object->ref_count == 1) && 25424a1cce3SDavid Greenman (object->handle == NULL) && 25524a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || 25624a1cce3SDavid Greenman object->type == OBJT_SWAP)) { 257a1f6d91cSDavid Greenman vm_object_t robject; 25824a1cce3SDavid Greenman robject = object->shadow_head.tqh_first; 259be6d5bfaSDavid Greenman if ((robject != NULL) && 26024a1cce3SDavid Greenman (robject->handle == NULL) && 26124a1cce3SDavid Greenman (robject->type == OBJT_DEFAULT || 26224a1cce3SDavid Greenman robject->type == OBJT_SWAP)) { 263a1f6d91cSDavid Greenman int s; 264a1f6d91cSDavid Greenman robject->ref_count += 2; 2659b4814bbSDavid Greenman object->ref_count += 2; 266a1f6d91cSDavid Greenman 267a1f6d91cSDavid Greenman do { 268a1f6d91cSDavid Greenman s = splhigh(); 269a1f6d91cSDavid Greenman while (robject->paging_in_progress) { 270c0503609SDavid Greenman robject->flags |= OBJ_PIPWNT; 271a1f6d91cSDavid Greenman tsleep(robject, PVM, "objde1", 0); 2720d94caffSDavid Greenman } 273a1f6d91cSDavid Greenman 274a1f6d91cSDavid Greenman while (object->paging_in_progress) { 275c0503609SDavid Greenman object->flags |= OBJ_PIPWNT; 276a1f6d91cSDavid Greenman tsleep(object, PVM, "objde2", 0); 277a1f6d91cSDavid Greenman } 278a1f6d91cSDavid Greenman splx(s); 279a1f6d91cSDavid Greenman 280a1f6d91cSDavid Greenman } while( object->paging_in_progress || robject->paging_in_progress); 281a1f6d91cSDavid Greenman 2829b4814bbSDavid Greenman object->ref_count -= 2; 283a1f6d91cSDavid Greenman robject->ref_count -= 2; 284a1f6d91cSDavid Greenman if( robject->ref_count == 0) { 285a1f6d91cSDavid Greenman robject->ref_count += 1; 286ba8da839SDavid Greenman object = robject; 287ba8da839SDavid Greenman continue; 288a1f6d91cSDavid Greenman } 289a1f6d91cSDavid Greenman vm_object_collapse(robject); 290a1f6d91cSDavid Greenman return; 291010cf3b9SDavid Greenman } 292010cf3b9SDavid Greenman } 293df8bae1dSRodney W. Grimes /* 2940d94caffSDavid Greenman * If there are still references, then we are done. 295df8bae1dSRodney W. Grimes */ 296df8bae1dSRodney W. Grimes return; 297df8bae1dSRodney W. Grimes } 298df8bae1dSRodney W. Grimes 29924a1cce3SDavid Greenman if (object->type == OBJT_VNODE) { 30024a1cce3SDavid Greenman struct vnode *vp = object->handle; 301f5cf85d4SDavid Greenman 30224a1cce3SDavid Greenman vp->v_flag &= ~VTEXT; 303f5cf85d4SDavid Greenman } 304f5cf85d4SDavid Greenman 305f919ebdeSDavid Greenman /* 306f919ebdeSDavid Greenman * See if this object can persist and has some resident 307f919ebdeSDavid Greenman * pages. If so, enter it in the cache. 308f919ebdeSDavid Greenman */ 309f5cf85d4SDavid Greenman if (object->flags & OBJ_CANPERSIST) { 310f5cf85d4SDavid Greenman if (object->resident_page_count != 0) { 31124a1cce3SDavid Greenman vm_object_page_clean(object, 0, 0 ,TRUE, TRUE); 312df8bae1dSRodney W. Grimes TAILQ_INSERT_TAIL(&vm_object_cached_list, object, 313df8bae1dSRodney W. Grimes cached_list); 314df8bae1dSRodney W. Grimes vm_object_cached++; 315df8bae1dSRodney W. Grimes 316df8bae1dSRodney W. Grimes vm_object_cache_trim(); 317df8bae1dSRodney W. Grimes return; 318f5cf85d4SDavid Greenman } else { 319f5cf85d4SDavid Greenman object->flags &= ~OBJ_CANPERSIST; 320f5cf85d4SDavid Greenman } 321df8bae1dSRodney W. Grimes } 322f919ebdeSDavid Greenman 323df8bae1dSRodney W. Grimes /* 32424a1cce3SDavid Greenman * Make sure no one uses us. 325df8bae1dSRodney W. Grimes */ 3260d94caffSDavid Greenman object->flags |= OBJ_DEAD; 327df8bae1dSRodney W. Grimes 32824a1cce3SDavid Greenman temp = object->backing_object; 3292fe6e4d7SDavid Greenman if (temp) 33024a1cce3SDavid Greenman TAILQ_REMOVE(&temp->shadow_head, object, shadow_list); 331df8bae1dSRodney W. Grimes vm_object_terminate(object); 332df8bae1dSRodney W. Grimes /* unlocks and deallocates object */ 333df8bae1dSRodney W. Grimes object = temp; 334df8bae1dSRodney W. Grimes } 335df8bae1dSRodney W. Grimes } 336df8bae1dSRodney W. Grimes 337df8bae1dSRodney W. Grimes /* 338df8bae1dSRodney W. Grimes * vm_object_terminate actually destroys the specified object, freeing 339df8bae1dSRodney W. Grimes * up all previously used resources. 340df8bae1dSRodney W. Grimes * 341df8bae1dSRodney W. Grimes * The object must be locked. 342df8bae1dSRodney W. Grimes */ 34326f9a767SRodney W. Grimes void 34426f9a767SRodney W. Grimes vm_object_terminate(object) 345df8bae1dSRodney W. Grimes register vm_object_t object; 346df8bae1dSRodney W. Grimes { 3470d94caffSDavid Greenman register vm_page_t p, next; 34824a1cce3SDavid Greenman vm_object_t backing_object; 34926f9a767SRodney W. Grimes int s; 350df8bae1dSRodney W. Grimes 351df8bae1dSRodney W. Grimes /* 352f6b04d2bSDavid Greenman * wait for the pageout daemon to be done with the object 353df8bae1dSRodney W. Grimes */ 3540d94caffSDavid Greenman s = splhigh(); 355df8bae1dSRodney W. Grimes while (object->paging_in_progress) { 356c0503609SDavid Greenman object->flags |= OBJ_PIPWNT; 35724a1cce3SDavid Greenman tsleep(object, PVM, "objtrm", 0); 358df8bae1dSRodney W. Grimes } 3590d94caffSDavid Greenman splx(s); 360df8bae1dSRodney W. Grimes 36126f9a767SRodney W. Grimes if (object->paging_in_progress != 0) 36226f9a767SRodney W. Grimes panic("vm_object_deallocate: pageout in progress"); 36326f9a767SRodney W. Grimes 36426f9a767SRodney W. Grimes /* 3650d94caffSDavid Greenman * Clean and free the pages, as appropriate. All references to the 3660d94caffSDavid Greenman * object are gone, so we don't need to lock it. 36726f9a767SRodney W. Grimes */ 36824a1cce3SDavid Greenman if (object->type == OBJT_VNODE) { 36924a1cce3SDavid Greenman struct vnode *vp = object->handle; 370f6b04d2bSDavid Greenman 3710426122fSDavid Greenman VOP_LOCK(vp); 37224a1cce3SDavid Greenman vm_object_page_clean(object, 0, 0, TRUE, FALSE); 373f6b04d2bSDavid Greenman vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0); 3740426122fSDavid Greenman VOP_UNLOCK(vp); 375df8bae1dSRodney W. Grimes } 37683edfd47SDavid Greenman 3770d94caffSDavid Greenman /* 3780d94caffSDavid Greenman * Now free the pages. For internal objects, this also removes them 3790d94caffSDavid Greenman * from paging queues. 380df8bae1dSRodney W. Grimes */ 381df8bae1dSRodney W. Grimes while ((p = object->memq.tqh_first) != NULL) { 382be6d5bfaSDavid Greenman if (p->flags & PG_BUSY) 383be6d5bfaSDavid Greenman printf("vm_object_terminate: freeing busy page\n"); 3840d94caffSDavid Greenman PAGE_WAKEUP(p); 385df8bae1dSRodney W. Grimes vm_page_free(p); 386df8bae1dSRodney W. Grimes cnt.v_pfree++; 387df8bae1dSRodney W. Grimes } 388df8bae1dSRodney W. Grimes 389df8bae1dSRodney W. Grimes /* 390df8bae1dSRodney W. Grimes * Let the pager know object is dead. 391df8bae1dSRodney W. Grimes */ 39224a1cce3SDavid Greenman vm_pager_deallocate(object); 393df8bae1dSRodney W. Grimes 394df8bae1dSRodney W. Grimes TAILQ_REMOVE(&vm_object_list, object, object_list); 395df8bae1dSRodney W. Grimes vm_object_count--; 396df8bae1dSRodney W. Grimes 3970426122fSDavid Greenman wakeup(object); 3980426122fSDavid Greenman 399df8bae1dSRodney W. Grimes /* 400df8bae1dSRodney W. Grimes * Free the space for the object. 401df8bae1dSRodney W. Grimes */ 402df8bae1dSRodney W. Grimes free((caddr_t) object, M_VMOBJ); 403df8bae1dSRodney W. Grimes } 404df8bae1dSRodney W. Grimes 405df8bae1dSRodney W. Grimes /* 406df8bae1dSRodney W. Grimes * vm_object_page_clean 407df8bae1dSRodney W. Grimes * 408df8bae1dSRodney W. Grimes * Clean all dirty pages in the specified range of object. 40926f9a767SRodney W. Grimes * Leaves page on whatever queue it is currently on. 41026f9a767SRodney W. Grimes * 41126f9a767SRodney W. Grimes * Odd semantics: if start == end, we clean everything. 41226f9a767SRodney W. Grimes * 41326f9a767SRodney W. Grimes * The object must be locked. 41426f9a767SRodney W. Grimes */ 415f6b04d2bSDavid Greenman 416f6b04d2bSDavid Greenman void 41724a1cce3SDavid Greenman vm_object_page_clean(object, start, end, syncio, lockflag) 418f6b04d2bSDavid Greenman vm_object_t object; 419f6b04d2bSDavid Greenman vm_offset_t start; 420f6b04d2bSDavid Greenman vm_offset_t end; 421f6b04d2bSDavid Greenman boolean_t syncio; 42224a1cce3SDavid Greenman boolean_t lockflag; 423f6b04d2bSDavid Greenman { 424f6b04d2bSDavid Greenman register vm_page_t p; 425f6b04d2bSDavid Greenman register vm_offset_t tstart, tend; 426f6b04d2bSDavid Greenman int pass; 427f6b04d2bSDavid Greenman int pgcount, s; 428f6b04d2bSDavid Greenman int allclean; 429ec4f9fb0SDavid Greenman int entireobj; 43024a1cce3SDavid Greenman struct vnode *vp; 431f6b04d2bSDavid Greenman 43224a1cce3SDavid Greenman if (object->type != OBJT_VNODE || (object->flags & OBJ_WRITEABLE) == 0) 433f6b04d2bSDavid Greenman return; 434f6b04d2bSDavid Greenman 43524a1cce3SDavid Greenman vp = object->handle; 43624a1cce3SDavid Greenman 43724a1cce3SDavid Greenman if (lockflag) 43824a1cce3SDavid Greenman VOP_LOCK(vp); 43924a1cce3SDavid Greenman 440f6b04d2bSDavid Greenman if (start != end) { 441f6b04d2bSDavid Greenman start = trunc_page(start); 442f6b04d2bSDavid Greenman end = round_page(end); 443f6b04d2bSDavid Greenman } 444f6b04d2bSDavid Greenman 445f6b04d2bSDavid Greenman pass = 0; 446f6b04d2bSDavid Greenman startover: 447f6b04d2bSDavid Greenman tstart = start; 448f6b04d2bSDavid Greenman if (end == 0) { 449f6b04d2bSDavid Greenman tend = object->size; 450f6b04d2bSDavid Greenman } else { 451f6b04d2bSDavid Greenman tend = end; 452f6b04d2bSDavid Greenman } 453ec4f9fb0SDavid Greenman entireobj = 0; 454ec4f9fb0SDavid Greenman if (tstart == 0 && tend == object->size) { 455ec4f9fb0SDavid Greenman object->flags &= ~OBJ_WRITEABLE; 456ec4f9fb0SDavid Greenman entireobj = 1; 457ec4f9fb0SDavid Greenman } 458f6b04d2bSDavid Greenman 459f6b04d2bSDavid Greenman pgcount = object->resident_page_count; 460f6b04d2bSDavid Greenman 461f6b04d2bSDavid Greenman if (pass == 0 && 462f6b04d2bSDavid Greenman (pgcount < 128 || pgcount > (object->size / (8 * PAGE_SIZE)))) { 463f6b04d2bSDavid Greenman allclean = 1; 464f6b04d2bSDavid Greenman for(; pgcount && (tstart < tend); tstart += PAGE_SIZE) { 465f6b04d2bSDavid Greenman p = vm_page_lookup(object, tstart); 466f6b04d2bSDavid Greenman if (!p) 467f6b04d2bSDavid Greenman continue; 468f6b04d2bSDavid Greenman --pgcount; 469f6b04d2bSDavid Greenman s = splhigh(); 470f6b04d2bSDavid Greenman TAILQ_REMOVE(&object->memq, p, listq); 471f6b04d2bSDavid Greenman TAILQ_INSERT_TAIL(&object->memq, p, listq); 472f6b04d2bSDavid Greenman splx(s); 473ec4f9fb0SDavid Greenman if (entireobj) 474ec4f9fb0SDavid Greenman vm_page_protect(p, VM_PROT_READ); 475ec4f9fb0SDavid Greenman if ((p->flags & (PG_BUSY|PG_CACHE)) || p->busy || 476ec4f9fb0SDavid Greenman p->valid == 0) { 477f6b04d2bSDavid Greenman continue; 478f6b04d2bSDavid Greenman } 479f6b04d2bSDavid Greenman vm_page_test_dirty(p); 480f6b04d2bSDavid Greenman if ((p->valid & p->dirty) != 0) { 481f6b04d2bSDavid Greenman vm_offset_t tincr; 482f6b04d2bSDavid Greenman tincr = vm_pageout_clean(p, VM_PAGEOUT_FORCE); 4837e15fd27SJohn Dyson if( tincr) { 484f6b04d2bSDavid Greenman pgcount -= (tincr - 1); 485f6b04d2bSDavid Greenman tincr *= PAGE_SIZE; 486f6b04d2bSDavid Greenman tstart += tincr - PAGE_SIZE; 4877e15fd27SJohn Dyson } 488f6b04d2bSDavid Greenman allclean = 0; 489f6b04d2bSDavid Greenman } 490f6b04d2bSDavid Greenman } 491f6b04d2bSDavid Greenman if (!allclean) { 492f6b04d2bSDavid Greenman pass = 1; 493f6b04d2bSDavid Greenman goto startover; 494f6b04d2bSDavid Greenman } 49524a1cce3SDavid Greenman if (lockflag) 49624a1cce3SDavid Greenman VOP_UNLOCK(vp); 497f6b04d2bSDavid Greenman return; 498f6b04d2bSDavid Greenman } 499f6b04d2bSDavid Greenman 500f6b04d2bSDavid Greenman allclean = 1; 501f6b04d2bSDavid Greenman while ((p = object->memq.tqh_first) != NULL && pgcount > 0) { 502f6b04d2bSDavid Greenman 503f6b04d2bSDavid Greenman if (p->flags & PG_CACHE) { 504f6b04d2bSDavid Greenman goto donext; 505f6b04d2bSDavid Greenman } 506f6b04d2bSDavid Greenman 507ec4f9fb0SDavid Greenman if (entireobj || (p->offset >= tstart && p->offset < tend)) { 508ec4f9fb0SDavid Greenman if (entireobj) 509ec4f9fb0SDavid Greenman vm_page_protect(p, VM_PROT_READ); 510ec4f9fb0SDavid Greenman 511ec4f9fb0SDavid Greenman if (p->valid == 0) { 512f6b04d2bSDavid Greenman goto donext; 513f6b04d2bSDavid Greenman } 514f6b04d2bSDavid Greenman 515f6b04d2bSDavid Greenman s = splhigh(); 516f6b04d2bSDavid Greenman if ((p->flags & PG_BUSY) || p->busy) { 517f6b04d2bSDavid Greenman allclean = 0; 518f6b04d2bSDavid Greenman if (pass > 0) { 519f6b04d2bSDavid Greenman p->flags |= PG_WANTED; 520f6b04d2bSDavid Greenman tsleep(p, PVM, "objpcn", 0); 521f6b04d2bSDavid Greenman splx(s); 522f6b04d2bSDavid Greenman continue; 523f6b04d2bSDavid Greenman } else { 524f6b04d2bSDavid Greenman splx(s); 525f6b04d2bSDavid Greenman goto donext; 526f6b04d2bSDavid Greenman } 527f6b04d2bSDavid Greenman } 528f6b04d2bSDavid Greenman 529f6b04d2bSDavid Greenman TAILQ_REMOVE(&object->memq, p, listq); 530f6b04d2bSDavid Greenman TAILQ_INSERT_TAIL(&object->memq, p, listq); 531f6b04d2bSDavid Greenman splx(s); 532f6b04d2bSDavid Greenman 533f6b04d2bSDavid Greenman pgcount--; 534f6b04d2bSDavid Greenman vm_page_test_dirty(p); 535f6b04d2bSDavid Greenman if ((p->valid & p->dirty) != 0) { 536f6b04d2bSDavid Greenman vm_pageout_clean(p, VM_PAGEOUT_FORCE); 537f6b04d2bSDavid Greenman allclean = 0; 538f6b04d2bSDavid Greenman } 539f6b04d2bSDavid Greenman continue; 540f6b04d2bSDavid Greenman } 541f6b04d2bSDavid Greenman donext: 542f6b04d2bSDavid Greenman TAILQ_REMOVE(&object->memq, p, listq); 543f6b04d2bSDavid Greenman TAILQ_INSERT_TAIL(&object->memq, p, listq); 544f6b04d2bSDavid Greenman pgcount--; 545f6b04d2bSDavid Greenman } 546ec4f9fb0SDavid Greenman if ((!allclean && (pass == 0)) || 547ec4f9fb0SDavid Greenman (entireobj && (object->flags & OBJ_WRITEABLE))) { 548f6b04d2bSDavid Greenman pass = 1; 549ec4f9fb0SDavid Greenman if (entireobj) 550f6b04d2bSDavid Greenman object->flags &= ~OBJ_WRITEABLE; 551f6b04d2bSDavid Greenman goto startover; 552f6b04d2bSDavid Greenman } 55324a1cce3SDavid Greenman if (lockflag) 554d3628763SRodney W. Grimes VOP_UNLOCK(vp); 555f5cf85d4SDavid Greenman return; 55626f9a767SRodney W. Grimes } 557df8bae1dSRodney W. Grimes 558df8bae1dSRodney W. Grimes /* 559df8bae1dSRodney W. Grimes * vm_object_deactivate_pages 560df8bae1dSRodney W. Grimes * 561df8bae1dSRodney W. Grimes * Deactivate all pages in the specified object. (Keep its pages 562df8bae1dSRodney W. Grimes * in memory even though it is no longer referenced.) 563df8bae1dSRodney W. Grimes * 564df8bae1dSRodney W. Grimes * The object must be locked. 565df8bae1dSRodney W. Grimes */ 566df8bae1dSRodney W. Grimes void 567df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object) 568df8bae1dSRodney W. Grimes register vm_object_t object; 569df8bae1dSRodney W. Grimes { 570df8bae1dSRodney W. Grimes register vm_page_t p, next; 571df8bae1dSRodney W. Grimes 572df8bae1dSRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = next) { 573df8bae1dSRodney W. Grimes next = p->listq.tqe_next; 574df8bae1dSRodney W. Grimes vm_page_deactivate(p); 575df8bae1dSRodney W. Grimes } 576df8bae1dSRodney W. Grimes } 577df8bae1dSRodney W. Grimes 578df8bae1dSRodney W. Grimes /* 579df8bae1dSRodney W. Grimes * Trim the object cache to size. 580df8bae1dSRodney W. Grimes */ 581df8bae1dSRodney W. Grimes void 582df8bae1dSRodney W. Grimes vm_object_cache_trim() 583df8bae1dSRodney W. Grimes { 584df8bae1dSRodney W. Grimes register vm_object_t object; 585df8bae1dSRodney W. Grimes 5860d94caffSDavid Greenman while (vm_object_cached > vm_object_cache_max) { 587df8bae1dSRodney W. Grimes object = vm_object_cached_list.tqh_first; 588df8bae1dSRodney W. Grimes 58924a1cce3SDavid Greenman vm_object_reference(object); 590df8bae1dSRodney W. Grimes pager_cache(object, FALSE); 591df8bae1dSRodney W. Grimes } 592df8bae1dSRodney W. Grimes } 593df8bae1dSRodney W. Grimes 59426f9a767SRodney W. Grimes 595df8bae1dSRodney W. Grimes /* 596df8bae1dSRodney W. Grimes * vm_object_pmap_copy: 597df8bae1dSRodney W. Grimes * 598df8bae1dSRodney W. Grimes * Makes all physical pages in the specified 599df8bae1dSRodney W. Grimes * object range copy-on-write. No writeable 600df8bae1dSRodney W. Grimes * references to these pages should remain. 601df8bae1dSRodney W. Grimes * 602df8bae1dSRodney W. Grimes * The object must *not* be locked. 603df8bae1dSRodney W. Grimes */ 6040d94caffSDavid Greenman void 6050d94caffSDavid Greenman vm_object_pmap_copy(object, start, end) 606df8bae1dSRodney W. Grimes register vm_object_t object; 607df8bae1dSRodney W. Grimes register vm_offset_t start; 608df8bae1dSRodney W. Grimes register vm_offset_t end; 609df8bae1dSRodney W. Grimes { 610df8bae1dSRodney W. Grimes register vm_page_t p; 611df8bae1dSRodney W. Grimes 612df8bae1dSRodney W. Grimes if (object == NULL) 613df8bae1dSRodney W. Grimes return; 614df8bae1dSRodney W. Grimes 615df8bae1dSRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) { 616df8bae1dSRodney W. Grimes if ((start <= p->offset) && (p->offset < end)) { 617f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_READ); 618df8bae1dSRodney W. Grimes p->flags |= PG_COPYONWRITE; 619df8bae1dSRodney W. Grimes } 620df8bae1dSRodney W. Grimes } 621df8bae1dSRodney W. Grimes } 622df8bae1dSRodney W. Grimes 623df8bae1dSRodney W. Grimes /* 624df8bae1dSRodney W. Grimes * vm_object_pmap_remove: 625df8bae1dSRodney W. Grimes * 626df8bae1dSRodney W. Grimes * Removes all physical pages in the specified 627df8bae1dSRodney W. Grimes * object range from all physical maps. 628df8bae1dSRodney W. Grimes * 629df8bae1dSRodney W. Grimes * The object must *not* be locked. 630df8bae1dSRodney W. Grimes */ 63126f9a767SRodney W. Grimes void 63226f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end) 633df8bae1dSRodney W. Grimes register vm_object_t object; 634df8bae1dSRodney W. Grimes register vm_offset_t start; 635df8bae1dSRodney W. Grimes register vm_offset_t end; 636df8bae1dSRodney W. Grimes { 637df8bae1dSRodney W. Grimes register vm_page_t p; 638a481f200SDavid Greenman int s; 639df8bae1dSRodney W. Grimes 640df8bae1dSRodney W. Grimes if (object == NULL) 641df8bae1dSRodney W. Grimes return; 6422fe6e4d7SDavid Greenman ++object->paging_in_progress; 643df8bae1dSRodney W. Grimes 64426f9a767SRodney W. Grimes again: 64526f9a767SRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) { 64626f9a767SRodney W. Grimes if ((start <= p->offset) && (p->offset < end)) { 647a481f200SDavid Greenman s = splhigh(); 6480d94caffSDavid Greenman if ((p->flags & PG_BUSY) || p->busy) { 64926f9a767SRodney W. Grimes p->flags |= PG_WANTED; 65024a1cce3SDavid Greenman tsleep(p, PVM, "vmopmr", 0); 651a481f200SDavid Greenman splx(s); 65226f9a767SRodney W. Grimes goto again; 65326f9a767SRodney W. Grimes } 654a481f200SDavid Greenman splx(s); 655f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 65626f9a767SRodney W. Grimes } 65726f9a767SRodney W. Grimes } 658f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 659c0503609SDavid Greenman } 660df8bae1dSRodney W. Grimes 661df8bae1dSRodney W. Grimes /* 662df8bae1dSRodney W. Grimes * vm_object_copy: 663df8bae1dSRodney W. Grimes * 664df8bae1dSRodney W. Grimes * Create a new object which is a copy of an existing 665df8bae1dSRodney W. Grimes * object, and mark all of the pages in the existing 666df8bae1dSRodney W. Grimes * object 'copy-on-write'. The new object has one reference. 667df8bae1dSRodney W. Grimes * Returns the new object. 668df8bae1dSRodney W. Grimes * 669df8bae1dSRodney W. Grimes * May defer the copy until later if the object is not backed 670df8bae1dSRodney W. Grimes * up by a non-default pager. 671df8bae1dSRodney W. Grimes */ 6720d94caffSDavid Greenman void 6730d94caffSDavid Greenman vm_object_copy(src_object, src_offset, size, 674df8bae1dSRodney W. Grimes dst_object, dst_offset, src_needs_copy) 675df8bae1dSRodney W. Grimes register vm_object_t src_object; 676df8bae1dSRodney W. Grimes vm_offset_t src_offset; 677df8bae1dSRodney W. Grimes vm_size_t size; 678df8bae1dSRodney W. Grimes vm_object_t *dst_object;/* OUT */ 679df8bae1dSRodney W. Grimes vm_offset_t *dst_offset;/* OUT */ 680df8bae1dSRodney W. Grimes boolean_t *src_needs_copy; /* OUT */ 681df8bae1dSRodney W. Grimes { 682df8bae1dSRodney W. Grimes register vm_object_t new_copy; 683df8bae1dSRodney W. Grimes register vm_object_t old_copy; 684df8bae1dSRodney W. Grimes vm_offset_t new_start, new_end; 685df8bae1dSRodney W. Grimes 686df8bae1dSRodney W. Grimes register vm_page_t p; 687df8bae1dSRodney W. Grimes 688df8bae1dSRodney W. Grimes if (src_object == NULL) { 689df8bae1dSRodney W. Grimes /* 690df8bae1dSRodney W. Grimes * Nothing to copy 691df8bae1dSRodney W. Grimes */ 692df8bae1dSRodney W. Grimes *dst_object = NULL; 693df8bae1dSRodney W. Grimes *dst_offset = 0; 694df8bae1dSRodney W. Grimes *src_needs_copy = FALSE; 695df8bae1dSRodney W. Grimes return; 696df8bae1dSRodney W. Grimes } 69726f9a767SRodney W. Grimes 69826f9a767SRodney W. Grimes /* 69926f9a767SRodney W. Grimes * Try to collapse the object before copying it. 70026f9a767SRodney W. Grimes */ 70124a1cce3SDavid Greenman if (src_object->handle == NULL && 70224a1cce3SDavid Greenman (src_object->type == OBJT_DEFAULT || 70324a1cce3SDavid Greenman src_object->type == OBJT_SWAP)) 70426f9a767SRodney W. Grimes vm_object_collapse(src_object); 70526f9a767SRodney W. Grimes 706df8bae1dSRodney W. Grimes 707df8bae1dSRodney W. Grimes /* 708df8bae1dSRodney W. Grimes * Make another reference to the object 709df8bae1dSRodney W. Grimes */ 710df8bae1dSRodney W. Grimes src_object->ref_count++; 711df8bae1dSRodney W. Grimes 712df8bae1dSRodney W. Grimes /* 713df8bae1dSRodney W. Grimes * Mark all of the pages copy-on-write. 714df8bae1dSRodney W. Grimes */ 715df8bae1dSRodney W. Grimes for (p = src_object->memq.tqh_first; p; p = p->listq.tqe_next) 716df8bae1dSRodney W. Grimes if (src_offset <= p->offset && 717df8bae1dSRodney W. Grimes p->offset < src_offset + size) 718df8bae1dSRodney W. Grimes p->flags |= PG_COPYONWRITE; 719df8bae1dSRodney W. Grimes 720df8bae1dSRodney W. Grimes *dst_object = src_object; 721df8bae1dSRodney W. Grimes *dst_offset = src_offset; 722df8bae1dSRodney W. Grimes 723df8bae1dSRodney W. Grimes /* 724df8bae1dSRodney W. Grimes * Must make a shadow when write is desired 725df8bae1dSRodney W. Grimes */ 726df8bae1dSRodney W. Grimes *src_needs_copy = TRUE; 727df8bae1dSRodney W. Grimes return; 728df8bae1dSRodney W. Grimes } 729df8bae1dSRodney W. Grimes 730df8bae1dSRodney W. Grimes /* 731df8bae1dSRodney W. Grimes * vm_object_shadow: 732df8bae1dSRodney W. Grimes * 733df8bae1dSRodney W. Grimes * Create a new object which is backed by the 734df8bae1dSRodney W. Grimes * specified existing object range. The source 735df8bae1dSRodney W. Grimes * object reference is deallocated. 736df8bae1dSRodney W. Grimes * 737df8bae1dSRodney W. Grimes * The new object and offset into that object 738df8bae1dSRodney W. Grimes * are returned in the source parameters. 739df8bae1dSRodney W. Grimes */ 740df8bae1dSRodney W. Grimes 74126f9a767SRodney W. Grimes void 74226f9a767SRodney W. Grimes vm_object_shadow(object, offset, length) 743df8bae1dSRodney W. Grimes vm_object_t *object; /* IN/OUT */ 744df8bae1dSRodney W. Grimes vm_offset_t *offset; /* IN/OUT */ 745df8bae1dSRodney W. Grimes vm_size_t length; 746df8bae1dSRodney W. Grimes { 747df8bae1dSRodney W. Grimes register vm_object_t source; 748df8bae1dSRodney W. Grimes register vm_object_t result; 749df8bae1dSRodney W. Grimes 750df8bae1dSRodney W. Grimes source = *object; 751df8bae1dSRodney W. Grimes 752df8bae1dSRodney W. Grimes /* 753df8bae1dSRodney W. Grimes * Allocate a new object with the given length 754df8bae1dSRodney W. Grimes */ 755df8bae1dSRodney W. Grimes 75624a1cce3SDavid Greenman if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL) 757df8bae1dSRodney W. Grimes panic("vm_object_shadow: no object for shadowing"); 758df8bae1dSRodney W. Grimes 759df8bae1dSRodney W. Grimes /* 7600d94caffSDavid Greenman * The new object shadows the source object, adding a reference to it. 7610d94caffSDavid Greenman * Our caller changes his reference to point to the new object, 7620d94caffSDavid Greenman * removing a reference to the source object. Net result: no change 7630d94caffSDavid Greenman * of reference count. 764df8bae1dSRodney W. Grimes */ 76524a1cce3SDavid Greenman result->backing_object = source; 7667b18a718SDavid Greenman if (source) 76724a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&result->backing_object->shadow_head, result, shadow_list); 768df8bae1dSRodney W. Grimes 769df8bae1dSRodney W. Grimes /* 7700d94caffSDavid Greenman * Store the offset into the source object, and fix up the offset into 7710d94caffSDavid Greenman * the new object. 772df8bae1dSRodney W. Grimes */ 773df8bae1dSRodney W. Grimes 77424a1cce3SDavid Greenman result->backing_object_offset = *offset; 775df8bae1dSRodney W. Grimes 776df8bae1dSRodney W. Grimes /* 777df8bae1dSRodney W. Grimes * Return the new things 778df8bae1dSRodney W. Grimes */ 779df8bae1dSRodney W. Grimes 780df8bae1dSRodney W. Grimes *offset = 0; 781df8bae1dSRodney W. Grimes *object = result; 782df8bae1dSRodney W. Grimes } 783df8bae1dSRodney W. Grimes 784df8bae1dSRodney W. Grimes 785df8bae1dSRodney W. Grimes /* 7862fe6e4d7SDavid Greenman * this version of collapse allows the operation to occur earlier and 7872fe6e4d7SDavid Greenman * when paging_in_progress is true for an object... This is not a complete 7882fe6e4d7SDavid Greenman * operation, but should plug 99.9% of the rest of the leaks. 7892fe6e4d7SDavid Greenman */ 7902fe6e4d7SDavid Greenman static void 7912fe6e4d7SDavid Greenman vm_object_qcollapse(object) 7922fe6e4d7SDavid Greenman register vm_object_t object; 7932fe6e4d7SDavid Greenman { 7942fe6e4d7SDavid Greenman register vm_object_t backing_object; 7952fe6e4d7SDavid Greenman register vm_offset_t backing_offset, new_offset; 7962fe6e4d7SDavid Greenman register vm_page_t p, pp; 7972fe6e4d7SDavid Greenman register vm_size_t size; 7982fe6e4d7SDavid Greenman 79924a1cce3SDavid Greenman backing_object = object->backing_object; 8002fe6e4d7SDavid Greenman if (backing_object->ref_count != 1) 8012fe6e4d7SDavid Greenman return; 8022fe6e4d7SDavid Greenman 803010cf3b9SDavid Greenman backing_object->ref_count += 2; 804010cf3b9SDavid Greenman 80524a1cce3SDavid Greenman backing_offset = object->backing_object_offset; 8062fe6e4d7SDavid Greenman size = object->size; 8072fe6e4d7SDavid Greenman p = backing_object->memq.tqh_first; 8082fe6e4d7SDavid Greenman while (p) { 8092fe6e4d7SDavid Greenman vm_page_t next; 8100d94caffSDavid Greenman 8112fe6e4d7SDavid Greenman next = p->listq.tqe_next; 8120d94caffSDavid Greenman if ((p->flags & (PG_BUSY | PG_FICTITIOUS | PG_CACHE)) || 813f6b04d2bSDavid Greenman !p->valid || p->hold_count || p->wire_count || p->busy) { 8142fe6e4d7SDavid Greenman p = next; 8152fe6e4d7SDavid Greenman continue; 8162fe6e4d7SDavid Greenman } 817f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 8182fe6e4d7SDavid Greenman new_offset = (p->offset - backing_offset); 8192fe6e4d7SDavid Greenman if (p->offset < backing_offset || 8202fe6e4d7SDavid Greenman new_offset >= size) { 82124a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) 82224a1cce3SDavid Greenman swap_pager_freespace(backing_object, 8230d94caffSDavid Greenman backing_object->paging_offset + p->offset, PAGE_SIZE); 8242fe6e4d7SDavid Greenman vm_page_free(p); 8252fe6e4d7SDavid Greenman } else { 8262fe6e4d7SDavid Greenman pp = vm_page_lookup(object, new_offset); 82724a1cce3SDavid Greenman if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object, 82824a1cce3SDavid Greenman object->paging_offset + new_offset, NULL, NULL))) { 82924a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) 83024a1cce3SDavid Greenman swap_pager_freespace(backing_object, 8310d94caffSDavid Greenman backing_object->paging_offset + p->offset, PAGE_SIZE); 8322fe6e4d7SDavid Greenman vm_page_free(p); 8332fe6e4d7SDavid Greenman } else { 83424a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) 83524a1cce3SDavid Greenman swap_pager_freespace(backing_object, 836a1f6d91cSDavid Greenman backing_object->paging_offset + p->offset, PAGE_SIZE); 8372fe6e4d7SDavid Greenman vm_page_rename(p, object, new_offset); 8389b4814bbSDavid Greenman p->dirty = VM_PAGE_BITS_ALL; 8392fe6e4d7SDavid Greenman } 8402fe6e4d7SDavid Greenman } 8412fe6e4d7SDavid Greenman p = next; 8422fe6e4d7SDavid Greenman } 843010cf3b9SDavid Greenman backing_object->ref_count -= 2; 8442fe6e4d7SDavid Greenman } 8452fe6e4d7SDavid Greenman 846df8bae1dSRodney W. Grimes /* 847df8bae1dSRodney W. Grimes * vm_object_collapse: 848df8bae1dSRodney W. Grimes * 849df8bae1dSRodney W. Grimes * Collapse an object with the object backing it. 850df8bae1dSRodney W. Grimes * Pages in the backing object are moved into the 851df8bae1dSRodney W. Grimes * parent, and the backing object is deallocated. 852df8bae1dSRodney W. Grimes */ 85326f9a767SRodney W. Grimes void 85426f9a767SRodney W. Grimes vm_object_collapse(object) 85524a1cce3SDavid Greenman vm_object_t object; 856df8bae1dSRodney W. Grimes 857df8bae1dSRodney W. Grimes { 85824a1cce3SDavid Greenman vm_object_t backing_object; 85924a1cce3SDavid Greenman vm_offset_t backing_offset; 86024a1cce3SDavid Greenman vm_size_t size; 86124a1cce3SDavid Greenman vm_offset_t new_offset; 86224a1cce3SDavid Greenman vm_page_t p, pp; 863df8bae1dSRodney W. Grimes 864df8bae1dSRodney W. Grimes while (TRUE) { 865df8bae1dSRodney W. Grimes /* 866df8bae1dSRodney W. Grimes * Verify that the conditions are right for collapse: 867df8bae1dSRodney W. Grimes * 8680d94caffSDavid Greenman * The object exists and no pages in it are currently being paged 8690d94caffSDavid Greenman * out. 870df8bae1dSRodney W. Grimes */ 8712fe6e4d7SDavid Greenman if (object == NULL) 872df8bae1dSRodney W. Grimes return; 873df8bae1dSRodney W. Grimes 874b9921222SDavid Greenman /* 875b9921222SDavid Greenman * Make sure there is a backing object. 876b9921222SDavid Greenman */ 87724a1cce3SDavid Greenman if ((backing_object = object->backing_object) == NULL) 878df8bae1dSRodney W. Grimes return; 879df8bae1dSRodney W. Grimes 880f919ebdeSDavid Greenman /* 881f919ebdeSDavid Greenman * we check the backing object first, because it is most likely 88224a1cce3SDavid Greenman * not collapsable. 883f919ebdeSDavid Greenman */ 88424a1cce3SDavid Greenman if (backing_object->handle != NULL || 88524a1cce3SDavid Greenman (backing_object->type != OBJT_DEFAULT && 88624a1cce3SDavid Greenman backing_object->type != OBJT_SWAP) || 887f919ebdeSDavid Greenman (backing_object->flags & OBJ_DEAD) || 88824a1cce3SDavid Greenman object->handle != NULL || 88924a1cce3SDavid Greenman (object->type != OBJT_DEFAULT && 89024a1cce3SDavid Greenman object->type != OBJT_SWAP) || 89124a1cce3SDavid Greenman (object->flags & OBJ_DEAD)) { 8929b4814bbSDavid Greenman return; 89324a1cce3SDavid Greenman } 8949b4814bbSDavid Greenman 895f919ebdeSDavid Greenman if (object->paging_in_progress != 0 || 896f919ebdeSDavid Greenman backing_object->paging_in_progress != 0) { 897b9921222SDavid Greenman vm_object_qcollapse(object); 898df8bae1dSRodney W. Grimes return; 899df8bae1dSRodney W. Grimes } 900f919ebdeSDavid Greenman 90126f9a767SRodney W. Grimes /* 9020d94caffSDavid Greenman * We know that we can either collapse the backing object (if 9030d94caffSDavid Greenman * the parent is the only reference to it) or (perhaps) remove 9040d94caffSDavid Greenman * the parent's reference to it. 905df8bae1dSRodney W. Grimes */ 906df8bae1dSRodney W. Grimes 90724a1cce3SDavid Greenman backing_offset = object->backing_object_offset; 908df8bae1dSRodney W. Grimes size = object->size; 909df8bae1dSRodney W. Grimes 910df8bae1dSRodney W. Grimes /* 9110d94caffSDavid Greenman * If there is exactly one reference to the backing object, we 9120d94caffSDavid Greenman * can collapse it into the parent. 913df8bae1dSRodney W. Grimes */ 914df8bae1dSRodney W. Grimes 915df8bae1dSRodney W. Grimes if (backing_object->ref_count == 1) { 916df8bae1dSRodney W. Grimes 917a1f6d91cSDavid Greenman backing_object->flags |= OBJ_DEAD; 918df8bae1dSRodney W. Grimes /* 919df8bae1dSRodney W. Grimes * We can collapse the backing object. 920df8bae1dSRodney W. Grimes * 9210d94caffSDavid Greenman * Move all in-memory pages from backing_object to the 9220d94caffSDavid Greenman * parent. Pages that have been paged out will be 9230d94caffSDavid Greenman * overwritten by any of the parent's pages that 9240d94caffSDavid Greenman * shadow them. 925df8bae1dSRodney W. Grimes */ 926df8bae1dSRodney W. Grimes 92705f0fdd2SPoul-Henning Kamp while ((p = backing_object->memq.tqh_first) != 0) { 92826f9a767SRodney W. Grimes 929df8bae1dSRodney W. Grimes new_offset = (p->offset - backing_offset); 930df8bae1dSRodney W. Grimes 931df8bae1dSRodney W. Grimes /* 9320d94caffSDavid Greenman * If the parent has a page here, or if this 9330d94caffSDavid Greenman * page falls outside the parent, dispose of 9340d94caffSDavid Greenman * it. 935df8bae1dSRodney W. Grimes * 936df8bae1dSRodney W. Grimes * Otherwise, move it as planned. 937df8bae1dSRodney W. Grimes */ 938df8bae1dSRodney W. Grimes 939df8bae1dSRodney W. Grimes if (p->offset < backing_offset || 940df8bae1dSRodney W. Grimes new_offset >= size) { 941f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 9420d94caffSDavid Greenman PAGE_WAKEUP(p); 943df8bae1dSRodney W. Grimes vm_page_free(p); 944df8bae1dSRodney W. Grimes } else { 945df8bae1dSRodney W. Grimes pp = vm_page_lookup(object, new_offset); 94624a1cce3SDavid Greenman if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object, 94724a1cce3SDavid Greenman object->paging_offset + new_offset, NULL, NULL))) { 948f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 9490d94caffSDavid Greenman PAGE_WAKEUP(p); 950df8bae1dSRodney W. Grimes vm_page_free(p); 95126f9a767SRodney W. Grimes } else { 952df8bae1dSRodney W. Grimes vm_page_rename(p, object, new_offset); 953df8bae1dSRodney W. Grimes } 954df8bae1dSRodney W. Grimes } 955df8bae1dSRodney W. Grimes } 956df8bae1dSRodney W. Grimes 957df8bae1dSRodney W. Grimes /* 958df8bae1dSRodney W. Grimes * Move the pager from backing_object to object. 959df8bae1dSRodney W. Grimes */ 960df8bae1dSRodney W. Grimes 96124a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) { 96226f9a767SRodney W. Grimes backing_object->paging_in_progress++; 96324a1cce3SDavid Greenman if (object->type == OBJT_SWAP) { 96426f9a767SRodney W. Grimes object->paging_in_progress++; 96526f9a767SRodney W. Grimes /* 96626f9a767SRodney W. Grimes * copy shadow object pages into ours 9670d94caffSDavid Greenman * and destroy unneeded pages in 9680d94caffSDavid Greenman * shadow object. 96926f9a767SRodney W. Grimes */ 97026f9a767SRodney W. Grimes swap_pager_copy( 97124a1cce3SDavid Greenman backing_object, backing_object->paging_offset, 97224a1cce3SDavid Greenman object, object->paging_offset, 97324a1cce3SDavid Greenman object->backing_object_offset); 974f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 97526f9a767SRodney W. Grimes } else { 97624a1cce3SDavid Greenman extern struct pagerlst swap_pager_un_object_list; 97724a1cce3SDavid Greenman 97826f9a767SRodney W. Grimes object->paging_in_progress++; 97926f9a767SRodney W. Grimes /* 98024a1cce3SDavid Greenman * move the shadow backing_object's pager data to 98124a1cce3SDavid Greenman * "object" and convert "object" type to OBJT_SWAP. 98226f9a767SRodney W. Grimes */ 98324a1cce3SDavid Greenman object->type = OBJT_SWAP; 9842a4895f4SDavid Greenman object->un_pager.swp.swp_nblocks = 9852a4895f4SDavid Greenman backing_object->un_pager.swp.swp_nblocks; 9862a4895f4SDavid Greenman object->un_pager.swp.swp_allocsize = 9872a4895f4SDavid Greenman backing_object->un_pager.swp.swp_allocsize; 9882a4895f4SDavid Greenman object->un_pager.swp.swp_blocks = 9892a4895f4SDavid Greenman backing_object->un_pager.swp.swp_blocks; 9902a4895f4SDavid Greenman object->un_pager.swp.swp_poip = /* XXX */ 9912a4895f4SDavid Greenman backing_object->un_pager.swp.swp_poip; 99226f9a767SRodney W. Grimes object->paging_offset = backing_object->paging_offset + backing_offset; 99324a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&swap_pager_un_object_list, object, pager_object_list); 99424a1cce3SDavid Greenman 99524a1cce3SDavid Greenman /* 99624a1cce3SDavid Greenman * Convert backing object from OBJT_SWAP to 99724a1cce3SDavid Greenman * OBJT_DEFAULT. XXX - only the TAILQ_REMOVE is 99824a1cce3SDavid Greenman * actually necessary. 99924a1cce3SDavid Greenman */ 100024a1cce3SDavid Greenman backing_object->type = OBJT_DEFAULT; 100124a1cce3SDavid Greenman TAILQ_REMOVE(&swap_pager_un_object_list, backing_object, pager_object_list); 100226f9a767SRodney W. Grimes /* 100326f9a767SRodney W. Grimes * free unnecessary blocks 100426f9a767SRodney W. Grimes */ 100524a1cce3SDavid Greenman swap_pager_freespace(object, 0, object->paging_offset); 1006f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 1007c0503609SDavid Greenman } 1008c0503609SDavid Greenman 1009f919ebdeSDavid Greenman vm_object_pip_wakeup(backing_object); 1010c0503609SDavid Greenman } 1011df8bae1dSRodney W. Grimes /* 1012df8bae1dSRodney W. Grimes * Object now shadows whatever backing_object did. 101324a1cce3SDavid Greenman * Note that the reference to backing_object->backing_object 1014df8bae1dSRodney W. Grimes * moves from within backing_object to within object. 1015df8bae1dSRodney W. Grimes */ 1016df8bae1dSRodney W. Grimes 101724a1cce3SDavid Greenman TAILQ_REMOVE(&object->backing_object->shadow_head, object, 101824a1cce3SDavid Greenman shadow_list); 101924a1cce3SDavid Greenman if (backing_object->backing_object) 102024a1cce3SDavid Greenman TAILQ_REMOVE(&backing_object->backing_object->shadow_head, 102124a1cce3SDavid Greenman backing_object, shadow_list); 102224a1cce3SDavid Greenman object->backing_object = backing_object->backing_object; 102324a1cce3SDavid Greenman if (object->backing_object) 102424a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&object->backing_object->shadow_head, 102524a1cce3SDavid Greenman object, shadow_list); 10262fe6e4d7SDavid Greenman 102724a1cce3SDavid Greenman object->backing_object_offset += backing_object->backing_object_offset; 1028df8bae1dSRodney W. Grimes /* 1029df8bae1dSRodney W. Grimes * Discard backing_object. 1030df8bae1dSRodney W. Grimes * 10310d94caffSDavid Greenman * Since the backing object has no pages, no pager left, 10320d94caffSDavid Greenman * and no object references within it, all that is 10330d94caffSDavid Greenman * necessary is to dispose of it. 1034df8bae1dSRodney W. Grimes */ 1035df8bae1dSRodney W. Grimes 1036df8bae1dSRodney W. Grimes TAILQ_REMOVE(&vm_object_list, backing_object, 1037df8bae1dSRodney W. Grimes object_list); 1038df8bae1dSRodney W. Grimes vm_object_count--; 1039df8bae1dSRodney W. Grimes 1040df8bae1dSRodney W. Grimes free((caddr_t) backing_object, M_VMOBJ); 1041df8bae1dSRodney W. Grimes 1042df8bae1dSRodney W. Grimes object_collapses++; 10430d94caffSDavid Greenman } else { 1044df8bae1dSRodney W. Grimes /* 1045df8bae1dSRodney W. Grimes * If all of the pages in the backing object are 10460d94caffSDavid Greenman * shadowed by the parent object, the parent object no 10470d94caffSDavid Greenman * longer has to shadow the backing object; it can 10480d94caffSDavid Greenman * shadow the next one in the chain. 1049df8bae1dSRodney W. Grimes * 10500d94caffSDavid Greenman * The backing object must not be paged out - we'd have 10510d94caffSDavid Greenman * to check all of the paged-out pages, as well. 1052df8bae1dSRodney W. Grimes */ 1053df8bae1dSRodney W. Grimes 105424a1cce3SDavid Greenman if (backing_object->type != OBJT_DEFAULT) { 1055df8bae1dSRodney W. Grimes return; 1056df8bae1dSRodney W. Grimes } 1057df8bae1dSRodney W. Grimes /* 10580d94caffSDavid Greenman * Should have a check for a 'small' number of pages 10590d94caffSDavid Greenman * here. 1060df8bae1dSRodney W. Grimes */ 1061df8bae1dSRodney W. Grimes 106226f9a767SRodney W. Grimes for (p = backing_object->memq.tqh_first; p; p = p->listq.tqe_next) { 1063df8bae1dSRodney W. Grimes new_offset = (p->offset - backing_offset); 1064df8bae1dSRodney W. Grimes 1065df8bae1dSRodney W. Grimes /* 10660d94caffSDavid Greenman * If the parent has a page here, or if this 10670d94caffSDavid Greenman * page falls outside the parent, keep going. 1068df8bae1dSRodney W. Grimes * 10690d94caffSDavid Greenman * Otherwise, the backing_object must be left in 10700d94caffSDavid Greenman * the chain. 1071df8bae1dSRodney W. Grimes */ 1072df8bae1dSRodney W. Grimes 107324a1cce3SDavid Greenman if (p->offset >= backing_offset && new_offset <= size) { 107424a1cce3SDavid Greenman 107524a1cce3SDavid Greenman pp = vm_page_lookup(object, new_offset); 107624a1cce3SDavid Greenman 107724a1cce3SDavid Greenman if ((pp == NULL || pp->valid == 0) && 107824a1cce3SDavid Greenman !vm_pager_has_page(object, object->paging_offset + new_offset, NULL, NULL)) { 107924a1cce3SDavid Greenman 1080df8bae1dSRodney W. Grimes /* 10810d94caffSDavid Greenman * Page still needed. Can't go any 10820d94caffSDavid Greenman * further. 1083df8bae1dSRodney W. Grimes */ 1084df8bae1dSRodney W. Grimes return; 1085df8bae1dSRodney W. Grimes } 1086df8bae1dSRodney W. Grimes } 108724a1cce3SDavid Greenman } 1088df8bae1dSRodney W. Grimes 1089df8bae1dSRodney W. Grimes /* 10900d94caffSDavid Greenman * Make the parent shadow the next object in the 10910d94caffSDavid Greenman * chain. Deallocating backing_object will not remove 10920d94caffSDavid Greenman * it, since its reference count is at least 2. 1093df8bae1dSRodney W. Grimes */ 1094df8bae1dSRodney W. Grimes 109524a1cce3SDavid Greenman TAILQ_REMOVE(&object->backing_object->shadow_head, 109624a1cce3SDavid Greenman object, shadow_list); 109724a1cce3SDavid Greenman vm_object_reference(object->backing_object = backing_object->backing_object); 109824a1cce3SDavid Greenman if (object->backing_object) 109924a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&object->backing_object->shadow_head, 110024a1cce3SDavid Greenman object, shadow_list); 110124a1cce3SDavid Greenman object->backing_object_offset += backing_object->backing_object_offset; 1102df8bae1dSRodney W. Grimes 1103df8bae1dSRodney W. Grimes /* 11040d94caffSDavid Greenman * Drop the reference count on backing_object. Since 11050d94caffSDavid Greenman * its ref_count was at least 2, it will not vanish; 11060d94caffSDavid Greenman * so we don't need to call vm_object_deallocate. 1107df8bae1dSRodney W. Grimes */ 110826f9a767SRodney W. Grimes if (backing_object->ref_count == 1) 110926f9a767SRodney W. Grimes printf("should have called obj deallocate\n"); 1110df8bae1dSRodney W. Grimes backing_object->ref_count--; 1111df8bae1dSRodney W. Grimes 1112df8bae1dSRodney W. Grimes object_bypasses++; 1113df8bae1dSRodney W. Grimes 1114df8bae1dSRodney W. Grimes } 1115df8bae1dSRodney W. Grimes 1116df8bae1dSRodney W. Grimes /* 1117df8bae1dSRodney W. Grimes * Try again with this object's new backing object. 1118df8bae1dSRodney W. Grimes */ 1119df8bae1dSRodney W. Grimes } 1120df8bae1dSRodney W. Grimes } 1121df8bae1dSRodney W. Grimes 1122df8bae1dSRodney W. Grimes /* 1123df8bae1dSRodney W. Grimes * vm_object_page_remove: [internal] 1124df8bae1dSRodney W. Grimes * 1125df8bae1dSRodney W. Grimes * Removes all physical pages in the specified 1126df8bae1dSRodney W. Grimes * object range from the object's list of pages. 1127df8bae1dSRodney W. Grimes * 1128df8bae1dSRodney W. Grimes * The object must be locked. 1129df8bae1dSRodney W. Grimes */ 113026f9a767SRodney W. Grimes void 11317c1f6cedSDavid Greenman vm_object_page_remove(object, start, end, clean_only) 1132df8bae1dSRodney W. Grimes register vm_object_t object; 1133df8bae1dSRodney W. Grimes register vm_offset_t start; 1134df8bae1dSRodney W. Grimes register vm_offset_t end; 11357c1f6cedSDavid Greenman boolean_t clean_only; 1136df8bae1dSRodney W. Grimes { 1137df8bae1dSRodney W. Grimes register vm_page_t p, next; 113826f9a767SRodney W. Grimes vm_offset_t size; 113926f9a767SRodney W. Grimes int s; 1140df8bae1dSRodney W. Grimes 1141df8bae1dSRodney W. Grimes if (object == NULL) 1142df8bae1dSRodney W. Grimes return; 1143df8bae1dSRodney W. Grimes 11442fe6e4d7SDavid Greenman object->paging_in_progress++; 114526f9a767SRodney W. Grimes start = trunc_page(start); 114626f9a767SRodney W. Grimes end = round_page(end); 114726f9a767SRodney W. Grimes again: 114826f9a767SRodney W. Grimes size = end - start; 114926f9a767SRodney W. Grimes if (size > 4 * PAGE_SIZE || size >= object->size / 4) { 11500d94caffSDavid Greenman for (p = object->memq.tqh_first; p != NULL; p = next) { 1151df8bae1dSRodney W. Grimes next = p->listq.tqe_next; 1152df8bae1dSRodney W. Grimes if ((start <= p->offset) && (p->offset < end)) { 1153a481f200SDavid Greenman s = splhigh(); 11540d94caffSDavid Greenman if (p->bmapped) { 11550d94caffSDavid Greenman splx(s); 11560d94caffSDavid Greenman continue; 11570d94caffSDavid Greenman } 11580d94caffSDavid Greenman if ((p->flags & PG_BUSY) || p->busy) { 115926f9a767SRodney W. Grimes p->flags |= PG_WANTED; 116024a1cce3SDavid Greenman tsleep(p, PVM, "vmopar", 0); 1161a481f200SDavid Greenman splx(s); 116226f9a767SRodney W. Grimes goto again; 116326f9a767SRodney W. Grimes } 1164a481f200SDavid Greenman splx(s); 11657c1f6cedSDavid Greenman if (clean_only) { 11667c1f6cedSDavid Greenman vm_page_test_dirty(p); 11677c1f6cedSDavid Greenman if (p->valid & p->dirty) 11687c1f6cedSDavid Greenman continue; 11697c1f6cedSDavid Greenman } 1170f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 11710d94caffSDavid Greenman PAGE_WAKEUP(p); 1172df8bae1dSRodney W. Grimes vm_page_free(p); 117326f9a767SRodney W. Grimes } 117426f9a767SRodney W. Grimes } 117526f9a767SRodney W. Grimes } else { 117626f9a767SRodney W. Grimes while (size > 0) { 117705f0fdd2SPoul-Henning Kamp while ((p = vm_page_lookup(object, start)) != 0) { 1178a481f200SDavid Greenman s = splhigh(); 11790d94caffSDavid Greenman if (p->bmapped) { 11800d94caffSDavid Greenman splx(s); 11810d94caffSDavid Greenman break; 11820d94caffSDavid Greenman } 11830d94caffSDavid Greenman if ((p->flags & PG_BUSY) || p->busy) { 118426f9a767SRodney W. Grimes p->flags |= PG_WANTED; 118524a1cce3SDavid Greenman tsleep(p, PVM, "vmopar", 0); 1186a481f200SDavid Greenman splx(s); 118726f9a767SRodney W. Grimes goto again; 118826f9a767SRodney W. Grimes } 1189a481f200SDavid Greenman splx(s); 11907c1f6cedSDavid Greenman if (clean_only) { 11917c1f6cedSDavid Greenman vm_page_test_dirty(p); 11927c1f6cedSDavid Greenman if (p->valid & p->dirty) 11937c1f6cedSDavid Greenman continue; 11947c1f6cedSDavid Greenman } 1195f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 11960d94caffSDavid Greenman PAGE_WAKEUP(p); 119726f9a767SRodney W. Grimes vm_page_free(p); 119826f9a767SRodney W. Grimes } 119926f9a767SRodney W. Grimes start += PAGE_SIZE; 120026f9a767SRodney W. Grimes size -= PAGE_SIZE; 1201df8bae1dSRodney W. Grimes } 1202df8bae1dSRodney W. Grimes } 1203f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 1204c0503609SDavid Greenman } 1205df8bae1dSRodney W. Grimes 1206df8bae1dSRodney W. Grimes /* 1207df8bae1dSRodney W. Grimes * Routine: vm_object_coalesce 1208df8bae1dSRodney W. Grimes * Function: Coalesces two objects backing up adjoining 1209df8bae1dSRodney W. Grimes * regions of memory into a single object. 1210df8bae1dSRodney W. Grimes * 1211df8bae1dSRodney W. Grimes * returns TRUE if objects were combined. 1212df8bae1dSRodney W. Grimes * 1213df8bae1dSRodney W. Grimes * NOTE: Only works at the moment if the second object is NULL - 1214df8bae1dSRodney W. Grimes * if it's not, which object do we lock first? 1215df8bae1dSRodney W. Grimes * 1216df8bae1dSRodney W. Grimes * Parameters: 1217df8bae1dSRodney W. Grimes * prev_object First object to coalesce 1218df8bae1dSRodney W. Grimes * prev_offset Offset into prev_object 1219df8bae1dSRodney W. Grimes * next_object Second object into coalesce 1220df8bae1dSRodney W. Grimes * next_offset Offset into next_object 1221df8bae1dSRodney W. Grimes * 1222df8bae1dSRodney W. Grimes * prev_size Size of reference to prev_object 1223df8bae1dSRodney W. Grimes * next_size Size of reference to next_object 1224df8bae1dSRodney W. Grimes * 1225df8bae1dSRodney W. Grimes * Conditions: 1226df8bae1dSRodney W. Grimes * The object must *not* be locked. 1227df8bae1dSRodney W. Grimes */ 12280d94caffSDavid Greenman boolean_t 12290d94caffSDavid Greenman vm_object_coalesce(prev_object, next_object, 1230df8bae1dSRodney W. Grimes prev_offset, next_offset, 1231df8bae1dSRodney W. Grimes prev_size, next_size) 1232df8bae1dSRodney W. Grimes register vm_object_t prev_object; 1233df8bae1dSRodney W. Grimes vm_object_t next_object; 1234df8bae1dSRodney W. Grimes vm_offset_t prev_offset, next_offset; 1235df8bae1dSRodney W. Grimes vm_size_t prev_size, next_size; 1236df8bae1dSRodney W. Grimes { 1237df8bae1dSRodney W. Grimes vm_size_t newsize; 1238df8bae1dSRodney W. Grimes 1239df8bae1dSRodney W. Grimes if (next_object != NULL) { 1240df8bae1dSRodney W. Grimes return (FALSE); 1241df8bae1dSRodney W. Grimes } 1242df8bae1dSRodney W. Grimes if (prev_object == NULL) { 1243df8bae1dSRodney W. Grimes return (TRUE); 1244df8bae1dSRodney W. Grimes } 1245df8bae1dSRodney W. Grimes 1246df8bae1dSRodney W. Grimes /* 1247df8bae1dSRodney W. Grimes * Try to collapse the object first 1248df8bae1dSRodney W. Grimes */ 1249df8bae1dSRodney W. Grimes vm_object_collapse(prev_object); 1250df8bae1dSRodney W. Grimes 1251df8bae1dSRodney W. Grimes /* 12520d94caffSDavid Greenman * Can't coalesce if: . more than one reference . paged out . shadows 12530d94caffSDavid Greenman * another object . has a copy elsewhere (any of which mean that the 12540d94caffSDavid Greenman * pages not mapped to prev_entry may be in use anyway) 1255df8bae1dSRodney W. Grimes */ 1256df8bae1dSRodney W. Grimes 1257df8bae1dSRodney W. Grimes if (prev_object->ref_count > 1 || 125824a1cce3SDavid Greenman prev_object->type != OBJT_DEFAULT || 125924a1cce3SDavid Greenman prev_object->backing_object != NULL) { 1260df8bae1dSRodney W. Grimes return (FALSE); 1261df8bae1dSRodney W. Grimes } 1262df8bae1dSRodney W. Grimes /* 12630d94caffSDavid Greenman * Remove any pages that may still be in the object from a previous 12640d94caffSDavid Greenman * deallocation. 1265df8bae1dSRodney W. Grimes */ 1266df8bae1dSRodney W. Grimes 1267df8bae1dSRodney W. Grimes vm_object_page_remove(prev_object, 1268df8bae1dSRodney W. Grimes prev_offset + prev_size, 12697c1f6cedSDavid Greenman prev_offset + prev_size + next_size, FALSE); 1270df8bae1dSRodney W. Grimes 1271df8bae1dSRodney W. Grimes /* 1272df8bae1dSRodney W. Grimes * Extend the object if necessary. 1273df8bae1dSRodney W. Grimes */ 1274df8bae1dSRodney W. Grimes newsize = prev_offset + prev_size + next_size; 1275df8bae1dSRodney W. Grimes if (newsize > prev_object->size) 1276df8bae1dSRodney W. Grimes prev_object->size = newsize; 1277df8bae1dSRodney W. Grimes 1278df8bae1dSRodney W. Grimes return (TRUE); 1279df8bae1dSRodney W. Grimes } 1280df8bae1dSRodney W. Grimes 1281df8bae1dSRodney W. Grimes /* 128226f9a767SRodney W. Grimes * returns page after looking up in shadow chain 128326f9a767SRodney W. Grimes */ 128426f9a767SRodney W. Grimes 128526f9a767SRodney W. Grimes vm_page_t 128626f9a767SRodney W. Grimes vm_object_page_lookup(object, offset) 128726f9a767SRodney W. Grimes vm_object_t object; 128826f9a767SRodney W. Grimes vm_offset_t offset; 128926f9a767SRodney W. Grimes { 129026f9a767SRodney W. Grimes vm_page_t m; 12910d94caffSDavid Greenman 129226f9a767SRodney W. Grimes if (!(m = vm_page_lookup(object, offset))) { 129324a1cce3SDavid Greenman if (!object->backing_object) 129426f9a767SRodney W. Grimes return 0; 129526f9a767SRodney W. Grimes else 129624a1cce3SDavid Greenman return vm_object_page_lookup(object->backing_object, offset + object->backing_object_offset); 129726f9a767SRodney W. Grimes } 129826f9a767SRodney W. Grimes return m; 129926f9a767SRodney W. Grimes } 130026f9a767SRodney W. Grimes 1301c3cb3e12SDavid Greenman #ifdef DDB 1302c3cb3e12SDavid Greenman 1303a1f6d91cSDavid Greenman int 1304a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry) 1305a1f6d91cSDavid Greenman vm_map_t map; 1306a1f6d91cSDavid Greenman vm_object_t object; 1307a1f6d91cSDavid Greenman vm_map_entry_t entry; 1308a1f6d91cSDavid Greenman { 1309a1f6d91cSDavid Greenman vm_map_t tmpm; 1310a1f6d91cSDavid Greenman vm_map_entry_t tmpe; 1311a1f6d91cSDavid Greenman vm_object_t obj; 1312a1f6d91cSDavid Greenman int entcount; 1313a1f6d91cSDavid Greenman 1314a1f6d91cSDavid Greenman if (map == 0) 1315a1f6d91cSDavid Greenman return 0; 1316a1f6d91cSDavid Greenman 1317a1f6d91cSDavid Greenman if (entry == 0) { 1318a1f6d91cSDavid Greenman tmpe = map->header.next; 1319a1f6d91cSDavid Greenman entcount = map->nentries; 1320a1f6d91cSDavid Greenman while (entcount-- && (tmpe != &map->header)) { 1321a1f6d91cSDavid Greenman if( _vm_object_in_map(map, object, tmpe)) { 1322a1f6d91cSDavid Greenman return 1; 1323a1f6d91cSDavid Greenman } 1324a1f6d91cSDavid Greenman tmpe = tmpe->next; 1325a1f6d91cSDavid Greenman } 1326a1f6d91cSDavid Greenman } else if (entry->is_sub_map || entry->is_a_map) { 1327a1f6d91cSDavid Greenman tmpm = entry->object.share_map; 1328a1f6d91cSDavid Greenman tmpe = tmpm->header.next; 1329a1f6d91cSDavid Greenman entcount = tmpm->nentries; 1330a1f6d91cSDavid Greenman while (entcount-- && tmpe != &tmpm->header) { 1331a1f6d91cSDavid Greenman if( _vm_object_in_map(tmpm, object, tmpe)) { 1332a1f6d91cSDavid Greenman return 1; 1333a1f6d91cSDavid Greenman } 1334a1f6d91cSDavid Greenman tmpe = tmpe->next; 1335a1f6d91cSDavid Greenman } 1336a1f6d91cSDavid Greenman } else if (obj = entry->object.vm_object) { 133724a1cce3SDavid Greenman for(; obj; obj=obj->backing_object) 1338a1f6d91cSDavid Greenman if( obj == object) { 1339a1f6d91cSDavid Greenman return 1; 1340a1f6d91cSDavid Greenman } 1341a1f6d91cSDavid Greenman } 1342a1f6d91cSDavid Greenman return 0; 1343a1f6d91cSDavid Greenman } 1344a1f6d91cSDavid Greenman 1345a1f6d91cSDavid Greenman int 1346a1f6d91cSDavid Greenman vm_object_in_map( object) 1347a1f6d91cSDavid Greenman vm_object_t object; 1348a1f6d91cSDavid Greenman { 1349a1f6d91cSDavid Greenman struct proc *p; 1350a1f6d91cSDavid Greenman for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 1351a1f6d91cSDavid Greenman if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 1352a1f6d91cSDavid Greenman continue; 1353a1f6d91cSDavid Greenman /* 1354a1f6d91cSDavid Greenman if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 1355a1f6d91cSDavid Greenman continue; 1356a1f6d91cSDavid Greenman } 1357a1f6d91cSDavid Greenman */ 1358a1f6d91cSDavid Greenman if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) 1359a1f6d91cSDavid Greenman return 1; 1360a1f6d91cSDavid Greenman } 1361a1f6d91cSDavid Greenman if( _vm_object_in_map( kernel_map, object, 0)) 1362a1f6d91cSDavid Greenman return 1; 1363a1f6d91cSDavid Greenman if( _vm_object_in_map( kmem_map, object, 0)) 1364a1f6d91cSDavid Greenman return 1; 1365a1f6d91cSDavid Greenman if( _vm_object_in_map( pager_map, object, 0)) 1366a1f6d91cSDavid Greenman return 1; 1367a1f6d91cSDavid Greenman if( _vm_object_in_map( buffer_map, object, 0)) 1368a1f6d91cSDavid Greenman return 1; 1369a1f6d91cSDavid Greenman if( _vm_object_in_map( io_map, object, 0)) 1370a1f6d91cSDavid Greenman return 1; 1371a1f6d91cSDavid Greenman if( _vm_object_in_map( phys_map, object, 0)) 1372a1f6d91cSDavid Greenman return 1; 1373a1f6d91cSDavid Greenman if( _vm_object_in_map( mb_map, object, 0)) 1374a1f6d91cSDavid Greenman return 1; 1375a1f6d91cSDavid Greenman if( _vm_object_in_map( u_map, object, 0)) 1376a1f6d91cSDavid Greenman return 1; 1377a1f6d91cSDavid Greenman return 0; 1378a1f6d91cSDavid Greenman } 1379a1f6d91cSDavid Greenman 1380c3cb3e12SDavid Greenman 1381a1f6d91cSDavid Greenman void 1382a1f6d91cSDavid Greenman vm_object_check() { 1383a1f6d91cSDavid Greenman int i; 1384a1f6d91cSDavid Greenman int maxhash = 0; 1385a1f6d91cSDavid Greenman vm_object_t object; 1386a1f6d91cSDavid Greenman 1387a1f6d91cSDavid Greenman /* 1388a1f6d91cSDavid Greenman * make sure that internal objs are in a map somewhere 1389a1f6d91cSDavid Greenman * and none have zero ref counts. 1390a1f6d91cSDavid Greenman */ 1391a1f6d91cSDavid Greenman for (object = vm_object_list.tqh_first; 1392a1f6d91cSDavid Greenman object != NULL; 1393a1f6d91cSDavid Greenman object = object->object_list.tqe_next) { 139424a1cce3SDavid Greenman if (object->handle == NULL && 139524a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 1396a1f6d91cSDavid Greenman if (object->ref_count == 0) { 1397a1f6d91cSDavid Greenman printf("vmochk: internal obj has zero ref count: %d\n", 1398a1f6d91cSDavid Greenman object->size); 1399a1f6d91cSDavid Greenman } 1400a1f6d91cSDavid Greenman if (!vm_object_in_map(object)) { 14012a4895f4SDavid Greenman printf("vmochk: internal obj is not in a map: ref: %d, size: %d: 0x%x, backing_object: 0x%x\n", 14022a4895f4SDavid Greenman object->ref_count, object->size, object->backing_object); 1403a1f6d91cSDavid Greenman } 1404a1f6d91cSDavid Greenman } 1405a1f6d91cSDavid Greenman } 1406a1f6d91cSDavid Greenman } 1407a1f6d91cSDavid Greenman 140826f9a767SRodney W. Grimes /* 1409df8bae1dSRodney W. Grimes * vm_object_print: [ debug ] 1410df8bae1dSRodney W. Grimes */ 14110d94caffSDavid Greenman void 14120d94caffSDavid Greenman vm_object_print(object, full) 1413df8bae1dSRodney W. Grimes vm_object_t object; 1414df8bae1dSRodney W. Grimes boolean_t full; 1415df8bae1dSRodney W. Grimes { 1416df8bae1dSRodney W. Grimes register vm_page_t p; 1417df8bae1dSRodney W. Grimes 1418df8bae1dSRodney W. Grimes register int count; 1419df8bae1dSRodney W. Grimes 1420df8bae1dSRodney W. Grimes if (object == NULL) 1421df8bae1dSRodney W. Grimes return; 1422df8bae1dSRodney W. Grimes 1423df8bae1dSRodney W. Grimes iprintf("Object 0x%x: size=0x%x, res=%d, ref=%d, ", 1424df8bae1dSRodney W. Grimes (int) object, (int) object->size, 1425df8bae1dSRodney W. Grimes object->resident_page_count, object->ref_count); 14262a4895f4SDavid Greenman printf("offset=0x%x, backing_object=(0x%x)+0x%x\n", 14272a4895f4SDavid Greenman (int) object->paging_offset, 142824a1cce3SDavid Greenman (int) object->backing_object, (int) object->backing_object_offset); 142905f0fdd2SPoul-Henning Kamp printf("cache: next=%p, prev=%p\n", 1430df8bae1dSRodney W. Grimes object->cached_list.tqe_next, object->cached_list.tqe_prev); 1431df8bae1dSRodney W. Grimes 1432df8bae1dSRodney W. Grimes if (!full) 1433df8bae1dSRodney W. Grimes return; 1434df8bae1dSRodney W. Grimes 1435df8bae1dSRodney W. Grimes indent += 2; 1436df8bae1dSRodney W. Grimes count = 0; 1437df8bae1dSRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) { 1438df8bae1dSRodney W. Grimes if (count == 0) 1439df8bae1dSRodney W. Grimes iprintf("memory:="); 1440df8bae1dSRodney W. Grimes else if (count == 6) { 1441df8bae1dSRodney W. Grimes printf("\n"); 1442df8bae1dSRodney W. Grimes iprintf(" ..."); 1443df8bae1dSRodney W. Grimes count = 0; 1444df8bae1dSRodney W. Grimes } else 1445df8bae1dSRodney W. Grimes printf(","); 1446df8bae1dSRodney W. Grimes count++; 1447df8bae1dSRodney W. Grimes 144805f0fdd2SPoul-Henning Kamp printf("(off=0x%lx,page=0x%lx)", 144905f0fdd2SPoul-Henning Kamp (u_long) p->offset, (u_long) VM_PAGE_TO_PHYS(p)); 1450df8bae1dSRodney W. Grimes } 1451df8bae1dSRodney W. Grimes if (count != 0) 1452df8bae1dSRodney W. Grimes printf("\n"); 1453df8bae1dSRodney W. Grimes indent -= 2; 1454df8bae1dSRodney W. Grimes } 1455c3cb3e12SDavid Greenman #endif /* DDB */ 1456