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 * 64f708ef1bSPoul-Henning Kamp * $Id: vm_object.c,v 1.59 1995/12/11 04:58:20 dyson 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> 78efeaf95aSDavid Greenman #include <sys/vmmeter.h> 79df8bae1dSRodney W. Grimes 80df8bae1dSRodney W. Grimes #include <vm/vm.h> 81efeaf95aSDavid Greenman #include <vm/vm_param.h> 82efeaf95aSDavid Greenman #include <vm/vm_prot.h> 83efeaf95aSDavid Greenman #include <vm/lock.h> 84efeaf95aSDavid Greenman #include <vm/pmap.h> 85efeaf95aSDavid Greenman #include <vm/vm_map.h> 86efeaf95aSDavid Greenman #include <vm/vm_object.h> 87df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 8826f9a767SRodney W. Grimes #include <vm/vm_pageout.h> 890d94caffSDavid Greenman #include <vm/vm_pager.h> 9005f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 91a1f6d91cSDavid Greenman #include <vm/vm_kern.h> 92efeaf95aSDavid Greenman #include <vm/vm_extern.h> 9326f9a767SRodney W. Grimes 94cac597e4SBruce Evans #ifdef DDB 95f708ef1bSPoul-Henning Kamp static void DDB_vm_object_check __P((void)); 96cac597e4SBruce Evans #endif 97df8bae1dSRodney W. Grimes 98cac597e4SBruce Evans static void _vm_object_allocate __P((objtype_t, vm_size_t, vm_object_t)); 99cac597e4SBruce Evans #ifdef DDB 100cac597e4SBruce Evans static int _vm_object_in_map __P((vm_map_t map, vm_object_t object, 101cac597e4SBruce Evans vm_map_entry_t entry)); 102cac597e4SBruce Evans static int vm_object_in_map __P((vm_object_t object)); 103cac597e4SBruce Evans #endif 104cac597e4SBruce Evans static void vm_object_qcollapse __P((vm_object_t object)); 105f708ef1bSPoul-Henning Kamp #ifdef not_used 106f708ef1bSPoul-Henning Kamp static void vm_object_deactivate_pages __P((vm_object_t)); 107f708ef1bSPoul-Henning Kamp #endif 108f708ef1bSPoul-Henning Kamp static void vm_object_terminate __P((vm_object_t)); 109f708ef1bSPoul-Henning Kamp static void vm_object_cache_trim __P((void)); 110f6b04d2bSDavid Greenman 111df8bae1dSRodney W. Grimes /* 112df8bae1dSRodney W. Grimes * Virtual memory objects maintain the actual data 113df8bae1dSRodney W. Grimes * associated with allocated virtual memory. A given 114df8bae1dSRodney W. Grimes * page of memory exists within exactly one object. 115df8bae1dSRodney W. Grimes * 116df8bae1dSRodney W. Grimes * An object is only deallocated when all "references" 117df8bae1dSRodney W. Grimes * are given up. Only one "reference" to a given 118df8bae1dSRodney W. Grimes * region of an object should be writeable. 119df8bae1dSRodney W. Grimes * 120df8bae1dSRodney W. Grimes * Associated with each object is a list of all resident 121df8bae1dSRodney W. Grimes * memory pages belonging to that object; this list is 122df8bae1dSRodney W. Grimes * maintained by the "vm_page" module, and locked by the object's 123df8bae1dSRodney W. Grimes * lock. 124df8bae1dSRodney W. Grimes * 125df8bae1dSRodney W. Grimes * Each object also records a "pager" routine which is 126df8bae1dSRodney W. Grimes * used to retrieve (and store) pages to the proper backing 127df8bae1dSRodney W. Grimes * storage. In addition, objects may be backed by other 128df8bae1dSRodney W. Grimes * objects from which they were virtual-copied. 129df8bae1dSRodney W. Grimes * 130df8bae1dSRodney W. Grimes * The only items within the object structure which are 131df8bae1dSRodney W. Grimes * modified after time of creation are: 132df8bae1dSRodney W. Grimes * reference count locked by object's lock 133df8bae1dSRodney W. Grimes * pager routine locked by object's lock 134df8bae1dSRodney W. Grimes * 135df8bae1dSRodney W. Grimes */ 136df8bae1dSRodney W. Grimes 13728f8db14SBruce Evans int vm_object_cache_max; 13828f8db14SBruce Evans struct object_q vm_object_cached_list; 139f708ef1bSPoul-Henning Kamp static int vm_object_cached; 14028f8db14SBruce Evans struct object_q vm_object_list; 141f708ef1bSPoul-Henning Kamp static long vm_object_count; 14228f8db14SBruce Evans vm_object_t kernel_object; 14328f8db14SBruce Evans vm_object_t kmem_object; 144f708ef1bSPoul-Henning Kamp static struct vm_object kernel_object_store; 145f708ef1bSPoul-Henning Kamp static struct vm_object kmem_object_store; 146aef922f5SJohn Dyson extern int vm_pageout_page_count; 147df8bae1dSRodney W. Grimes 148f708ef1bSPoul-Henning Kamp static long object_collapses; 149f708ef1bSPoul-Henning Kamp static long object_bypasses; 150df8bae1dSRodney W. Grimes 151df8bae1dSRodney W. Grimes static void 15224a1cce3SDavid Greenman _vm_object_allocate(type, size, object) 15324a1cce3SDavid Greenman objtype_t type; 154df8bae1dSRodney W. Grimes vm_size_t size; 155df8bae1dSRodney W. Grimes register vm_object_t object; 156df8bae1dSRodney W. Grimes { 157df8bae1dSRodney W. Grimes TAILQ_INIT(&object->memq); 15824a1cce3SDavid Greenman TAILQ_INIT(&object->shadow_head); 159a1f6d91cSDavid Greenman 16024a1cce3SDavid Greenman object->type = type; 161df8bae1dSRodney W. Grimes object->size = size; 162a1f6d91cSDavid Greenman object->ref_count = 1; 16324a1cce3SDavid Greenman object->flags = 0; 164df8bae1dSRodney W. Grimes object->paging_in_progress = 0; 165a1f6d91cSDavid Greenman object->resident_page_count = 0; 16624a1cce3SDavid Greenman object->handle = NULL; 167a316d390SJohn Dyson object->paging_offset = (vm_ooffset_t) 0; 16824a1cce3SDavid Greenman object->backing_object = NULL; 169a316d390SJohn Dyson object->backing_object_offset = (vm_ooffset_t) 0; 170a1f6d91cSDavid Greenman 171a1f6d91cSDavid Greenman object->last_read = 0; 172df8bae1dSRodney W. Grimes 173df8bae1dSRodney W. Grimes TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 174df8bae1dSRodney W. Grimes vm_object_count++; 175df8bae1dSRodney W. Grimes } 176df8bae1dSRodney W. Grimes 177df8bae1dSRodney W. Grimes /* 17826f9a767SRodney W. Grimes * vm_object_init: 17926f9a767SRodney W. Grimes * 18026f9a767SRodney W. Grimes * Initialize the VM objects module. 18126f9a767SRodney W. Grimes */ 18226f9a767SRodney W. Grimes void 183a316d390SJohn Dyson vm_object_init() 18426f9a767SRodney W. Grimes { 18526f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_cached_list); 18626f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_list); 18726f9a767SRodney W. Grimes vm_object_count = 0; 1880217125fSDavid Greenman 1890217125fSDavid Greenman vm_object_cache_max = 84; 1900217125fSDavid Greenman if (cnt.v_page_count > 1000) 1910217125fSDavid Greenman vm_object_cache_max += (cnt.v_page_count - 1000) / 4; 19226f9a767SRodney W. Grimes 19326f9a767SRodney W. Grimes kernel_object = &kernel_object_store; 194a316d390SJohn Dyson _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 19526f9a767SRodney W. Grimes kernel_object); 19626f9a767SRodney W. Grimes 19726f9a767SRodney W. Grimes kmem_object = &kmem_object_store; 198a316d390SJohn Dyson _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 19926f9a767SRodney W. Grimes kmem_object); 20026f9a767SRodney W. Grimes } 20126f9a767SRodney W. Grimes 20226f9a767SRodney W. Grimes /* 20326f9a767SRodney W. Grimes * vm_object_allocate: 20426f9a767SRodney W. Grimes * 20526f9a767SRodney W. Grimes * Returns a new object with the given size. 20626f9a767SRodney W. Grimes */ 20726f9a767SRodney W. Grimes 20826f9a767SRodney W. Grimes vm_object_t 20924a1cce3SDavid Greenman vm_object_allocate(type, size) 21024a1cce3SDavid Greenman objtype_t type; 21126f9a767SRodney W. Grimes vm_size_t size; 21226f9a767SRodney W. Grimes { 21326f9a767SRodney W. Grimes register vm_object_t result; 21426f9a767SRodney W. Grimes 21526f9a767SRodney W. Grimes result = (vm_object_t) 21626f9a767SRodney W. Grimes malloc((u_long) sizeof *result, M_VMOBJ, M_WAITOK); 21726f9a767SRodney W. Grimes 21826f9a767SRodney W. Grimes 21924a1cce3SDavid Greenman _vm_object_allocate(type, size, result); 22026f9a767SRodney W. Grimes 22126f9a767SRodney W. Grimes return (result); 22226f9a767SRodney W. Grimes } 22326f9a767SRodney W. Grimes 22426f9a767SRodney W. Grimes 22526f9a767SRodney W. Grimes /* 226df8bae1dSRodney W. Grimes * vm_object_reference: 227df8bae1dSRodney W. Grimes * 228df8bae1dSRodney W. Grimes * Gets another reference to the given object. 229df8bae1dSRodney W. Grimes */ 23026f9a767SRodney W. Grimes inline void 23126f9a767SRodney W. Grimes vm_object_reference(object) 232df8bae1dSRodney W. Grimes register vm_object_t object; 233df8bae1dSRodney W. Grimes { 234df8bae1dSRodney W. Grimes if (object == NULL) 235df8bae1dSRodney W. Grimes return; 236df8bae1dSRodney W. Grimes 23724a1cce3SDavid Greenman if (object->ref_count == 0) { 23824a1cce3SDavid Greenman if ((object->flags & OBJ_CANPERSIST) == 0) 23924a1cce3SDavid Greenman panic("vm_object_reference: non-persistent object with 0 ref_count"); 24024a1cce3SDavid Greenman TAILQ_REMOVE(&vm_object_cached_list, object, cached_list); 24124a1cce3SDavid Greenman vm_object_cached--; 24224a1cce3SDavid Greenman } 243df8bae1dSRodney W. Grimes object->ref_count++; 244df8bae1dSRodney W. Grimes } 245df8bae1dSRodney W. Grimes 246df8bae1dSRodney W. Grimes /* 247df8bae1dSRodney W. Grimes * vm_object_deallocate: 248df8bae1dSRodney W. Grimes * 249df8bae1dSRodney W. Grimes * Release a reference to the specified object, 250df8bae1dSRodney W. Grimes * gained either through a vm_object_allocate 251df8bae1dSRodney W. Grimes * or a vm_object_reference call. When all references 252df8bae1dSRodney W. Grimes * are gone, storage associated with this object 253df8bae1dSRodney W. Grimes * may be relinquished. 254df8bae1dSRodney W. Grimes * 255df8bae1dSRodney W. Grimes * No object may be locked. 256df8bae1dSRodney W. Grimes */ 25726f9a767SRodney W. Grimes void 25826f9a767SRodney W. Grimes vm_object_deallocate(object) 25926f9a767SRodney W. Grimes vm_object_t object; 260df8bae1dSRodney W. Grimes { 261df8bae1dSRodney W. Grimes vm_object_t temp; 262df8bae1dSRodney W. Grimes 263df8bae1dSRodney W. Grimes while (object != NULL) { 264df8bae1dSRodney W. Grimes 265ba8da839SDavid Greenman if (object->ref_count == 0) 266ba8da839SDavid Greenman panic("vm_object_deallocate: object deallocated too many times"); 267ba8da839SDavid Greenman 268df8bae1dSRodney W. Grimes /* 269df8bae1dSRodney W. Grimes * Lose the reference 270df8bae1dSRodney W. Grimes */ 271be6d5bfaSDavid Greenman object->ref_count--; 272be6d5bfaSDavid Greenman 273be6d5bfaSDavid Greenman if (object->ref_count != 0) { 274be6d5bfaSDavid Greenman if ((object->ref_count == 1) && 27524a1cce3SDavid Greenman (object->handle == NULL) && 27624a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || 27724a1cce3SDavid Greenman object->type == OBJT_SWAP)) { 278a1f6d91cSDavid Greenman vm_object_t robject; 27924a1cce3SDavid Greenman robject = object->shadow_head.tqh_first; 280be6d5bfaSDavid Greenman if ((robject != NULL) && 28124a1cce3SDavid Greenman (robject->handle == NULL) && 28224a1cce3SDavid Greenman (robject->type == OBJT_DEFAULT || 28324a1cce3SDavid Greenman robject->type == OBJT_SWAP)) { 284a1f6d91cSDavid Greenman int s; 285a1f6d91cSDavid Greenman robject->ref_count += 2; 2869b4814bbSDavid Greenman object->ref_count += 2; 287a1f6d91cSDavid Greenman 288a1f6d91cSDavid Greenman do { 289a1f6d91cSDavid Greenman s = splhigh(); 290a1f6d91cSDavid Greenman while (robject->paging_in_progress) { 291c0503609SDavid Greenman robject->flags |= OBJ_PIPWNT; 292a1f6d91cSDavid Greenman tsleep(robject, PVM, "objde1", 0); 2930d94caffSDavid Greenman } 294a1f6d91cSDavid Greenman 295a1f6d91cSDavid Greenman while (object->paging_in_progress) { 296c0503609SDavid Greenman object->flags |= OBJ_PIPWNT; 297a1f6d91cSDavid Greenman tsleep(object, PVM, "objde2", 0); 298a1f6d91cSDavid Greenman } 299a1f6d91cSDavid Greenman splx(s); 300a1f6d91cSDavid Greenman 301a1f6d91cSDavid Greenman } while( object->paging_in_progress || robject->paging_in_progress); 302a1f6d91cSDavid Greenman 3039b4814bbSDavid Greenman object->ref_count -= 2; 304a1f6d91cSDavid Greenman robject->ref_count -= 2; 305a1f6d91cSDavid Greenman if( robject->ref_count == 0) { 306a1f6d91cSDavid Greenman robject->ref_count += 1; 307ba8da839SDavid Greenman object = robject; 308ba8da839SDavid Greenman continue; 309a1f6d91cSDavid Greenman } 310a1f6d91cSDavid Greenman vm_object_collapse(robject); 311a1f6d91cSDavid Greenman return; 312010cf3b9SDavid Greenman } 313010cf3b9SDavid Greenman } 314df8bae1dSRodney W. Grimes /* 3150d94caffSDavid Greenman * If there are still references, then we are done. 316df8bae1dSRodney W. Grimes */ 317df8bae1dSRodney W. Grimes return; 318df8bae1dSRodney W. Grimes } 319df8bae1dSRodney W. Grimes 32024a1cce3SDavid Greenman if (object->type == OBJT_VNODE) { 32124a1cce3SDavid Greenman struct vnode *vp = object->handle; 322f5cf85d4SDavid Greenman 32324a1cce3SDavid Greenman vp->v_flag &= ~VTEXT; 324f5cf85d4SDavid Greenman } 325f5cf85d4SDavid Greenman 326f919ebdeSDavid Greenman /* 327f919ebdeSDavid Greenman * See if this object can persist and has some resident 328f919ebdeSDavid Greenman * pages. If so, enter it in the cache. 329f919ebdeSDavid Greenman */ 330f5cf85d4SDavid Greenman if (object->flags & OBJ_CANPERSIST) { 331f5cf85d4SDavid Greenman if (object->resident_page_count != 0) { 33224a1cce3SDavid Greenman vm_object_page_clean(object, 0, 0 ,TRUE, TRUE); 333df8bae1dSRodney W. Grimes TAILQ_INSERT_TAIL(&vm_object_cached_list, object, 334df8bae1dSRodney W. Grimes cached_list); 335df8bae1dSRodney W. Grimes vm_object_cached++; 336df8bae1dSRodney W. Grimes 337df8bae1dSRodney W. Grimes vm_object_cache_trim(); 338df8bae1dSRodney W. Grimes return; 339f5cf85d4SDavid Greenman } else { 340f5cf85d4SDavid Greenman object->flags &= ~OBJ_CANPERSIST; 341f5cf85d4SDavid Greenman } 342df8bae1dSRodney W. Grimes } 343f919ebdeSDavid Greenman 344df8bae1dSRodney W. Grimes /* 34524a1cce3SDavid Greenman * Make sure no one uses us. 346df8bae1dSRodney W. Grimes */ 3470d94caffSDavid Greenman object->flags |= OBJ_DEAD; 348df8bae1dSRodney W. Grimes 34924a1cce3SDavid Greenman temp = object->backing_object; 3502fe6e4d7SDavid Greenman if (temp) 35124a1cce3SDavid Greenman TAILQ_REMOVE(&temp->shadow_head, object, shadow_list); 352df8bae1dSRodney W. Grimes vm_object_terminate(object); 353df8bae1dSRodney W. Grimes /* unlocks and deallocates object */ 354df8bae1dSRodney W. Grimes object = temp; 355df8bae1dSRodney W. Grimes } 356df8bae1dSRodney W. Grimes } 357df8bae1dSRodney W. Grimes 358df8bae1dSRodney W. Grimes /* 359df8bae1dSRodney W. Grimes * vm_object_terminate actually destroys the specified object, freeing 360df8bae1dSRodney W. Grimes * up all previously used resources. 361df8bae1dSRodney W. Grimes * 362df8bae1dSRodney W. Grimes * The object must be locked. 363df8bae1dSRodney W. Grimes */ 364f708ef1bSPoul-Henning Kamp static void 36526f9a767SRodney W. Grimes vm_object_terminate(object) 366df8bae1dSRodney W. Grimes register vm_object_t object; 367df8bae1dSRodney W. Grimes { 3683af76890SPoul-Henning Kamp register vm_page_t p; 36926f9a767SRodney W. Grimes int s; 370df8bae1dSRodney W. Grimes 371df8bae1dSRodney W. Grimes /* 372f6b04d2bSDavid Greenman * wait for the pageout daemon to be done with the object 373df8bae1dSRodney W. Grimes */ 3740d94caffSDavid Greenman s = splhigh(); 375df8bae1dSRodney W. Grimes while (object->paging_in_progress) { 376c0503609SDavid Greenman object->flags |= OBJ_PIPWNT; 37724a1cce3SDavid Greenman tsleep(object, PVM, "objtrm", 0); 378df8bae1dSRodney W. Grimes } 3790d94caffSDavid Greenman splx(s); 380df8bae1dSRodney W. Grimes 38126f9a767SRodney W. Grimes if (object->paging_in_progress != 0) 38226f9a767SRodney W. Grimes panic("vm_object_deallocate: pageout in progress"); 38326f9a767SRodney W. Grimes 38426f9a767SRodney W. Grimes /* 3850d94caffSDavid Greenman * Clean and free the pages, as appropriate. All references to the 3860d94caffSDavid Greenman * object are gone, so we don't need to lock it. 38726f9a767SRodney W. Grimes */ 38824a1cce3SDavid Greenman if (object->type == OBJT_VNODE) { 38924a1cce3SDavid Greenman struct vnode *vp = object->handle; 390f6b04d2bSDavid Greenman 3910426122fSDavid Greenman VOP_LOCK(vp); 39224a1cce3SDavid Greenman vm_object_page_clean(object, 0, 0, TRUE, FALSE); 393f6b04d2bSDavid Greenman vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0); 3940426122fSDavid Greenman VOP_UNLOCK(vp); 395df8bae1dSRodney W. Grimes } 39683edfd47SDavid Greenman 3970d94caffSDavid Greenman /* 3980d94caffSDavid Greenman * Now free the pages. For internal objects, this also removes them 3990d94caffSDavid Greenman * from paging queues. 400df8bae1dSRodney W. Grimes */ 401df8bae1dSRodney W. Grimes while ((p = object->memq.tqh_first) != NULL) { 402be6d5bfaSDavid Greenman if (p->flags & PG_BUSY) 403be6d5bfaSDavid Greenman printf("vm_object_terminate: freeing busy page\n"); 4040d94caffSDavid Greenman PAGE_WAKEUP(p); 405df8bae1dSRodney W. Grimes vm_page_free(p); 406df8bae1dSRodney W. Grimes cnt.v_pfree++; 407df8bae1dSRodney W. Grimes } 408df8bae1dSRodney W. Grimes 409df8bae1dSRodney W. Grimes /* 410df8bae1dSRodney W. Grimes * Let the pager know object is dead. 411df8bae1dSRodney W. Grimes */ 41224a1cce3SDavid Greenman vm_pager_deallocate(object); 413df8bae1dSRodney W. Grimes 414df8bae1dSRodney W. Grimes TAILQ_REMOVE(&vm_object_list, object, object_list); 415df8bae1dSRodney W. Grimes vm_object_count--; 416df8bae1dSRodney W. Grimes 4170426122fSDavid Greenman wakeup(object); 4180426122fSDavid Greenman 419df8bae1dSRodney W. Grimes /* 420df8bae1dSRodney W. Grimes * Free the space for the object. 421df8bae1dSRodney W. Grimes */ 422df8bae1dSRodney W. Grimes free((caddr_t) object, M_VMOBJ); 423df8bae1dSRodney W. Grimes } 424df8bae1dSRodney W. Grimes 425df8bae1dSRodney W. Grimes /* 426df8bae1dSRodney W. Grimes * vm_object_page_clean 427df8bae1dSRodney W. Grimes * 428df8bae1dSRodney W. Grimes * Clean all dirty pages in the specified range of object. 42926f9a767SRodney W. Grimes * Leaves page on whatever queue it is currently on. 43026f9a767SRodney W. Grimes * 43126f9a767SRodney W. Grimes * Odd semantics: if start == end, we clean everything. 43226f9a767SRodney W. Grimes * 43326f9a767SRodney W. Grimes * The object must be locked. 43426f9a767SRodney W. Grimes */ 435f6b04d2bSDavid Greenman 436f6b04d2bSDavid Greenman void 43724a1cce3SDavid Greenman vm_object_page_clean(object, start, end, syncio, lockflag) 438f6b04d2bSDavid Greenman vm_object_t object; 439a316d390SJohn Dyson vm_pindex_t start; 440a316d390SJohn Dyson vm_pindex_t end; 441f6b04d2bSDavid Greenman boolean_t syncio; 44224a1cce3SDavid Greenman boolean_t lockflag; 443f6b04d2bSDavid Greenman { 444f6b04d2bSDavid Greenman register vm_page_t p; 445f6b04d2bSDavid Greenman register vm_offset_t tstart, tend; 446aef922f5SJohn Dyson int s; 44724a1cce3SDavid Greenman struct vnode *vp; 448aef922f5SJohn Dyson int runlen; 449aef922f5SJohn Dyson vm_page_t ma[vm_pageout_page_count]; 450f6b04d2bSDavid Greenman 451aef922f5SJohn Dyson if (object->type != OBJT_VNODE || 452aef922f5SJohn Dyson (object->flags & OBJ_MIGHTBEDIRTY) == 0) 453f6b04d2bSDavid Greenman return; 454f6b04d2bSDavid Greenman 45524a1cce3SDavid Greenman vp = object->handle; 45624a1cce3SDavid Greenman 45724a1cce3SDavid Greenman if (lockflag) 45824a1cce3SDavid Greenman VOP_LOCK(vp); 459aef922f5SJohn Dyson object->flags |= OBJ_CLEANING; 46024a1cce3SDavid Greenman 461f6b04d2bSDavid Greenman tstart = start; 462f6b04d2bSDavid Greenman if (end == 0) { 463f6b04d2bSDavid Greenman tend = object->size; 464f6b04d2bSDavid Greenman } else { 465f6b04d2bSDavid Greenman tend = end; 466f6b04d2bSDavid Greenman } 467a316d390SJohn Dyson if ((tstart == 0) && (tend == object->size)) { 468aef922f5SJohn Dyson object->flags &= ~(OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY); 469ec4f9fb0SDavid Greenman } 470f6b04d2bSDavid Greenman 471aef922f5SJohn Dyson runlen = 0; 472a316d390SJohn Dyson for(;tstart < tend; tstart += 1) { 473aef922f5SJohn Dyson relookup: 474f6b04d2bSDavid Greenman p = vm_page_lookup(object, tstart); 475aef922f5SJohn Dyson if (!p) { 476aef922f5SJohn Dyson if (runlen > 0) { 477aef922f5SJohn Dyson vm_pageout_flush(ma, runlen, syncio); 478aef922f5SJohn Dyson runlen = 0; 479aef922f5SJohn Dyson } 480f6b04d2bSDavid Greenman continue; 481f6b04d2bSDavid Greenman } 482a316d390SJohn Dyson if ((p->valid == 0) || (p->flags & PG_CACHE)) { 483aef922f5SJohn Dyson if (runlen > 0) { 484aef922f5SJohn Dyson vm_pageout_flush(ma, runlen, syncio); 485aef922f5SJohn Dyson runlen = 0; 4867e15fd27SJohn Dyson } 487aef922f5SJohn Dyson continue; 488f6b04d2bSDavid Greenman } 489f6b04d2bSDavid Greenman 490ec4f9fb0SDavid Greenman vm_page_protect(p, VM_PROT_READ); 491ec4f9fb0SDavid Greenman 492f6b04d2bSDavid Greenman s = splhigh(); 493aef922f5SJohn Dyson while ((p->flags & PG_BUSY) || p->busy) { 494aef922f5SJohn Dyson if (runlen > 0) { 495f6b04d2bSDavid Greenman splx(s); 496aef922f5SJohn Dyson vm_pageout_flush(ma, runlen, syncio); 497aef922f5SJohn Dyson runlen = 0; 498aef922f5SJohn Dyson goto relookup; 499aef922f5SJohn Dyson } 500aef922f5SJohn Dyson p->flags |= PG_WANTED|PG_REFERENCED; 501aef922f5SJohn Dyson tsleep(p, PVM, "vpcwai", 0); 502f6b04d2bSDavid Greenman splx(s); 503aef922f5SJohn Dyson goto relookup; 504f6b04d2bSDavid Greenman } 505f6b04d2bSDavid Greenman splx(s); 506f6b04d2bSDavid Greenman 507aef922f5SJohn Dyson if (p->dirty == 0) 508f6b04d2bSDavid Greenman vm_page_test_dirty(p); 509aef922f5SJohn Dyson 510f6b04d2bSDavid Greenman if ((p->valid & p->dirty) != 0) { 511aef922f5SJohn Dyson ma[runlen] = p; 512aef922f5SJohn Dyson p->flags |= PG_BUSY; 513aef922f5SJohn Dyson runlen++; 514aef922f5SJohn Dyson if (runlen >= vm_pageout_page_count) { 515aef922f5SJohn Dyson vm_pageout_flush(ma, runlen, syncio); 516aef922f5SJohn Dyson runlen = 0; 517f6b04d2bSDavid Greenman } 518aef922f5SJohn Dyson } else if (runlen > 0) { 519aef922f5SJohn Dyson vm_pageout_flush(ma, runlen, syncio); 520aef922f5SJohn Dyson runlen = 0; 521f6b04d2bSDavid Greenman } 522aef922f5SJohn Dyson 523f6b04d2bSDavid Greenman } 524aef922f5SJohn Dyson if (runlen > 0) { 525aef922f5SJohn Dyson vm_pageout_flush(ma, runlen, syncio); 526f6b04d2bSDavid Greenman } 527aef922f5SJohn Dyson 528aef922f5SJohn Dyson VOP_FSYNC(vp, NULL, syncio, curproc); 529aef922f5SJohn Dyson 53024a1cce3SDavid Greenman if (lockflag) 531d3628763SRodney W. Grimes VOP_UNLOCK(vp); 532aef922f5SJohn Dyson object->flags &= ~OBJ_CLEANING; 533f5cf85d4SDavid Greenman return; 53426f9a767SRodney W. Grimes } 535df8bae1dSRodney W. Grimes 536f708ef1bSPoul-Henning Kamp #ifdef not_used 537f708ef1bSPoul-Henning Kamp /* XXX I cannot tell if this should be an exported symbol */ 538df8bae1dSRodney W. Grimes /* 539df8bae1dSRodney W. Grimes * vm_object_deactivate_pages 540df8bae1dSRodney W. Grimes * 541df8bae1dSRodney W. Grimes * Deactivate all pages in the specified object. (Keep its pages 542df8bae1dSRodney W. Grimes * in memory even though it is no longer referenced.) 543df8bae1dSRodney W. Grimes * 544df8bae1dSRodney W. Grimes * The object must be locked. 545df8bae1dSRodney W. Grimes */ 546f708ef1bSPoul-Henning Kamp static void 547df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object) 548df8bae1dSRodney W. Grimes register vm_object_t object; 549df8bae1dSRodney W. Grimes { 550df8bae1dSRodney W. Grimes register vm_page_t p, next; 551df8bae1dSRodney W. Grimes 552df8bae1dSRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = next) { 553df8bae1dSRodney W. Grimes next = p->listq.tqe_next; 554df8bae1dSRodney W. Grimes vm_page_deactivate(p); 555df8bae1dSRodney W. Grimes } 556df8bae1dSRodney W. Grimes } 557f708ef1bSPoul-Henning Kamp #endif 558df8bae1dSRodney W. Grimes 559df8bae1dSRodney W. Grimes /* 560df8bae1dSRodney W. Grimes * Trim the object cache to size. 561df8bae1dSRodney W. Grimes */ 562f708ef1bSPoul-Henning Kamp static void 563df8bae1dSRodney W. Grimes vm_object_cache_trim() 564df8bae1dSRodney W. Grimes { 565df8bae1dSRodney W. Grimes register vm_object_t object; 566df8bae1dSRodney W. Grimes 5670d94caffSDavid Greenman while (vm_object_cached > vm_object_cache_max) { 568df8bae1dSRodney W. Grimes object = vm_object_cached_list.tqh_first; 569df8bae1dSRodney W. Grimes 57024a1cce3SDavid Greenman vm_object_reference(object); 571df8bae1dSRodney W. Grimes pager_cache(object, FALSE); 572df8bae1dSRodney W. Grimes } 573df8bae1dSRodney W. Grimes } 574df8bae1dSRodney W. Grimes 57526f9a767SRodney W. Grimes 576df8bae1dSRodney W. Grimes /* 577df8bae1dSRodney W. Grimes * vm_object_pmap_copy: 578df8bae1dSRodney W. Grimes * 579df8bae1dSRodney W. Grimes * Makes all physical pages in the specified 580df8bae1dSRodney W. Grimes * object range copy-on-write. No writeable 581df8bae1dSRodney W. Grimes * references to these pages should remain. 582df8bae1dSRodney W. Grimes * 583df8bae1dSRodney W. Grimes * The object must *not* be locked. 584df8bae1dSRodney W. Grimes */ 5850d94caffSDavid Greenman void 5860d94caffSDavid Greenman vm_object_pmap_copy(object, start, end) 587df8bae1dSRodney W. Grimes register vm_object_t object; 588a316d390SJohn Dyson register vm_pindex_t start; 589a316d390SJohn Dyson register vm_pindex_t end; 590df8bae1dSRodney W. Grimes { 591df8bae1dSRodney W. Grimes register vm_page_t p; 592df8bae1dSRodney W. Grimes 593aef922f5SJohn Dyson if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0) 594df8bae1dSRodney W. Grimes return; 595df8bae1dSRodney W. Grimes 596df8bae1dSRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) { 597f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_READ); 598df8bae1dSRodney W. Grimes } 599aef922f5SJohn Dyson 600aef922f5SJohn Dyson object->flags &= ~OBJ_WRITEABLE; 601df8bae1dSRodney W. Grimes } 602df8bae1dSRodney W. Grimes 603df8bae1dSRodney W. Grimes /* 604df8bae1dSRodney W. Grimes * vm_object_pmap_remove: 605df8bae1dSRodney W. Grimes * 606df8bae1dSRodney W. Grimes * Removes all physical pages in the specified 607df8bae1dSRodney W. Grimes * object range from all physical maps. 608df8bae1dSRodney W. Grimes * 609df8bae1dSRodney W. Grimes * The object must *not* be locked. 610df8bae1dSRodney W. Grimes */ 61126f9a767SRodney W. Grimes void 61226f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end) 613df8bae1dSRodney W. Grimes register vm_object_t object; 614a316d390SJohn Dyson register vm_pindex_t start; 615a316d390SJohn Dyson register vm_pindex_t end; 616df8bae1dSRodney W. Grimes { 617df8bae1dSRodney W. Grimes register vm_page_t p; 618df8bae1dSRodney W. Grimes if (object == NULL) 619df8bae1dSRodney W. Grimes return; 62026f9a767SRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) { 621f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 62226f9a767SRodney W. Grimes } 62326f9a767SRodney W. Grimes } 624df8bae1dSRodney W. Grimes 625df8bae1dSRodney W. Grimes /* 626df8bae1dSRodney W. Grimes * vm_object_copy: 627df8bae1dSRodney W. Grimes * 628df8bae1dSRodney W. Grimes * Create a new object which is a copy of an existing 629df8bae1dSRodney W. Grimes * object, and mark all of the pages in the existing 630df8bae1dSRodney W. Grimes * object 'copy-on-write'. The new object has one reference. 631df8bae1dSRodney W. Grimes * Returns the new object. 632df8bae1dSRodney W. Grimes * 633df8bae1dSRodney W. Grimes * May defer the copy until later if the object is not backed 634df8bae1dSRodney W. Grimes * up by a non-default pager. 635df8bae1dSRodney W. Grimes */ 6360d94caffSDavid Greenman void 637a316d390SJohn Dyson vm_object_copy(src_object, src_offset, 638df8bae1dSRodney W. Grimes dst_object, dst_offset, src_needs_copy) 639df8bae1dSRodney W. Grimes register vm_object_t src_object; 640a316d390SJohn Dyson vm_pindex_t src_offset; 641df8bae1dSRodney W. Grimes vm_object_t *dst_object;/* OUT */ 642a316d390SJohn Dyson vm_pindex_t *dst_offset;/* OUT */ 643df8bae1dSRodney W. Grimes boolean_t *src_needs_copy; /* OUT */ 644df8bae1dSRodney W. Grimes { 645df8bae1dSRodney W. Grimes if (src_object == NULL) { 646df8bae1dSRodney W. Grimes /* 647df8bae1dSRodney W. Grimes * Nothing to copy 648df8bae1dSRodney W. Grimes */ 649df8bae1dSRodney W. Grimes *dst_object = NULL; 650df8bae1dSRodney W. Grimes *dst_offset = 0; 651df8bae1dSRodney W. Grimes *src_needs_copy = FALSE; 652df8bae1dSRodney W. Grimes return; 653df8bae1dSRodney W. Grimes } 65426f9a767SRodney W. Grimes 65526f9a767SRodney W. Grimes /* 65626f9a767SRodney W. Grimes * Try to collapse the object before copying it. 65726f9a767SRodney W. Grimes */ 65824a1cce3SDavid Greenman if (src_object->handle == NULL && 65924a1cce3SDavid Greenman (src_object->type == OBJT_DEFAULT || 66024a1cce3SDavid Greenman src_object->type == OBJT_SWAP)) 66126f9a767SRodney W. Grimes vm_object_collapse(src_object); 66226f9a767SRodney W. Grimes 663df8bae1dSRodney W. Grimes 664df8bae1dSRodney W. Grimes /* 665df8bae1dSRodney W. Grimes * Make another reference to the object 666df8bae1dSRodney W. Grimes */ 667df8bae1dSRodney W. Grimes src_object->ref_count++; 668df8bae1dSRodney W. Grimes 669df8bae1dSRodney W. Grimes *dst_object = src_object; 670df8bae1dSRodney W. Grimes *dst_offset = src_offset; 671df8bae1dSRodney W. Grimes 672df8bae1dSRodney W. Grimes /* 673df8bae1dSRodney W. Grimes * Must make a shadow when write is desired 674df8bae1dSRodney W. Grimes */ 675df8bae1dSRodney W. Grimes *src_needs_copy = TRUE; 676df8bae1dSRodney W. Grimes return; 677df8bae1dSRodney W. Grimes } 678df8bae1dSRodney W. Grimes 679df8bae1dSRodney W. Grimes /* 680df8bae1dSRodney W. Grimes * vm_object_shadow: 681df8bae1dSRodney W. Grimes * 682df8bae1dSRodney W. Grimes * Create a new object which is backed by the 683df8bae1dSRodney W. Grimes * specified existing object range. The source 684df8bae1dSRodney W. Grimes * object reference is deallocated. 685df8bae1dSRodney W. Grimes * 686df8bae1dSRodney W. Grimes * The new object and offset into that object 687df8bae1dSRodney W. Grimes * are returned in the source parameters. 688df8bae1dSRodney W. Grimes */ 689df8bae1dSRodney W. Grimes 69026f9a767SRodney W. Grimes void 69126f9a767SRodney W. Grimes vm_object_shadow(object, offset, length) 692df8bae1dSRodney W. Grimes vm_object_t *object; /* IN/OUT */ 693a316d390SJohn Dyson vm_ooffset_t *offset; /* IN/OUT */ 694df8bae1dSRodney W. Grimes vm_size_t length; 695df8bae1dSRodney W. Grimes { 696df8bae1dSRodney W. Grimes register vm_object_t source; 697df8bae1dSRodney W. Grimes register vm_object_t result; 698df8bae1dSRodney W. Grimes 699df8bae1dSRodney W. Grimes source = *object; 700df8bae1dSRodney W. Grimes 701df8bae1dSRodney W. Grimes /* 702df8bae1dSRodney W. Grimes * Allocate a new object with the given length 703df8bae1dSRodney W. Grimes */ 704df8bae1dSRodney W. Grimes 70524a1cce3SDavid Greenman if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL) 706df8bae1dSRodney W. Grimes panic("vm_object_shadow: no object for shadowing"); 707df8bae1dSRodney W. Grimes 708df8bae1dSRodney W. Grimes /* 7090d94caffSDavid Greenman * The new object shadows the source object, adding a reference to it. 7100d94caffSDavid Greenman * Our caller changes his reference to point to the new object, 7110d94caffSDavid Greenman * removing a reference to the source object. Net result: no change 7120d94caffSDavid Greenman * of reference count. 713df8bae1dSRodney W. Grimes */ 71424a1cce3SDavid Greenman result->backing_object = source; 7157b18a718SDavid Greenman if (source) 71624a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&result->backing_object->shadow_head, result, shadow_list); 717df8bae1dSRodney W. Grimes 718df8bae1dSRodney W. Grimes /* 7190d94caffSDavid Greenman * Store the offset into the source object, and fix up the offset into 7200d94caffSDavid Greenman * the new object. 721df8bae1dSRodney W. Grimes */ 722df8bae1dSRodney W. Grimes 72324a1cce3SDavid Greenman result->backing_object_offset = *offset; 724df8bae1dSRodney W. Grimes 725df8bae1dSRodney W. Grimes /* 726df8bae1dSRodney W. Grimes * Return the new things 727df8bae1dSRodney W. Grimes */ 728df8bae1dSRodney W. Grimes 729df8bae1dSRodney W. Grimes *offset = 0; 730df8bae1dSRodney W. Grimes *object = result; 731df8bae1dSRodney W. Grimes } 732df8bae1dSRodney W. Grimes 733df8bae1dSRodney W. Grimes 734df8bae1dSRodney W. Grimes /* 7352fe6e4d7SDavid Greenman * this version of collapse allows the operation to occur earlier and 7362fe6e4d7SDavid Greenman * when paging_in_progress is true for an object... This is not a complete 7372fe6e4d7SDavid Greenman * operation, but should plug 99.9% of the rest of the leaks. 7382fe6e4d7SDavid Greenman */ 7392fe6e4d7SDavid Greenman static void 7402fe6e4d7SDavid Greenman vm_object_qcollapse(object) 7412fe6e4d7SDavid Greenman register vm_object_t object; 7422fe6e4d7SDavid Greenman { 7432fe6e4d7SDavid Greenman register vm_object_t backing_object; 744a316d390SJohn Dyson register vm_pindex_t backing_offset_index, paging_offset_index; 745a316d390SJohn Dyson vm_pindex_t backing_object_paging_offset_index; 746a316d390SJohn Dyson vm_pindex_t new_pindex; 7472fe6e4d7SDavid Greenman register vm_page_t p, pp; 7482fe6e4d7SDavid Greenman register vm_size_t size; 7492fe6e4d7SDavid Greenman 75024a1cce3SDavid Greenman backing_object = object->backing_object; 7512fe6e4d7SDavid Greenman if (backing_object->ref_count != 1) 7522fe6e4d7SDavid Greenman return; 7532fe6e4d7SDavid Greenman 754010cf3b9SDavid Greenman backing_object->ref_count += 2; 755010cf3b9SDavid Greenman 756a316d390SJohn Dyson backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 757a316d390SJohn Dyson backing_object_paging_offset_index = OFF_TO_IDX(backing_object->paging_offset); 758a316d390SJohn Dyson paging_offset_index = OFF_TO_IDX(object->paging_offset); 7592fe6e4d7SDavid Greenman size = object->size; 7602fe6e4d7SDavid Greenman p = backing_object->memq.tqh_first; 7612fe6e4d7SDavid Greenman while (p) { 7622fe6e4d7SDavid Greenman vm_page_t next; 7630d94caffSDavid Greenman 7642fe6e4d7SDavid Greenman next = p->listq.tqe_next; 7650d94caffSDavid Greenman if ((p->flags & (PG_BUSY | PG_FICTITIOUS | PG_CACHE)) || 766f6b04d2bSDavid Greenman !p->valid || p->hold_count || p->wire_count || p->busy) { 7672fe6e4d7SDavid Greenman p = next; 7682fe6e4d7SDavid Greenman continue; 7692fe6e4d7SDavid Greenman } 770f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 771a316d390SJohn Dyson new_pindex = p->pindex - backing_offset_index; 772a316d390SJohn Dyson if (p->pindex < backing_offset_index || 773a316d390SJohn Dyson new_pindex >= size) { 77424a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) 77524a1cce3SDavid Greenman swap_pager_freespace(backing_object, 776a316d390SJohn Dyson backing_object_paging_offset_index+p->pindex, 777a316d390SJohn Dyson 1); 7782fe6e4d7SDavid Greenman vm_page_free(p); 7792fe6e4d7SDavid Greenman } else { 780a316d390SJohn Dyson pp = vm_page_lookup(object, new_pindex); 78124a1cce3SDavid Greenman if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object, 782a316d390SJohn Dyson paging_offset_index + new_pindex, NULL, NULL))) { 78324a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) 78424a1cce3SDavid Greenman swap_pager_freespace(backing_object, 785a316d390SJohn Dyson backing_object_paging_offset_index + p->pindex, 1); 7862fe6e4d7SDavid Greenman vm_page_free(p); 7872fe6e4d7SDavid Greenman } else { 78824a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) 78924a1cce3SDavid Greenman swap_pager_freespace(backing_object, 790a316d390SJohn Dyson backing_object_paging_offset_index + p->pindex, 1); 791a316d390SJohn Dyson vm_page_rename(p, object, new_pindex); 7929b4814bbSDavid Greenman p->dirty = VM_PAGE_BITS_ALL; 7932fe6e4d7SDavid Greenman } 7942fe6e4d7SDavid Greenman } 7952fe6e4d7SDavid Greenman p = next; 7962fe6e4d7SDavid Greenman } 797010cf3b9SDavid Greenman backing_object->ref_count -= 2; 7982fe6e4d7SDavid Greenman } 7992fe6e4d7SDavid Greenman 800df8bae1dSRodney W. Grimes /* 801df8bae1dSRodney W. Grimes * vm_object_collapse: 802df8bae1dSRodney W. Grimes * 803df8bae1dSRodney W. Grimes * Collapse an object with the object backing it. 804df8bae1dSRodney W. Grimes * Pages in the backing object are moved into the 805df8bae1dSRodney W. Grimes * parent, and the backing object is deallocated. 806df8bae1dSRodney W. Grimes */ 80726f9a767SRodney W. Grimes void 80826f9a767SRodney W. Grimes vm_object_collapse(object) 80924a1cce3SDavid Greenman vm_object_t object; 810df8bae1dSRodney W. Grimes 811df8bae1dSRodney W. Grimes { 81224a1cce3SDavid Greenman vm_object_t backing_object; 813a316d390SJohn Dyson vm_ooffset_t backing_offset; 81424a1cce3SDavid Greenman vm_size_t size; 815a316d390SJohn Dyson vm_pindex_t new_pindex, backing_offset_index; 81624a1cce3SDavid Greenman vm_page_t p, pp; 817df8bae1dSRodney W. Grimes 818df8bae1dSRodney W. Grimes while (TRUE) { 819df8bae1dSRodney W. Grimes /* 820df8bae1dSRodney W. Grimes * Verify that the conditions are right for collapse: 821df8bae1dSRodney W. Grimes * 8220d94caffSDavid Greenman * The object exists and no pages in it are currently being paged 8230d94caffSDavid Greenman * out. 824df8bae1dSRodney W. Grimes */ 8252fe6e4d7SDavid Greenman if (object == NULL) 826df8bae1dSRodney W. Grimes return; 827df8bae1dSRodney W. Grimes 828b9921222SDavid Greenman /* 829b9921222SDavid Greenman * Make sure there is a backing object. 830b9921222SDavid Greenman */ 83124a1cce3SDavid Greenman if ((backing_object = object->backing_object) == NULL) 832df8bae1dSRodney W. Grimes return; 833df8bae1dSRodney W. Grimes 834f919ebdeSDavid Greenman /* 835f919ebdeSDavid Greenman * we check the backing object first, because it is most likely 83624a1cce3SDavid Greenman * not collapsable. 837f919ebdeSDavid Greenman */ 83824a1cce3SDavid Greenman if (backing_object->handle != NULL || 83924a1cce3SDavid Greenman (backing_object->type != OBJT_DEFAULT && 84024a1cce3SDavid Greenman backing_object->type != OBJT_SWAP) || 841f919ebdeSDavid Greenman (backing_object->flags & OBJ_DEAD) || 84224a1cce3SDavid Greenman object->handle != NULL || 84324a1cce3SDavid Greenman (object->type != OBJT_DEFAULT && 84424a1cce3SDavid Greenman object->type != OBJT_SWAP) || 84524a1cce3SDavid Greenman (object->flags & OBJ_DEAD)) { 8469b4814bbSDavid Greenman return; 84724a1cce3SDavid Greenman } 8489b4814bbSDavid Greenman 849f919ebdeSDavid Greenman if (object->paging_in_progress != 0 || 850f919ebdeSDavid Greenman backing_object->paging_in_progress != 0) { 851b9921222SDavid Greenman vm_object_qcollapse(object); 852df8bae1dSRodney W. Grimes return; 853df8bae1dSRodney W. Grimes } 854f919ebdeSDavid Greenman 85526f9a767SRodney W. Grimes /* 8560d94caffSDavid Greenman * We know that we can either collapse the backing object (if 8570d94caffSDavid Greenman * the parent is the only reference to it) or (perhaps) remove 8580d94caffSDavid Greenman * the parent's reference to it. 859df8bae1dSRodney W. Grimes */ 860df8bae1dSRodney W. Grimes 86124a1cce3SDavid Greenman backing_offset = object->backing_object_offset; 862a316d390SJohn Dyson backing_offset_index = OFF_TO_IDX(backing_offset); 863df8bae1dSRodney W. Grimes size = object->size; 864df8bae1dSRodney W. Grimes 865df8bae1dSRodney W. Grimes /* 8660d94caffSDavid Greenman * If there is exactly one reference to the backing object, we 8670d94caffSDavid Greenman * can collapse it into the parent. 868df8bae1dSRodney W. Grimes */ 869df8bae1dSRodney W. Grimes 870df8bae1dSRodney W. Grimes if (backing_object->ref_count == 1) { 871df8bae1dSRodney W. Grimes 872a1f6d91cSDavid Greenman backing_object->flags |= OBJ_DEAD; 873df8bae1dSRodney W. Grimes /* 874df8bae1dSRodney W. Grimes * We can collapse the backing object. 875df8bae1dSRodney W. Grimes * 8760d94caffSDavid Greenman * Move all in-memory pages from backing_object to the 8770d94caffSDavid Greenman * parent. Pages that have been paged out will be 8780d94caffSDavid Greenman * overwritten by any of the parent's pages that 8790d94caffSDavid Greenman * shadow them. 880df8bae1dSRodney W. Grimes */ 881df8bae1dSRodney W. Grimes 88205f0fdd2SPoul-Henning Kamp while ((p = backing_object->memq.tqh_first) != 0) { 88326f9a767SRodney W. Grimes 884a316d390SJohn Dyson new_pindex = p->pindex - backing_offset_index; 885df8bae1dSRodney W. Grimes 886df8bae1dSRodney W. Grimes /* 8870d94caffSDavid Greenman * If the parent has a page here, or if this 8880d94caffSDavid Greenman * page falls outside the parent, dispose of 8890d94caffSDavid Greenman * it. 890df8bae1dSRodney W. Grimes * 891df8bae1dSRodney W. Grimes * Otherwise, move it as planned. 892df8bae1dSRodney W. Grimes */ 893df8bae1dSRodney W. Grimes 894a316d390SJohn Dyson if (p->pindex < backing_offset_index || 895a316d390SJohn Dyson new_pindex >= size) { 896f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 8970d94caffSDavid Greenman PAGE_WAKEUP(p); 898df8bae1dSRodney W. Grimes vm_page_free(p); 899df8bae1dSRodney W. Grimes } else { 900a316d390SJohn Dyson pp = vm_page_lookup(object, new_pindex); 90124a1cce3SDavid Greenman if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object, 902a316d390SJohn Dyson OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL))) { 903f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 9040d94caffSDavid Greenman PAGE_WAKEUP(p); 905df8bae1dSRodney W. Grimes vm_page_free(p); 90626f9a767SRodney W. Grimes } else { 907a316d390SJohn Dyson vm_page_rename(p, object, new_pindex); 908df8bae1dSRodney W. Grimes } 909df8bae1dSRodney W. Grimes } 910df8bae1dSRodney W. Grimes } 911df8bae1dSRodney W. Grimes 912df8bae1dSRodney W. Grimes /* 913df8bae1dSRodney W. Grimes * Move the pager from backing_object to object. 914df8bae1dSRodney W. Grimes */ 915df8bae1dSRodney W. Grimes 91624a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) { 91726f9a767SRodney W. Grimes backing_object->paging_in_progress++; 91824a1cce3SDavid Greenman if (object->type == OBJT_SWAP) { 91926f9a767SRodney W. Grimes object->paging_in_progress++; 92026f9a767SRodney W. Grimes /* 92126f9a767SRodney W. Grimes * copy shadow object pages into ours 9220d94caffSDavid Greenman * and destroy unneeded pages in 9230d94caffSDavid Greenman * shadow object. 92426f9a767SRodney W. Grimes */ 92526f9a767SRodney W. Grimes swap_pager_copy( 926a316d390SJohn Dyson backing_object, 927a316d390SJohn Dyson OFF_TO_IDX(backing_object->paging_offset), 928a316d390SJohn Dyson object, 929a316d390SJohn Dyson OFF_TO_IDX(object->paging_offset), 930a316d390SJohn Dyson OFF_TO_IDX(object->backing_object_offset)); 931f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 93226f9a767SRodney W. Grimes } else { 93326f9a767SRodney W. Grimes object->paging_in_progress++; 93426f9a767SRodney W. Grimes /* 93524a1cce3SDavid Greenman * move the shadow backing_object's pager data to 93624a1cce3SDavid Greenman * "object" and convert "object" type to OBJT_SWAP. 93726f9a767SRodney W. Grimes */ 93824a1cce3SDavid Greenman object->type = OBJT_SWAP; 9392a4895f4SDavid Greenman object->un_pager.swp.swp_nblocks = 9402a4895f4SDavid Greenman backing_object->un_pager.swp.swp_nblocks; 9412a4895f4SDavid Greenman object->un_pager.swp.swp_allocsize = 9422a4895f4SDavid Greenman backing_object->un_pager.swp.swp_allocsize; 9432a4895f4SDavid Greenman object->un_pager.swp.swp_blocks = 9442a4895f4SDavid Greenman backing_object->un_pager.swp.swp_blocks; 9452a4895f4SDavid Greenman object->un_pager.swp.swp_poip = /* XXX */ 9462a4895f4SDavid Greenman backing_object->un_pager.swp.swp_poip; 94726f9a767SRodney W. Grimes object->paging_offset = backing_object->paging_offset + backing_offset; 94824a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&swap_pager_un_object_list, object, pager_object_list); 94924a1cce3SDavid Greenman 95024a1cce3SDavid Greenman /* 95124a1cce3SDavid Greenman * Convert backing object from OBJT_SWAP to 95224a1cce3SDavid Greenman * OBJT_DEFAULT. XXX - only the TAILQ_REMOVE is 95324a1cce3SDavid Greenman * actually necessary. 95424a1cce3SDavid Greenman */ 95524a1cce3SDavid Greenman backing_object->type = OBJT_DEFAULT; 95624a1cce3SDavid Greenman TAILQ_REMOVE(&swap_pager_un_object_list, backing_object, pager_object_list); 95726f9a767SRodney W. Grimes /* 95826f9a767SRodney W. Grimes * free unnecessary blocks 95926f9a767SRodney W. Grimes */ 960a316d390SJohn Dyson swap_pager_freespace(object, 0, 961a316d390SJohn Dyson OFF_TO_IDX(object->paging_offset)); 962f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 963c0503609SDavid Greenman } 964c0503609SDavid Greenman 965f919ebdeSDavid Greenman vm_object_pip_wakeup(backing_object); 966c0503609SDavid Greenman } 967df8bae1dSRodney W. Grimes /* 968df8bae1dSRodney W. Grimes * Object now shadows whatever backing_object did. 96924a1cce3SDavid Greenman * Note that the reference to backing_object->backing_object 970df8bae1dSRodney W. Grimes * moves from within backing_object to within object. 971df8bae1dSRodney W. Grimes */ 972df8bae1dSRodney W. Grimes 97324a1cce3SDavid Greenman TAILQ_REMOVE(&object->backing_object->shadow_head, object, 97424a1cce3SDavid Greenman shadow_list); 97524a1cce3SDavid Greenman if (backing_object->backing_object) 97624a1cce3SDavid Greenman TAILQ_REMOVE(&backing_object->backing_object->shadow_head, 97724a1cce3SDavid Greenman backing_object, shadow_list); 97824a1cce3SDavid Greenman object->backing_object = backing_object->backing_object; 97924a1cce3SDavid Greenman if (object->backing_object) 98024a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&object->backing_object->shadow_head, 98124a1cce3SDavid Greenman object, shadow_list); 9822fe6e4d7SDavid Greenman 98324a1cce3SDavid Greenman object->backing_object_offset += backing_object->backing_object_offset; 984df8bae1dSRodney W. Grimes /* 985df8bae1dSRodney W. Grimes * Discard backing_object. 986df8bae1dSRodney W. Grimes * 9870d94caffSDavid Greenman * Since the backing object has no pages, no pager left, 9880d94caffSDavid Greenman * and no object references within it, all that is 9890d94caffSDavid Greenman * necessary is to dispose of it. 990df8bae1dSRodney W. Grimes */ 991df8bae1dSRodney W. Grimes 992df8bae1dSRodney W. Grimes TAILQ_REMOVE(&vm_object_list, backing_object, 993df8bae1dSRodney W. Grimes object_list); 994df8bae1dSRodney W. Grimes vm_object_count--; 995df8bae1dSRodney W. Grimes 996df8bae1dSRodney W. Grimes free((caddr_t) backing_object, M_VMOBJ); 997df8bae1dSRodney W. Grimes 998df8bae1dSRodney W. Grimes object_collapses++; 9990d94caffSDavid Greenman } else { 1000df8bae1dSRodney W. Grimes /* 1001df8bae1dSRodney W. Grimes * If all of the pages in the backing object are 10020d94caffSDavid Greenman * shadowed by the parent object, the parent object no 10030d94caffSDavid Greenman * longer has to shadow the backing object; it can 10040d94caffSDavid Greenman * shadow the next one in the chain. 1005df8bae1dSRodney W. Grimes * 10060d94caffSDavid Greenman * The backing object must not be paged out - we'd have 10070d94caffSDavid Greenman * to check all of the paged-out pages, as well. 1008df8bae1dSRodney W. Grimes */ 1009df8bae1dSRodney W. Grimes 101024a1cce3SDavid Greenman if (backing_object->type != OBJT_DEFAULT) { 1011df8bae1dSRodney W. Grimes return; 1012df8bae1dSRodney W. Grimes } 1013df8bae1dSRodney W. Grimes /* 10140d94caffSDavid Greenman * Should have a check for a 'small' number of pages 10150d94caffSDavid Greenman * here. 1016df8bae1dSRodney W. Grimes */ 1017df8bae1dSRodney W. Grimes 101826f9a767SRodney W. Grimes for (p = backing_object->memq.tqh_first; p; p = p->listq.tqe_next) { 1019a316d390SJohn Dyson new_pindex = p->pindex - backing_offset_index; 1020df8bae1dSRodney W. Grimes 1021df8bae1dSRodney W. Grimes /* 10220d94caffSDavid Greenman * If the parent has a page here, or if this 10230d94caffSDavid Greenman * page falls outside the parent, keep going. 1024df8bae1dSRodney W. Grimes * 10250d94caffSDavid Greenman * Otherwise, the backing_object must be left in 10260d94caffSDavid Greenman * the chain. 1027df8bae1dSRodney W. Grimes */ 1028df8bae1dSRodney W. Grimes 1029a316d390SJohn Dyson if (p->pindex >= backing_offset_index && 1030a316d390SJohn Dyson new_pindex <= size) { 103124a1cce3SDavid Greenman 1032a316d390SJohn Dyson pp = vm_page_lookup(object, new_pindex); 103324a1cce3SDavid Greenman 103424a1cce3SDavid Greenman if ((pp == NULL || pp->valid == 0) && 1035a316d390SJohn Dyson !vm_pager_has_page(object, OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL)) { 1036df8bae1dSRodney W. Grimes /* 10370d94caffSDavid Greenman * Page still needed. Can't go any 10380d94caffSDavid Greenman * further. 1039df8bae1dSRodney W. Grimes */ 1040df8bae1dSRodney W. Grimes return; 1041df8bae1dSRodney W. Grimes } 1042df8bae1dSRodney W. Grimes } 104324a1cce3SDavid Greenman } 1044df8bae1dSRodney W. Grimes 1045df8bae1dSRodney W. Grimes /* 10460d94caffSDavid Greenman * Make the parent shadow the next object in the 10470d94caffSDavid Greenman * chain. Deallocating backing_object will not remove 10480d94caffSDavid Greenman * it, since its reference count is at least 2. 1049df8bae1dSRodney W. Grimes */ 1050df8bae1dSRodney W. Grimes 105124a1cce3SDavid Greenman TAILQ_REMOVE(&object->backing_object->shadow_head, 105224a1cce3SDavid Greenman object, shadow_list); 105324a1cce3SDavid Greenman vm_object_reference(object->backing_object = backing_object->backing_object); 105424a1cce3SDavid Greenman if (object->backing_object) 105524a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&object->backing_object->shadow_head, 105624a1cce3SDavid Greenman object, shadow_list); 105724a1cce3SDavid Greenman object->backing_object_offset += backing_object->backing_object_offset; 1058df8bae1dSRodney W. Grimes 1059df8bae1dSRodney W. Grimes /* 10600d94caffSDavid Greenman * Drop the reference count on backing_object. Since 10610d94caffSDavid Greenman * its ref_count was at least 2, it will not vanish; 10620d94caffSDavid Greenman * so we don't need to call vm_object_deallocate. 1063df8bae1dSRodney W. Grimes */ 106426f9a767SRodney W. Grimes if (backing_object->ref_count == 1) 106526f9a767SRodney W. Grimes printf("should have called obj deallocate\n"); 1066df8bae1dSRodney W. Grimes backing_object->ref_count--; 1067df8bae1dSRodney W. Grimes 1068df8bae1dSRodney W. Grimes object_bypasses++; 1069df8bae1dSRodney W. Grimes 1070df8bae1dSRodney W. Grimes } 1071df8bae1dSRodney W. Grimes 1072df8bae1dSRodney W. Grimes /* 1073df8bae1dSRodney W. Grimes * Try again with this object's new backing object. 1074df8bae1dSRodney W. Grimes */ 1075df8bae1dSRodney W. Grimes } 1076df8bae1dSRodney W. Grimes } 1077df8bae1dSRodney W. Grimes 1078df8bae1dSRodney W. Grimes /* 1079df8bae1dSRodney W. Grimes * vm_object_page_remove: [internal] 1080df8bae1dSRodney W. Grimes * 1081df8bae1dSRodney W. Grimes * Removes all physical pages in the specified 1082df8bae1dSRodney W. Grimes * object range from the object's list of pages. 1083df8bae1dSRodney W. Grimes * 1084df8bae1dSRodney W. Grimes * The object must be locked. 1085df8bae1dSRodney W. Grimes */ 108626f9a767SRodney W. Grimes void 10877c1f6cedSDavid Greenman vm_object_page_remove(object, start, end, clean_only) 1088df8bae1dSRodney W. Grimes register vm_object_t object; 1089a316d390SJohn Dyson register vm_pindex_t start; 1090a316d390SJohn Dyson register vm_pindex_t end; 10917c1f6cedSDavid Greenman boolean_t clean_only; 1092df8bae1dSRodney W. Grimes { 1093df8bae1dSRodney W. Grimes register vm_page_t p, next; 1094a316d390SJohn Dyson unsigned int size; 109526f9a767SRodney W. Grimes int s; 1096df8bae1dSRodney W. Grimes 1097df8bae1dSRodney W. Grimes if (object == NULL) 1098df8bae1dSRodney W. Grimes return; 1099df8bae1dSRodney W. Grimes 11002fe6e4d7SDavid Greenman object->paging_in_progress++; 110126f9a767SRodney W. Grimes again: 110226f9a767SRodney W. Grimes size = end - start; 1103a316d390SJohn Dyson if (size > 4 || size >= object->size / 4) { 11040d94caffSDavid Greenman for (p = object->memq.tqh_first; p != NULL; p = next) { 1105df8bae1dSRodney W. Grimes next = p->listq.tqe_next; 1106a316d390SJohn Dyson if ((start <= p->pindex) && (p->pindex < end)) { 1107a481f200SDavid Greenman s = splhigh(); 11080d94caffSDavid Greenman if (p->bmapped) { 11090d94caffSDavid Greenman splx(s); 11100d94caffSDavid Greenman continue; 11110d94caffSDavid Greenman } 11120d94caffSDavid Greenman if ((p->flags & PG_BUSY) || p->busy) { 111326f9a767SRodney W. Grimes p->flags |= PG_WANTED; 111424a1cce3SDavid Greenman tsleep(p, PVM, "vmopar", 0); 1115a481f200SDavid Greenman splx(s); 111626f9a767SRodney W. Grimes goto again; 111726f9a767SRodney W. Grimes } 1118a481f200SDavid Greenman splx(s); 11197c1f6cedSDavid Greenman if (clean_only) { 11207c1f6cedSDavid Greenman vm_page_test_dirty(p); 11217c1f6cedSDavid Greenman if (p->valid & p->dirty) 11227c1f6cedSDavid Greenman continue; 11237c1f6cedSDavid Greenman } 1124f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 11250d94caffSDavid Greenman PAGE_WAKEUP(p); 1126df8bae1dSRodney W. Grimes vm_page_free(p); 112726f9a767SRodney W. Grimes } 112826f9a767SRodney W. Grimes } 112926f9a767SRodney W. Grimes } else { 113026f9a767SRodney W. Grimes while (size > 0) { 113105f0fdd2SPoul-Henning Kamp while ((p = vm_page_lookup(object, start)) != 0) { 1132a481f200SDavid Greenman s = splhigh(); 11330d94caffSDavid Greenman if (p->bmapped) { 11340d94caffSDavid Greenman splx(s); 11350d94caffSDavid Greenman break; 11360d94caffSDavid Greenman } 11370d94caffSDavid Greenman if ((p->flags & PG_BUSY) || p->busy) { 113826f9a767SRodney W. Grimes p->flags |= PG_WANTED; 113924a1cce3SDavid Greenman tsleep(p, PVM, "vmopar", 0); 1140a481f200SDavid Greenman splx(s); 114126f9a767SRodney W. Grimes goto again; 114226f9a767SRodney W. Grimes } 1143a481f200SDavid Greenman splx(s); 11447c1f6cedSDavid Greenman if (clean_only) { 11457c1f6cedSDavid Greenman vm_page_test_dirty(p); 11467c1f6cedSDavid Greenman if (p->valid & p->dirty) 11477c1f6cedSDavid Greenman continue; 11487c1f6cedSDavid Greenman } 1149f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 11500d94caffSDavid Greenman PAGE_WAKEUP(p); 115126f9a767SRodney W. Grimes vm_page_free(p); 115226f9a767SRodney W. Grimes } 1153a316d390SJohn Dyson start += 1; 1154a316d390SJohn Dyson size -= 1; 1155df8bae1dSRodney W. Grimes } 1156df8bae1dSRodney W. Grimes } 1157f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 1158c0503609SDavid Greenman } 1159df8bae1dSRodney W. Grimes 1160df8bae1dSRodney W. Grimes /* 1161df8bae1dSRodney W. Grimes * Routine: vm_object_coalesce 1162df8bae1dSRodney W. Grimes * Function: Coalesces two objects backing up adjoining 1163df8bae1dSRodney W. Grimes * regions of memory into a single object. 1164df8bae1dSRodney W. Grimes * 1165df8bae1dSRodney W. Grimes * returns TRUE if objects were combined. 1166df8bae1dSRodney W. Grimes * 1167df8bae1dSRodney W. Grimes * NOTE: Only works at the moment if the second object is NULL - 1168df8bae1dSRodney W. Grimes * if it's not, which object do we lock first? 1169df8bae1dSRodney W. Grimes * 1170df8bae1dSRodney W. Grimes * Parameters: 1171df8bae1dSRodney W. Grimes * prev_object First object to coalesce 1172df8bae1dSRodney W. Grimes * prev_offset Offset into prev_object 1173df8bae1dSRodney W. Grimes * next_object Second object into coalesce 1174df8bae1dSRodney W. Grimes * next_offset Offset into next_object 1175df8bae1dSRodney W. Grimes * 1176df8bae1dSRodney W. Grimes * prev_size Size of reference to prev_object 1177df8bae1dSRodney W. Grimes * next_size Size of reference to next_object 1178df8bae1dSRodney W. Grimes * 1179df8bae1dSRodney W. Grimes * Conditions: 1180df8bae1dSRodney W. Grimes * The object must *not* be locked. 1181df8bae1dSRodney W. Grimes */ 11820d94caffSDavid Greenman boolean_t 1183a316d390SJohn Dyson vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size) 1184df8bae1dSRodney W. Grimes register vm_object_t prev_object; 1185a316d390SJohn Dyson vm_pindex_t prev_pindex; 1186df8bae1dSRodney W. Grimes vm_size_t prev_size, next_size; 1187df8bae1dSRodney W. Grimes { 1188df8bae1dSRodney W. Grimes vm_size_t newsize; 1189df8bae1dSRodney W. Grimes 1190df8bae1dSRodney W. Grimes if (prev_object == NULL) { 1191df8bae1dSRodney W. Grimes return (TRUE); 1192df8bae1dSRodney W. Grimes } 1193df8bae1dSRodney W. Grimes 1194df8bae1dSRodney W. Grimes /* 1195df8bae1dSRodney W. Grimes * Try to collapse the object first 1196df8bae1dSRodney W. Grimes */ 1197df8bae1dSRodney W. Grimes vm_object_collapse(prev_object); 1198df8bae1dSRodney W. Grimes 1199df8bae1dSRodney W. Grimes /* 12000d94caffSDavid Greenman * Can't coalesce if: . more than one reference . paged out . shadows 12010d94caffSDavid Greenman * another object . has a copy elsewhere (any of which mean that the 12020d94caffSDavid Greenman * pages not mapped to prev_entry may be in use anyway) 1203df8bae1dSRodney W. Grimes */ 1204df8bae1dSRodney W. Grimes 1205df8bae1dSRodney W. Grimes if (prev_object->ref_count > 1 || 120624a1cce3SDavid Greenman prev_object->type != OBJT_DEFAULT || 120724a1cce3SDavid Greenman prev_object->backing_object != NULL) { 1208df8bae1dSRodney W. Grimes return (FALSE); 1209df8bae1dSRodney W. Grimes } 1210a316d390SJohn Dyson 1211a316d390SJohn Dyson prev_size >>= PAGE_SHIFT; 1212a316d390SJohn Dyson next_size >>= PAGE_SHIFT; 1213df8bae1dSRodney W. Grimes /* 12140d94caffSDavid Greenman * Remove any pages that may still be in the object from a previous 12150d94caffSDavid Greenman * deallocation. 1216df8bae1dSRodney W. Grimes */ 1217df8bae1dSRodney W. Grimes 1218df8bae1dSRodney W. Grimes vm_object_page_remove(prev_object, 1219a316d390SJohn Dyson prev_pindex + prev_size, 1220a316d390SJohn Dyson prev_pindex + prev_size + next_size, FALSE); 1221df8bae1dSRodney W. Grimes 1222df8bae1dSRodney W. Grimes /* 1223df8bae1dSRodney W. Grimes * Extend the object if necessary. 1224df8bae1dSRodney W. Grimes */ 1225a316d390SJohn Dyson newsize = prev_pindex + prev_size + next_size; 1226df8bae1dSRodney W. Grimes if (newsize > prev_object->size) 1227df8bae1dSRodney W. Grimes prev_object->size = newsize; 1228df8bae1dSRodney W. Grimes 1229df8bae1dSRodney W. Grimes return (TRUE); 1230df8bae1dSRodney W. Grimes } 1231df8bae1dSRodney W. Grimes 1232c3cb3e12SDavid Greenman #ifdef DDB 1233c3cb3e12SDavid Greenman 1234cac597e4SBruce Evans static int 1235a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry) 1236a1f6d91cSDavid Greenman vm_map_t map; 1237a1f6d91cSDavid Greenman vm_object_t object; 1238a1f6d91cSDavid Greenman vm_map_entry_t entry; 1239a1f6d91cSDavid Greenman { 1240a1f6d91cSDavid Greenman vm_map_t tmpm; 1241a1f6d91cSDavid Greenman vm_map_entry_t tmpe; 1242a1f6d91cSDavid Greenman vm_object_t obj; 1243a1f6d91cSDavid Greenman int entcount; 1244a1f6d91cSDavid Greenman 1245a1f6d91cSDavid Greenman if (map == 0) 1246a1f6d91cSDavid Greenman return 0; 1247a1f6d91cSDavid Greenman 1248a1f6d91cSDavid Greenman if (entry == 0) { 1249a1f6d91cSDavid Greenman tmpe = map->header.next; 1250a1f6d91cSDavid Greenman entcount = map->nentries; 1251a1f6d91cSDavid Greenman while (entcount-- && (tmpe != &map->header)) { 1252a1f6d91cSDavid Greenman if( _vm_object_in_map(map, object, tmpe)) { 1253a1f6d91cSDavid Greenman return 1; 1254a1f6d91cSDavid Greenman } 1255a1f6d91cSDavid Greenman tmpe = tmpe->next; 1256a1f6d91cSDavid Greenman } 1257a1f6d91cSDavid Greenman } else if (entry->is_sub_map || entry->is_a_map) { 1258a1f6d91cSDavid Greenman tmpm = entry->object.share_map; 1259a1f6d91cSDavid Greenman tmpe = tmpm->header.next; 1260a1f6d91cSDavid Greenman entcount = tmpm->nentries; 1261a1f6d91cSDavid Greenman while (entcount-- && tmpe != &tmpm->header) { 1262a1f6d91cSDavid Greenman if( _vm_object_in_map(tmpm, object, tmpe)) { 1263a1f6d91cSDavid Greenman return 1; 1264a1f6d91cSDavid Greenman } 1265a1f6d91cSDavid Greenman tmpe = tmpe->next; 1266a1f6d91cSDavid Greenman } 1267a1f6d91cSDavid Greenman } else if (obj = entry->object.vm_object) { 126824a1cce3SDavid Greenman for(; obj; obj=obj->backing_object) 1269a1f6d91cSDavid Greenman if( obj == object) { 1270a1f6d91cSDavid Greenman return 1; 1271a1f6d91cSDavid Greenman } 1272a1f6d91cSDavid Greenman } 1273a1f6d91cSDavid Greenman return 0; 1274a1f6d91cSDavid Greenman } 1275a1f6d91cSDavid Greenman 1276cac597e4SBruce Evans static int 1277a1f6d91cSDavid Greenman vm_object_in_map( object) 1278a1f6d91cSDavid Greenman vm_object_t object; 1279a1f6d91cSDavid Greenman { 1280a1f6d91cSDavid Greenman struct proc *p; 1281a1f6d91cSDavid Greenman for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 1282a1f6d91cSDavid Greenman if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 1283a1f6d91cSDavid Greenman continue; 1284a1f6d91cSDavid Greenman /* 1285a1f6d91cSDavid Greenman if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 1286a1f6d91cSDavid Greenman continue; 1287a1f6d91cSDavid Greenman } 1288a1f6d91cSDavid Greenman */ 1289a1f6d91cSDavid Greenman if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) 1290a1f6d91cSDavid Greenman return 1; 1291a1f6d91cSDavid Greenman } 1292a1f6d91cSDavid Greenman if( _vm_object_in_map( kernel_map, object, 0)) 1293a1f6d91cSDavid Greenman return 1; 1294a1f6d91cSDavid Greenman if( _vm_object_in_map( kmem_map, object, 0)) 1295a1f6d91cSDavid Greenman return 1; 1296a1f6d91cSDavid Greenman if( _vm_object_in_map( pager_map, object, 0)) 1297a1f6d91cSDavid Greenman return 1; 1298a1f6d91cSDavid Greenman if( _vm_object_in_map( buffer_map, object, 0)) 1299a1f6d91cSDavid Greenman return 1; 1300a1f6d91cSDavid Greenman if( _vm_object_in_map( io_map, object, 0)) 1301a1f6d91cSDavid Greenman return 1; 1302a1f6d91cSDavid Greenman if( _vm_object_in_map( phys_map, object, 0)) 1303a1f6d91cSDavid Greenman return 1; 1304a1f6d91cSDavid Greenman if( _vm_object_in_map( mb_map, object, 0)) 1305a1f6d91cSDavid Greenman return 1; 1306a1f6d91cSDavid Greenman if( _vm_object_in_map( u_map, object, 0)) 1307a1f6d91cSDavid Greenman return 1; 1308a1f6d91cSDavid Greenman return 0; 1309a1f6d91cSDavid Greenman } 1310a1f6d91cSDavid Greenman 1311c3cb3e12SDavid Greenman 1312f708ef1bSPoul-Henning Kamp #ifdef DDB 1313f708ef1bSPoul-Henning Kamp static void 1314f708ef1bSPoul-Henning Kamp DDB_vm_object_check() 1315f708ef1bSPoul-Henning Kamp { 1316a1f6d91cSDavid Greenman vm_object_t object; 1317a1f6d91cSDavid Greenman 1318a1f6d91cSDavid Greenman /* 1319a1f6d91cSDavid Greenman * make sure that internal objs are in a map somewhere 1320a1f6d91cSDavid Greenman * and none have zero ref counts. 1321a1f6d91cSDavid Greenman */ 1322a1f6d91cSDavid Greenman for (object = vm_object_list.tqh_first; 1323a1f6d91cSDavid Greenman object != NULL; 1324a1f6d91cSDavid Greenman object = object->object_list.tqe_next) { 132524a1cce3SDavid Greenman if (object->handle == NULL && 132624a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 1327a1f6d91cSDavid Greenman if (object->ref_count == 0) { 1328a1f6d91cSDavid Greenman printf("vmochk: internal obj has zero ref count: %d\n", 1329a1f6d91cSDavid Greenman object->size); 1330a1f6d91cSDavid Greenman } 1331a1f6d91cSDavid Greenman if (!vm_object_in_map(object)) { 13323af76890SPoul-Henning Kamp printf("vmochk: internal obj is not in a map: " 13333af76890SPoul-Henning Kamp "ref: %d, size: %d: 0x%x, backing_object: 0x%x\n", 13343af76890SPoul-Henning Kamp object->ref_count, object->size, 13353af76890SPoul-Henning Kamp object->size, object->backing_object); 1336a1f6d91cSDavid Greenman } 1337a1f6d91cSDavid Greenman } 1338a1f6d91cSDavid Greenman } 1339a1f6d91cSDavid Greenman } 1340f708ef1bSPoul-Henning Kamp #endif /* DDB */ 1341a1f6d91cSDavid Greenman 134226f9a767SRodney W. Grimes /* 1343df8bae1dSRodney W. Grimes * vm_object_print: [ debug ] 1344df8bae1dSRodney W. Grimes */ 13450d94caffSDavid Greenman void 1346187f0238SBruce Evans vm_object_print(iobject, full, dummy3, dummy4) 1347187f0238SBruce Evans /* db_expr_t */ int iobject; 1348df8bae1dSRodney W. Grimes boolean_t full; 1349187f0238SBruce Evans /* db_expr_t */ int dummy3; 1350187f0238SBruce Evans char *dummy4; 1351df8bae1dSRodney W. Grimes { 1352187f0238SBruce Evans vm_object_t object = (vm_object_t)iobject; /* XXX */ 1353df8bae1dSRodney W. Grimes register vm_page_t p; 1354df8bae1dSRodney W. Grimes 1355df8bae1dSRodney W. Grimes register int count; 1356df8bae1dSRodney W. Grimes 1357df8bae1dSRodney W. Grimes if (object == NULL) 1358df8bae1dSRodney W. Grimes return; 1359df8bae1dSRodney W. Grimes 1360df8bae1dSRodney W. Grimes iprintf("Object 0x%x: size=0x%x, res=%d, ref=%d, ", 1361df8bae1dSRodney W. Grimes (int) object, (int) object->size, 1362df8bae1dSRodney W. Grimes object->resident_page_count, object->ref_count); 13632a4895f4SDavid Greenman printf("offset=0x%x, backing_object=(0x%x)+0x%x\n", 13642a4895f4SDavid Greenman (int) object->paging_offset, 136524a1cce3SDavid Greenman (int) object->backing_object, (int) object->backing_object_offset); 136605f0fdd2SPoul-Henning Kamp printf("cache: next=%p, prev=%p\n", 1367df8bae1dSRodney W. Grimes object->cached_list.tqe_next, object->cached_list.tqe_prev); 1368df8bae1dSRodney W. Grimes 1369df8bae1dSRodney W. Grimes if (!full) 1370df8bae1dSRodney W. Grimes return; 1371df8bae1dSRodney W. Grimes 1372df8bae1dSRodney W. Grimes indent += 2; 1373df8bae1dSRodney W. Grimes count = 0; 1374df8bae1dSRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) { 1375df8bae1dSRodney W. Grimes if (count == 0) 1376df8bae1dSRodney W. Grimes iprintf("memory:="); 1377df8bae1dSRodney W. Grimes else if (count == 6) { 1378df8bae1dSRodney W. Grimes printf("\n"); 1379df8bae1dSRodney W. Grimes iprintf(" ..."); 1380df8bae1dSRodney W. Grimes count = 0; 1381df8bae1dSRodney W. Grimes } else 1382df8bae1dSRodney W. Grimes printf(","); 1383df8bae1dSRodney W. Grimes count++; 1384df8bae1dSRodney W. Grimes 138505f0fdd2SPoul-Henning Kamp printf("(off=0x%lx,page=0x%lx)", 1386a316d390SJohn Dyson (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p)); 1387df8bae1dSRodney W. Grimes } 1388df8bae1dSRodney W. Grimes if (count != 0) 1389df8bae1dSRodney W. Grimes printf("\n"); 1390df8bae1dSRodney W. Grimes indent -= 2; 1391df8bae1dSRodney W. Grimes } 1392c3cb3e12SDavid Greenman #endif /* DDB */ 1393