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 * 64efeaf95aSDavid Greenman * $Id: vm_object.c,v 1.57 1995/12/03 12:18:37 bde 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; 165df8bae1dSRodney W. Grimes object->paging_offset = 0; 16624a1cce3SDavid Greenman object->backing_object = NULL; 16724a1cce3SDavid Greenman object->backing_object_offset = (vm_offset_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 18126f9a767SRodney W. Grimes vm_object_init(vm_offset_t nothing) 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; 19224a1cce3SDavid Greenman _vm_object_allocate(OBJT_DEFAULT, VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS, 19326f9a767SRodney W. Grimes kernel_object); 19426f9a767SRodney W. Grimes 19526f9a767SRodney W. Grimes kmem_object = &kmem_object_store; 19624a1cce3SDavid Greenman _vm_object_allocate(OBJT_DEFAULT, 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; 437f6b04d2bSDavid Greenman vm_offset_t start; 438f6b04d2bSDavid Greenman vm_offset_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 if (start != end) { 460f6b04d2bSDavid Greenman start = trunc_page(start); 461f6b04d2bSDavid Greenman end = round_page(end); 462f6b04d2bSDavid Greenman } 463f6b04d2bSDavid Greenman 464f6b04d2bSDavid Greenman tstart = start; 465f6b04d2bSDavid Greenman if (end == 0) { 466f6b04d2bSDavid Greenman tend = object->size; 467f6b04d2bSDavid Greenman } else { 468f6b04d2bSDavid Greenman tend = end; 469f6b04d2bSDavid Greenman } 470ec4f9fb0SDavid Greenman if (tstart == 0 && tend == object->size) { 471aef922f5SJohn Dyson object->flags &= ~(OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY); 472ec4f9fb0SDavid Greenman } 473f6b04d2bSDavid Greenman 474aef922f5SJohn Dyson runlen = 0; 475aef922f5SJohn Dyson for(;tstart < tend; tstart += PAGE_SIZE) { 476aef922f5SJohn Dyson relookup: 477f6b04d2bSDavid Greenman p = vm_page_lookup(object, tstart); 478aef922f5SJohn Dyson if (!p) { 479aef922f5SJohn Dyson if (runlen > 0) { 480aef922f5SJohn Dyson vm_pageout_flush(ma, runlen, syncio); 481aef922f5SJohn Dyson runlen = 0; 482aef922f5SJohn Dyson } 483f6b04d2bSDavid Greenman continue; 484f6b04d2bSDavid Greenman } 485aef922f5SJohn Dyson if (p->valid == 0 || (p->flags & PG_CACHE)) { 486aef922f5SJohn Dyson if (runlen > 0) { 487aef922f5SJohn Dyson vm_pageout_flush(ma, runlen, syncio); 488aef922f5SJohn Dyson runlen = 0; 4897e15fd27SJohn Dyson } 490aef922f5SJohn Dyson continue; 491f6b04d2bSDavid Greenman } 492f6b04d2bSDavid Greenman 493ec4f9fb0SDavid Greenman vm_page_protect(p, VM_PROT_READ); 494ec4f9fb0SDavid Greenman 495f6b04d2bSDavid Greenman s = splhigh(); 496aef922f5SJohn Dyson while ((p->flags & PG_BUSY) || p->busy) { 497aef922f5SJohn Dyson if (runlen > 0) { 498f6b04d2bSDavid Greenman splx(s); 499aef922f5SJohn Dyson vm_pageout_flush(ma, runlen, syncio); 500aef922f5SJohn Dyson runlen = 0; 501aef922f5SJohn Dyson goto relookup; 502aef922f5SJohn Dyson } 503aef922f5SJohn Dyson p->flags |= PG_WANTED|PG_REFERENCED; 504aef922f5SJohn Dyson tsleep(p, PVM, "vpcwai", 0); 505f6b04d2bSDavid Greenman splx(s); 506aef922f5SJohn Dyson goto relookup; 507f6b04d2bSDavid Greenman } 508f6b04d2bSDavid Greenman splx(s); 509f6b04d2bSDavid Greenman 510aef922f5SJohn Dyson if (p->dirty == 0) 511f6b04d2bSDavid Greenman vm_page_test_dirty(p); 512aef922f5SJohn Dyson 513f6b04d2bSDavid Greenman if ((p->valid & p->dirty) != 0) { 514aef922f5SJohn Dyson ma[runlen] = p; 515aef922f5SJohn Dyson p->flags |= PG_BUSY; 516aef922f5SJohn Dyson runlen++; 517aef922f5SJohn Dyson if (runlen >= vm_pageout_page_count) { 518aef922f5SJohn Dyson vm_pageout_flush(ma, runlen, syncio); 519aef922f5SJohn Dyson runlen = 0; 520f6b04d2bSDavid Greenman } 521aef922f5SJohn Dyson } else if (runlen > 0) { 522aef922f5SJohn Dyson vm_pageout_flush(ma, runlen, syncio); 523aef922f5SJohn Dyson runlen = 0; 524f6b04d2bSDavid Greenman } 525aef922f5SJohn Dyson 526f6b04d2bSDavid Greenman } 527aef922f5SJohn Dyson if (runlen > 0) { 528aef922f5SJohn Dyson vm_pageout_flush(ma, runlen, syncio); 529f6b04d2bSDavid Greenman } 530aef922f5SJohn Dyson 531aef922f5SJohn Dyson VOP_FSYNC(vp, NULL, syncio, curproc); 532aef922f5SJohn Dyson 53324a1cce3SDavid Greenman if (lockflag) 534d3628763SRodney W. Grimes VOP_UNLOCK(vp); 535aef922f5SJohn Dyson object->flags &= ~OBJ_CLEANING; 536f5cf85d4SDavid Greenman return; 53726f9a767SRodney W. Grimes } 538df8bae1dSRodney W. Grimes 539df8bae1dSRodney W. Grimes /* 540df8bae1dSRodney W. Grimes * vm_object_deactivate_pages 541df8bae1dSRodney W. Grimes * 542df8bae1dSRodney W. Grimes * Deactivate all pages in the specified object. (Keep its pages 543df8bae1dSRodney W. Grimes * in memory even though it is no longer referenced.) 544df8bae1dSRodney W. Grimes * 545df8bae1dSRodney W. Grimes * The object must be locked. 546df8bae1dSRodney W. Grimes */ 547df8bae1dSRodney W. Grimes void 548df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object) 549df8bae1dSRodney W. Grimes register vm_object_t object; 550df8bae1dSRodney W. Grimes { 551df8bae1dSRodney W. Grimes register vm_page_t p, next; 552df8bae1dSRodney W. Grimes 553df8bae1dSRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = next) { 554df8bae1dSRodney W. Grimes next = p->listq.tqe_next; 555df8bae1dSRodney W. Grimes vm_page_deactivate(p); 556df8bae1dSRodney W. Grimes } 557df8bae1dSRodney W. Grimes } 558df8bae1dSRodney W. Grimes 559df8bae1dSRodney W. Grimes /* 560df8bae1dSRodney W. Grimes * Trim the object cache to size. 561df8bae1dSRodney W. Grimes */ 562df8bae1dSRodney W. Grimes 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; 588df8bae1dSRodney W. Grimes register vm_offset_t start; 589df8bae1dSRodney W. Grimes register vm_offset_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; 614df8bae1dSRodney W. Grimes register vm_offset_t start; 615df8bae1dSRodney W. Grimes register vm_offset_t end; 616df8bae1dSRodney W. Grimes { 617df8bae1dSRodney W. Grimes register vm_page_t p; 618df8bae1dSRodney W. Grimes 619df8bae1dSRodney W. Grimes if (object == NULL) 620df8bae1dSRodney W. Grimes return; 62126f9a767SRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) { 622f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 62326f9a767SRodney W. Grimes } 62426f9a767SRodney W. Grimes } 625df8bae1dSRodney W. Grimes 626df8bae1dSRodney W. Grimes /* 627df8bae1dSRodney W. Grimes * vm_object_copy: 628df8bae1dSRodney W. Grimes * 629df8bae1dSRodney W. Grimes * Create a new object which is a copy of an existing 630df8bae1dSRodney W. Grimes * object, and mark all of the pages in the existing 631df8bae1dSRodney W. Grimes * object 'copy-on-write'. The new object has one reference. 632df8bae1dSRodney W. Grimes * Returns the new object. 633df8bae1dSRodney W. Grimes * 634df8bae1dSRodney W. Grimes * May defer the copy until later if the object is not backed 635df8bae1dSRodney W. Grimes * up by a non-default pager. 636df8bae1dSRodney W. Grimes */ 6370d94caffSDavid Greenman void 6380d94caffSDavid Greenman vm_object_copy(src_object, src_offset, size, 639df8bae1dSRodney W. Grimes dst_object, dst_offset, src_needs_copy) 640df8bae1dSRodney W. Grimes register vm_object_t src_object; 641df8bae1dSRodney W. Grimes vm_offset_t src_offset; 642df8bae1dSRodney W. Grimes vm_size_t size; 643df8bae1dSRodney W. Grimes vm_object_t *dst_object;/* OUT */ 644df8bae1dSRodney W. Grimes vm_offset_t *dst_offset;/* OUT */ 645df8bae1dSRodney W. Grimes boolean_t *src_needs_copy; /* OUT */ 646df8bae1dSRodney W. Grimes { 647df8bae1dSRodney W. Grimes if (src_object == NULL) { 648df8bae1dSRodney W. Grimes /* 649df8bae1dSRodney W. Grimes * Nothing to copy 650df8bae1dSRodney W. Grimes */ 651df8bae1dSRodney W. Grimes *dst_object = NULL; 652df8bae1dSRodney W. Grimes *dst_offset = 0; 653df8bae1dSRodney W. Grimes *src_needs_copy = FALSE; 654df8bae1dSRodney W. Grimes return; 655df8bae1dSRodney W. Grimes } 65626f9a767SRodney W. Grimes 65726f9a767SRodney W. Grimes /* 65826f9a767SRodney W. Grimes * Try to collapse the object before copying it. 65926f9a767SRodney W. Grimes */ 66024a1cce3SDavid Greenman if (src_object->handle == NULL && 66124a1cce3SDavid Greenman (src_object->type == OBJT_DEFAULT || 66224a1cce3SDavid Greenman src_object->type == OBJT_SWAP)) 66326f9a767SRodney W. Grimes vm_object_collapse(src_object); 66426f9a767SRodney W. Grimes 665df8bae1dSRodney W. Grimes 666df8bae1dSRodney W. Grimes /* 667df8bae1dSRodney W. Grimes * Make another reference to the object 668df8bae1dSRodney W. Grimes */ 669df8bae1dSRodney W. Grimes src_object->ref_count++; 670df8bae1dSRodney W. Grimes 671df8bae1dSRodney W. Grimes *dst_object = src_object; 672df8bae1dSRodney W. Grimes *dst_offset = src_offset; 673df8bae1dSRodney W. Grimes 674df8bae1dSRodney W. Grimes /* 675df8bae1dSRodney W. Grimes * Must make a shadow when write is desired 676df8bae1dSRodney W. Grimes */ 677df8bae1dSRodney W. Grimes *src_needs_copy = TRUE; 678df8bae1dSRodney W. Grimes return; 679df8bae1dSRodney W. Grimes } 680df8bae1dSRodney W. Grimes 681df8bae1dSRodney W. Grimes /* 682df8bae1dSRodney W. Grimes * vm_object_shadow: 683df8bae1dSRodney W. Grimes * 684df8bae1dSRodney W. Grimes * Create a new object which is backed by the 685df8bae1dSRodney W. Grimes * specified existing object range. The source 686df8bae1dSRodney W. Grimes * object reference is deallocated. 687df8bae1dSRodney W. Grimes * 688df8bae1dSRodney W. Grimes * The new object and offset into that object 689df8bae1dSRodney W. Grimes * are returned in the source parameters. 690df8bae1dSRodney W. Grimes */ 691df8bae1dSRodney W. Grimes 69226f9a767SRodney W. Grimes void 69326f9a767SRodney W. Grimes vm_object_shadow(object, offset, length) 694df8bae1dSRodney W. Grimes vm_object_t *object; /* IN/OUT */ 695df8bae1dSRodney W. Grimes vm_offset_t *offset; /* IN/OUT */ 696df8bae1dSRodney W. Grimes vm_size_t length; 697df8bae1dSRodney W. Grimes { 698df8bae1dSRodney W. Grimes register vm_object_t source; 699df8bae1dSRodney W. Grimes register vm_object_t result; 700df8bae1dSRodney W. Grimes 701df8bae1dSRodney W. Grimes source = *object; 702df8bae1dSRodney W. Grimes 703df8bae1dSRodney W. Grimes /* 704df8bae1dSRodney W. Grimes * Allocate a new object with the given length 705df8bae1dSRodney W. Grimes */ 706df8bae1dSRodney W. Grimes 70724a1cce3SDavid Greenman if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL) 708df8bae1dSRodney W. Grimes panic("vm_object_shadow: no object for shadowing"); 709df8bae1dSRodney W. Grimes 710df8bae1dSRodney W. Grimes /* 7110d94caffSDavid Greenman * The new object shadows the source object, adding a reference to it. 7120d94caffSDavid Greenman * Our caller changes his reference to point to the new object, 7130d94caffSDavid Greenman * removing a reference to the source object. Net result: no change 7140d94caffSDavid Greenman * of reference count. 715df8bae1dSRodney W. Grimes */ 71624a1cce3SDavid Greenman result->backing_object = source; 7177b18a718SDavid Greenman if (source) 71824a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&result->backing_object->shadow_head, result, shadow_list); 719df8bae1dSRodney W. Grimes 720df8bae1dSRodney W. Grimes /* 7210d94caffSDavid Greenman * Store the offset into the source object, and fix up the offset into 7220d94caffSDavid Greenman * the new object. 723df8bae1dSRodney W. Grimes */ 724df8bae1dSRodney W. Grimes 72524a1cce3SDavid Greenman result->backing_object_offset = *offset; 726df8bae1dSRodney W. Grimes 727df8bae1dSRodney W. Grimes /* 728df8bae1dSRodney W. Grimes * Return the new things 729df8bae1dSRodney W. Grimes */ 730df8bae1dSRodney W. Grimes 731df8bae1dSRodney W. Grimes *offset = 0; 732df8bae1dSRodney W. Grimes *object = result; 733df8bae1dSRodney W. Grimes } 734df8bae1dSRodney W. Grimes 735df8bae1dSRodney W. Grimes 736df8bae1dSRodney W. Grimes /* 7372fe6e4d7SDavid Greenman * this version of collapse allows the operation to occur earlier and 7382fe6e4d7SDavid Greenman * when paging_in_progress is true for an object... This is not a complete 7392fe6e4d7SDavid Greenman * operation, but should plug 99.9% of the rest of the leaks. 7402fe6e4d7SDavid Greenman */ 7412fe6e4d7SDavid Greenman static void 7422fe6e4d7SDavid Greenman vm_object_qcollapse(object) 7432fe6e4d7SDavid Greenman register vm_object_t object; 7442fe6e4d7SDavid Greenman { 7452fe6e4d7SDavid Greenman register vm_object_t backing_object; 7462fe6e4d7SDavid Greenman register vm_offset_t backing_offset, new_offset; 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 75624a1cce3SDavid Greenman backing_offset = object->backing_object_offset; 7572fe6e4d7SDavid Greenman size = object->size; 7582fe6e4d7SDavid Greenman p = backing_object->memq.tqh_first; 7592fe6e4d7SDavid Greenman while (p) { 7602fe6e4d7SDavid Greenman vm_page_t next; 7610d94caffSDavid Greenman 7622fe6e4d7SDavid Greenman next = p->listq.tqe_next; 7630d94caffSDavid Greenman if ((p->flags & (PG_BUSY | PG_FICTITIOUS | PG_CACHE)) || 764f6b04d2bSDavid Greenman !p->valid || p->hold_count || p->wire_count || p->busy) { 7652fe6e4d7SDavid Greenman p = next; 7662fe6e4d7SDavid Greenman continue; 7672fe6e4d7SDavid Greenman } 768f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 7692fe6e4d7SDavid Greenman new_offset = (p->offset - backing_offset); 7702fe6e4d7SDavid Greenman if (p->offset < backing_offset || 7712fe6e4d7SDavid Greenman new_offset >= size) { 77224a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) 77324a1cce3SDavid Greenman swap_pager_freespace(backing_object, 7740d94caffSDavid Greenman backing_object->paging_offset + p->offset, PAGE_SIZE); 7752fe6e4d7SDavid Greenman vm_page_free(p); 7762fe6e4d7SDavid Greenman } else { 7772fe6e4d7SDavid Greenman pp = vm_page_lookup(object, new_offset); 77824a1cce3SDavid Greenman if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object, 77924a1cce3SDavid Greenman object->paging_offset + new_offset, NULL, NULL))) { 78024a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) 78124a1cce3SDavid Greenman swap_pager_freespace(backing_object, 7820d94caffSDavid Greenman backing_object->paging_offset + p->offset, PAGE_SIZE); 7832fe6e4d7SDavid Greenman vm_page_free(p); 7842fe6e4d7SDavid Greenman } else { 78524a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) 78624a1cce3SDavid Greenman swap_pager_freespace(backing_object, 787a1f6d91cSDavid Greenman backing_object->paging_offset + p->offset, PAGE_SIZE); 7882fe6e4d7SDavid Greenman vm_page_rename(p, object, new_offset); 7899b4814bbSDavid Greenman p->dirty = VM_PAGE_BITS_ALL; 7902fe6e4d7SDavid Greenman } 7912fe6e4d7SDavid Greenman } 7922fe6e4d7SDavid Greenman p = next; 7932fe6e4d7SDavid Greenman } 794010cf3b9SDavid Greenman backing_object->ref_count -= 2; 7952fe6e4d7SDavid Greenman } 7962fe6e4d7SDavid Greenman 797df8bae1dSRodney W. Grimes /* 798df8bae1dSRodney W. Grimes * vm_object_collapse: 799df8bae1dSRodney W. Grimes * 800df8bae1dSRodney W. Grimes * Collapse an object with the object backing it. 801df8bae1dSRodney W. Grimes * Pages in the backing object are moved into the 802df8bae1dSRodney W. Grimes * parent, and the backing object is deallocated. 803df8bae1dSRodney W. Grimes */ 80426f9a767SRodney W. Grimes void 80526f9a767SRodney W. Grimes vm_object_collapse(object) 80624a1cce3SDavid Greenman vm_object_t object; 807df8bae1dSRodney W. Grimes 808df8bae1dSRodney W. Grimes { 80924a1cce3SDavid Greenman vm_object_t backing_object; 81024a1cce3SDavid Greenman vm_offset_t backing_offset; 81124a1cce3SDavid Greenman vm_size_t size; 81224a1cce3SDavid Greenman vm_offset_t new_offset; 81324a1cce3SDavid Greenman vm_page_t p, pp; 814df8bae1dSRodney W. Grimes 815df8bae1dSRodney W. Grimes while (TRUE) { 816df8bae1dSRodney W. Grimes /* 817df8bae1dSRodney W. Grimes * Verify that the conditions are right for collapse: 818df8bae1dSRodney W. Grimes * 8190d94caffSDavid Greenman * The object exists and no pages in it are currently being paged 8200d94caffSDavid Greenman * out. 821df8bae1dSRodney W. Grimes */ 8222fe6e4d7SDavid Greenman if (object == NULL) 823df8bae1dSRodney W. Grimes return; 824df8bae1dSRodney W. Grimes 825b9921222SDavid Greenman /* 826b9921222SDavid Greenman * Make sure there is a backing object. 827b9921222SDavid Greenman */ 82824a1cce3SDavid Greenman if ((backing_object = object->backing_object) == NULL) 829df8bae1dSRodney W. Grimes return; 830df8bae1dSRodney W. Grimes 831f919ebdeSDavid Greenman /* 832f919ebdeSDavid Greenman * we check the backing object first, because it is most likely 83324a1cce3SDavid Greenman * not collapsable. 834f919ebdeSDavid Greenman */ 83524a1cce3SDavid Greenman if (backing_object->handle != NULL || 83624a1cce3SDavid Greenman (backing_object->type != OBJT_DEFAULT && 83724a1cce3SDavid Greenman backing_object->type != OBJT_SWAP) || 838f919ebdeSDavid Greenman (backing_object->flags & OBJ_DEAD) || 83924a1cce3SDavid Greenman object->handle != NULL || 84024a1cce3SDavid Greenman (object->type != OBJT_DEFAULT && 84124a1cce3SDavid Greenman object->type != OBJT_SWAP) || 84224a1cce3SDavid Greenman (object->flags & OBJ_DEAD)) { 8439b4814bbSDavid Greenman return; 84424a1cce3SDavid Greenman } 8459b4814bbSDavid Greenman 846f919ebdeSDavid Greenman if (object->paging_in_progress != 0 || 847f919ebdeSDavid Greenman backing_object->paging_in_progress != 0) { 848b9921222SDavid Greenman vm_object_qcollapse(object); 849df8bae1dSRodney W. Grimes return; 850df8bae1dSRodney W. Grimes } 851f919ebdeSDavid Greenman 85226f9a767SRodney W. Grimes /* 8530d94caffSDavid Greenman * We know that we can either collapse the backing object (if 8540d94caffSDavid Greenman * the parent is the only reference to it) or (perhaps) remove 8550d94caffSDavid Greenman * the parent's reference to it. 856df8bae1dSRodney W. Grimes */ 857df8bae1dSRodney W. Grimes 85824a1cce3SDavid Greenman backing_offset = object->backing_object_offset; 859df8bae1dSRodney W. Grimes size = object->size; 860df8bae1dSRodney W. Grimes 861df8bae1dSRodney W. Grimes /* 8620d94caffSDavid Greenman * If there is exactly one reference to the backing object, we 8630d94caffSDavid Greenman * can collapse it into the parent. 864df8bae1dSRodney W. Grimes */ 865df8bae1dSRodney W. Grimes 866df8bae1dSRodney W. Grimes if (backing_object->ref_count == 1) { 867df8bae1dSRodney W. Grimes 868a1f6d91cSDavid Greenman backing_object->flags |= OBJ_DEAD; 869df8bae1dSRodney W. Grimes /* 870df8bae1dSRodney W. Grimes * We can collapse the backing object. 871df8bae1dSRodney W. Grimes * 8720d94caffSDavid Greenman * Move all in-memory pages from backing_object to the 8730d94caffSDavid Greenman * parent. Pages that have been paged out will be 8740d94caffSDavid Greenman * overwritten by any of the parent's pages that 8750d94caffSDavid Greenman * shadow them. 876df8bae1dSRodney W. Grimes */ 877df8bae1dSRodney W. Grimes 87805f0fdd2SPoul-Henning Kamp while ((p = backing_object->memq.tqh_first) != 0) { 87926f9a767SRodney W. Grimes 880df8bae1dSRodney W. Grimes new_offset = (p->offset - backing_offset); 881df8bae1dSRodney W. Grimes 882df8bae1dSRodney W. Grimes /* 8830d94caffSDavid Greenman * If the parent has a page here, or if this 8840d94caffSDavid Greenman * page falls outside the parent, dispose of 8850d94caffSDavid Greenman * it. 886df8bae1dSRodney W. Grimes * 887df8bae1dSRodney W. Grimes * Otherwise, move it as planned. 888df8bae1dSRodney W. Grimes */ 889df8bae1dSRodney W. Grimes 890df8bae1dSRodney W. Grimes if (p->offset < backing_offset || 891df8bae1dSRodney W. Grimes new_offset >= size) { 892f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 8930d94caffSDavid Greenman PAGE_WAKEUP(p); 894df8bae1dSRodney W. Grimes vm_page_free(p); 895df8bae1dSRodney W. Grimes } else { 896df8bae1dSRodney W. Grimes pp = vm_page_lookup(object, new_offset); 89724a1cce3SDavid Greenman if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object, 89824a1cce3SDavid Greenman object->paging_offset + new_offset, NULL, NULL))) { 899f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 9000d94caffSDavid Greenman PAGE_WAKEUP(p); 901df8bae1dSRodney W. Grimes vm_page_free(p); 90226f9a767SRodney W. Grimes } else { 903df8bae1dSRodney W. Grimes vm_page_rename(p, object, new_offset); 904df8bae1dSRodney W. Grimes } 905df8bae1dSRodney W. Grimes } 906df8bae1dSRodney W. Grimes } 907df8bae1dSRodney W. Grimes 908df8bae1dSRodney W. Grimes /* 909df8bae1dSRodney W. Grimes * Move the pager from backing_object to object. 910df8bae1dSRodney W. Grimes */ 911df8bae1dSRodney W. Grimes 91224a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) { 91326f9a767SRodney W. Grimes backing_object->paging_in_progress++; 91424a1cce3SDavid Greenman if (object->type == OBJT_SWAP) { 91526f9a767SRodney W. Grimes object->paging_in_progress++; 91626f9a767SRodney W. Grimes /* 91726f9a767SRodney W. Grimes * copy shadow object pages into ours 9180d94caffSDavid Greenman * and destroy unneeded pages in 9190d94caffSDavid Greenman * shadow object. 92026f9a767SRodney W. Grimes */ 92126f9a767SRodney W. Grimes swap_pager_copy( 92224a1cce3SDavid Greenman backing_object, backing_object->paging_offset, 92324a1cce3SDavid Greenman object, object->paging_offset, 92424a1cce3SDavid Greenman object->backing_object_offset); 925f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 92626f9a767SRodney W. Grimes } else { 92726f9a767SRodney W. Grimes object->paging_in_progress++; 92826f9a767SRodney W. Grimes /* 92924a1cce3SDavid Greenman * move the shadow backing_object's pager data to 93024a1cce3SDavid Greenman * "object" and convert "object" type to OBJT_SWAP. 93126f9a767SRodney W. Grimes */ 93224a1cce3SDavid Greenman object->type = OBJT_SWAP; 9332a4895f4SDavid Greenman object->un_pager.swp.swp_nblocks = 9342a4895f4SDavid Greenman backing_object->un_pager.swp.swp_nblocks; 9352a4895f4SDavid Greenman object->un_pager.swp.swp_allocsize = 9362a4895f4SDavid Greenman backing_object->un_pager.swp.swp_allocsize; 9372a4895f4SDavid Greenman object->un_pager.swp.swp_blocks = 9382a4895f4SDavid Greenman backing_object->un_pager.swp.swp_blocks; 9392a4895f4SDavid Greenman object->un_pager.swp.swp_poip = /* XXX */ 9402a4895f4SDavid Greenman backing_object->un_pager.swp.swp_poip; 94126f9a767SRodney W. Grimes object->paging_offset = backing_object->paging_offset + backing_offset; 94224a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&swap_pager_un_object_list, object, pager_object_list); 94324a1cce3SDavid Greenman 94424a1cce3SDavid Greenman /* 94524a1cce3SDavid Greenman * Convert backing object from OBJT_SWAP to 94624a1cce3SDavid Greenman * OBJT_DEFAULT. XXX - only the TAILQ_REMOVE is 94724a1cce3SDavid Greenman * actually necessary. 94824a1cce3SDavid Greenman */ 94924a1cce3SDavid Greenman backing_object->type = OBJT_DEFAULT; 95024a1cce3SDavid Greenman TAILQ_REMOVE(&swap_pager_un_object_list, backing_object, pager_object_list); 95126f9a767SRodney W. Grimes /* 95226f9a767SRodney W. Grimes * free unnecessary blocks 95326f9a767SRodney W. Grimes */ 95424a1cce3SDavid Greenman swap_pager_freespace(object, 0, object->paging_offset); 955f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 956c0503609SDavid Greenman } 957c0503609SDavid Greenman 958f919ebdeSDavid Greenman vm_object_pip_wakeup(backing_object); 959c0503609SDavid Greenman } 960df8bae1dSRodney W. Grimes /* 961df8bae1dSRodney W. Grimes * Object now shadows whatever backing_object did. 96224a1cce3SDavid Greenman * Note that the reference to backing_object->backing_object 963df8bae1dSRodney W. Grimes * moves from within backing_object to within object. 964df8bae1dSRodney W. Grimes */ 965df8bae1dSRodney W. Grimes 96624a1cce3SDavid Greenman TAILQ_REMOVE(&object->backing_object->shadow_head, object, 96724a1cce3SDavid Greenman shadow_list); 96824a1cce3SDavid Greenman if (backing_object->backing_object) 96924a1cce3SDavid Greenman TAILQ_REMOVE(&backing_object->backing_object->shadow_head, 97024a1cce3SDavid Greenman backing_object, shadow_list); 97124a1cce3SDavid Greenman object->backing_object = backing_object->backing_object; 97224a1cce3SDavid Greenman if (object->backing_object) 97324a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&object->backing_object->shadow_head, 97424a1cce3SDavid Greenman object, shadow_list); 9752fe6e4d7SDavid Greenman 97624a1cce3SDavid Greenman object->backing_object_offset += backing_object->backing_object_offset; 977df8bae1dSRodney W. Grimes /* 978df8bae1dSRodney W. Grimes * Discard backing_object. 979df8bae1dSRodney W. Grimes * 9800d94caffSDavid Greenman * Since the backing object has no pages, no pager left, 9810d94caffSDavid Greenman * and no object references within it, all that is 9820d94caffSDavid Greenman * necessary is to dispose of it. 983df8bae1dSRodney W. Grimes */ 984df8bae1dSRodney W. Grimes 985df8bae1dSRodney W. Grimes TAILQ_REMOVE(&vm_object_list, backing_object, 986df8bae1dSRodney W. Grimes object_list); 987df8bae1dSRodney W. Grimes vm_object_count--; 988df8bae1dSRodney W. Grimes 989df8bae1dSRodney W. Grimes free((caddr_t) backing_object, M_VMOBJ); 990df8bae1dSRodney W. Grimes 991df8bae1dSRodney W. Grimes object_collapses++; 9920d94caffSDavid Greenman } else { 993df8bae1dSRodney W. Grimes /* 994df8bae1dSRodney W. Grimes * If all of the pages in the backing object are 9950d94caffSDavid Greenman * shadowed by the parent object, the parent object no 9960d94caffSDavid Greenman * longer has to shadow the backing object; it can 9970d94caffSDavid Greenman * shadow the next one in the chain. 998df8bae1dSRodney W. Grimes * 9990d94caffSDavid Greenman * The backing object must not be paged out - we'd have 10000d94caffSDavid Greenman * to check all of the paged-out pages, as well. 1001df8bae1dSRodney W. Grimes */ 1002df8bae1dSRodney W. Grimes 100324a1cce3SDavid Greenman if (backing_object->type != OBJT_DEFAULT) { 1004df8bae1dSRodney W. Grimes return; 1005df8bae1dSRodney W. Grimes } 1006df8bae1dSRodney W. Grimes /* 10070d94caffSDavid Greenman * Should have a check for a 'small' number of pages 10080d94caffSDavid Greenman * here. 1009df8bae1dSRodney W. Grimes */ 1010df8bae1dSRodney W. Grimes 101126f9a767SRodney W. Grimes for (p = backing_object->memq.tqh_first; p; p = p->listq.tqe_next) { 1012df8bae1dSRodney W. Grimes new_offset = (p->offset - backing_offset); 1013df8bae1dSRodney W. Grimes 1014df8bae1dSRodney W. Grimes /* 10150d94caffSDavid Greenman * If the parent has a page here, or if this 10160d94caffSDavid Greenman * page falls outside the parent, keep going. 1017df8bae1dSRodney W. Grimes * 10180d94caffSDavid Greenman * Otherwise, the backing_object must be left in 10190d94caffSDavid Greenman * the chain. 1020df8bae1dSRodney W. Grimes */ 1021df8bae1dSRodney W. Grimes 102224a1cce3SDavid Greenman if (p->offset >= backing_offset && new_offset <= size) { 102324a1cce3SDavid Greenman 102424a1cce3SDavid Greenman pp = vm_page_lookup(object, new_offset); 102524a1cce3SDavid Greenman 102624a1cce3SDavid Greenman if ((pp == NULL || pp->valid == 0) && 102724a1cce3SDavid Greenman !vm_pager_has_page(object, object->paging_offset + new_offset, NULL, NULL)) { 102824a1cce3SDavid Greenman 1029df8bae1dSRodney W. Grimes /* 10300d94caffSDavid Greenman * Page still needed. Can't go any 10310d94caffSDavid Greenman * further. 1032df8bae1dSRodney W. Grimes */ 1033df8bae1dSRodney W. Grimes return; 1034df8bae1dSRodney W. Grimes } 1035df8bae1dSRodney W. Grimes } 103624a1cce3SDavid Greenman } 1037df8bae1dSRodney W. Grimes 1038df8bae1dSRodney W. Grimes /* 10390d94caffSDavid Greenman * Make the parent shadow the next object in the 10400d94caffSDavid Greenman * chain. Deallocating backing_object will not remove 10410d94caffSDavid Greenman * it, since its reference count is at least 2. 1042df8bae1dSRodney W. Grimes */ 1043df8bae1dSRodney W. Grimes 104424a1cce3SDavid Greenman TAILQ_REMOVE(&object->backing_object->shadow_head, 104524a1cce3SDavid Greenman object, shadow_list); 104624a1cce3SDavid Greenman vm_object_reference(object->backing_object = backing_object->backing_object); 104724a1cce3SDavid Greenman if (object->backing_object) 104824a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&object->backing_object->shadow_head, 104924a1cce3SDavid Greenman object, shadow_list); 105024a1cce3SDavid Greenman object->backing_object_offset += backing_object->backing_object_offset; 1051df8bae1dSRodney W. Grimes 1052df8bae1dSRodney W. Grimes /* 10530d94caffSDavid Greenman * Drop the reference count on backing_object. Since 10540d94caffSDavid Greenman * its ref_count was at least 2, it will not vanish; 10550d94caffSDavid Greenman * so we don't need to call vm_object_deallocate. 1056df8bae1dSRodney W. Grimes */ 105726f9a767SRodney W. Grimes if (backing_object->ref_count == 1) 105826f9a767SRodney W. Grimes printf("should have called obj deallocate\n"); 1059df8bae1dSRodney W. Grimes backing_object->ref_count--; 1060df8bae1dSRodney W. Grimes 1061df8bae1dSRodney W. Grimes object_bypasses++; 1062df8bae1dSRodney W. Grimes 1063df8bae1dSRodney W. Grimes } 1064df8bae1dSRodney W. Grimes 1065df8bae1dSRodney W. Grimes /* 1066df8bae1dSRodney W. Grimes * Try again with this object's new backing object. 1067df8bae1dSRodney W. Grimes */ 1068df8bae1dSRodney W. Grimes } 1069df8bae1dSRodney W. Grimes } 1070df8bae1dSRodney W. Grimes 1071df8bae1dSRodney W. Grimes /* 1072df8bae1dSRodney W. Grimes * vm_object_page_remove: [internal] 1073df8bae1dSRodney W. Grimes * 1074df8bae1dSRodney W. Grimes * Removes all physical pages in the specified 1075df8bae1dSRodney W. Grimes * object range from the object's list of pages. 1076df8bae1dSRodney W. Grimes * 1077df8bae1dSRodney W. Grimes * The object must be locked. 1078df8bae1dSRodney W. Grimes */ 107926f9a767SRodney W. Grimes void 10807c1f6cedSDavid Greenman vm_object_page_remove(object, start, end, clean_only) 1081df8bae1dSRodney W. Grimes register vm_object_t object; 1082df8bae1dSRodney W. Grimes register vm_offset_t start; 1083df8bae1dSRodney W. Grimes register vm_offset_t end; 10847c1f6cedSDavid Greenman boolean_t clean_only; 1085df8bae1dSRodney W. Grimes { 1086df8bae1dSRodney W. Grimes register vm_page_t p, next; 108726f9a767SRodney W. Grimes vm_offset_t size; 108826f9a767SRodney W. Grimes int s; 1089df8bae1dSRodney W. Grimes 1090df8bae1dSRodney W. Grimes if (object == NULL) 1091df8bae1dSRodney W. Grimes return; 1092df8bae1dSRodney W. Grimes 10932fe6e4d7SDavid Greenman object->paging_in_progress++; 109426f9a767SRodney W. Grimes start = trunc_page(start); 109526f9a767SRodney W. Grimes end = round_page(end); 109626f9a767SRodney W. Grimes again: 109726f9a767SRodney W. Grimes size = end - start; 109826f9a767SRodney W. Grimes if (size > 4 * PAGE_SIZE || size >= object->size / 4) { 10990d94caffSDavid Greenman for (p = object->memq.tqh_first; p != NULL; p = next) { 1100df8bae1dSRodney W. Grimes next = p->listq.tqe_next; 1101df8bae1dSRodney W. Grimes if ((start <= p->offset) && (p->offset < 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 } 114826f9a767SRodney W. Grimes start += PAGE_SIZE; 114926f9a767SRodney W. Grimes size -= PAGE_SIZE; 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 11780d94caffSDavid Greenman vm_object_coalesce(prev_object, next_object, 1179df8bae1dSRodney W. Grimes prev_offset, next_offset, 1180df8bae1dSRodney W. Grimes prev_size, next_size) 1181df8bae1dSRodney W. Grimes register vm_object_t prev_object; 1182df8bae1dSRodney W. Grimes vm_object_t next_object; 1183df8bae1dSRodney W. Grimes vm_offset_t prev_offset, next_offset; 1184df8bae1dSRodney W. Grimes vm_size_t prev_size, next_size; 1185df8bae1dSRodney W. Grimes { 1186df8bae1dSRodney W. Grimes vm_size_t newsize; 1187df8bae1dSRodney W. Grimes 1188df8bae1dSRodney W. Grimes if (next_object != NULL) { 1189df8bae1dSRodney W. Grimes return (FALSE); 1190df8bae1dSRodney W. Grimes } 1191df8bae1dSRodney W. Grimes if (prev_object == NULL) { 1192df8bae1dSRodney W. Grimes return (TRUE); 1193df8bae1dSRodney W. Grimes } 1194df8bae1dSRodney W. Grimes 1195df8bae1dSRodney W. Grimes /* 1196df8bae1dSRodney W. Grimes * Try to collapse the object first 1197df8bae1dSRodney W. Grimes */ 1198df8bae1dSRodney W. Grimes vm_object_collapse(prev_object); 1199df8bae1dSRodney W. Grimes 1200df8bae1dSRodney W. Grimes /* 12010d94caffSDavid Greenman * Can't coalesce if: . more than one reference . paged out . shadows 12020d94caffSDavid Greenman * another object . has a copy elsewhere (any of which mean that the 12030d94caffSDavid Greenman * pages not mapped to prev_entry may be in use anyway) 1204df8bae1dSRodney W. Grimes */ 1205df8bae1dSRodney W. Grimes 1206df8bae1dSRodney W. Grimes if (prev_object->ref_count > 1 || 120724a1cce3SDavid Greenman prev_object->type != OBJT_DEFAULT || 120824a1cce3SDavid Greenman prev_object->backing_object != NULL) { 1209df8bae1dSRodney W. Grimes return (FALSE); 1210df8bae1dSRodney W. Grimes } 1211df8bae1dSRodney W. Grimes /* 12120d94caffSDavid Greenman * Remove any pages that may still be in the object from a previous 12130d94caffSDavid Greenman * deallocation. 1214df8bae1dSRodney W. Grimes */ 1215df8bae1dSRodney W. Grimes 1216df8bae1dSRodney W. Grimes vm_object_page_remove(prev_object, 1217df8bae1dSRodney W. Grimes prev_offset + prev_size, 12187c1f6cedSDavid Greenman prev_offset + prev_size + next_size, FALSE); 1219df8bae1dSRodney W. Grimes 1220df8bae1dSRodney W. Grimes /* 1221df8bae1dSRodney W. Grimes * Extend the object if necessary. 1222df8bae1dSRodney W. Grimes */ 1223df8bae1dSRodney W. Grimes newsize = prev_offset + prev_size + next_size; 1224df8bae1dSRodney W. Grimes if (newsize > prev_object->size) 1225df8bae1dSRodney W. Grimes prev_object->size = newsize; 1226df8bae1dSRodney W. Grimes 1227df8bae1dSRodney W. Grimes return (TRUE); 1228df8bae1dSRodney W. Grimes } 1229df8bae1dSRodney W. Grimes 1230df8bae1dSRodney W. Grimes /* 123126f9a767SRodney W. Grimes * returns page after looking up in shadow chain 123226f9a767SRodney W. Grimes */ 123326f9a767SRodney W. Grimes 1234cac597e4SBruce Evans static vm_page_t 123526f9a767SRodney W. Grimes vm_object_page_lookup(object, offset) 123626f9a767SRodney W. Grimes vm_object_t object; 123726f9a767SRodney W. Grimes vm_offset_t offset; 123826f9a767SRodney W. Grimes { 123926f9a767SRodney W. Grimes vm_page_t m; 12400d94caffSDavid Greenman 124126f9a767SRodney W. Grimes if (!(m = vm_page_lookup(object, offset))) { 124224a1cce3SDavid Greenman if (!object->backing_object) 124326f9a767SRodney W. Grimes return 0; 124426f9a767SRodney W. Grimes else 124524a1cce3SDavid Greenman return vm_object_page_lookup(object->backing_object, offset + object->backing_object_offset); 124626f9a767SRodney W. Grimes } 124726f9a767SRodney W. Grimes return m; 124826f9a767SRodney W. Grimes } 124926f9a767SRodney W. Grimes 1250c3cb3e12SDavid Greenman #ifdef DDB 1251c3cb3e12SDavid Greenman 1252cac597e4SBruce Evans static int 1253a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry) 1254a1f6d91cSDavid Greenman vm_map_t map; 1255a1f6d91cSDavid Greenman vm_object_t object; 1256a1f6d91cSDavid Greenman vm_map_entry_t entry; 1257a1f6d91cSDavid Greenman { 1258a1f6d91cSDavid Greenman vm_map_t tmpm; 1259a1f6d91cSDavid Greenman vm_map_entry_t tmpe; 1260a1f6d91cSDavid Greenman vm_object_t obj; 1261a1f6d91cSDavid Greenman int entcount; 1262a1f6d91cSDavid Greenman 1263a1f6d91cSDavid Greenman if (map == 0) 1264a1f6d91cSDavid Greenman return 0; 1265a1f6d91cSDavid Greenman 1266a1f6d91cSDavid Greenman if (entry == 0) { 1267a1f6d91cSDavid Greenman tmpe = map->header.next; 1268a1f6d91cSDavid Greenman entcount = map->nentries; 1269a1f6d91cSDavid Greenman while (entcount-- && (tmpe != &map->header)) { 1270a1f6d91cSDavid Greenman if( _vm_object_in_map(map, object, tmpe)) { 1271a1f6d91cSDavid Greenman return 1; 1272a1f6d91cSDavid Greenman } 1273a1f6d91cSDavid Greenman tmpe = tmpe->next; 1274a1f6d91cSDavid Greenman } 1275a1f6d91cSDavid Greenman } else if (entry->is_sub_map || entry->is_a_map) { 1276a1f6d91cSDavid Greenman tmpm = entry->object.share_map; 1277a1f6d91cSDavid Greenman tmpe = tmpm->header.next; 1278a1f6d91cSDavid Greenman entcount = tmpm->nentries; 1279a1f6d91cSDavid Greenman while (entcount-- && tmpe != &tmpm->header) { 1280a1f6d91cSDavid Greenman if( _vm_object_in_map(tmpm, object, tmpe)) { 1281a1f6d91cSDavid Greenman return 1; 1282a1f6d91cSDavid Greenman } 1283a1f6d91cSDavid Greenman tmpe = tmpe->next; 1284a1f6d91cSDavid Greenman } 1285a1f6d91cSDavid Greenman } else if (obj = entry->object.vm_object) { 128624a1cce3SDavid Greenman for(; obj; obj=obj->backing_object) 1287a1f6d91cSDavid Greenman if( obj == object) { 1288a1f6d91cSDavid Greenman return 1; 1289a1f6d91cSDavid Greenman } 1290a1f6d91cSDavid Greenman } 1291a1f6d91cSDavid Greenman return 0; 1292a1f6d91cSDavid Greenman } 1293a1f6d91cSDavid Greenman 1294cac597e4SBruce Evans static int 1295a1f6d91cSDavid Greenman vm_object_in_map( object) 1296a1f6d91cSDavid Greenman vm_object_t object; 1297a1f6d91cSDavid Greenman { 1298a1f6d91cSDavid Greenman struct proc *p; 1299a1f6d91cSDavid Greenman for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 1300a1f6d91cSDavid Greenman if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 1301a1f6d91cSDavid Greenman continue; 1302a1f6d91cSDavid Greenman /* 1303a1f6d91cSDavid Greenman if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 1304a1f6d91cSDavid Greenman continue; 1305a1f6d91cSDavid Greenman } 1306a1f6d91cSDavid Greenman */ 1307a1f6d91cSDavid Greenman if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) 1308a1f6d91cSDavid Greenman return 1; 1309a1f6d91cSDavid Greenman } 1310a1f6d91cSDavid Greenman if( _vm_object_in_map( kernel_map, object, 0)) 1311a1f6d91cSDavid Greenman return 1; 1312a1f6d91cSDavid Greenman if( _vm_object_in_map( kmem_map, object, 0)) 1313a1f6d91cSDavid Greenman return 1; 1314a1f6d91cSDavid Greenman if( _vm_object_in_map( pager_map, object, 0)) 1315a1f6d91cSDavid Greenman return 1; 1316a1f6d91cSDavid Greenman if( _vm_object_in_map( buffer_map, object, 0)) 1317a1f6d91cSDavid Greenman return 1; 1318a1f6d91cSDavid Greenman if( _vm_object_in_map( io_map, object, 0)) 1319a1f6d91cSDavid Greenman return 1; 1320a1f6d91cSDavid Greenman if( _vm_object_in_map( phys_map, object, 0)) 1321a1f6d91cSDavid Greenman return 1; 1322a1f6d91cSDavid Greenman if( _vm_object_in_map( mb_map, object, 0)) 1323a1f6d91cSDavid Greenman return 1; 1324a1f6d91cSDavid Greenman if( _vm_object_in_map( u_map, object, 0)) 1325a1f6d91cSDavid Greenman return 1; 1326a1f6d91cSDavid Greenman return 0; 1327a1f6d91cSDavid Greenman } 1328a1f6d91cSDavid Greenman 1329c3cb3e12SDavid Greenman 1330a1f6d91cSDavid Greenman void 1331a1f6d91cSDavid Greenman vm_object_check() { 1332a1f6d91cSDavid Greenman int i; 1333a1f6d91cSDavid Greenman int maxhash = 0; 1334a1f6d91cSDavid Greenman vm_object_t object; 1335a1f6d91cSDavid Greenman 1336a1f6d91cSDavid Greenman /* 1337a1f6d91cSDavid Greenman * make sure that internal objs are in a map somewhere 1338a1f6d91cSDavid Greenman * and none have zero ref counts. 1339a1f6d91cSDavid Greenman */ 1340a1f6d91cSDavid Greenman for (object = vm_object_list.tqh_first; 1341a1f6d91cSDavid Greenman object != NULL; 1342a1f6d91cSDavid Greenman object = object->object_list.tqe_next) { 134324a1cce3SDavid Greenman if (object->handle == NULL && 134424a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 1345a1f6d91cSDavid Greenman if (object->ref_count == 0) { 1346a1f6d91cSDavid Greenman printf("vmochk: internal obj has zero ref count: %d\n", 1347a1f6d91cSDavid Greenman object->size); 1348a1f6d91cSDavid Greenman } 1349a1f6d91cSDavid Greenman if (!vm_object_in_map(object)) { 13503af76890SPoul-Henning Kamp printf("vmochk: internal obj is not in a map: " 13513af76890SPoul-Henning Kamp "ref: %d, size: %d: 0x%x, backing_object: 0x%x\n", 13523af76890SPoul-Henning Kamp object->ref_count, object->size, 13533af76890SPoul-Henning Kamp object->size, object->backing_object); 1354a1f6d91cSDavid Greenman } 1355a1f6d91cSDavid Greenman } 1356a1f6d91cSDavid Greenman } 1357a1f6d91cSDavid Greenman } 1358a1f6d91cSDavid Greenman 135926f9a767SRodney W. Grimes /* 1360df8bae1dSRodney W. Grimes * vm_object_print: [ debug ] 1361df8bae1dSRodney W. Grimes */ 13620d94caffSDavid Greenman void 1363187f0238SBruce Evans vm_object_print(iobject, full, dummy3, dummy4) 1364187f0238SBruce Evans /* db_expr_t */ int iobject; 1365df8bae1dSRodney W. Grimes boolean_t full; 1366187f0238SBruce Evans /* db_expr_t */ int dummy3; 1367187f0238SBruce Evans char *dummy4; 1368df8bae1dSRodney W. Grimes { 1369187f0238SBruce Evans vm_object_t object = (vm_object_t)iobject; /* XXX */ 1370df8bae1dSRodney W. Grimes register vm_page_t p; 1371df8bae1dSRodney W. Grimes 1372df8bae1dSRodney W. Grimes register int count; 1373df8bae1dSRodney W. Grimes 1374df8bae1dSRodney W. Grimes if (object == NULL) 1375df8bae1dSRodney W. Grimes return; 1376df8bae1dSRodney W. Grimes 1377df8bae1dSRodney W. Grimes iprintf("Object 0x%x: size=0x%x, res=%d, ref=%d, ", 1378df8bae1dSRodney W. Grimes (int) object, (int) object->size, 1379df8bae1dSRodney W. Grimes object->resident_page_count, object->ref_count); 13802a4895f4SDavid Greenman printf("offset=0x%x, backing_object=(0x%x)+0x%x\n", 13812a4895f4SDavid Greenman (int) object->paging_offset, 138224a1cce3SDavid Greenman (int) object->backing_object, (int) object->backing_object_offset); 138305f0fdd2SPoul-Henning Kamp printf("cache: next=%p, prev=%p\n", 1384df8bae1dSRodney W. Grimes object->cached_list.tqe_next, object->cached_list.tqe_prev); 1385df8bae1dSRodney W. Grimes 1386df8bae1dSRodney W. Grimes if (!full) 1387df8bae1dSRodney W. Grimes return; 1388df8bae1dSRodney W. Grimes 1389df8bae1dSRodney W. Grimes indent += 2; 1390df8bae1dSRodney W. Grimes count = 0; 1391df8bae1dSRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) { 1392df8bae1dSRodney W. Grimes if (count == 0) 1393df8bae1dSRodney W. Grimes iprintf("memory:="); 1394df8bae1dSRodney W. Grimes else if (count == 6) { 1395df8bae1dSRodney W. Grimes printf("\n"); 1396df8bae1dSRodney W. Grimes iprintf(" ..."); 1397df8bae1dSRodney W. Grimes count = 0; 1398df8bae1dSRodney W. Grimes } else 1399df8bae1dSRodney W. Grimes printf(","); 1400df8bae1dSRodney W. Grimes count++; 1401df8bae1dSRodney W. Grimes 140205f0fdd2SPoul-Henning Kamp printf("(off=0x%lx,page=0x%lx)", 140305f0fdd2SPoul-Henning Kamp (u_long) p->offset, (u_long) VM_PAGE_TO_PHYS(p)); 1404df8bae1dSRodney W. Grimes } 1405df8bae1dSRodney W. Grimes if (count != 0) 1406df8bae1dSRodney W. Grimes printf("\n"); 1407df8bae1dSRodney W. Grimes indent -= 2; 1408df8bae1dSRodney W. Grimes } 1409c3cb3e12SDavid Greenman #endif /* DDB */ 1410