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 * 64a316d390SJohn Dyson * $Id: vm_object.c,v 1.58 1995/12/07 12:48:21 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> 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 95cac597e4SBruce Evans extern void 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 vm_page_t 105cac597e4SBruce Evans vm_object_page_lookup __P((vm_object_t object, 106cac597e4SBruce Evans vm_offset_t offset)); 107cac597e4SBruce Evans static void vm_object_qcollapse __P((vm_object_t object)); 108f6b04d2bSDavid Greenman 109df8bae1dSRodney W. Grimes /* 110df8bae1dSRodney W. Grimes * Virtual memory objects maintain the actual data 111df8bae1dSRodney W. Grimes * associated with allocated virtual memory. A given 112df8bae1dSRodney W. Grimes * page of memory exists within exactly one object. 113df8bae1dSRodney W. Grimes * 114df8bae1dSRodney W. Grimes * An object is only deallocated when all "references" 115df8bae1dSRodney W. Grimes * are given up. Only one "reference" to a given 116df8bae1dSRodney W. Grimes * region of an object should be writeable. 117df8bae1dSRodney W. Grimes * 118df8bae1dSRodney W. Grimes * Associated with each object is a list of all resident 119df8bae1dSRodney W. Grimes * memory pages belonging to that object; this list is 120df8bae1dSRodney W. Grimes * maintained by the "vm_page" module, and locked by the object's 121df8bae1dSRodney W. Grimes * lock. 122df8bae1dSRodney W. Grimes * 123df8bae1dSRodney W. Grimes * Each object also records a "pager" routine which is 124df8bae1dSRodney W. Grimes * used to retrieve (and store) pages to the proper backing 125df8bae1dSRodney W. Grimes * storage. In addition, objects may be backed by other 126df8bae1dSRodney W. Grimes * objects from which they were virtual-copied. 127df8bae1dSRodney W. Grimes * 128df8bae1dSRodney W. Grimes * The only items within the object structure which are 129df8bae1dSRodney W. Grimes * modified after time of creation are: 130df8bae1dSRodney W. Grimes * reference count locked by object's lock 131df8bae1dSRodney W. Grimes * pager routine locked by object's lock 132df8bae1dSRodney W. Grimes * 133df8bae1dSRodney W. Grimes */ 134df8bae1dSRodney W. Grimes 13528f8db14SBruce Evans int vm_object_cache_max; 13628f8db14SBruce Evans struct object_q vm_object_cached_list; 13728f8db14SBruce Evans int vm_object_cached; 13828f8db14SBruce Evans struct object_q vm_object_list; 13928f8db14SBruce Evans long vm_object_count; 14028f8db14SBruce Evans vm_object_t kernel_object; 14128f8db14SBruce Evans vm_object_t kmem_object; 142df8bae1dSRodney W. Grimes struct vm_object kernel_object_store; 143df8bae1dSRodney W. Grimes struct vm_object kmem_object_store; 144aef922f5SJohn Dyson extern int vm_pageout_page_count; 145df8bae1dSRodney W. Grimes 146c3cb3e12SDavid Greenman long object_collapses; 147c3cb3e12SDavid Greenman long object_bypasses; 148df8bae1dSRodney W. Grimes 149df8bae1dSRodney W. Grimes static void 15024a1cce3SDavid Greenman _vm_object_allocate(type, size, object) 15124a1cce3SDavid Greenman objtype_t type; 152df8bae1dSRodney W. Grimes vm_size_t size; 153df8bae1dSRodney W. Grimes register vm_object_t object; 154df8bae1dSRodney W. Grimes { 155df8bae1dSRodney W. Grimes TAILQ_INIT(&object->memq); 15624a1cce3SDavid Greenman TAILQ_INIT(&object->shadow_head); 157a1f6d91cSDavid Greenman 15824a1cce3SDavid Greenman object->type = type; 159df8bae1dSRodney W. Grimes object->size = size; 160a1f6d91cSDavid Greenman object->ref_count = 1; 16124a1cce3SDavid Greenman object->flags = 0; 162df8bae1dSRodney W. Grimes object->paging_in_progress = 0; 163a1f6d91cSDavid Greenman object->resident_page_count = 0; 16424a1cce3SDavid Greenman object->handle = NULL; 165a316d390SJohn Dyson object->paging_offset = (vm_ooffset_t) 0; 16624a1cce3SDavid Greenman object->backing_object = NULL; 167a316d390SJohn Dyson object->backing_object_offset = (vm_ooffset_t) 0; 168a1f6d91cSDavid Greenman 169a1f6d91cSDavid Greenman object->last_read = 0; 170df8bae1dSRodney W. Grimes 171df8bae1dSRodney W. Grimes TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 172df8bae1dSRodney W. Grimes vm_object_count++; 173df8bae1dSRodney W. Grimes } 174df8bae1dSRodney W. Grimes 175df8bae1dSRodney W. Grimes /* 17626f9a767SRodney W. Grimes * vm_object_init: 17726f9a767SRodney W. Grimes * 17826f9a767SRodney W. Grimes * Initialize the VM objects module. 17926f9a767SRodney W. Grimes */ 18026f9a767SRodney W. Grimes void 181a316d390SJohn Dyson vm_object_init() 18226f9a767SRodney W. Grimes { 18326f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_cached_list); 18426f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_list); 18526f9a767SRodney W. Grimes vm_object_count = 0; 1860217125fSDavid Greenman 1870217125fSDavid Greenman vm_object_cache_max = 84; 1880217125fSDavid Greenman if (cnt.v_page_count > 1000) 1890217125fSDavid Greenman vm_object_cache_max += (cnt.v_page_count - 1000) / 4; 19026f9a767SRodney W. Grimes 19126f9a767SRodney W. Grimes kernel_object = &kernel_object_store; 192a316d390SJohn Dyson _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 19326f9a767SRodney W. Grimes kernel_object); 19426f9a767SRodney W. Grimes 19526f9a767SRodney W. Grimes kmem_object = &kmem_object_store; 196a316d390SJohn Dyson _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 19726f9a767SRodney W. Grimes kmem_object); 19826f9a767SRodney W. Grimes } 19926f9a767SRodney W. Grimes 20026f9a767SRodney W. Grimes /* 20126f9a767SRodney W. Grimes * vm_object_allocate: 20226f9a767SRodney W. Grimes * 20326f9a767SRodney W. Grimes * Returns a new object with the given size. 20426f9a767SRodney W. Grimes */ 20526f9a767SRodney W. Grimes 20626f9a767SRodney W. Grimes vm_object_t 20724a1cce3SDavid Greenman vm_object_allocate(type, size) 20824a1cce3SDavid Greenman objtype_t type; 20926f9a767SRodney W. Grimes vm_size_t size; 21026f9a767SRodney W. Grimes { 21126f9a767SRodney W. Grimes register vm_object_t result; 21226f9a767SRodney W. Grimes 21326f9a767SRodney W. Grimes result = (vm_object_t) 21426f9a767SRodney W. Grimes malloc((u_long) sizeof *result, M_VMOBJ, M_WAITOK); 21526f9a767SRodney W. Grimes 21626f9a767SRodney W. Grimes 21724a1cce3SDavid Greenman _vm_object_allocate(type, size, result); 21826f9a767SRodney W. Grimes 21926f9a767SRodney W. Grimes return (result); 22026f9a767SRodney W. Grimes } 22126f9a767SRodney W. Grimes 22226f9a767SRodney W. Grimes 22326f9a767SRodney W. Grimes /* 224df8bae1dSRodney W. Grimes * vm_object_reference: 225df8bae1dSRodney W. Grimes * 226df8bae1dSRodney W. Grimes * Gets another reference to the given object. 227df8bae1dSRodney W. Grimes */ 22826f9a767SRodney W. Grimes inline void 22926f9a767SRodney W. Grimes vm_object_reference(object) 230df8bae1dSRodney W. Grimes register vm_object_t object; 231df8bae1dSRodney W. Grimes { 232df8bae1dSRodney W. Grimes if (object == NULL) 233df8bae1dSRodney W. Grimes return; 234df8bae1dSRodney W. Grimes 23524a1cce3SDavid Greenman if (object->ref_count == 0) { 23624a1cce3SDavid Greenman if ((object->flags & OBJ_CANPERSIST) == 0) 23724a1cce3SDavid Greenman panic("vm_object_reference: non-persistent object with 0 ref_count"); 23824a1cce3SDavid Greenman TAILQ_REMOVE(&vm_object_cached_list, object, cached_list); 23924a1cce3SDavid Greenman vm_object_cached--; 24024a1cce3SDavid Greenman } 241df8bae1dSRodney W. Grimes object->ref_count++; 242df8bae1dSRodney W. Grimes } 243df8bae1dSRodney W. Grimes 244df8bae1dSRodney W. Grimes /* 245df8bae1dSRodney W. Grimes * vm_object_deallocate: 246df8bae1dSRodney W. Grimes * 247df8bae1dSRodney W. Grimes * Release a reference to the specified object, 248df8bae1dSRodney W. Grimes * gained either through a vm_object_allocate 249df8bae1dSRodney W. Grimes * or a vm_object_reference call. When all references 250df8bae1dSRodney W. Grimes * are gone, storage associated with this object 251df8bae1dSRodney W. Grimes * may be relinquished. 252df8bae1dSRodney W. Grimes * 253df8bae1dSRodney W. Grimes * No object may be locked. 254df8bae1dSRodney W. Grimes */ 25526f9a767SRodney W. Grimes void 25626f9a767SRodney W. Grimes vm_object_deallocate(object) 25726f9a767SRodney W. Grimes vm_object_t object; 258df8bae1dSRodney W. Grimes { 259df8bae1dSRodney W. Grimes vm_object_t temp; 260df8bae1dSRodney W. Grimes 261df8bae1dSRodney W. Grimes while (object != NULL) { 262df8bae1dSRodney W. Grimes 263ba8da839SDavid Greenman if (object->ref_count == 0) 264ba8da839SDavid Greenman panic("vm_object_deallocate: object deallocated too many times"); 265ba8da839SDavid Greenman 266df8bae1dSRodney W. Grimes /* 267df8bae1dSRodney W. Grimes * Lose the reference 268df8bae1dSRodney W. Grimes */ 269be6d5bfaSDavid Greenman object->ref_count--; 270be6d5bfaSDavid Greenman 271be6d5bfaSDavid Greenman if (object->ref_count != 0) { 272be6d5bfaSDavid Greenman if ((object->ref_count == 1) && 27324a1cce3SDavid Greenman (object->handle == NULL) && 27424a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || 27524a1cce3SDavid Greenman object->type == OBJT_SWAP)) { 276a1f6d91cSDavid Greenman vm_object_t robject; 27724a1cce3SDavid Greenman robject = object->shadow_head.tqh_first; 278be6d5bfaSDavid Greenman if ((robject != NULL) && 27924a1cce3SDavid Greenman (robject->handle == NULL) && 28024a1cce3SDavid Greenman (robject->type == OBJT_DEFAULT || 28124a1cce3SDavid Greenman robject->type == OBJT_SWAP)) { 282a1f6d91cSDavid Greenman int s; 283a1f6d91cSDavid Greenman robject->ref_count += 2; 2849b4814bbSDavid Greenman object->ref_count += 2; 285a1f6d91cSDavid Greenman 286a1f6d91cSDavid Greenman do { 287a1f6d91cSDavid Greenman s = splhigh(); 288a1f6d91cSDavid Greenman while (robject->paging_in_progress) { 289c0503609SDavid Greenman robject->flags |= OBJ_PIPWNT; 290a1f6d91cSDavid Greenman tsleep(robject, PVM, "objde1", 0); 2910d94caffSDavid Greenman } 292a1f6d91cSDavid Greenman 293a1f6d91cSDavid Greenman while (object->paging_in_progress) { 294c0503609SDavid Greenman object->flags |= OBJ_PIPWNT; 295a1f6d91cSDavid Greenman tsleep(object, PVM, "objde2", 0); 296a1f6d91cSDavid Greenman } 297a1f6d91cSDavid Greenman splx(s); 298a1f6d91cSDavid Greenman 299a1f6d91cSDavid Greenman } while( object->paging_in_progress || robject->paging_in_progress); 300a1f6d91cSDavid Greenman 3019b4814bbSDavid Greenman object->ref_count -= 2; 302a1f6d91cSDavid Greenman robject->ref_count -= 2; 303a1f6d91cSDavid Greenman if( robject->ref_count == 0) { 304a1f6d91cSDavid Greenman robject->ref_count += 1; 305ba8da839SDavid Greenman object = robject; 306ba8da839SDavid Greenman continue; 307a1f6d91cSDavid Greenman } 308a1f6d91cSDavid Greenman vm_object_collapse(robject); 309a1f6d91cSDavid Greenman return; 310010cf3b9SDavid Greenman } 311010cf3b9SDavid Greenman } 312df8bae1dSRodney W. Grimes /* 3130d94caffSDavid Greenman * If there are still references, then we are done. 314df8bae1dSRodney W. Grimes */ 315df8bae1dSRodney W. Grimes return; 316df8bae1dSRodney W. Grimes } 317df8bae1dSRodney W. Grimes 31824a1cce3SDavid Greenman if (object->type == OBJT_VNODE) { 31924a1cce3SDavid Greenman struct vnode *vp = object->handle; 320f5cf85d4SDavid Greenman 32124a1cce3SDavid Greenman vp->v_flag &= ~VTEXT; 322f5cf85d4SDavid Greenman } 323f5cf85d4SDavid Greenman 324f919ebdeSDavid Greenman /* 325f919ebdeSDavid Greenman * See if this object can persist and has some resident 326f919ebdeSDavid Greenman * pages. If so, enter it in the cache. 327f919ebdeSDavid Greenman */ 328f5cf85d4SDavid Greenman if (object->flags & OBJ_CANPERSIST) { 329f5cf85d4SDavid Greenman if (object->resident_page_count != 0) { 33024a1cce3SDavid Greenman vm_object_page_clean(object, 0, 0 ,TRUE, TRUE); 331df8bae1dSRodney W. Grimes TAILQ_INSERT_TAIL(&vm_object_cached_list, object, 332df8bae1dSRodney W. Grimes cached_list); 333df8bae1dSRodney W. Grimes vm_object_cached++; 334df8bae1dSRodney W. Grimes 335df8bae1dSRodney W. Grimes vm_object_cache_trim(); 336df8bae1dSRodney W. Grimes return; 337f5cf85d4SDavid Greenman } else { 338f5cf85d4SDavid Greenman object->flags &= ~OBJ_CANPERSIST; 339f5cf85d4SDavid Greenman } 340df8bae1dSRodney W. Grimes } 341f919ebdeSDavid Greenman 342df8bae1dSRodney W. Grimes /* 34324a1cce3SDavid Greenman * Make sure no one uses us. 344df8bae1dSRodney W. Grimes */ 3450d94caffSDavid Greenman object->flags |= OBJ_DEAD; 346df8bae1dSRodney W. Grimes 34724a1cce3SDavid Greenman temp = object->backing_object; 3482fe6e4d7SDavid Greenman if (temp) 34924a1cce3SDavid Greenman TAILQ_REMOVE(&temp->shadow_head, object, shadow_list); 350df8bae1dSRodney W. Grimes vm_object_terminate(object); 351df8bae1dSRodney W. Grimes /* unlocks and deallocates object */ 352df8bae1dSRodney W. Grimes object = temp; 353df8bae1dSRodney W. Grimes } 354df8bae1dSRodney W. Grimes } 355df8bae1dSRodney W. Grimes 356df8bae1dSRodney W. Grimes /* 357df8bae1dSRodney W. Grimes * vm_object_terminate actually destroys the specified object, freeing 358df8bae1dSRodney W. Grimes * up all previously used resources. 359df8bae1dSRodney W. Grimes * 360df8bae1dSRodney W. Grimes * The object must be locked. 361df8bae1dSRodney W. Grimes */ 36226f9a767SRodney W. Grimes void 36326f9a767SRodney W. Grimes vm_object_terminate(object) 364df8bae1dSRodney W. Grimes register vm_object_t object; 365df8bae1dSRodney W. Grimes { 3663af76890SPoul-Henning Kamp register vm_page_t p; 36726f9a767SRodney W. Grimes int s; 368df8bae1dSRodney W. Grimes 369df8bae1dSRodney W. Grimes /* 370f6b04d2bSDavid Greenman * wait for the pageout daemon to be done with the object 371df8bae1dSRodney W. Grimes */ 3720d94caffSDavid Greenman s = splhigh(); 373df8bae1dSRodney W. Grimes while (object->paging_in_progress) { 374c0503609SDavid Greenman object->flags |= OBJ_PIPWNT; 37524a1cce3SDavid Greenman tsleep(object, PVM, "objtrm", 0); 376df8bae1dSRodney W. Grimes } 3770d94caffSDavid Greenman splx(s); 378df8bae1dSRodney W. Grimes 37926f9a767SRodney W. Grimes if (object->paging_in_progress != 0) 38026f9a767SRodney W. Grimes panic("vm_object_deallocate: pageout in progress"); 38126f9a767SRodney W. Grimes 38226f9a767SRodney W. Grimes /* 3830d94caffSDavid Greenman * Clean and free the pages, as appropriate. All references to the 3840d94caffSDavid Greenman * object are gone, so we don't need to lock it. 38526f9a767SRodney W. Grimes */ 38624a1cce3SDavid Greenman if (object->type == OBJT_VNODE) { 38724a1cce3SDavid Greenman struct vnode *vp = object->handle; 388f6b04d2bSDavid Greenman 3890426122fSDavid Greenman VOP_LOCK(vp); 39024a1cce3SDavid Greenman vm_object_page_clean(object, 0, 0, TRUE, FALSE); 391f6b04d2bSDavid Greenman vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0); 3920426122fSDavid Greenman VOP_UNLOCK(vp); 393df8bae1dSRodney W. Grimes } 39483edfd47SDavid Greenman 3950d94caffSDavid Greenman /* 3960d94caffSDavid Greenman * Now free the pages. For internal objects, this also removes them 3970d94caffSDavid Greenman * from paging queues. 398df8bae1dSRodney W. Grimes */ 399df8bae1dSRodney W. Grimes while ((p = object->memq.tqh_first) != NULL) { 400be6d5bfaSDavid Greenman if (p->flags & PG_BUSY) 401be6d5bfaSDavid Greenman printf("vm_object_terminate: freeing busy page\n"); 4020d94caffSDavid Greenman PAGE_WAKEUP(p); 403df8bae1dSRodney W. Grimes vm_page_free(p); 404df8bae1dSRodney W. Grimes cnt.v_pfree++; 405df8bae1dSRodney W. Grimes } 406df8bae1dSRodney W. Grimes 407df8bae1dSRodney W. Grimes /* 408df8bae1dSRodney W. Grimes * Let the pager know object is dead. 409df8bae1dSRodney W. Grimes */ 41024a1cce3SDavid Greenman vm_pager_deallocate(object); 411df8bae1dSRodney W. Grimes 412df8bae1dSRodney W. Grimes TAILQ_REMOVE(&vm_object_list, object, object_list); 413df8bae1dSRodney W. Grimes vm_object_count--; 414df8bae1dSRodney W. Grimes 4150426122fSDavid Greenman wakeup(object); 4160426122fSDavid Greenman 417df8bae1dSRodney W. Grimes /* 418df8bae1dSRodney W. Grimes * Free the space for the object. 419df8bae1dSRodney W. Grimes */ 420df8bae1dSRodney W. Grimes free((caddr_t) object, M_VMOBJ); 421df8bae1dSRodney W. Grimes } 422df8bae1dSRodney W. Grimes 423df8bae1dSRodney W. Grimes /* 424df8bae1dSRodney W. Grimes * vm_object_page_clean 425df8bae1dSRodney W. Grimes * 426df8bae1dSRodney W. Grimes * Clean all dirty pages in the specified range of object. 42726f9a767SRodney W. Grimes * Leaves page on whatever queue it is currently on. 42826f9a767SRodney W. Grimes * 42926f9a767SRodney W. Grimes * Odd semantics: if start == end, we clean everything. 43026f9a767SRodney W. Grimes * 43126f9a767SRodney W. Grimes * The object must be locked. 43226f9a767SRodney W. Grimes */ 433f6b04d2bSDavid Greenman 434f6b04d2bSDavid Greenman void 43524a1cce3SDavid Greenman vm_object_page_clean(object, start, end, syncio, lockflag) 436f6b04d2bSDavid Greenman vm_object_t object; 437a316d390SJohn Dyson vm_pindex_t start; 438a316d390SJohn Dyson vm_pindex_t end; 439f6b04d2bSDavid Greenman boolean_t syncio; 44024a1cce3SDavid Greenman boolean_t lockflag; 441f6b04d2bSDavid Greenman { 442f6b04d2bSDavid Greenman register vm_page_t p; 443f6b04d2bSDavid Greenman register vm_offset_t tstart, tend; 444aef922f5SJohn Dyson int s; 44524a1cce3SDavid Greenman struct vnode *vp; 446aef922f5SJohn Dyson int runlen; 447aef922f5SJohn Dyson vm_page_t ma[vm_pageout_page_count]; 448f6b04d2bSDavid Greenman 449aef922f5SJohn Dyson if (object->type != OBJT_VNODE || 450aef922f5SJohn Dyson (object->flags & OBJ_MIGHTBEDIRTY) == 0) 451f6b04d2bSDavid Greenman return; 452f6b04d2bSDavid Greenman 45324a1cce3SDavid Greenman vp = object->handle; 45424a1cce3SDavid Greenman 45524a1cce3SDavid Greenman if (lockflag) 45624a1cce3SDavid Greenman VOP_LOCK(vp); 457aef922f5SJohn Dyson object->flags |= OBJ_CLEANING; 45824a1cce3SDavid Greenman 459f6b04d2bSDavid Greenman tstart = start; 460f6b04d2bSDavid Greenman if (end == 0) { 461f6b04d2bSDavid Greenman tend = object->size; 462f6b04d2bSDavid Greenman } else { 463f6b04d2bSDavid Greenman tend = end; 464f6b04d2bSDavid Greenman } 465a316d390SJohn Dyson if ((tstart == 0) && (tend == object->size)) { 466aef922f5SJohn Dyson object->flags &= ~(OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY); 467ec4f9fb0SDavid Greenman } 468f6b04d2bSDavid Greenman 469aef922f5SJohn Dyson runlen = 0; 470a316d390SJohn Dyson for(;tstart < tend; tstart += 1) { 471aef922f5SJohn Dyson relookup: 472f6b04d2bSDavid Greenman p = vm_page_lookup(object, tstart); 473aef922f5SJohn Dyson if (!p) { 474aef922f5SJohn Dyson if (runlen > 0) { 475aef922f5SJohn Dyson vm_pageout_flush(ma, runlen, syncio); 476aef922f5SJohn Dyson runlen = 0; 477aef922f5SJohn Dyson } 478f6b04d2bSDavid Greenman continue; 479f6b04d2bSDavid Greenman } 480a316d390SJohn Dyson if ((p->valid == 0) || (p->flags & PG_CACHE)) { 481aef922f5SJohn Dyson if (runlen > 0) { 482aef922f5SJohn Dyson vm_pageout_flush(ma, runlen, syncio); 483aef922f5SJohn Dyson runlen = 0; 4847e15fd27SJohn Dyson } 485aef922f5SJohn Dyson continue; 486f6b04d2bSDavid Greenman } 487f6b04d2bSDavid Greenman 488ec4f9fb0SDavid Greenman vm_page_protect(p, VM_PROT_READ); 489ec4f9fb0SDavid Greenman 490f6b04d2bSDavid Greenman s = splhigh(); 491aef922f5SJohn Dyson while ((p->flags & PG_BUSY) || p->busy) { 492aef922f5SJohn Dyson if (runlen > 0) { 493f6b04d2bSDavid Greenman splx(s); 494aef922f5SJohn Dyson vm_pageout_flush(ma, runlen, syncio); 495aef922f5SJohn Dyson runlen = 0; 496aef922f5SJohn Dyson goto relookup; 497aef922f5SJohn Dyson } 498aef922f5SJohn Dyson p->flags |= PG_WANTED|PG_REFERENCED; 499aef922f5SJohn Dyson tsleep(p, PVM, "vpcwai", 0); 500f6b04d2bSDavid Greenman splx(s); 501aef922f5SJohn Dyson goto relookup; 502f6b04d2bSDavid Greenman } 503f6b04d2bSDavid Greenman splx(s); 504f6b04d2bSDavid Greenman 505aef922f5SJohn Dyson if (p->dirty == 0) 506f6b04d2bSDavid Greenman vm_page_test_dirty(p); 507aef922f5SJohn Dyson 508f6b04d2bSDavid Greenman if ((p->valid & p->dirty) != 0) { 509aef922f5SJohn Dyson ma[runlen] = p; 510aef922f5SJohn Dyson p->flags |= PG_BUSY; 511aef922f5SJohn Dyson runlen++; 512aef922f5SJohn Dyson if (runlen >= vm_pageout_page_count) { 513aef922f5SJohn Dyson vm_pageout_flush(ma, runlen, syncio); 514aef922f5SJohn Dyson runlen = 0; 515f6b04d2bSDavid Greenman } 516aef922f5SJohn Dyson } else if (runlen > 0) { 517aef922f5SJohn Dyson vm_pageout_flush(ma, runlen, syncio); 518aef922f5SJohn Dyson runlen = 0; 519f6b04d2bSDavid Greenman } 520aef922f5SJohn Dyson 521f6b04d2bSDavid Greenman } 522aef922f5SJohn Dyson if (runlen > 0) { 523aef922f5SJohn Dyson vm_pageout_flush(ma, runlen, syncio); 524f6b04d2bSDavid Greenman } 525aef922f5SJohn Dyson 526aef922f5SJohn Dyson VOP_FSYNC(vp, NULL, syncio, curproc); 527aef922f5SJohn Dyson 52824a1cce3SDavid Greenman if (lockflag) 529d3628763SRodney W. Grimes VOP_UNLOCK(vp); 530aef922f5SJohn Dyson object->flags &= ~OBJ_CLEANING; 531f5cf85d4SDavid Greenman return; 53226f9a767SRodney W. Grimes } 533df8bae1dSRodney W. Grimes 534df8bae1dSRodney W. Grimes /* 535df8bae1dSRodney W. Grimes * vm_object_deactivate_pages 536df8bae1dSRodney W. Grimes * 537df8bae1dSRodney W. Grimes * Deactivate all pages in the specified object. (Keep its pages 538df8bae1dSRodney W. Grimes * in memory even though it is no longer referenced.) 539df8bae1dSRodney W. Grimes * 540df8bae1dSRodney W. Grimes * The object must be locked. 541df8bae1dSRodney W. Grimes */ 542df8bae1dSRodney W. Grimes void 543df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object) 544df8bae1dSRodney W. Grimes register vm_object_t object; 545df8bae1dSRodney W. Grimes { 546df8bae1dSRodney W. Grimes register vm_page_t p, next; 547df8bae1dSRodney W. Grimes 548df8bae1dSRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = next) { 549df8bae1dSRodney W. Grimes next = p->listq.tqe_next; 550df8bae1dSRodney W. Grimes vm_page_deactivate(p); 551df8bae1dSRodney W. Grimes } 552df8bae1dSRodney W. Grimes } 553df8bae1dSRodney W. Grimes 554df8bae1dSRodney W. Grimes /* 555df8bae1dSRodney W. Grimes * Trim the object cache to size. 556df8bae1dSRodney W. Grimes */ 557df8bae1dSRodney W. Grimes void 558df8bae1dSRodney W. Grimes vm_object_cache_trim() 559df8bae1dSRodney W. Grimes { 560df8bae1dSRodney W. Grimes register vm_object_t object; 561df8bae1dSRodney W. Grimes 5620d94caffSDavid Greenman while (vm_object_cached > vm_object_cache_max) { 563df8bae1dSRodney W. Grimes object = vm_object_cached_list.tqh_first; 564df8bae1dSRodney W. Grimes 56524a1cce3SDavid Greenman vm_object_reference(object); 566df8bae1dSRodney W. Grimes pager_cache(object, FALSE); 567df8bae1dSRodney W. Grimes } 568df8bae1dSRodney W. Grimes } 569df8bae1dSRodney W. Grimes 57026f9a767SRodney W. Grimes 571df8bae1dSRodney W. Grimes /* 572df8bae1dSRodney W. Grimes * vm_object_pmap_copy: 573df8bae1dSRodney W. Grimes * 574df8bae1dSRodney W. Grimes * Makes all physical pages in the specified 575df8bae1dSRodney W. Grimes * object range copy-on-write. No writeable 576df8bae1dSRodney W. Grimes * references to these pages should remain. 577df8bae1dSRodney W. Grimes * 578df8bae1dSRodney W. Grimes * The object must *not* be locked. 579df8bae1dSRodney W. Grimes */ 5800d94caffSDavid Greenman void 5810d94caffSDavid Greenman vm_object_pmap_copy(object, start, end) 582df8bae1dSRodney W. Grimes register vm_object_t object; 583a316d390SJohn Dyson register vm_pindex_t start; 584a316d390SJohn Dyson register vm_pindex_t end; 585df8bae1dSRodney W. Grimes { 586df8bae1dSRodney W. Grimes register vm_page_t p; 587df8bae1dSRodney W. Grimes 588aef922f5SJohn Dyson if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0) 589df8bae1dSRodney W. Grimes return; 590df8bae1dSRodney W. Grimes 591df8bae1dSRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) { 592f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_READ); 593df8bae1dSRodney W. Grimes } 594aef922f5SJohn Dyson 595aef922f5SJohn Dyson object->flags &= ~OBJ_WRITEABLE; 596df8bae1dSRodney W. Grimes } 597df8bae1dSRodney W. Grimes 598df8bae1dSRodney W. Grimes /* 599df8bae1dSRodney W. Grimes * vm_object_pmap_remove: 600df8bae1dSRodney W. Grimes * 601df8bae1dSRodney W. Grimes * Removes all physical pages in the specified 602df8bae1dSRodney W. Grimes * object range from all physical maps. 603df8bae1dSRodney W. Grimes * 604df8bae1dSRodney W. Grimes * The object must *not* be locked. 605df8bae1dSRodney W. Grimes */ 60626f9a767SRodney W. Grimes void 60726f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end) 608df8bae1dSRodney W. Grimes register vm_object_t object; 609a316d390SJohn Dyson register vm_pindex_t start; 610a316d390SJohn Dyson register vm_pindex_t end; 611df8bae1dSRodney W. Grimes { 612df8bae1dSRodney W. Grimes register vm_page_t p; 613df8bae1dSRodney W. Grimes if (object == NULL) 614df8bae1dSRodney W. Grimes return; 61526f9a767SRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) { 616f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 61726f9a767SRodney W. Grimes } 61826f9a767SRodney W. Grimes } 619df8bae1dSRodney W. Grimes 620df8bae1dSRodney W. Grimes /* 621df8bae1dSRodney W. Grimes * vm_object_copy: 622df8bae1dSRodney W. Grimes * 623df8bae1dSRodney W. Grimes * Create a new object which is a copy of an existing 624df8bae1dSRodney W. Grimes * object, and mark all of the pages in the existing 625df8bae1dSRodney W. Grimes * object 'copy-on-write'. The new object has one reference. 626df8bae1dSRodney W. Grimes * Returns the new object. 627df8bae1dSRodney W. Grimes * 628df8bae1dSRodney W. Grimes * May defer the copy until later if the object is not backed 629df8bae1dSRodney W. Grimes * up by a non-default pager. 630df8bae1dSRodney W. Grimes */ 6310d94caffSDavid Greenman void 632a316d390SJohn Dyson vm_object_copy(src_object, src_offset, 633df8bae1dSRodney W. Grimes dst_object, dst_offset, src_needs_copy) 634df8bae1dSRodney W. Grimes register vm_object_t src_object; 635a316d390SJohn Dyson vm_pindex_t src_offset; 636df8bae1dSRodney W. Grimes vm_object_t *dst_object;/* OUT */ 637a316d390SJohn Dyson vm_pindex_t *dst_offset;/* OUT */ 638df8bae1dSRodney W. Grimes boolean_t *src_needs_copy; /* OUT */ 639df8bae1dSRodney W. Grimes { 640df8bae1dSRodney W. Grimes if (src_object == NULL) { 641df8bae1dSRodney W. Grimes /* 642df8bae1dSRodney W. Grimes * Nothing to copy 643df8bae1dSRodney W. Grimes */ 644df8bae1dSRodney W. Grimes *dst_object = NULL; 645df8bae1dSRodney W. Grimes *dst_offset = 0; 646df8bae1dSRodney W. Grimes *src_needs_copy = FALSE; 647df8bae1dSRodney W. Grimes return; 648df8bae1dSRodney W. Grimes } 64926f9a767SRodney W. Grimes 65026f9a767SRodney W. Grimes /* 65126f9a767SRodney W. Grimes * Try to collapse the object before copying it. 65226f9a767SRodney W. Grimes */ 65324a1cce3SDavid Greenman if (src_object->handle == NULL && 65424a1cce3SDavid Greenman (src_object->type == OBJT_DEFAULT || 65524a1cce3SDavid Greenman src_object->type == OBJT_SWAP)) 65626f9a767SRodney W. Grimes vm_object_collapse(src_object); 65726f9a767SRodney W. Grimes 658df8bae1dSRodney W. Grimes 659df8bae1dSRodney W. Grimes /* 660df8bae1dSRodney W. Grimes * Make another reference to the object 661df8bae1dSRodney W. Grimes */ 662df8bae1dSRodney W. Grimes src_object->ref_count++; 663df8bae1dSRodney W. Grimes 664df8bae1dSRodney W. Grimes *dst_object = src_object; 665df8bae1dSRodney W. Grimes *dst_offset = src_offset; 666df8bae1dSRodney W. Grimes 667df8bae1dSRodney W. Grimes /* 668df8bae1dSRodney W. Grimes * Must make a shadow when write is desired 669df8bae1dSRodney W. Grimes */ 670df8bae1dSRodney W. Grimes *src_needs_copy = TRUE; 671df8bae1dSRodney W. Grimes return; 672df8bae1dSRodney W. Grimes } 673df8bae1dSRodney W. Grimes 674df8bae1dSRodney W. Grimes /* 675df8bae1dSRodney W. Grimes * vm_object_shadow: 676df8bae1dSRodney W. Grimes * 677df8bae1dSRodney W. Grimes * Create a new object which is backed by the 678df8bae1dSRodney W. Grimes * specified existing object range. The source 679df8bae1dSRodney W. Grimes * object reference is deallocated. 680df8bae1dSRodney W. Grimes * 681df8bae1dSRodney W. Grimes * The new object and offset into that object 682df8bae1dSRodney W. Grimes * are returned in the source parameters. 683df8bae1dSRodney W. Grimes */ 684df8bae1dSRodney W. Grimes 68526f9a767SRodney W. Grimes void 68626f9a767SRodney W. Grimes vm_object_shadow(object, offset, length) 687df8bae1dSRodney W. Grimes vm_object_t *object; /* IN/OUT */ 688a316d390SJohn Dyson vm_ooffset_t *offset; /* IN/OUT */ 689df8bae1dSRodney W. Grimes vm_size_t length; 690df8bae1dSRodney W. Grimes { 691df8bae1dSRodney W. Grimes register vm_object_t source; 692df8bae1dSRodney W. Grimes register vm_object_t result; 693df8bae1dSRodney W. Grimes 694df8bae1dSRodney W. Grimes source = *object; 695df8bae1dSRodney W. Grimes 696df8bae1dSRodney W. Grimes /* 697df8bae1dSRodney W. Grimes * Allocate a new object with the given length 698df8bae1dSRodney W. Grimes */ 699df8bae1dSRodney W. Grimes 70024a1cce3SDavid Greenman if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL) 701df8bae1dSRodney W. Grimes panic("vm_object_shadow: no object for shadowing"); 702df8bae1dSRodney W. Grimes 703df8bae1dSRodney W. Grimes /* 7040d94caffSDavid Greenman * The new object shadows the source object, adding a reference to it. 7050d94caffSDavid Greenman * Our caller changes his reference to point to the new object, 7060d94caffSDavid Greenman * removing a reference to the source object. Net result: no change 7070d94caffSDavid Greenman * of reference count. 708df8bae1dSRodney W. Grimes */ 70924a1cce3SDavid Greenman result->backing_object = source; 7107b18a718SDavid Greenman if (source) 71124a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&result->backing_object->shadow_head, result, shadow_list); 712df8bae1dSRodney W. Grimes 713df8bae1dSRodney W. Grimes /* 7140d94caffSDavid Greenman * Store the offset into the source object, and fix up the offset into 7150d94caffSDavid Greenman * the new object. 716df8bae1dSRodney W. Grimes */ 717df8bae1dSRodney W. Grimes 71824a1cce3SDavid Greenman result->backing_object_offset = *offset; 719df8bae1dSRodney W. Grimes 720df8bae1dSRodney W. Grimes /* 721df8bae1dSRodney W. Grimes * Return the new things 722df8bae1dSRodney W. Grimes */ 723df8bae1dSRodney W. Grimes 724df8bae1dSRodney W. Grimes *offset = 0; 725df8bae1dSRodney W. Grimes *object = result; 726df8bae1dSRodney W. Grimes } 727df8bae1dSRodney W. Grimes 728df8bae1dSRodney W. Grimes 729df8bae1dSRodney W. Grimes /* 7302fe6e4d7SDavid Greenman * this version of collapse allows the operation to occur earlier and 7312fe6e4d7SDavid Greenman * when paging_in_progress is true for an object... This is not a complete 7322fe6e4d7SDavid Greenman * operation, but should plug 99.9% of the rest of the leaks. 7332fe6e4d7SDavid Greenman */ 7342fe6e4d7SDavid Greenman static void 7352fe6e4d7SDavid Greenman vm_object_qcollapse(object) 7362fe6e4d7SDavid Greenman register vm_object_t object; 7372fe6e4d7SDavid Greenman { 7382fe6e4d7SDavid Greenman register vm_object_t backing_object; 739a316d390SJohn Dyson register vm_pindex_t backing_offset_index, paging_offset_index; 740a316d390SJohn Dyson vm_pindex_t backing_object_paging_offset_index; 741a316d390SJohn Dyson vm_pindex_t new_pindex; 7422fe6e4d7SDavid Greenman register vm_page_t p, pp; 7432fe6e4d7SDavid Greenman register vm_size_t size; 7442fe6e4d7SDavid Greenman 74524a1cce3SDavid Greenman backing_object = object->backing_object; 7462fe6e4d7SDavid Greenman if (backing_object->ref_count != 1) 7472fe6e4d7SDavid Greenman return; 7482fe6e4d7SDavid Greenman 749010cf3b9SDavid Greenman backing_object->ref_count += 2; 750010cf3b9SDavid Greenman 751a316d390SJohn Dyson backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 752a316d390SJohn Dyson backing_object_paging_offset_index = OFF_TO_IDX(backing_object->paging_offset); 753a316d390SJohn Dyson paging_offset_index = OFF_TO_IDX(object->paging_offset); 7542fe6e4d7SDavid Greenman size = object->size; 7552fe6e4d7SDavid Greenman p = backing_object->memq.tqh_first; 7562fe6e4d7SDavid Greenman while (p) { 7572fe6e4d7SDavid Greenman vm_page_t next; 7580d94caffSDavid Greenman 7592fe6e4d7SDavid Greenman next = p->listq.tqe_next; 7600d94caffSDavid Greenman if ((p->flags & (PG_BUSY | PG_FICTITIOUS | PG_CACHE)) || 761f6b04d2bSDavid Greenman !p->valid || p->hold_count || p->wire_count || p->busy) { 7622fe6e4d7SDavid Greenman p = next; 7632fe6e4d7SDavid Greenman continue; 7642fe6e4d7SDavid Greenman } 765f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 766a316d390SJohn Dyson new_pindex = p->pindex - backing_offset_index; 767a316d390SJohn Dyson if (p->pindex < backing_offset_index || 768a316d390SJohn Dyson new_pindex >= size) { 76924a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) 77024a1cce3SDavid Greenman swap_pager_freespace(backing_object, 771a316d390SJohn Dyson backing_object_paging_offset_index+p->pindex, 772a316d390SJohn Dyson 1); 7732fe6e4d7SDavid Greenman vm_page_free(p); 7742fe6e4d7SDavid Greenman } else { 775a316d390SJohn Dyson pp = vm_page_lookup(object, new_pindex); 77624a1cce3SDavid Greenman if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object, 777a316d390SJohn Dyson paging_offset_index + new_pindex, NULL, NULL))) { 77824a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) 77924a1cce3SDavid Greenman swap_pager_freespace(backing_object, 780a316d390SJohn Dyson backing_object_paging_offset_index + p->pindex, 1); 7812fe6e4d7SDavid Greenman vm_page_free(p); 7822fe6e4d7SDavid Greenman } else { 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); 786a316d390SJohn Dyson vm_page_rename(p, object, new_pindex); 7879b4814bbSDavid Greenman p->dirty = VM_PAGE_BITS_ALL; 7882fe6e4d7SDavid Greenman } 7892fe6e4d7SDavid Greenman } 7902fe6e4d7SDavid Greenman p = next; 7912fe6e4d7SDavid Greenman } 792010cf3b9SDavid Greenman backing_object->ref_count -= 2; 7932fe6e4d7SDavid Greenman } 7942fe6e4d7SDavid Greenman 795df8bae1dSRodney W. Grimes /* 796df8bae1dSRodney W. Grimes * vm_object_collapse: 797df8bae1dSRodney W. Grimes * 798df8bae1dSRodney W. Grimes * Collapse an object with the object backing it. 799df8bae1dSRodney W. Grimes * Pages in the backing object are moved into the 800df8bae1dSRodney W. Grimes * parent, and the backing object is deallocated. 801df8bae1dSRodney W. Grimes */ 80226f9a767SRodney W. Grimes void 80326f9a767SRodney W. Grimes vm_object_collapse(object) 80424a1cce3SDavid Greenman vm_object_t object; 805df8bae1dSRodney W. Grimes 806df8bae1dSRodney W. Grimes { 80724a1cce3SDavid Greenman vm_object_t backing_object; 808a316d390SJohn Dyson vm_ooffset_t backing_offset; 80924a1cce3SDavid Greenman vm_size_t size; 810a316d390SJohn Dyson vm_pindex_t new_pindex, backing_offset_index; 81124a1cce3SDavid Greenman vm_page_t p, pp; 812df8bae1dSRodney W. Grimes 813df8bae1dSRodney W. Grimes while (TRUE) { 814df8bae1dSRodney W. Grimes /* 815df8bae1dSRodney W. Grimes * Verify that the conditions are right for collapse: 816df8bae1dSRodney W. Grimes * 8170d94caffSDavid Greenman * The object exists and no pages in it are currently being paged 8180d94caffSDavid Greenman * out. 819df8bae1dSRodney W. Grimes */ 8202fe6e4d7SDavid Greenman if (object == NULL) 821df8bae1dSRodney W. Grimes return; 822df8bae1dSRodney W. Grimes 823b9921222SDavid Greenman /* 824b9921222SDavid Greenman * Make sure there is a backing object. 825b9921222SDavid Greenman */ 82624a1cce3SDavid Greenman if ((backing_object = object->backing_object) == NULL) 827df8bae1dSRodney W. Grimes return; 828df8bae1dSRodney W. Grimes 829f919ebdeSDavid Greenman /* 830f919ebdeSDavid Greenman * we check the backing object first, because it is most likely 83124a1cce3SDavid Greenman * not collapsable. 832f919ebdeSDavid Greenman */ 83324a1cce3SDavid Greenman if (backing_object->handle != NULL || 83424a1cce3SDavid Greenman (backing_object->type != OBJT_DEFAULT && 83524a1cce3SDavid Greenman backing_object->type != OBJT_SWAP) || 836f919ebdeSDavid Greenman (backing_object->flags & OBJ_DEAD) || 83724a1cce3SDavid Greenman object->handle != NULL || 83824a1cce3SDavid Greenman (object->type != OBJT_DEFAULT && 83924a1cce3SDavid Greenman object->type != OBJT_SWAP) || 84024a1cce3SDavid Greenman (object->flags & OBJ_DEAD)) { 8419b4814bbSDavid Greenman return; 84224a1cce3SDavid Greenman } 8439b4814bbSDavid Greenman 844f919ebdeSDavid Greenman if (object->paging_in_progress != 0 || 845f919ebdeSDavid Greenman backing_object->paging_in_progress != 0) { 846b9921222SDavid Greenman vm_object_qcollapse(object); 847df8bae1dSRodney W. Grimes return; 848df8bae1dSRodney W. Grimes } 849f919ebdeSDavid Greenman 85026f9a767SRodney W. Grimes /* 8510d94caffSDavid Greenman * We know that we can either collapse the backing object (if 8520d94caffSDavid Greenman * the parent is the only reference to it) or (perhaps) remove 8530d94caffSDavid Greenman * the parent's reference to it. 854df8bae1dSRodney W. Grimes */ 855df8bae1dSRodney W. Grimes 85624a1cce3SDavid Greenman backing_offset = object->backing_object_offset; 857a316d390SJohn Dyson backing_offset_index = OFF_TO_IDX(backing_offset); 858df8bae1dSRodney W. Grimes size = object->size; 859df8bae1dSRodney W. Grimes 860df8bae1dSRodney W. Grimes /* 8610d94caffSDavid Greenman * If there is exactly one reference to the backing object, we 8620d94caffSDavid Greenman * can collapse it into the parent. 863df8bae1dSRodney W. Grimes */ 864df8bae1dSRodney W. Grimes 865df8bae1dSRodney W. Grimes if (backing_object->ref_count == 1) { 866df8bae1dSRodney W. Grimes 867a1f6d91cSDavid Greenman backing_object->flags |= OBJ_DEAD; 868df8bae1dSRodney W. Grimes /* 869df8bae1dSRodney W. Grimes * We can collapse the backing object. 870df8bae1dSRodney W. Grimes * 8710d94caffSDavid Greenman * Move all in-memory pages from backing_object to the 8720d94caffSDavid Greenman * parent. Pages that have been paged out will be 8730d94caffSDavid Greenman * overwritten by any of the parent's pages that 8740d94caffSDavid Greenman * shadow them. 875df8bae1dSRodney W. Grimes */ 876df8bae1dSRodney W. Grimes 87705f0fdd2SPoul-Henning Kamp while ((p = backing_object->memq.tqh_first) != 0) { 87826f9a767SRodney W. Grimes 879a316d390SJohn Dyson new_pindex = p->pindex - backing_offset_index; 880df8bae1dSRodney W. Grimes 881df8bae1dSRodney W. Grimes /* 8820d94caffSDavid Greenman * If the parent has a page here, or if this 8830d94caffSDavid Greenman * page falls outside the parent, dispose of 8840d94caffSDavid Greenman * it. 885df8bae1dSRodney W. Grimes * 886df8bae1dSRodney W. Grimes * Otherwise, move it as planned. 887df8bae1dSRodney W. Grimes */ 888df8bae1dSRodney W. Grimes 889a316d390SJohn Dyson if (p->pindex < backing_offset_index || 890a316d390SJohn Dyson new_pindex >= size) { 891f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 8920d94caffSDavid Greenman PAGE_WAKEUP(p); 893df8bae1dSRodney W. Grimes vm_page_free(p); 894df8bae1dSRodney W. Grimes } else { 895a316d390SJohn Dyson pp = vm_page_lookup(object, new_pindex); 89624a1cce3SDavid Greenman if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object, 897a316d390SJohn Dyson OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL))) { 898f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 8990d94caffSDavid Greenman PAGE_WAKEUP(p); 900df8bae1dSRodney W. Grimes vm_page_free(p); 90126f9a767SRodney W. Grimes } else { 902a316d390SJohn Dyson vm_page_rename(p, object, new_pindex); 903df8bae1dSRodney W. Grimes } 904df8bae1dSRodney W. Grimes } 905df8bae1dSRodney W. Grimes } 906df8bae1dSRodney W. Grimes 907df8bae1dSRodney W. Grimes /* 908df8bae1dSRodney W. Grimes * Move the pager from backing_object to object. 909df8bae1dSRodney W. Grimes */ 910df8bae1dSRodney W. Grimes 91124a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) { 91226f9a767SRodney W. Grimes backing_object->paging_in_progress++; 91324a1cce3SDavid Greenman if (object->type == OBJT_SWAP) { 91426f9a767SRodney W. Grimes object->paging_in_progress++; 91526f9a767SRodney W. Grimes /* 91626f9a767SRodney W. Grimes * copy shadow object pages into ours 9170d94caffSDavid Greenman * and destroy unneeded pages in 9180d94caffSDavid Greenman * shadow object. 91926f9a767SRodney W. Grimes */ 92026f9a767SRodney W. Grimes swap_pager_copy( 921a316d390SJohn Dyson backing_object, 922a316d390SJohn Dyson OFF_TO_IDX(backing_object->paging_offset), 923a316d390SJohn Dyson object, 924a316d390SJohn Dyson OFF_TO_IDX(object->paging_offset), 925a316d390SJohn Dyson OFF_TO_IDX(object->backing_object_offset)); 926f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 92726f9a767SRodney W. Grimes } else { 92826f9a767SRodney W. Grimes object->paging_in_progress++; 92926f9a767SRodney W. Grimes /* 93024a1cce3SDavid Greenman * move the shadow backing_object's pager data to 93124a1cce3SDavid Greenman * "object" and convert "object" type to OBJT_SWAP. 93226f9a767SRodney W. Grimes */ 93324a1cce3SDavid Greenman object->type = OBJT_SWAP; 9342a4895f4SDavid Greenman object->un_pager.swp.swp_nblocks = 9352a4895f4SDavid Greenman backing_object->un_pager.swp.swp_nblocks; 9362a4895f4SDavid Greenman object->un_pager.swp.swp_allocsize = 9372a4895f4SDavid Greenman backing_object->un_pager.swp.swp_allocsize; 9382a4895f4SDavid Greenman object->un_pager.swp.swp_blocks = 9392a4895f4SDavid Greenman backing_object->un_pager.swp.swp_blocks; 9402a4895f4SDavid Greenman object->un_pager.swp.swp_poip = /* XXX */ 9412a4895f4SDavid Greenman backing_object->un_pager.swp.swp_poip; 94226f9a767SRodney W. Grimes object->paging_offset = backing_object->paging_offset + backing_offset; 94324a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&swap_pager_un_object_list, object, pager_object_list); 94424a1cce3SDavid Greenman 94524a1cce3SDavid Greenman /* 94624a1cce3SDavid Greenman * Convert backing object from OBJT_SWAP to 94724a1cce3SDavid Greenman * OBJT_DEFAULT. XXX - only the TAILQ_REMOVE is 94824a1cce3SDavid Greenman * actually necessary. 94924a1cce3SDavid Greenman */ 95024a1cce3SDavid Greenman backing_object->type = OBJT_DEFAULT; 95124a1cce3SDavid Greenman TAILQ_REMOVE(&swap_pager_un_object_list, backing_object, pager_object_list); 95226f9a767SRodney W. Grimes /* 95326f9a767SRodney W. Grimes * free unnecessary blocks 95426f9a767SRodney W. Grimes */ 955a316d390SJohn Dyson swap_pager_freespace(object, 0, 956a316d390SJohn Dyson OFF_TO_IDX(object->paging_offset)); 957f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 958c0503609SDavid Greenman } 959c0503609SDavid Greenman 960f919ebdeSDavid Greenman vm_object_pip_wakeup(backing_object); 961c0503609SDavid Greenman } 962df8bae1dSRodney W. Grimes /* 963df8bae1dSRodney W. Grimes * Object now shadows whatever backing_object did. 96424a1cce3SDavid Greenman * Note that the reference to backing_object->backing_object 965df8bae1dSRodney W. Grimes * moves from within backing_object to within object. 966df8bae1dSRodney W. Grimes */ 967df8bae1dSRodney W. Grimes 96824a1cce3SDavid Greenman TAILQ_REMOVE(&object->backing_object->shadow_head, object, 96924a1cce3SDavid Greenman shadow_list); 97024a1cce3SDavid Greenman if (backing_object->backing_object) 97124a1cce3SDavid Greenman TAILQ_REMOVE(&backing_object->backing_object->shadow_head, 97224a1cce3SDavid Greenman backing_object, shadow_list); 97324a1cce3SDavid Greenman object->backing_object = backing_object->backing_object; 97424a1cce3SDavid Greenman if (object->backing_object) 97524a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&object->backing_object->shadow_head, 97624a1cce3SDavid Greenman object, shadow_list); 9772fe6e4d7SDavid Greenman 97824a1cce3SDavid Greenman object->backing_object_offset += backing_object->backing_object_offset; 979df8bae1dSRodney W. Grimes /* 980df8bae1dSRodney W. Grimes * Discard backing_object. 981df8bae1dSRodney W. Grimes * 9820d94caffSDavid Greenman * Since the backing object has no pages, no pager left, 9830d94caffSDavid Greenman * and no object references within it, all that is 9840d94caffSDavid Greenman * necessary is to dispose of it. 985df8bae1dSRodney W. Grimes */ 986df8bae1dSRodney W. Grimes 987df8bae1dSRodney W. Grimes TAILQ_REMOVE(&vm_object_list, backing_object, 988df8bae1dSRodney W. Grimes object_list); 989df8bae1dSRodney W. Grimes vm_object_count--; 990df8bae1dSRodney W. Grimes 991df8bae1dSRodney W. Grimes free((caddr_t) backing_object, M_VMOBJ); 992df8bae1dSRodney W. Grimes 993df8bae1dSRodney W. Grimes object_collapses++; 9940d94caffSDavid Greenman } else { 995df8bae1dSRodney W. Grimes /* 996df8bae1dSRodney W. Grimes * If all of the pages in the backing object are 9970d94caffSDavid Greenman * shadowed by the parent object, the parent object no 9980d94caffSDavid Greenman * longer has to shadow the backing object; it can 9990d94caffSDavid Greenman * shadow the next one in the chain. 1000df8bae1dSRodney W. Grimes * 10010d94caffSDavid Greenman * The backing object must not be paged out - we'd have 10020d94caffSDavid Greenman * to check all of the paged-out pages, as well. 1003df8bae1dSRodney W. Grimes */ 1004df8bae1dSRodney W. Grimes 100524a1cce3SDavid Greenman if (backing_object->type != OBJT_DEFAULT) { 1006df8bae1dSRodney W. Grimes return; 1007df8bae1dSRodney W. Grimes } 1008df8bae1dSRodney W. Grimes /* 10090d94caffSDavid Greenman * Should have a check for a 'small' number of pages 10100d94caffSDavid Greenman * here. 1011df8bae1dSRodney W. Grimes */ 1012df8bae1dSRodney W. Grimes 101326f9a767SRodney W. Grimes for (p = backing_object->memq.tqh_first; p; p = p->listq.tqe_next) { 1014a316d390SJohn Dyson new_pindex = p->pindex - backing_offset_index; 1015df8bae1dSRodney W. Grimes 1016df8bae1dSRodney W. Grimes /* 10170d94caffSDavid Greenman * If the parent has a page here, or if this 10180d94caffSDavid Greenman * page falls outside the parent, keep going. 1019df8bae1dSRodney W. Grimes * 10200d94caffSDavid Greenman * Otherwise, the backing_object must be left in 10210d94caffSDavid Greenman * the chain. 1022df8bae1dSRodney W. Grimes */ 1023df8bae1dSRodney W. Grimes 1024a316d390SJohn Dyson if (p->pindex >= backing_offset_index && 1025a316d390SJohn Dyson new_pindex <= size) { 102624a1cce3SDavid Greenman 1027a316d390SJohn Dyson pp = vm_page_lookup(object, new_pindex); 102824a1cce3SDavid Greenman 102924a1cce3SDavid Greenman if ((pp == NULL || pp->valid == 0) && 1030a316d390SJohn Dyson !vm_pager_has_page(object, OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL)) { 1031df8bae1dSRodney W. Grimes /* 10320d94caffSDavid Greenman * Page still needed. Can't go any 10330d94caffSDavid Greenman * further. 1034df8bae1dSRodney W. Grimes */ 1035df8bae1dSRodney W. Grimes return; 1036df8bae1dSRodney W. Grimes } 1037df8bae1dSRodney W. Grimes } 103824a1cce3SDavid Greenman } 1039df8bae1dSRodney W. Grimes 1040df8bae1dSRodney W. Grimes /* 10410d94caffSDavid Greenman * Make the parent shadow the next object in the 10420d94caffSDavid Greenman * chain. Deallocating backing_object will not remove 10430d94caffSDavid Greenman * it, since its reference count is at least 2. 1044df8bae1dSRodney W. Grimes */ 1045df8bae1dSRodney W. Grimes 104624a1cce3SDavid Greenman TAILQ_REMOVE(&object->backing_object->shadow_head, 104724a1cce3SDavid Greenman object, shadow_list); 104824a1cce3SDavid Greenman vm_object_reference(object->backing_object = backing_object->backing_object); 104924a1cce3SDavid Greenman if (object->backing_object) 105024a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&object->backing_object->shadow_head, 105124a1cce3SDavid Greenman object, shadow_list); 105224a1cce3SDavid Greenman object->backing_object_offset += backing_object->backing_object_offset; 1053df8bae1dSRodney W. Grimes 1054df8bae1dSRodney W. Grimes /* 10550d94caffSDavid Greenman * Drop the reference count on backing_object. Since 10560d94caffSDavid Greenman * its ref_count was at least 2, it will not vanish; 10570d94caffSDavid Greenman * so we don't need to call vm_object_deallocate. 1058df8bae1dSRodney W. Grimes */ 105926f9a767SRodney W. Grimes if (backing_object->ref_count == 1) 106026f9a767SRodney W. Grimes printf("should have called obj deallocate\n"); 1061df8bae1dSRodney W. Grimes backing_object->ref_count--; 1062df8bae1dSRodney W. Grimes 1063df8bae1dSRodney W. Grimes object_bypasses++; 1064df8bae1dSRodney W. Grimes 1065df8bae1dSRodney W. Grimes } 1066df8bae1dSRodney W. Grimes 1067df8bae1dSRodney W. Grimes /* 1068df8bae1dSRodney W. Grimes * Try again with this object's new backing object. 1069df8bae1dSRodney W. Grimes */ 1070df8bae1dSRodney W. Grimes } 1071df8bae1dSRodney W. Grimes } 1072df8bae1dSRodney W. Grimes 1073df8bae1dSRodney W. Grimes /* 1074df8bae1dSRodney W. Grimes * vm_object_page_remove: [internal] 1075df8bae1dSRodney W. Grimes * 1076df8bae1dSRodney W. Grimes * Removes all physical pages in the specified 1077df8bae1dSRodney W. Grimes * object range from the object's list of pages. 1078df8bae1dSRodney W. Grimes * 1079df8bae1dSRodney W. Grimes * The object must be locked. 1080df8bae1dSRodney W. Grimes */ 108126f9a767SRodney W. Grimes void 10827c1f6cedSDavid Greenman vm_object_page_remove(object, start, end, clean_only) 1083df8bae1dSRodney W. Grimes register vm_object_t object; 1084a316d390SJohn Dyson register vm_pindex_t start; 1085a316d390SJohn Dyson register vm_pindex_t end; 10867c1f6cedSDavid Greenman boolean_t clean_only; 1087df8bae1dSRodney W. Grimes { 1088df8bae1dSRodney W. Grimes register vm_page_t p, next; 1089a316d390SJohn Dyson unsigned int size; 109026f9a767SRodney W. Grimes int s; 1091df8bae1dSRodney W. Grimes 1092df8bae1dSRodney W. Grimes if (object == NULL) 1093df8bae1dSRodney W. Grimes return; 1094df8bae1dSRodney W. Grimes 10952fe6e4d7SDavid Greenman object->paging_in_progress++; 109626f9a767SRodney W. Grimes again: 109726f9a767SRodney W. Grimes size = end - start; 1098a316d390SJohn Dyson if (size > 4 || size >= object->size / 4) { 10990d94caffSDavid Greenman for (p = object->memq.tqh_first; p != NULL; p = next) { 1100df8bae1dSRodney W. Grimes next = p->listq.tqe_next; 1101a316d390SJohn Dyson if ((start <= p->pindex) && (p->pindex < end)) { 1102a481f200SDavid Greenman s = splhigh(); 11030d94caffSDavid Greenman if (p->bmapped) { 11040d94caffSDavid Greenman splx(s); 11050d94caffSDavid Greenman continue; 11060d94caffSDavid Greenman } 11070d94caffSDavid Greenman if ((p->flags & PG_BUSY) || p->busy) { 110826f9a767SRodney W. Grimes p->flags |= PG_WANTED; 110924a1cce3SDavid Greenman tsleep(p, PVM, "vmopar", 0); 1110a481f200SDavid Greenman splx(s); 111126f9a767SRodney W. Grimes goto again; 111226f9a767SRodney W. Grimes } 1113a481f200SDavid Greenman splx(s); 11147c1f6cedSDavid Greenman if (clean_only) { 11157c1f6cedSDavid Greenman vm_page_test_dirty(p); 11167c1f6cedSDavid Greenman if (p->valid & p->dirty) 11177c1f6cedSDavid Greenman continue; 11187c1f6cedSDavid Greenman } 1119f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 11200d94caffSDavid Greenman PAGE_WAKEUP(p); 1121df8bae1dSRodney W. Grimes vm_page_free(p); 112226f9a767SRodney W. Grimes } 112326f9a767SRodney W. Grimes } 112426f9a767SRodney W. Grimes } else { 112526f9a767SRodney W. Grimes while (size > 0) { 112605f0fdd2SPoul-Henning Kamp while ((p = vm_page_lookup(object, start)) != 0) { 1127a481f200SDavid Greenman s = splhigh(); 11280d94caffSDavid Greenman if (p->bmapped) { 11290d94caffSDavid Greenman splx(s); 11300d94caffSDavid Greenman break; 11310d94caffSDavid Greenman } 11320d94caffSDavid Greenman if ((p->flags & PG_BUSY) || p->busy) { 113326f9a767SRodney W. Grimes p->flags |= PG_WANTED; 113424a1cce3SDavid Greenman tsleep(p, PVM, "vmopar", 0); 1135a481f200SDavid Greenman splx(s); 113626f9a767SRodney W. Grimes goto again; 113726f9a767SRodney W. Grimes } 1138a481f200SDavid Greenman splx(s); 11397c1f6cedSDavid Greenman if (clean_only) { 11407c1f6cedSDavid Greenman vm_page_test_dirty(p); 11417c1f6cedSDavid Greenman if (p->valid & p->dirty) 11427c1f6cedSDavid Greenman continue; 11437c1f6cedSDavid Greenman } 1144f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 11450d94caffSDavid Greenman PAGE_WAKEUP(p); 114626f9a767SRodney W. Grimes vm_page_free(p); 114726f9a767SRodney W. Grimes } 1148a316d390SJohn Dyson start += 1; 1149a316d390SJohn Dyson size -= 1; 1150df8bae1dSRodney W. Grimes } 1151df8bae1dSRodney W. Grimes } 1152f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 1153c0503609SDavid Greenman } 1154df8bae1dSRodney W. Grimes 1155df8bae1dSRodney W. Grimes /* 1156df8bae1dSRodney W. Grimes * Routine: vm_object_coalesce 1157df8bae1dSRodney W. Grimes * Function: Coalesces two objects backing up adjoining 1158df8bae1dSRodney W. Grimes * regions of memory into a single object. 1159df8bae1dSRodney W. Grimes * 1160df8bae1dSRodney W. Grimes * returns TRUE if objects were combined. 1161df8bae1dSRodney W. Grimes * 1162df8bae1dSRodney W. Grimes * NOTE: Only works at the moment if the second object is NULL - 1163df8bae1dSRodney W. Grimes * if it's not, which object do we lock first? 1164df8bae1dSRodney W. Grimes * 1165df8bae1dSRodney W. Grimes * Parameters: 1166df8bae1dSRodney W. Grimes * prev_object First object to coalesce 1167df8bae1dSRodney W. Grimes * prev_offset Offset into prev_object 1168df8bae1dSRodney W. Grimes * next_object Second object into coalesce 1169df8bae1dSRodney W. Grimes * next_offset Offset into next_object 1170df8bae1dSRodney W. Grimes * 1171df8bae1dSRodney W. Grimes * prev_size Size of reference to prev_object 1172df8bae1dSRodney W. Grimes * next_size Size of reference to next_object 1173df8bae1dSRodney W. Grimes * 1174df8bae1dSRodney W. Grimes * Conditions: 1175df8bae1dSRodney W. Grimes * The object must *not* be locked. 1176df8bae1dSRodney W. Grimes */ 11770d94caffSDavid Greenman boolean_t 1178a316d390SJohn Dyson vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size) 1179df8bae1dSRodney W. Grimes register vm_object_t prev_object; 1180a316d390SJohn Dyson vm_pindex_t prev_pindex; 1181df8bae1dSRodney W. Grimes vm_size_t prev_size, next_size; 1182df8bae1dSRodney W. Grimes { 1183df8bae1dSRodney W. Grimes vm_size_t newsize; 1184df8bae1dSRodney W. Grimes 1185df8bae1dSRodney W. Grimes if (prev_object == NULL) { 1186df8bae1dSRodney W. Grimes return (TRUE); 1187df8bae1dSRodney W. Grimes } 1188df8bae1dSRodney W. Grimes 1189df8bae1dSRodney W. Grimes /* 1190df8bae1dSRodney W. Grimes * Try to collapse the object first 1191df8bae1dSRodney W. Grimes */ 1192df8bae1dSRodney W. Grimes vm_object_collapse(prev_object); 1193df8bae1dSRodney W. Grimes 1194df8bae1dSRodney W. Grimes /* 11950d94caffSDavid Greenman * Can't coalesce if: . more than one reference . paged out . shadows 11960d94caffSDavid Greenman * another object . has a copy elsewhere (any of which mean that the 11970d94caffSDavid Greenman * pages not mapped to prev_entry may be in use anyway) 1198df8bae1dSRodney W. Grimes */ 1199df8bae1dSRodney W. Grimes 1200df8bae1dSRodney W. Grimes if (prev_object->ref_count > 1 || 120124a1cce3SDavid Greenman prev_object->type != OBJT_DEFAULT || 120224a1cce3SDavid Greenman prev_object->backing_object != NULL) { 1203df8bae1dSRodney W. Grimes return (FALSE); 1204df8bae1dSRodney W. Grimes } 1205a316d390SJohn Dyson 1206a316d390SJohn Dyson prev_size >>= PAGE_SHIFT; 1207a316d390SJohn Dyson next_size >>= PAGE_SHIFT; 1208df8bae1dSRodney W. Grimes /* 12090d94caffSDavid Greenman * Remove any pages that may still be in the object from a previous 12100d94caffSDavid Greenman * deallocation. 1211df8bae1dSRodney W. Grimes */ 1212df8bae1dSRodney W. Grimes 1213df8bae1dSRodney W. Grimes vm_object_page_remove(prev_object, 1214a316d390SJohn Dyson prev_pindex + prev_size, 1215a316d390SJohn Dyson prev_pindex + prev_size + next_size, FALSE); 1216df8bae1dSRodney W. Grimes 1217df8bae1dSRodney W. Grimes /* 1218df8bae1dSRodney W. Grimes * Extend the object if necessary. 1219df8bae1dSRodney W. Grimes */ 1220a316d390SJohn Dyson newsize = prev_pindex + prev_size + next_size; 1221df8bae1dSRodney W. Grimes if (newsize > prev_object->size) 1222df8bae1dSRodney W. Grimes prev_object->size = newsize; 1223df8bae1dSRodney W. Grimes 1224df8bae1dSRodney W. Grimes return (TRUE); 1225df8bae1dSRodney W. Grimes } 1226df8bae1dSRodney W. Grimes 1227c3cb3e12SDavid Greenman #ifdef DDB 1228c3cb3e12SDavid Greenman 1229cac597e4SBruce Evans static int 1230a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry) 1231a1f6d91cSDavid Greenman vm_map_t map; 1232a1f6d91cSDavid Greenman vm_object_t object; 1233a1f6d91cSDavid Greenman vm_map_entry_t entry; 1234a1f6d91cSDavid Greenman { 1235a1f6d91cSDavid Greenman vm_map_t tmpm; 1236a1f6d91cSDavid Greenman vm_map_entry_t tmpe; 1237a1f6d91cSDavid Greenman vm_object_t obj; 1238a1f6d91cSDavid Greenman int entcount; 1239a1f6d91cSDavid Greenman 1240a1f6d91cSDavid Greenman if (map == 0) 1241a1f6d91cSDavid Greenman return 0; 1242a1f6d91cSDavid Greenman 1243a1f6d91cSDavid Greenman if (entry == 0) { 1244a1f6d91cSDavid Greenman tmpe = map->header.next; 1245a1f6d91cSDavid Greenman entcount = map->nentries; 1246a1f6d91cSDavid Greenman while (entcount-- && (tmpe != &map->header)) { 1247a1f6d91cSDavid Greenman if( _vm_object_in_map(map, object, tmpe)) { 1248a1f6d91cSDavid Greenman return 1; 1249a1f6d91cSDavid Greenman } 1250a1f6d91cSDavid Greenman tmpe = tmpe->next; 1251a1f6d91cSDavid Greenman } 1252a1f6d91cSDavid Greenman } else if (entry->is_sub_map || entry->is_a_map) { 1253a1f6d91cSDavid Greenman tmpm = entry->object.share_map; 1254a1f6d91cSDavid Greenman tmpe = tmpm->header.next; 1255a1f6d91cSDavid Greenman entcount = tmpm->nentries; 1256a1f6d91cSDavid Greenman while (entcount-- && tmpe != &tmpm->header) { 1257a1f6d91cSDavid Greenman if( _vm_object_in_map(tmpm, object, tmpe)) { 1258a1f6d91cSDavid Greenman return 1; 1259a1f6d91cSDavid Greenman } 1260a1f6d91cSDavid Greenman tmpe = tmpe->next; 1261a1f6d91cSDavid Greenman } 1262a1f6d91cSDavid Greenman } else if (obj = entry->object.vm_object) { 126324a1cce3SDavid Greenman for(; obj; obj=obj->backing_object) 1264a1f6d91cSDavid Greenman if( obj == object) { 1265a1f6d91cSDavid Greenman return 1; 1266a1f6d91cSDavid Greenman } 1267a1f6d91cSDavid Greenman } 1268a1f6d91cSDavid Greenman return 0; 1269a1f6d91cSDavid Greenman } 1270a1f6d91cSDavid Greenman 1271cac597e4SBruce Evans static int 1272a1f6d91cSDavid Greenman vm_object_in_map( object) 1273a1f6d91cSDavid Greenman vm_object_t object; 1274a1f6d91cSDavid Greenman { 1275a1f6d91cSDavid Greenman struct proc *p; 1276a1f6d91cSDavid Greenman for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 1277a1f6d91cSDavid Greenman if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 1278a1f6d91cSDavid Greenman continue; 1279a1f6d91cSDavid Greenman /* 1280a1f6d91cSDavid Greenman if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 1281a1f6d91cSDavid Greenman continue; 1282a1f6d91cSDavid Greenman } 1283a1f6d91cSDavid Greenman */ 1284a1f6d91cSDavid Greenman if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) 1285a1f6d91cSDavid Greenman return 1; 1286a1f6d91cSDavid Greenman } 1287a1f6d91cSDavid Greenman if( _vm_object_in_map( kernel_map, object, 0)) 1288a1f6d91cSDavid Greenman return 1; 1289a1f6d91cSDavid Greenman if( _vm_object_in_map( kmem_map, object, 0)) 1290a1f6d91cSDavid Greenman return 1; 1291a1f6d91cSDavid Greenman if( _vm_object_in_map( pager_map, object, 0)) 1292a1f6d91cSDavid Greenman return 1; 1293a1f6d91cSDavid Greenman if( _vm_object_in_map( buffer_map, object, 0)) 1294a1f6d91cSDavid Greenman return 1; 1295a1f6d91cSDavid Greenman if( _vm_object_in_map( io_map, object, 0)) 1296a1f6d91cSDavid Greenman return 1; 1297a1f6d91cSDavid Greenman if( _vm_object_in_map( phys_map, object, 0)) 1298a1f6d91cSDavid Greenman return 1; 1299a1f6d91cSDavid Greenman if( _vm_object_in_map( mb_map, object, 0)) 1300a1f6d91cSDavid Greenman return 1; 1301a1f6d91cSDavid Greenman if( _vm_object_in_map( u_map, object, 0)) 1302a1f6d91cSDavid Greenman return 1; 1303a1f6d91cSDavid Greenman return 0; 1304a1f6d91cSDavid Greenman } 1305a1f6d91cSDavid Greenman 1306c3cb3e12SDavid Greenman 1307a1f6d91cSDavid Greenman void 1308a1f6d91cSDavid Greenman vm_object_check() { 1309a1f6d91cSDavid Greenman vm_object_t object; 1310a1f6d91cSDavid Greenman 1311a1f6d91cSDavid Greenman /* 1312a1f6d91cSDavid Greenman * make sure that internal objs are in a map somewhere 1313a1f6d91cSDavid Greenman * and none have zero ref counts. 1314a1f6d91cSDavid Greenman */ 1315a1f6d91cSDavid Greenman for (object = vm_object_list.tqh_first; 1316a1f6d91cSDavid Greenman object != NULL; 1317a1f6d91cSDavid Greenman object = object->object_list.tqe_next) { 131824a1cce3SDavid Greenman if (object->handle == NULL && 131924a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 1320a1f6d91cSDavid Greenman if (object->ref_count == 0) { 1321a1f6d91cSDavid Greenman printf("vmochk: internal obj has zero ref count: %d\n", 1322a1f6d91cSDavid Greenman object->size); 1323a1f6d91cSDavid Greenman } 1324a1f6d91cSDavid Greenman if (!vm_object_in_map(object)) { 13253af76890SPoul-Henning Kamp printf("vmochk: internal obj is not in a map: " 13263af76890SPoul-Henning Kamp "ref: %d, size: %d: 0x%x, backing_object: 0x%x\n", 13273af76890SPoul-Henning Kamp object->ref_count, object->size, 13283af76890SPoul-Henning Kamp object->size, object->backing_object); 1329a1f6d91cSDavid Greenman } 1330a1f6d91cSDavid Greenman } 1331a1f6d91cSDavid Greenman } 1332a1f6d91cSDavid Greenman } 1333a1f6d91cSDavid Greenman 133426f9a767SRodney W. Grimes /* 1335df8bae1dSRodney W. Grimes * vm_object_print: [ debug ] 1336df8bae1dSRodney W. Grimes */ 13370d94caffSDavid Greenman void 1338187f0238SBruce Evans vm_object_print(iobject, full, dummy3, dummy4) 1339187f0238SBruce Evans /* db_expr_t */ int iobject; 1340df8bae1dSRodney W. Grimes boolean_t full; 1341187f0238SBruce Evans /* db_expr_t */ int dummy3; 1342187f0238SBruce Evans char *dummy4; 1343df8bae1dSRodney W. Grimes { 1344187f0238SBruce Evans vm_object_t object = (vm_object_t)iobject; /* XXX */ 1345df8bae1dSRodney W. Grimes register vm_page_t p; 1346df8bae1dSRodney W. Grimes 1347df8bae1dSRodney W. Grimes register int count; 1348df8bae1dSRodney W. Grimes 1349df8bae1dSRodney W. Grimes if (object == NULL) 1350df8bae1dSRodney W. Grimes return; 1351df8bae1dSRodney W. Grimes 1352df8bae1dSRodney W. Grimes iprintf("Object 0x%x: size=0x%x, res=%d, ref=%d, ", 1353df8bae1dSRodney W. Grimes (int) object, (int) object->size, 1354df8bae1dSRodney W. Grimes object->resident_page_count, object->ref_count); 13552a4895f4SDavid Greenman printf("offset=0x%x, backing_object=(0x%x)+0x%x\n", 13562a4895f4SDavid Greenman (int) object->paging_offset, 135724a1cce3SDavid Greenman (int) object->backing_object, (int) object->backing_object_offset); 135805f0fdd2SPoul-Henning Kamp printf("cache: next=%p, prev=%p\n", 1359df8bae1dSRodney W. Grimes object->cached_list.tqe_next, object->cached_list.tqe_prev); 1360df8bae1dSRodney W. Grimes 1361df8bae1dSRodney W. Grimes if (!full) 1362df8bae1dSRodney W. Grimes return; 1363df8bae1dSRodney W. Grimes 1364df8bae1dSRodney W. Grimes indent += 2; 1365df8bae1dSRodney W. Grimes count = 0; 1366df8bae1dSRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) { 1367df8bae1dSRodney W. Grimes if (count == 0) 1368df8bae1dSRodney W. Grimes iprintf("memory:="); 1369df8bae1dSRodney W. Grimes else if (count == 6) { 1370df8bae1dSRodney W. Grimes printf("\n"); 1371df8bae1dSRodney W. Grimes iprintf(" ..."); 1372df8bae1dSRodney W. Grimes count = 0; 1373df8bae1dSRodney W. Grimes } else 1374df8bae1dSRodney W. Grimes printf(","); 1375df8bae1dSRodney W. Grimes count++; 1376df8bae1dSRodney W. Grimes 137705f0fdd2SPoul-Henning Kamp printf("(off=0x%lx,page=0x%lx)", 1378a316d390SJohn Dyson (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p)); 1379df8bae1dSRodney W. Grimes } 1380df8bae1dSRodney W. Grimes if (count != 0) 1381df8bae1dSRodney W. Grimes printf("\n"); 1382df8bae1dSRodney W. Grimes indent -= 2; 1383df8bae1dSRodney W. Grimes } 1384c3cb3e12SDavid Greenman #endif /* DDB */ 1385