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 * 64bd7e5f99SJohn Dyson * $Id: vm_object.c,v 1.62 1996/01/04 21:13:20 wollman Exp $ 65df8bae1dSRodney W. Grimes */ 66df8bae1dSRodney W. Grimes 67df8bae1dSRodney W. Grimes /* 68df8bae1dSRodney W. Grimes * Virtual memory object module. 69df8bae1dSRodney W. Grimes */ 700e41ee30SGarrett Wollman #include "opt_ddb.h" 71df8bae1dSRodney W. Grimes 72df8bae1dSRodney W. Grimes #include <sys/param.h> 73df8bae1dSRodney W. Grimes #include <sys/systm.h> 74f23b4c91SGarrett Wollman #include <sys/kernel.h> 75f23b4c91SGarrett Wollman #include <sys/proc.h> /* for curproc, pageproc */ 76df8bae1dSRodney W. Grimes #include <sys/malloc.h> 770d94caffSDavid Greenman #include <sys/vnode.h> 780d94caffSDavid Greenman #include <sys/mount.h> 79efeaf95aSDavid Greenman #include <sys/vmmeter.h> 80df8bae1dSRodney W. Grimes 81df8bae1dSRodney W. Grimes #include <vm/vm.h> 82efeaf95aSDavid Greenman #include <vm/vm_param.h> 83efeaf95aSDavid Greenman #include <vm/vm_prot.h> 84efeaf95aSDavid Greenman #include <vm/lock.h> 85efeaf95aSDavid Greenman #include <vm/pmap.h> 86efeaf95aSDavid Greenman #include <vm/vm_map.h> 87efeaf95aSDavid Greenman #include <vm/vm_object.h> 88df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 8926f9a767SRodney W. Grimes #include <vm/vm_pageout.h> 900d94caffSDavid Greenman #include <vm/vm_pager.h> 9105f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 92a1f6d91cSDavid Greenman #include <vm/vm_kern.h> 93efeaf95aSDavid Greenman #include <vm/vm_extern.h> 9426f9a767SRodney W. Grimes 95cac597e4SBruce Evans #ifdef DDB 96f708ef1bSPoul-Henning Kamp static void DDB_vm_object_check __P((void)); 97cac597e4SBruce Evans #endif 98df8bae1dSRodney W. Grimes 99cac597e4SBruce Evans static void _vm_object_allocate __P((objtype_t, vm_size_t, vm_object_t)); 100cac597e4SBruce Evans #ifdef DDB 101cac597e4SBruce Evans static int _vm_object_in_map __P((vm_map_t map, vm_object_t object, 102cac597e4SBruce Evans vm_map_entry_t entry)); 103cac597e4SBruce Evans static int vm_object_in_map __P((vm_object_t object)); 104cac597e4SBruce Evans #endif 105cac597e4SBruce Evans static void vm_object_qcollapse __P((vm_object_t object)); 106f708ef1bSPoul-Henning Kamp #ifdef not_used 107f708ef1bSPoul-Henning Kamp static void vm_object_deactivate_pages __P((vm_object_t)); 108f708ef1bSPoul-Henning Kamp #endif 109f708ef1bSPoul-Henning Kamp static void vm_object_terminate __P((vm_object_t)); 110f708ef1bSPoul-Henning Kamp static void vm_object_cache_trim __P((void)); 111f6b04d2bSDavid Greenman 112df8bae1dSRodney W. Grimes /* 113df8bae1dSRodney W. Grimes * Virtual memory objects maintain the actual data 114df8bae1dSRodney W. Grimes * associated with allocated virtual memory. A given 115df8bae1dSRodney W. Grimes * page of memory exists within exactly one object. 116df8bae1dSRodney W. Grimes * 117df8bae1dSRodney W. Grimes * An object is only deallocated when all "references" 118df8bae1dSRodney W. Grimes * are given up. Only one "reference" to a given 119df8bae1dSRodney W. Grimes * region of an object should be writeable. 120df8bae1dSRodney W. Grimes * 121df8bae1dSRodney W. Grimes * Associated with each object is a list of all resident 122df8bae1dSRodney W. Grimes * memory pages belonging to that object; this list is 123df8bae1dSRodney W. Grimes * maintained by the "vm_page" module, and locked by the object's 124df8bae1dSRodney W. Grimes * lock. 125df8bae1dSRodney W. Grimes * 126df8bae1dSRodney W. Grimes * Each object also records a "pager" routine which is 127df8bae1dSRodney W. Grimes * used to retrieve (and store) pages to the proper backing 128df8bae1dSRodney W. Grimes * storage. In addition, objects may be backed by other 129df8bae1dSRodney W. Grimes * objects from which they were virtual-copied. 130df8bae1dSRodney W. Grimes * 131df8bae1dSRodney W. Grimes * The only items within the object structure which are 132df8bae1dSRodney W. Grimes * modified after time of creation are: 133df8bae1dSRodney W. Grimes * reference count locked by object's lock 134df8bae1dSRodney W. Grimes * pager routine locked by object's lock 135df8bae1dSRodney W. Grimes * 136df8bae1dSRodney W. Grimes */ 137df8bae1dSRodney W. Grimes 13828f8db14SBruce Evans int vm_object_cache_max; 13928f8db14SBruce Evans struct object_q vm_object_cached_list; 140f708ef1bSPoul-Henning Kamp static int vm_object_cached; 14128f8db14SBruce Evans struct object_q vm_object_list; 142f708ef1bSPoul-Henning Kamp static long vm_object_count; 14328f8db14SBruce Evans vm_object_t kernel_object; 14428f8db14SBruce Evans vm_object_t kmem_object; 145f708ef1bSPoul-Henning Kamp static struct vm_object kernel_object_store; 146f708ef1bSPoul-Henning Kamp static struct vm_object kmem_object_store; 147aef922f5SJohn Dyson extern int vm_pageout_page_count; 148df8bae1dSRodney W. Grimes 149f708ef1bSPoul-Henning Kamp static long object_collapses; 150f708ef1bSPoul-Henning Kamp static long object_bypasses; 151df8bae1dSRodney W. Grimes 152df8bae1dSRodney W. Grimes static void 15324a1cce3SDavid Greenman _vm_object_allocate(type, size, object) 15424a1cce3SDavid Greenman objtype_t type; 155df8bae1dSRodney W. Grimes vm_size_t size; 156df8bae1dSRodney W. Grimes register vm_object_t object; 157df8bae1dSRodney W. Grimes { 158df8bae1dSRodney W. Grimes TAILQ_INIT(&object->memq); 15924a1cce3SDavid Greenman TAILQ_INIT(&object->shadow_head); 160a1f6d91cSDavid Greenman 16124a1cce3SDavid Greenman object->type = type; 162df8bae1dSRodney W. Grimes object->size = size; 163a1f6d91cSDavid Greenman object->ref_count = 1; 16424a1cce3SDavid Greenman object->flags = 0; 165df8bae1dSRodney W. Grimes object->paging_in_progress = 0; 166a1f6d91cSDavid Greenman object->resident_page_count = 0; 16724a1cce3SDavid Greenman object->handle = NULL; 168a316d390SJohn Dyson object->paging_offset = (vm_ooffset_t) 0; 16924a1cce3SDavid Greenman object->backing_object = NULL; 170a316d390SJohn Dyson object->backing_object_offset = (vm_ooffset_t) 0; 171a1f6d91cSDavid Greenman 172a1f6d91cSDavid Greenman object->last_read = 0; 173df8bae1dSRodney W. Grimes 174df8bae1dSRodney W. Grimes TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 175df8bae1dSRodney W. Grimes vm_object_count++; 176df8bae1dSRodney W. Grimes } 177df8bae1dSRodney W. Grimes 178df8bae1dSRodney W. Grimes /* 17926f9a767SRodney W. Grimes * vm_object_init: 18026f9a767SRodney W. Grimes * 18126f9a767SRodney W. Grimes * Initialize the VM objects module. 18226f9a767SRodney W. Grimes */ 18326f9a767SRodney W. Grimes void 184a316d390SJohn Dyson vm_object_init() 18526f9a767SRodney W. Grimes { 18626f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_cached_list); 18726f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_list); 18826f9a767SRodney W. Grimes vm_object_count = 0; 1890217125fSDavid Greenman 1900217125fSDavid Greenman vm_object_cache_max = 84; 1910217125fSDavid Greenman if (cnt.v_page_count > 1000) 192a2d5b142SDavid Greenman vm_object_cache_max += (cnt.v_page_count - 1000) / 3; 19326f9a767SRodney W. Grimes 19426f9a767SRodney W. Grimes kernel_object = &kernel_object_store; 195a316d390SJohn Dyson _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 19626f9a767SRodney W. Grimes kernel_object); 19726f9a767SRodney W. Grimes 19826f9a767SRodney W. Grimes kmem_object = &kmem_object_store; 199a316d390SJohn Dyson _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 20026f9a767SRodney W. Grimes kmem_object); 20126f9a767SRodney W. Grimes } 20226f9a767SRodney W. Grimes 20326f9a767SRodney W. Grimes /* 20426f9a767SRodney W. Grimes * vm_object_allocate: 20526f9a767SRodney W. Grimes * 20626f9a767SRodney W. Grimes * Returns a new object with the given size. 20726f9a767SRodney W. Grimes */ 20826f9a767SRodney W. Grimes 20926f9a767SRodney W. Grimes vm_object_t 21024a1cce3SDavid Greenman vm_object_allocate(type, size) 21124a1cce3SDavid Greenman objtype_t type; 21226f9a767SRodney W. Grimes vm_size_t size; 21326f9a767SRodney W. Grimes { 21426f9a767SRodney W. Grimes register vm_object_t result; 21526f9a767SRodney W. Grimes 21626f9a767SRodney W. Grimes result = (vm_object_t) 21726f9a767SRodney W. Grimes malloc((u_long) sizeof *result, M_VMOBJ, M_WAITOK); 21826f9a767SRodney W. Grimes 21926f9a767SRodney W. Grimes 22024a1cce3SDavid Greenman _vm_object_allocate(type, size, result); 22126f9a767SRodney W. Grimes 22226f9a767SRodney W. Grimes return (result); 22326f9a767SRodney W. Grimes } 22426f9a767SRodney W. Grimes 22526f9a767SRodney W. Grimes 22626f9a767SRodney W. Grimes /* 227df8bae1dSRodney W. Grimes * vm_object_reference: 228df8bae1dSRodney W. Grimes * 229df8bae1dSRodney W. Grimes * Gets another reference to the given object. 230df8bae1dSRodney W. Grimes */ 23126f9a767SRodney W. Grimes inline void 23226f9a767SRodney W. Grimes vm_object_reference(object) 233df8bae1dSRodney W. Grimes register vm_object_t object; 234df8bae1dSRodney W. Grimes { 235df8bae1dSRodney W. Grimes if (object == NULL) 236df8bae1dSRodney W. Grimes return; 237df8bae1dSRodney W. Grimes 23824a1cce3SDavid Greenman if (object->ref_count == 0) { 23924a1cce3SDavid Greenman if ((object->flags & OBJ_CANPERSIST) == 0) 24024a1cce3SDavid Greenman panic("vm_object_reference: non-persistent object with 0 ref_count"); 24124a1cce3SDavid Greenman TAILQ_REMOVE(&vm_object_cached_list, object, cached_list); 24224a1cce3SDavid Greenman vm_object_cached--; 24324a1cce3SDavid Greenman } 244df8bae1dSRodney W. Grimes object->ref_count++; 245df8bae1dSRodney W. Grimes } 246df8bae1dSRodney W. Grimes 247df8bae1dSRodney W. Grimes /* 248df8bae1dSRodney W. Grimes * vm_object_deallocate: 249df8bae1dSRodney W. Grimes * 250df8bae1dSRodney W. Grimes * Release a reference to the specified object, 251df8bae1dSRodney W. Grimes * gained either through a vm_object_allocate 252df8bae1dSRodney W. Grimes * or a vm_object_reference call. When all references 253df8bae1dSRodney W. Grimes * are gone, storage associated with this object 254df8bae1dSRodney W. Grimes * may be relinquished. 255df8bae1dSRodney W. Grimes * 256df8bae1dSRodney W. Grimes * No object may be locked. 257df8bae1dSRodney W. Grimes */ 25826f9a767SRodney W. Grimes void 25926f9a767SRodney W. Grimes vm_object_deallocate(object) 26026f9a767SRodney W. Grimes vm_object_t object; 261df8bae1dSRodney W. Grimes { 262df8bae1dSRodney W. Grimes vm_object_t temp; 263df8bae1dSRodney W. Grimes 264df8bae1dSRodney W. Grimes while (object != NULL) { 265df8bae1dSRodney W. Grimes 266ba8da839SDavid Greenman if (object->ref_count == 0) 267ba8da839SDavid Greenman panic("vm_object_deallocate: object deallocated too many times"); 268ba8da839SDavid Greenman 269df8bae1dSRodney W. Grimes /* 270df8bae1dSRodney W. Grimes * Lose the reference 271df8bae1dSRodney W. Grimes */ 272be6d5bfaSDavid Greenman object->ref_count--; 273be6d5bfaSDavid Greenman 274be6d5bfaSDavid Greenman if (object->ref_count != 0) { 275be6d5bfaSDavid Greenman if ((object->ref_count == 1) && 27624a1cce3SDavid Greenman (object->handle == NULL) && 27724a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || 27824a1cce3SDavid Greenman object->type == OBJT_SWAP)) { 279a1f6d91cSDavid Greenman vm_object_t robject; 28024a1cce3SDavid Greenman robject = object->shadow_head.tqh_first; 281be6d5bfaSDavid Greenman if ((robject != NULL) && 28224a1cce3SDavid Greenman (robject->handle == NULL) && 28324a1cce3SDavid Greenman (robject->type == OBJT_DEFAULT || 28424a1cce3SDavid Greenman robject->type == OBJT_SWAP)) { 285a1f6d91cSDavid Greenman int s; 286a1f6d91cSDavid Greenman robject->ref_count += 2; 2879b4814bbSDavid Greenman object->ref_count += 2; 288a1f6d91cSDavid Greenman 289a1f6d91cSDavid Greenman do { 290a1f6d91cSDavid Greenman s = splhigh(); 291a1f6d91cSDavid Greenman while (robject->paging_in_progress) { 292c0503609SDavid Greenman robject->flags |= OBJ_PIPWNT; 293a1f6d91cSDavid Greenman tsleep(robject, PVM, "objde1", 0); 2940d94caffSDavid Greenman } 295a1f6d91cSDavid Greenman 296a1f6d91cSDavid Greenman while (object->paging_in_progress) { 297c0503609SDavid Greenman object->flags |= OBJ_PIPWNT; 298a1f6d91cSDavid Greenman tsleep(object, PVM, "objde2", 0); 299a1f6d91cSDavid Greenman } 300a1f6d91cSDavid Greenman splx(s); 301a1f6d91cSDavid Greenman 302a1f6d91cSDavid Greenman } while( object->paging_in_progress || robject->paging_in_progress); 303a1f6d91cSDavid Greenman 3049b4814bbSDavid Greenman object->ref_count -= 2; 305a1f6d91cSDavid Greenman robject->ref_count -= 2; 306a1f6d91cSDavid Greenman if( robject->ref_count == 0) { 307a1f6d91cSDavid Greenman robject->ref_count += 1; 308ba8da839SDavid Greenman object = robject; 309ba8da839SDavid Greenman continue; 310a1f6d91cSDavid Greenman } 311a1f6d91cSDavid Greenman vm_object_collapse(robject); 312a1f6d91cSDavid Greenman return; 313010cf3b9SDavid Greenman } 314010cf3b9SDavid Greenman } 315df8bae1dSRodney W. Grimes /* 3160d94caffSDavid Greenman * If there are still references, then we are done. 317df8bae1dSRodney W. Grimes */ 318df8bae1dSRodney W. Grimes return; 319df8bae1dSRodney W. Grimes } 320df8bae1dSRodney W. Grimes 32124a1cce3SDavid Greenman if (object->type == OBJT_VNODE) { 32224a1cce3SDavid Greenman struct vnode *vp = object->handle; 323f5cf85d4SDavid Greenman 32424a1cce3SDavid Greenman vp->v_flag &= ~VTEXT; 325f5cf85d4SDavid Greenman } 326f5cf85d4SDavid Greenman 327f919ebdeSDavid Greenman /* 328f919ebdeSDavid Greenman * See if this object can persist and has some resident 329f919ebdeSDavid Greenman * pages. If so, enter it in the cache. 330f919ebdeSDavid Greenman */ 331f5cf85d4SDavid Greenman if (object->flags & OBJ_CANPERSIST) { 332f5cf85d4SDavid Greenman if (object->resident_page_count != 0) { 33324a1cce3SDavid Greenman vm_object_page_clean(object, 0, 0 ,TRUE, TRUE); 334df8bae1dSRodney W. Grimes TAILQ_INSERT_TAIL(&vm_object_cached_list, object, 335df8bae1dSRodney W. Grimes cached_list); 336df8bae1dSRodney W. Grimes vm_object_cached++; 337df8bae1dSRodney W. Grimes 338df8bae1dSRodney W. Grimes vm_object_cache_trim(); 339df8bae1dSRodney W. Grimes return; 340f5cf85d4SDavid Greenman } else { 341f5cf85d4SDavid Greenman object->flags &= ~OBJ_CANPERSIST; 342f5cf85d4SDavid Greenman } 343df8bae1dSRodney W. Grimes } 344f919ebdeSDavid Greenman 345df8bae1dSRodney W. Grimes /* 34624a1cce3SDavid Greenman * Make sure no one uses us. 347df8bae1dSRodney W. Grimes */ 3480d94caffSDavid Greenman object->flags |= OBJ_DEAD; 349df8bae1dSRodney W. Grimes 35024a1cce3SDavid Greenman temp = object->backing_object; 3512fe6e4d7SDavid Greenman if (temp) 35224a1cce3SDavid Greenman TAILQ_REMOVE(&temp->shadow_head, object, shadow_list); 353df8bae1dSRodney W. Grimes vm_object_terminate(object); 354df8bae1dSRodney W. Grimes /* unlocks and deallocates object */ 355df8bae1dSRodney W. Grimes object = temp; 356df8bae1dSRodney W. Grimes } 357df8bae1dSRodney W. Grimes } 358df8bae1dSRodney W. Grimes 359df8bae1dSRodney W. Grimes /* 360df8bae1dSRodney W. Grimes * vm_object_terminate actually destroys the specified object, freeing 361df8bae1dSRodney W. Grimes * up all previously used resources. 362df8bae1dSRodney W. Grimes * 363df8bae1dSRodney W. Grimes * The object must be locked. 364df8bae1dSRodney W. Grimes */ 365f708ef1bSPoul-Henning Kamp static void 36626f9a767SRodney W. Grimes vm_object_terminate(object) 367df8bae1dSRodney W. Grimes register vm_object_t object; 368df8bae1dSRodney W. Grimes { 3693af76890SPoul-Henning Kamp register vm_page_t p; 37026f9a767SRodney W. Grimes int s; 371df8bae1dSRodney W. Grimes 372df8bae1dSRodney W. Grimes /* 373f6b04d2bSDavid Greenman * wait for the pageout daemon to be done with the object 374df8bae1dSRodney W. Grimes */ 3750d94caffSDavid Greenman s = splhigh(); 376df8bae1dSRodney W. Grimes while (object->paging_in_progress) { 377c0503609SDavid Greenman object->flags |= OBJ_PIPWNT; 37824a1cce3SDavid Greenman tsleep(object, PVM, "objtrm", 0); 379df8bae1dSRodney W. Grimes } 3800d94caffSDavid Greenman splx(s); 381df8bae1dSRodney W. Grimes 38226f9a767SRodney W. Grimes if (object->paging_in_progress != 0) 38326f9a767SRodney W. Grimes panic("vm_object_deallocate: pageout in progress"); 38426f9a767SRodney W. Grimes 38526f9a767SRodney W. Grimes /* 3860d94caffSDavid Greenman * Clean and free the pages, as appropriate. All references to the 3870d94caffSDavid Greenman * object are gone, so we don't need to lock it. 38826f9a767SRodney W. Grimes */ 38924a1cce3SDavid Greenman if (object->type == OBJT_VNODE) { 39024a1cce3SDavid Greenman struct vnode *vp = object->handle; 391f6b04d2bSDavid Greenman 3920426122fSDavid Greenman VOP_LOCK(vp); 39324a1cce3SDavid Greenman vm_object_page_clean(object, 0, 0, TRUE, FALSE); 394f6b04d2bSDavid Greenman vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0); 3950426122fSDavid Greenman VOP_UNLOCK(vp); 396df8bae1dSRodney W. Grimes } 39783edfd47SDavid Greenman 3980d94caffSDavid Greenman /* 3990d94caffSDavid Greenman * Now free the pages. For internal objects, this also removes them 4000d94caffSDavid Greenman * from paging queues. 401df8bae1dSRodney W. Grimes */ 402df8bae1dSRodney W. Grimes while ((p = object->memq.tqh_first) != NULL) { 403be6d5bfaSDavid Greenman if (p->flags & PG_BUSY) 404be6d5bfaSDavid Greenman printf("vm_object_terminate: freeing busy page\n"); 4050d94caffSDavid Greenman PAGE_WAKEUP(p); 406df8bae1dSRodney W. Grimes vm_page_free(p); 407df8bae1dSRodney W. Grimes cnt.v_pfree++; 408df8bae1dSRodney W. Grimes } 409df8bae1dSRodney W. Grimes 410df8bae1dSRodney W. Grimes /* 411df8bae1dSRodney W. Grimes * Let the pager know object is dead. 412df8bae1dSRodney W. Grimes */ 41324a1cce3SDavid Greenman vm_pager_deallocate(object); 414df8bae1dSRodney W. Grimes 415df8bae1dSRodney W. Grimes TAILQ_REMOVE(&vm_object_list, object, object_list); 416df8bae1dSRodney W. Grimes vm_object_count--; 417df8bae1dSRodney W. Grimes 4180426122fSDavid Greenman wakeup(object); 4190426122fSDavid Greenman 420df8bae1dSRodney W. Grimes /* 421df8bae1dSRodney W. Grimes * Free the space for the object. 422df8bae1dSRodney W. Grimes */ 423df8bae1dSRodney W. Grimes free((caddr_t) object, M_VMOBJ); 424df8bae1dSRodney W. Grimes } 425df8bae1dSRodney W. Grimes 426df8bae1dSRodney W. Grimes /* 427df8bae1dSRodney W. Grimes * vm_object_page_clean 428df8bae1dSRodney W. Grimes * 429df8bae1dSRodney W. Grimes * Clean all dirty pages in the specified range of object. 43026f9a767SRodney W. Grimes * Leaves page on whatever queue it is currently on. 43126f9a767SRodney W. Grimes * 43226f9a767SRodney W. Grimes * Odd semantics: if start == end, we clean everything. 43326f9a767SRodney W. Grimes * 43426f9a767SRodney W. Grimes * The object must be locked. 43526f9a767SRodney W. Grimes */ 436f6b04d2bSDavid Greenman 437f6b04d2bSDavid Greenman void 43824a1cce3SDavid Greenman vm_object_page_clean(object, start, end, syncio, lockflag) 439f6b04d2bSDavid Greenman vm_object_t object; 440a316d390SJohn Dyson vm_pindex_t start; 441a316d390SJohn Dyson vm_pindex_t end; 442f6b04d2bSDavid Greenman boolean_t syncio; 44324a1cce3SDavid Greenman boolean_t lockflag; 444f6b04d2bSDavid Greenman { 445bd7e5f99SJohn Dyson register vm_page_t p, np, tp; 446f6b04d2bSDavid Greenman register vm_offset_t tstart, tend; 447bd7e5f99SJohn Dyson vm_pindex_t pi; 448aef922f5SJohn Dyson int s; 44924a1cce3SDavid Greenman struct vnode *vp; 450aef922f5SJohn Dyson int runlen; 451bd7e5f99SJohn Dyson int maxf; 452bd7e5f99SJohn Dyson int chkb; 453bd7e5f99SJohn Dyson int maxb; 454bd7e5f99SJohn Dyson int i; 455bd7e5f99SJohn Dyson vm_page_t maf[vm_pageout_page_count]; 456bd7e5f99SJohn Dyson vm_page_t mab[vm_pageout_page_count]; 457aef922f5SJohn Dyson vm_page_t ma[vm_pageout_page_count]; 458f6b04d2bSDavid Greenman 459aef922f5SJohn Dyson if (object->type != OBJT_VNODE || 460aef922f5SJohn Dyson (object->flags & OBJ_MIGHTBEDIRTY) == 0) 461f6b04d2bSDavid Greenman return; 462f6b04d2bSDavid Greenman 46324a1cce3SDavid Greenman vp = object->handle; 46424a1cce3SDavid Greenman 46524a1cce3SDavid Greenman if (lockflag) 46624a1cce3SDavid Greenman VOP_LOCK(vp); 467aef922f5SJohn Dyson object->flags |= OBJ_CLEANING; 46824a1cce3SDavid Greenman 469f6b04d2bSDavid Greenman tstart = start; 470f6b04d2bSDavid Greenman if (end == 0) { 471f6b04d2bSDavid Greenman tend = object->size; 472f6b04d2bSDavid Greenman } else { 473f6b04d2bSDavid Greenman tend = end; 474f6b04d2bSDavid Greenman } 475a316d390SJohn Dyson if ((tstart == 0) && (tend == object->size)) { 476aef922f5SJohn Dyson object->flags &= ~(OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY); 477ec4f9fb0SDavid Greenman } 478bd7e5f99SJohn Dyson for(p = object->memq.tqh_first; p; p = p->listq.tqe_next) 479bd7e5f99SJohn Dyson p->flags |= PG_CLEANCHK; 480f6b04d2bSDavid Greenman 481bd7e5f99SJohn Dyson rescan: 482bd7e5f99SJohn Dyson for(p = object->memq.tqh_first; p; p = np) { 483bd7e5f99SJohn Dyson np = p->listq.tqe_next; 484bd7e5f99SJohn Dyson 485bd7e5f99SJohn Dyson pi = p->pindex; 486bd7e5f99SJohn Dyson if (((p->flags & PG_CLEANCHK) == 0) || 487bd7e5f99SJohn Dyson (pi < tstart) || (pi >= tend) || 488bd7e5f99SJohn Dyson (p->valid == 0) || (p->queue == PQ_CACHE)) { 489bd7e5f99SJohn Dyson p->flags &= ~PG_CLEANCHK; 490aef922f5SJohn Dyson continue; 491f6b04d2bSDavid Greenman } 492f6b04d2bSDavid Greenman 493bd7e5f99SJohn Dyson vm_page_test_dirty(p); 494bd7e5f99SJohn Dyson if ((p->dirty & p->valid) == 0) { 495bd7e5f99SJohn Dyson p->flags &= ~PG_CLEANCHK; 496bd7e5f99SJohn Dyson continue; 497bd7e5f99SJohn Dyson } 498ec4f9fb0SDavid Greenman 499f6b04d2bSDavid Greenman s = splhigh(); 500bd7e5f99SJohn Dyson if ((p->flags & PG_BUSY) || p->busy) { 501aef922f5SJohn Dyson p->flags |= PG_WANTED|PG_REFERENCED; 502aef922f5SJohn Dyson tsleep(p, PVM, "vpcwai", 0); 503f6b04d2bSDavid Greenman splx(s); 504bd7e5f99SJohn Dyson goto rescan; 505f6b04d2bSDavid Greenman } 506f6b04d2bSDavid Greenman splx(s); 507f6b04d2bSDavid Greenman 508bd7e5f99SJohn Dyson maxf = 0; 509bd7e5f99SJohn Dyson for(i=1;i<vm_pageout_page_count;i++) { 510bd7e5f99SJohn Dyson if (tp = vm_page_lookup(object, pi + i)) { 511bd7e5f99SJohn Dyson if ((tp->flags & PG_BUSY) || 512bd7e5f99SJohn Dyson (tp->flags & PG_CLEANCHK) == 0) 513bd7e5f99SJohn Dyson break; 514bd7e5f99SJohn Dyson vm_page_test_dirty(tp); 515bd7e5f99SJohn Dyson if ((tp->dirty & tp->valid) == 0) { 516bd7e5f99SJohn Dyson tp->flags &= ~PG_CLEANCHK; 517bd7e5f99SJohn Dyson break; 518bd7e5f99SJohn Dyson } 519bd7e5f99SJohn Dyson maf[ i - 1 ] = tp; 520bd7e5f99SJohn Dyson maxf++; 521bd7e5f99SJohn Dyson continue; 522bd7e5f99SJohn Dyson } 523bd7e5f99SJohn Dyson break; 524bd7e5f99SJohn Dyson } 525aef922f5SJohn Dyson 526bd7e5f99SJohn Dyson maxb = 0; 527bd7e5f99SJohn Dyson chkb = vm_pageout_page_count - maxf; 528bd7e5f99SJohn Dyson if (chkb) { 529bd7e5f99SJohn Dyson for(i = 1; i < chkb;i++) { 530bd7e5f99SJohn Dyson if (tp = vm_page_lookup(object, pi - i)) { 531bd7e5f99SJohn Dyson if ((tp->flags & PG_BUSY) || 532bd7e5f99SJohn Dyson (tp->flags & PG_CLEANCHK) == 0) 533bd7e5f99SJohn Dyson break; 534bd7e5f99SJohn Dyson vm_page_test_dirty(tp); 535bd7e5f99SJohn Dyson if ((tp->dirty & tp->valid) == 0) { 536bd7e5f99SJohn Dyson tp->flags &= ~PG_CLEANCHK; 537bd7e5f99SJohn Dyson break; 538bd7e5f99SJohn Dyson } 539bd7e5f99SJohn Dyson mab[ i - 1 ] = tp; 540bd7e5f99SJohn Dyson maxb++; 541bd7e5f99SJohn Dyson continue; 542bd7e5f99SJohn Dyson } 543bd7e5f99SJohn Dyson break; 544bd7e5f99SJohn Dyson } 545bd7e5f99SJohn Dyson } 546bd7e5f99SJohn Dyson 547bd7e5f99SJohn Dyson for(i=0;i<maxb;i++) { 548bd7e5f99SJohn Dyson int index = (maxb - i) - 1; 549bd7e5f99SJohn Dyson ma[index] = mab[i]; 550bd7e5f99SJohn Dyson ma[index]->flags |= PG_BUSY; 551bd7e5f99SJohn Dyson ma[index]->flags &= ~PG_CLEANCHK; 552bd7e5f99SJohn Dyson vm_page_protect(ma[index], VM_PROT_READ); 553bd7e5f99SJohn Dyson } 554bd7e5f99SJohn Dyson vm_page_protect(p, VM_PROT_READ); 555aef922f5SJohn Dyson p->flags |= PG_BUSY; 556bd7e5f99SJohn Dyson p->flags &= ~PG_CLEANCHK; 557bd7e5f99SJohn Dyson ma[maxb] = p; 558bd7e5f99SJohn Dyson for(i=0;i<maxf;i++) { 559bd7e5f99SJohn Dyson int index = (maxb + i) + 1; 560bd7e5f99SJohn Dyson ma[index] = maf[i]; 561bd7e5f99SJohn Dyson ma[index]->flags |= PG_BUSY; 562bd7e5f99SJohn Dyson ma[index]->flags &= ~PG_CLEANCHK; 563bd7e5f99SJohn Dyson vm_page_protect(ma[index], VM_PROT_READ); 564f6b04d2bSDavid Greenman } 565bd7e5f99SJohn Dyson runlen = maxb + maxf + 1; 566bd7e5f99SJohn Dyson /* 567bd7e5f99SJohn Dyson printf("maxb: %d, maxf: %d, runlen: %d, offset: %d\n", maxb, maxf, runlen, ma[0]->pindex); 568bd7e5f99SJohn Dyson */ 569bd7e5f99SJohn Dyson vm_pageout_flush(ma, runlen, 0); 570bd7e5f99SJohn Dyson goto rescan; 571f6b04d2bSDavid Greenman } 572aef922f5SJohn Dyson 573aef922f5SJohn Dyson VOP_FSYNC(vp, NULL, syncio, curproc); 574aef922f5SJohn Dyson 57524a1cce3SDavid Greenman if (lockflag) 576d3628763SRodney W. Grimes VOP_UNLOCK(vp); 577aef922f5SJohn Dyson object->flags &= ~OBJ_CLEANING; 578f5cf85d4SDavid Greenman return; 57926f9a767SRodney W. Grimes } 580df8bae1dSRodney W. Grimes 581f708ef1bSPoul-Henning Kamp #ifdef not_used 582f708ef1bSPoul-Henning Kamp /* XXX I cannot tell if this should be an exported symbol */ 583df8bae1dSRodney W. Grimes /* 584df8bae1dSRodney W. Grimes * vm_object_deactivate_pages 585df8bae1dSRodney W. Grimes * 586df8bae1dSRodney W. Grimes * Deactivate all pages in the specified object. (Keep its pages 587df8bae1dSRodney W. Grimes * in memory even though it is no longer referenced.) 588df8bae1dSRodney W. Grimes * 589df8bae1dSRodney W. Grimes * The object must be locked. 590df8bae1dSRodney W. Grimes */ 591f708ef1bSPoul-Henning Kamp static void 592df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object) 593df8bae1dSRodney W. Grimes register vm_object_t object; 594df8bae1dSRodney W. Grimes { 595df8bae1dSRodney W. Grimes register vm_page_t p, next; 596df8bae1dSRodney W. Grimes 597df8bae1dSRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = next) { 598df8bae1dSRodney W. Grimes next = p->listq.tqe_next; 599df8bae1dSRodney W. Grimes vm_page_deactivate(p); 600df8bae1dSRodney W. Grimes } 601df8bae1dSRodney W. Grimes } 602f708ef1bSPoul-Henning Kamp #endif 603df8bae1dSRodney W. Grimes 604df8bae1dSRodney W. Grimes /* 605df8bae1dSRodney W. Grimes * Trim the object cache to size. 606df8bae1dSRodney W. Grimes */ 607f708ef1bSPoul-Henning Kamp static void 608df8bae1dSRodney W. Grimes vm_object_cache_trim() 609df8bae1dSRodney W. Grimes { 610df8bae1dSRodney W. Grimes register vm_object_t object; 611df8bae1dSRodney W. Grimes 6120d94caffSDavid Greenman while (vm_object_cached > vm_object_cache_max) { 613df8bae1dSRodney W. Grimes object = vm_object_cached_list.tqh_first; 614df8bae1dSRodney W. Grimes 61524a1cce3SDavid Greenman vm_object_reference(object); 616df8bae1dSRodney W. Grimes pager_cache(object, FALSE); 617df8bae1dSRodney W. Grimes } 618df8bae1dSRodney W. Grimes } 619df8bae1dSRodney W. Grimes 62026f9a767SRodney W. Grimes 621df8bae1dSRodney W. Grimes /* 622df8bae1dSRodney W. Grimes * vm_object_pmap_copy: 623df8bae1dSRodney W. Grimes * 624df8bae1dSRodney W. Grimes * Makes all physical pages in the specified 625df8bae1dSRodney W. Grimes * object range copy-on-write. No writeable 626df8bae1dSRodney W. Grimes * references to these pages should remain. 627df8bae1dSRodney W. Grimes * 628df8bae1dSRodney W. Grimes * The object must *not* be locked. 629df8bae1dSRodney W. Grimes */ 6300d94caffSDavid Greenman void 6310d94caffSDavid Greenman vm_object_pmap_copy(object, start, end) 632df8bae1dSRodney W. Grimes register vm_object_t object; 633a316d390SJohn Dyson register vm_pindex_t start; 634a316d390SJohn Dyson register vm_pindex_t end; 635df8bae1dSRodney W. Grimes { 636df8bae1dSRodney W. Grimes register vm_page_t p; 637df8bae1dSRodney W. Grimes 638aef922f5SJohn Dyson if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0) 639df8bae1dSRodney W. Grimes return; 640df8bae1dSRodney W. Grimes 641df8bae1dSRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) { 642f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_READ); 643df8bae1dSRodney W. Grimes } 644aef922f5SJohn Dyson 645aef922f5SJohn Dyson object->flags &= ~OBJ_WRITEABLE; 646df8bae1dSRodney W. Grimes } 647df8bae1dSRodney W. Grimes 648df8bae1dSRodney W. Grimes /* 649df8bae1dSRodney W. Grimes * vm_object_pmap_remove: 650df8bae1dSRodney W. Grimes * 651df8bae1dSRodney W. Grimes * Removes all physical pages in the specified 652df8bae1dSRodney W. Grimes * object range from all physical maps. 653df8bae1dSRodney W. Grimes * 654df8bae1dSRodney W. Grimes * The object must *not* be locked. 655df8bae1dSRodney W. Grimes */ 65626f9a767SRodney W. Grimes void 65726f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end) 658df8bae1dSRodney W. Grimes register vm_object_t object; 659a316d390SJohn Dyson register vm_pindex_t start; 660a316d390SJohn Dyson register vm_pindex_t end; 661df8bae1dSRodney W. Grimes { 662df8bae1dSRodney W. Grimes register vm_page_t p; 663df8bae1dSRodney W. Grimes if (object == NULL) 664df8bae1dSRodney W. Grimes return; 66526f9a767SRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) { 666bd7e5f99SJohn Dyson if (p->pindex >= start && p->pindex < end) 667f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 66826f9a767SRodney W. Grimes } 66926f9a767SRodney W. Grimes } 670df8bae1dSRodney W. Grimes 671df8bae1dSRodney W. Grimes /* 672df8bae1dSRodney W. Grimes * vm_object_copy: 673df8bae1dSRodney W. Grimes * 674df8bae1dSRodney W. Grimes * Create a new object which is a copy of an existing 675df8bae1dSRodney W. Grimes * object, and mark all of the pages in the existing 676df8bae1dSRodney W. Grimes * object 'copy-on-write'. The new object has one reference. 677df8bae1dSRodney W. Grimes * Returns the new object. 678df8bae1dSRodney W. Grimes * 679df8bae1dSRodney W. Grimes * May defer the copy until later if the object is not backed 680df8bae1dSRodney W. Grimes * up by a non-default pager. 681df8bae1dSRodney W. Grimes */ 6820d94caffSDavid Greenman void 683a316d390SJohn Dyson vm_object_copy(src_object, src_offset, 684df8bae1dSRodney W. Grimes dst_object, dst_offset, src_needs_copy) 685df8bae1dSRodney W. Grimes register vm_object_t src_object; 686a316d390SJohn Dyson vm_pindex_t src_offset; 687df8bae1dSRodney W. Grimes vm_object_t *dst_object;/* OUT */ 688a316d390SJohn Dyson vm_pindex_t *dst_offset;/* OUT */ 689df8bae1dSRodney W. Grimes boolean_t *src_needs_copy; /* OUT */ 690df8bae1dSRodney W. Grimes { 691df8bae1dSRodney W. Grimes if (src_object == NULL) { 692df8bae1dSRodney W. Grimes /* 693df8bae1dSRodney W. Grimes * Nothing to copy 694df8bae1dSRodney W. Grimes */ 695df8bae1dSRodney W. Grimes *dst_object = NULL; 696df8bae1dSRodney W. Grimes *dst_offset = 0; 697df8bae1dSRodney W. Grimes *src_needs_copy = FALSE; 698df8bae1dSRodney W. Grimes return; 699df8bae1dSRodney W. Grimes } 70026f9a767SRodney W. Grimes 70126f9a767SRodney W. Grimes /* 70226f9a767SRodney W. Grimes * Try to collapse the object before copying it. 70326f9a767SRodney W. Grimes */ 70424a1cce3SDavid Greenman if (src_object->handle == NULL && 70524a1cce3SDavid Greenman (src_object->type == OBJT_DEFAULT || 70624a1cce3SDavid Greenman src_object->type == OBJT_SWAP)) 70726f9a767SRodney W. Grimes vm_object_collapse(src_object); 70826f9a767SRodney W. Grimes 709df8bae1dSRodney W. Grimes 710df8bae1dSRodney W. Grimes /* 711df8bae1dSRodney W. Grimes * Make another reference to the object 712df8bae1dSRodney W. Grimes */ 713df8bae1dSRodney W. Grimes src_object->ref_count++; 714df8bae1dSRodney W. Grimes 715df8bae1dSRodney W. Grimes *dst_object = src_object; 716df8bae1dSRodney W. Grimes *dst_offset = src_offset; 717df8bae1dSRodney W. Grimes 718df8bae1dSRodney W. Grimes /* 719df8bae1dSRodney W. Grimes * Must make a shadow when write is desired 720df8bae1dSRodney W. Grimes */ 721df8bae1dSRodney W. Grimes *src_needs_copy = TRUE; 722df8bae1dSRodney W. Grimes return; 723df8bae1dSRodney W. Grimes } 724df8bae1dSRodney W. Grimes 725df8bae1dSRodney W. Grimes /* 726df8bae1dSRodney W. Grimes * vm_object_shadow: 727df8bae1dSRodney W. Grimes * 728df8bae1dSRodney W. Grimes * Create a new object which is backed by the 729df8bae1dSRodney W. Grimes * specified existing object range. The source 730df8bae1dSRodney W. Grimes * object reference is deallocated. 731df8bae1dSRodney W. Grimes * 732df8bae1dSRodney W. Grimes * The new object and offset into that object 733df8bae1dSRodney W. Grimes * are returned in the source parameters. 734df8bae1dSRodney W. Grimes */ 735df8bae1dSRodney W. Grimes 73626f9a767SRodney W. Grimes void 73726f9a767SRodney W. Grimes vm_object_shadow(object, offset, length) 738df8bae1dSRodney W. Grimes vm_object_t *object; /* IN/OUT */ 739a316d390SJohn Dyson vm_ooffset_t *offset; /* IN/OUT */ 740df8bae1dSRodney W. Grimes vm_size_t length; 741df8bae1dSRodney W. Grimes { 742df8bae1dSRodney W. Grimes register vm_object_t source; 743df8bae1dSRodney W. Grimes register vm_object_t result; 744df8bae1dSRodney W. Grimes 745df8bae1dSRodney W. Grimes source = *object; 746df8bae1dSRodney W. Grimes 747df8bae1dSRodney W. Grimes /* 748df8bae1dSRodney W. Grimes * Allocate a new object with the given length 749df8bae1dSRodney W. Grimes */ 750df8bae1dSRodney W. Grimes 75124a1cce3SDavid Greenman if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL) 752df8bae1dSRodney W. Grimes panic("vm_object_shadow: no object for shadowing"); 753df8bae1dSRodney W. Grimes 754df8bae1dSRodney W. Grimes /* 7550d94caffSDavid Greenman * The new object shadows the source object, adding a reference to it. 7560d94caffSDavid Greenman * Our caller changes his reference to point to the new object, 7570d94caffSDavid Greenman * removing a reference to the source object. Net result: no change 7580d94caffSDavid Greenman * of reference count. 759df8bae1dSRodney W. Grimes */ 76024a1cce3SDavid Greenman result->backing_object = source; 7617b18a718SDavid Greenman if (source) 76224a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&result->backing_object->shadow_head, result, shadow_list); 763df8bae1dSRodney W. Grimes 764df8bae1dSRodney W. Grimes /* 7650d94caffSDavid Greenman * Store the offset into the source object, and fix up the offset into 7660d94caffSDavid Greenman * the new object. 767df8bae1dSRodney W. Grimes */ 768df8bae1dSRodney W. Grimes 76924a1cce3SDavid Greenman result->backing_object_offset = *offset; 770df8bae1dSRodney W. Grimes 771df8bae1dSRodney W. Grimes /* 772df8bae1dSRodney W. Grimes * Return the new things 773df8bae1dSRodney W. Grimes */ 774df8bae1dSRodney W. Grimes 775df8bae1dSRodney W. Grimes *offset = 0; 776df8bae1dSRodney W. Grimes *object = result; 777df8bae1dSRodney W. Grimes } 778df8bae1dSRodney W. Grimes 779df8bae1dSRodney W. Grimes 780df8bae1dSRodney W. Grimes /* 7812fe6e4d7SDavid Greenman * this version of collapse allows the operation to occur earlier and 7822fe6e4d7SDavid Greenman * when paging_in_progress is true for an object... This is not a complete 7832fe6e4d7SDavid Greenman * operation, but should plug 99.9% of the rest of the leaks. 7842fe6e4d7SDavid Greenman */ 7852fe6e4d7SDavid Greenman static void 7862fe6e4d7SDavid Greenman vm_object_qcollapse(object) 7872fe6e4d7SDavid Greenman register vm_object_t object; 7882fe6e4d7SDavid Greenman { 7892fe6e4d7SDavid Greenman register vm_object_t backing_object; 790a316d390SJohn Dyson register vm_pindex_t backing_offset_index, paging_offset_index; 791a316d390SJohn Dyson vm_pindex_t backing_object_paging_offset_index; 792a316d390SJohn Dyson vm_pindex_t new_pindex; 7932fe6e4d7SDavid Greenman register vm_page_t p, pp; 7942fe6e4d7SDavid Greenman register vm_size_t size; 7952fe6e4d7SDavid Greenman 79624a1cce3SDavid Greenman backing_object = object->backing_object; 7972fe6e4d7SDavid Greenman if (backing_object->ref_count != 1) 7982fe6e4d7SDavid Greenman return; 7992fe6e4d7SDavid Greenman 800010cf3b9SDavid Greenman backing_object->ref_count += 2; 801010cf3b9SDavid Greenman 802a316d390SJohn Dyson backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 803a316d390SJohn Dyson backing_object_paging_offset_index = OFF_TO_IDX(backing_object->paging_offset); 804a316d390SJohn Dyson paging_offset_index = OFF_TO_IDX(object->paging_offset); 8052fe6e4d7SDavid Greenman size = object->size; 8062fe6e4d7SDavid Greenman p = backing_object->memq.tqh_first; 8072fe6e4d7SDavid Greenman while (p) { 8082fe6e4d7SDavid Greenman vm_page_t next; 8090d94caffSDavid Greenman 8102fe6e4d7SDavid Greenman next = p->listq.tqe_next; 811bd7e5f99SJohn Dyson if ((p->flags & (PG_BUSY | PG_FICTITIOUS)) || 812bd7e5f99SJohn Dyson (p->queue == PQ_CACHE) || !p->valid || p->hold_count || p->wire_count || p->busy) { 8132fe6e4d7SDavid Greenman p = next; 8142fe6e4d7SDavid Greenman continue; 8152fe6e4d7SDavid Greenman } 816f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 817a316d390SJohn Dyson new_pindex = p->pindex - backing_offset_index; 818a316d390SJohn Dyson if (p->pindex < backing_offset_index || 819a316d390SJohn Dyson new_pindex >= size) { 82024a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) 82124a1cce3SDavid Greenman swap_pager_freespace(backing_object, 822a316d390SJohn Dyson backing_object_paging_offset_index+p->pindex, 823a316d390SJohn Dyson 1); 8242fe6e4d7SDavid Greenman vm_page_free(p); 8252fe6e4d7SDavid Greenman } else { 826a316d390SJohn Dyson pp = vm_page_lookup(object, new_pindex); 82724a1cce3SDavid Greenman if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object, 828a316d390SJohn Dyson paging_offset_index + new_pindex, NULL, NULL))) { 82924a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) 83024a1cce3SDavid Greenman swap_pager_freespace(backing_object, 831a316d390SJohn Dyson backing_object_paging_offset_index + p->pindex, 1); 8322fe6e4d7SDavid Greenman vm_page_free(p); 8332fe6e4d7SDavid Greenman } else { 83424a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) 83524a1cce3SDavid Greenman swap_pager_freespace(backing_object, 836a316d390SJohn Dyson backing_object_paging_offset_index + p->pindex, 1); 837a316d390SJohn Dyson vm_page_rename(p, object, new_pindex); 8389b4814bbSDavid Greenman p->dirty = VM_PAGE_BITS_ALL; 8392fe6e4d7SDavid Greenman } 8402fe6e4d7SDavid Greenman } 8412fe6e4d7SDavid Greenman p = next; 8422fe6e4d7SDavid Greenman } 843010cf3b9SDavid Greenman backing_object->ref_count -= 2; 8442fe6e4d7SDavid Greenman } 8452fe6e4d7SDavid Greenman 846df8bae1dSRodney W. Grimes /* 847df8bae1dSRodney W. Grimes * vm_object_collapse: 848df8bae1dSRodney W. Grimes * 849df8bae1dSRodney W. Grimes * Collapse an object with the object backing it. 850df8bae1dSRodney W. Grimes * Pages in the backing object are moved into the 851df8bae1dSRodney W. Grimes * parent, and the backing object is deallocated. 852df8bae1dSRodney W. Grimes */ 85326f9a767SRodney W. Grimes void 85426f9a767SRodney W. Grimes vm_object_collapse(object) 85524a1cce3SDavid Greenman vm_object_t object; 856df8bae1dSRodney W. Grimes 857df8bae1dSRodney W. Grimes { 85824a1cce3SDavid Greenman vm_object_t backing_object; 859a316d390SJohn Dyson vm_ooffset_t backing_offset; 86024a1cce3SDavid Greenman vm_size_t size; 861a316d390SJohn Dyson vm_pindex_t new_pindex, backing_offset_index; 86224a1cce3SDavid Greenman vm_page_t p, pp; 863df8bae1dSRodney W. Grimes 864df8bae1dSRodney W. Grimes while (TRUE) { 865df8bae1dSRodney W. Grimes /* 866df8bae1dSRodney W. Grimes * Verify that the conditions are right for collapse: 867df8bae1dSRodney W. Grimes * 8680d94caffSDavid Greenman * The object exists and no pages in it are currently being paged 8690d94caffSDavid Greenman * out. 870df8bae1dSRodney W. Grimes */ 8712fe6e4d7SDavid Greenman if (object == NULL) 872df8bae1dSRodney W. Grimes return; 873df8bae1dSRodney W. Grimes 874b9921222SDavid Greenman /* 875b9921222SDavid Greenman * Make sure there is a backing object. 876b9921222SDavid Greenman */ 87724a1cce3SDavid Greenman if ((backing_object = object->backing_object) == NULL) 878df8bae1dSRodney W. Grimes return; 879df8bae1dSRodney W. Grimes 880f919ebdeSDavid Greenman /* 881f919ebdeSDavid Greenman * we check the backing object first, because it is most likely 88224a1cce3SDavid Greenman * not collapsable. 883f919ebdeSDavid Greenman */ 88424a1cce3SDavid Greenman if (backing_object->handle != NULL || 88524a1cce3SDavid Greenman (backing_object->type != OBJT_DEFAULT && 88624a1cce3SDavid Greenman backing_object->type != OBJT_SWAP) || 887f919ebdeSDavid Greenman (backing_object->flags & OBJ_DEAD) || 88824a1cce3SDavid Greenman object->handle != NULL || 88924a1cce3SDavid Greenman (object->type != OBJT_DEFAULT && 89024a1cce3SDavid Greenman object->type != OBJT_SWAP) || 89124a1cce3SDavid Greenman (object->flags & OBJ_DEAD)) { 8929b4814bbSDavid Greenman return; 89324a1cce3SDavid Greenman } 8949b4814bbSDavid Greenman 895f919ebdeSDavid Greenman if (object->paging_in_progress != 0 || 896f919ebdeSDavid Greenman backing_object->paging_in_progress != 0) { 897b9921222SDavid Greenman vm_object_qcollapse(object); 898df8bae1dSRodney W. Grimes return; 899df8bae1dSRodney W. Grimes } 900f919ebdeSDavid Greenman 90126f9a767SRodney W. Grimes /* 9020d94caffSDavid Greenman * We know that we can either collapse the backing object (if 9030d94caffSDavid Greenman * the parent is the only reference to it) or (perhaps) remove 9040d94caffSDavid Greenman * the parent's reference to it. 905df8bae1dSRodney W. Grimes */ 906df8bae1dSRodney W. Grimes 90724a1cce3SDavid Greenman backing_offset = object->backing_object_offset; 908a316d390SJohn Dyson backing_offset_index = OFF_TO_IDX(backing_offset); 909df8bae1dSRodney W. Grimes size = object->size; 910df8bae1dSRodney W. Grimes 911df8bae1dSRodney W. Grimes /* 9120d94caffSDavid Greenman * If there is exactly one reference to the backing object, we 9130d94caffSDavid Greenman * can collapse it into the parent. 914df8bae1dSRodney W. Grimes */ 915df8bae1dSRodney W. Grimes 916df8bae1dSRodney W. Grimes if (backing_object->ref_count == 1) { 917df8bae1dSRodney W. Grimes 918a1f6d91cSDavid Greenman backing_object->flags |= OBJ_DEAD; 919df8bae1dSRodney W. Grimes /* 920df8bae1dSRodney W. Grimes * We can collapse the backing object. 921df8bae1dSRodney W. Grimes * 9220d94caffSDavid Greenman * Move all in-memory pages from backing_object to the 9230d94caffSDavid Greenman * parent. Pages that have been paged out will be 9240d94caffSDavid Greenman * overwritten by any of the parent's pages that 9250d94caffSDavid Greenman * shadow them. 926df8bae1dSRodney W. Grimes */ 927df8bae1dSRodney W. Grimes 92805f0fdd2SPoul-Henning Kamp while ((p = backing_object->memq.tqh_first) != 0) { 92926f9a767SRodney W. Grimes 930a316d390SJohn Dyson new_pindex = p->pindex - backing_offset_index; 931df8bae1dSRodney W. Grimes 932df8bae1dSRodney W. Grimes /* 9330d94caffSDavid Greenman * If the parent has a page here, or if this 9340d94caffSDavid Greenman * page falls outside the parent, dispose of 9350d94caffSDavid Greenman * it. 936df8bae1dSRodney W. Grimes * 937df8bae1dSRodney W. Grimes * Otherwise, move it as planned. 938df8bae1dSRodney W. Grimes */ 939df8bae1dSRodney W. Grimes 940a316d390SJohn Dyson if (p->pindex < backing_offset_index || 941a316d390SJohn Dyson new_pindex >= size) { 942f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 9430d94caffSDavid Greenman PAGE_WAKEUP(p); 944df8bae1dSRodney W. Grimes vm_page_free(p); 945df8bae1dSRodney W. Grimes } else { 946a316d390SJohn Dyson pp = vm_page_lookup(object, new_pindex); 94724a1cce3SDavid Greenman if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object, 948a316d390SJohn Dyson OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL))) { 949f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 9500d94caffSDavid Greenman PAGE_WAKEUP(p); 951df8bae1dSRodney W. Grimes vm_page_free(p); 95226f9a767SRodney W. Grimes } else { 953a316d390SJohn Dyson vm_page_rename(p, object, new_pindex); 954df8bae1dSRodney W. Grimes } 955df8bae1dSRodney W. Grimes } 956df8bae1dSRodney W. Grimes } 957df8bae1dSRodney W. Grimes 958df8bae1dSRodney W. Grimes /* 959df8bae1dSRodney W. Grimes * Move the pager from backing_object to object. 960df8bae1dSRodney W. Grimes */ 961df8bae1dSRodney W. Grimes 96224a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) { 96326f9a767SRodney W. Grimes backing_object->paging_in_progress++; 96424a1cce3SDavid Greenman if (object->type == OBJT_SWAP) { 96526f9a767SRodney W. Grimes object->paging_in_progress++; 96626f9a767SRodney W. Grimes /* 96726f9a767SRodney W. Grimes * copy shadow object pages into ours 9680d94caffSDavid Greenman * and destroy unneeded pages in 9690d94caffSDavid Greenman * shadow object. 97026f9a767SRodney W. Grimes */ 97126f9a767SRodney W. Grimes swap_pager_copy( 972a316d390SJohn Dyson backing_object, 973a316d390SJohn Dyson OFF_TO_IDX(backing_object->paging_offset), 974a316d390SJohn Dyson object, 975a316d390SJohn Dyson OFF_TO_IDX(object->paging_offset), 976a316d390SJohn Dyson OFF_TO_IDX(object->backing_object_offset)); 977f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 97826f9a767SRodney W. Grimes } else { 97926f9a767SRodney W. Grimes object->paging_in_progress++; 98026f9a767SRodney W. Grimes /* 98124a1cce3SDavid Greenman * move the shadow backing_object's pager data to 98224a1cce3SDavid Greenman * "object" and convert "object" type to OBJT_SWAP. 98326f9a767SRodney W. Grimes */ 98424a1cce3SDavid Greenman object->type = OBJT_SWAP; 9852a4895f4SDavid Greenman object->un_pager.swp.swp_nblocks = 9862a4895f4SDavid Greenman backing_object->un_pager.swp.swp_nblocks; 9872a4895f4SDavid Greenman object->un_pager.swp.swp_allocsize = 9882a4895f4SDavid Greenman backing_object->un_pager.swp.swp_allocsize; 9892a4895f4SDavid Greenman object->un_pager.swp.swp_blocks = 9902a4895f4SDavid Greenman backing_object->un_pager.swp.swp_blocks; 9912a4895f4SDavid Greenman object->un_pager.swp.swp_poip = /* XXX */ 9922a4895f4SDavid Greenman backing_object->un_pager.swp.swp_poip; 99326f9a767SRodney W. Grimes object->paging_offset = backing_object->paging_offset + backing_offset; 99424a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&swap_pager_un_object_list, object, pager_object_list); 99524a1cce3SDavid Greenman 99624a1cce3SDavid Greenman /* 99724a1cce3SDavid Greenman * Convert backing object from OBJT_SWAP to 99824a1cce3SDavid Greenman * OBJT_DEFAULT. XXX - only the TAILQ_REMOVE is 99924a1cce3SDavid Greenman * actually necessary. 100024a1cce3SDavid Greenman */ 100124a1cce3SDavid Greenman backing_object->type = OBJT_DEFAULT; 100224a1cce3SDavid Greenman TAILQ_REMOVE(&swap_pager_un_object_list, backing_object, pager_object_list); 100326f9a767SRodney W. Grimes /* 100426f9a767SRodney W. Grimes * free unnecessary blocks 100526f9a767SRodney W. Grimes */ 1006a316d390SJohn Dyson swap_pager_freespace(object, 0, 1007a316d390SJohn Dyson OFF_TO_IDX(object->paging_offset)); 1008f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 1009c0503609SDavid Greenman } 1010c0503609SDavid Greenman 1011f919ebdeSDavid Greenman vm_object_pip_wakeup(backing_object); 1012c0503609SDavid Greenman } 1013df8bae1dSRodney W. Grimes /* 1014df8bae1dSRodney W. Grimes * Object now shadows whatever backing_object did. 101524a1cce3SDavid Greenman * Note that the reference to backing_object->backing_object 1016df8bae1dSRodney W. Grimes * moves from within backing_object to within object. 1017df8bae1dSRodney W. Grimes */ 1018df8bae1dSRodney W. Grimes 101924a1cce3SDavid Greenman TAILQ_REMOVE(&object->backing_object->shadow_head, object, 102024a1cce3SDavid Greenman shadow_list); 102124a1cce3SDavid Greenman if (backing_object->backing_object) 102224a1cce3SDavid Greenman TAILQ_REMOVE(&backing_object->backing_object->shadow_head, 102324a1cce3SDavid Greenman backing_object, shadow_list); 102424a1cce3SDavid Greenman object->backing_object = backing_object->backing_object; 102524a1cce3SDavid Greenman if (object->backing_object) 102624a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&object->backing_object->shadow_head, 102724a1cce3SDavid Greenman object, shadow_list); 10282fe6e4d7SDavid Greenman 102924a1cce3SDavid Greenman object->backing_object_offset += backing_object->backing_object_offset; 1030df8bae1dSRodney W. Grimes /* 1031df8bae1dSRodney W. Grimes * Discard backing_object. 1032df8bae1dSRodney W. Grimes * 10330d94caffSDavid Greenman * Since the backing object has no pages, no pager left, 10340d94caffSDavid Greenman * and no object references within it, all that is 10350d94caffSDavid Greenman * necessary is to dispose of it. 1036df8bae1dSRodney W. Grimes */ 1037df8bae1dSRodney W. Grimes 1038df8bae1dSRodney W. Grimes TAILQ_REMOVE(&vm_object_list, backing_object, 1039df8bae1dSRodney W. Grimes object_list); 1040df8bae1dSRodney W. Grimes vm_object_count--; 1041df8bae1dSRodney W. Grimes 1042df8bae1dSRodney W. Grimes free((caddr_t) backing_object, M_VMOBJ); 1043df8bae1dSRodney W. Grimes 1044df8bae1dSRodney W. Grimes object_collapses++; 10450d94caffSDavid Greenman } else { 1046df8bae1dSRodney W. Grimes /* 1047df8bae1dSRodney W. Grimes * If all of the pages in the backing object are 10480d94caffSDavid Greenman * shadowed by the parent object, the parent object no 10490d94caffSDavid Greenman * longer has to shadow the backing object; it can 10500d94caffSDavid Greenman * shadow the next one in the chain. 1051df8bae1dSRodney W. Grimes * 10520d94caffSDavid Greenman * The backing object must not be paged out - we'd have 10530d94caffSDavid Greenman * to check all of the paged-out pages, as well. 1054df8bae1dSRodney W. Grimes */ 1055df8bae1dSRodney W. Grimes 105624a1cce3SDavid Greenman if (backing_object->type != OBJT_DEFAULT) { 1057df8bae1dSRodney W. Grimes return; 1058df8bae1dSRodney W. Grimes } 1059df8bae1dSRodney W. Grimes /* 10600d94caffSDavid Greenman * Should have a check for a 'small' number of pages 10610d94caffSDavid Greenman * here. 1062df8bae1dSRodney W. Grimes */ 1063df8bae1dSRodney W. Grimes 106426f9a767SRodney W. Grimes for (p = backing_object->memq.tqh_first; p; p = p->listq.tqe_next) { 1065a316d390SJohn Dyson new_pindex = p->pindex - backing_offset_index; 1066df8bae1dSRodney W. Grimes 1067df8bae1dSRodney W. Grimes /* 10680d94caffSDavid Greenman * If the parent has a page here, or if this 10690d94caffSDavid Greenman * page falls outside the parent, keep going. 1070df8bae1dSRodney W. Grimes * 10710d94caffSDavid Greenman * Otherwise, the backing_object must be left in 10720d94caffSDavid Greenman * the chain. 1073df8bae1dSRodney W. Grimes */ 1074df8bae1dSRodney W. Grimes 1075a316d390SJohn Dyson if (p->pindex >= backing_offset_index && 1076a316d390SJohn Dyson new_pindex <= size) { 107724a1cce3SDavid Greenman 1078a316d390SJohn Dyson pp = vm_page_lookup(object, new_pindex); 107924a1cce3SDavid Greenman 108024a1cce3SDavid Greenman if ((pp == NULL || pp->valid == 0) && 1081a316d390SJohn Dyson !vm_pager_has_page(object, OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL)) { 1082df8bae1dSRodney W. Grimes /* 10830d94caffSDavid Greenman * Page still needed. Can't go any 10840d94caffSDavid Greenman * further. 1085df8bae1dSRodney W. Grimes */ 1086df8bae1dSRodney W. Grimes return; 1087df8bae1dSRodney W. Grimes } 1088df8bae1dSRodney W. Grimes } 108924a1cce3SDavid Greenman } 1090df8bae1dSRodney W. Grimes 1091df8bae1dSRodney W. Grimes /* 10920d94caffSDavid Greenman * Make the parent shadow the next object in the 10930d94caffSDavid Greenman * chain. Deallocating backing_object will not remove 10940d94caffSDavid Greenman * it, since its reference count is at least 2. 1095df8bae1dSRodney W. Grimes */ 1096df8bae1dSRodney W. Grimes 109724a1cce3SDavid Greenman TAILQ_REMOVE(&object->backing_object->shadow_head, 109824a1cce3SDavid Greenman object, shadow_list); 109924a1cce3SDavid Greenman vm_object_reference(object->backing_object = backing_object->backing_object); 110024a1cce3SDavid Greenman if (object->backing_object) 110124a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&object->backing_object->shadow_head, 110224a1cce3SDavid Greenman object, shadow_list); 110324a1cce3SDavid Greenman object->backing_object_offset += backing_object->backing_object_offset; 1104df8bae1dSRodney W. Grimes 1105df8bae1dSRodney W. Grimes /* 11060d94caffSDavid Greenman * Drop the reference count on backing_object. Since 11070d94caffSDavid Greenman * its ref_count was at least 2, it will not vanish; 11080d94caffSDavid Greenman * so we don't need to call vm_object_deallocate. 1109df8bae1dSRodney W. Grimes */ 111026f9a767SRodney W. Grimes if (backing_object->ref_count == 1) 111126f9a767SRodney W. Grimes printf("should have called obj deallocate\n"); 1112df8bae1dSRodney W. Grimes backing_object->ref_count--; 1113df8bae1dSRodney W. Grimes 1114df8bae1dSRodney W. Grimes object_bypasses++; 1115df8bae1dSRodney W. Grimes 1116df8bae1dSRodney W. Grimes } 1117df8bae1dSRodney W. Grimes 1118df8bae1dSRodney W. Grimes /* 1119df8bae1dSRodney W. Grimes * Try again with this object's new backing object. 1120df8bae1dSRodney W. Grimes */ 1121df8bae1dSRodney W. Grimes } 1122df8bae1dSRodney W. Grimes } 1123df8bae1dSRodney W. Grimes 1124df8bae1dSRodney W. Grimes /* 1125df8bae1dSRodney W. Grimes * vm_object_page_remove: [internal] 1126df8bae1dSRodney W. Grimes * 1127df8bae1dSRodney W. Grimes * Removes all physical pages in the specified 1128df8bae1dSRodney W. Grimes * object range from the object's list of pages. 1129df8bae1dSRodney W. Grimes * 1130df8bae1dSRodney W. Grimes * The object must be locked. 1131df8bae1dSRodney W. Grimes */ 113226f9a767SRodney W. Grimes void 11337c1f6cedSDavid Greenman vm_object_page_remove(object, start, end, clean_only) 1134df8bae1dSRodney W. Grimes register vm_object_t object; 1135a316d390SJohn Dyson register vm_pindex_t start; 1136a316d390SJohn Dyson register vm_pindex_t end; 11377c1f6cedSDavid Greenman boolean_t clean_only; 1138df8bae1dSRodney W. Grimes { 1139df8bae1dSRodney W. Grimes register vm_page_t p, next; 1140a316d390SJohn Dyson unsigned int size; 114126f9a767SRodney W. Grimes int s; 1142df8bae1dSRodney W. Grimes 1143df8bae1dSRodney W. Grimes if (object == NULL) 1144df8bae1dSRodney W. Grimes return; 1145df8bae1dSRodney W. Grimes 11462fe6e4d7SDavid Greenman object->paging_in_progress++; 114726f9a767SRodney W. Grimes again: 114826f9a767SRodney W. Grimes size = end - start; 1149a316d390SJohn Dyson if (size > 4 || size >= object->size / 4) { 11500d94caffSDavid Greenman for (p = object->memq.tqh_first; p != NULL; p = next) { 1151df8bae1dSRodney W. Grimes next = p->listq.tqe_next; 1152bd7e5f99SJohn Dyson if (p->wire_count != 0) { 1153bd7e5f99SJohn Dyson vm_page_protect(p, VM_PROT_NONE); 1154bd7e5f99SJohn Dyson p->valid = 0; 11550d94caffSDavid Greenman continue; 11560d94caffSDavid Greenman } 1157bd7e5f99SJohn Dyson if ((start <= p->pindex) && (p->pindex < end)) { 1158bd7e5f99SJohn Dyson s = splhigh(); 11590d94caffSDavid Greenman if ((p->flags & PG_BUSY) || p->busy) { 116026f9a767SRodney W. Grimes p->flags |= PG_WANTED; 116124a1cce3SDavid Greenman tsleep(p, PVM, "vmopar", 0); 1162a481f200SDavid Greenman splx(s); 116326f9a767SRodney W. Grimes goto again; 116426f9a767SRodney W. Grimes } 1165a481f200SDavid Greenman splx(s); 11667c1f6cedSDavid Greenman if (clean_only) { 11677c1f6cedSDavid Greenman vm_page_test_dirty(p); 11687c1f6cedSDavid Greenman if (p->valid & p->dirty) 11697c1f6cedSDavid Greenman continue; 11707c1f6cedSDavid Greenman } 1171f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 11720d94caffSDavid Greenman PAGE_WAKEUP(p); 1173df8bae1dSRodney W. Grimes vm_page_free(p); 117426f9a767SRodney W. Grimes } 117526f9a767SRodney W. Grimes } 117626f9a767SRodney W. Grimes } else { 117726f9a767SRodney W. Grimes while (size > 0) { 1178bd7e5f99SJohn Dyson if ((p = vm_page_lookup(object, start)) != 0) { 1179bd7e5f99SJohn Dyson if (p->wire_count != 0) { 1180bd7e5f99SJohn Dyson p->valid = 0; 1181bd7e5f99SJohn Dyson vm_page_protect(p, VM_PROT_NONE); 1182bd7e5f99SJohn Dyson start += 1; 1183bd7e5f99SJohn Dyson size -= 1; 1184bd7e5f99SJohn Dyson continue; 11850d94caffSDavid Greenman } 1186bd7e5f99SJohn Dyson s = splhigh(); 11870d94caffSDavid Greenman if ((p->flags & PG_BUSY) || p->busy) { 118826f9a767SRodney W. Grimes p->flags |= PG_WANTED; 118924a1cce3SDavid Greenman tsleep(p, PVM, "vmopar", 0); 1190a481f200SDavid Greenman splx(s); 119126f9a767SRodney W. Grimes goto again; 119226f9a767SRodney W. Grimes } 1193a481f200SDavid Greenman splx(s); 11947c1f6cedSDavid Greenman if (clean_only) { 11957c1f6cedSDavid Greenman vm_page_test_dirty(p); 1196bd7e5f99SJohn Dyson if (p->valid & p->dirty) { 1197bd7e5f99SJohn Dyson start += 1; 1198bd7e5f99SJohn Dyson size -= 1; 11997c1f6cedSDavid Greenman continue; 12007c1f6cedSDavid Greenman } 1201bd7e5f99SJohn Dyson } 1202f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 12030d94caffSDavid Greenman PAGE_WAKEUP(p); 120426f9a767SRodney W. Grimes vm_page_free(p); 120526f9a767SRodney W. Grimes } 1206a316d390SJohn Dyson start += 1; 1207a316d390SJohn Dyson size -= 1; 1208df8bae1dSRodney W. Grimes } 1209df8bae1dSRodney W. Grimes } 1210f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 1211c0503609SDavid Greenman } 1212df8bae1dSRodney W. Grimes 1213df8bae1dSRodney W. Grimes /* 1214df8bae1dSRodney W. Grimes * Routine: vm_object_coalesce 1215df8bae1dSRodney W. Grimes * Function: Coalesces two objects backing up adjoining 1216df8bae1dSRodney W. Grimes * regions of memory into a single object. 1217df8bae1dSRodney W. Grimes * 1218df8bae1dSRodney W. Grimes * returns TRUE if objects were combined. 1219df8bae1dSRodney W. Grimes * 1220df8bae1dSRodney W. Grimes * NOTE: Only works at the moment if the second object is NULL - 1221df8bae1dSRodney W. Grimes * if it's not, which object do we lock first? 1222df8bae1dSRodney W. Grimes * 1223df8bae1dSRodney W. Grimes * Parameters: 1224df8bae1dSRodney W. Grimes * prev_object First object to coalesce 1225df8bae1dSRodney W. Grimes * prev_offset Offset into prev_object 1226df8bae1dSRodney W. Grimes * next_object Second object into coalesce 1227df8bae1dSRodney W. Grimes * next_offset Offset into next_object 1228df8bae1dSRodney W. Grimes * 1229df8bae1dSRodney W. Grimes * prev_size Size of reference to prev_object 1230df8bae1dSRodney W. Grimes * next_size Size of reference to next_object 1231df8bae1dSRodney W. Grimes * 1232df8bae1dSRodney W. Grimes * Conditions: 1233df8bae1dSRodney W. Grimes * The object must *not* be locked. 1234df8bae1dSRodney W. Grimes */ 12350d94caffSDavid Greenman boolean_t 1236a316d390SJohn Dyson vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size) 1237df8bae1dSRodney W. Grimes register vm_object_t prev_object; 1238a316d390SJohn Dyson vm_pindex_t prev_pindex; 1239df8bae1dSRodney W. Grimes vm_size_t prev_size, next_size; 1240df8bae1dSRodney W. Grimes { 1241df8bae1dSRodney W. Grimes vm_size_t newsize; 1242df8bae1dSRodney W. Grimes 1243df8bae1dSRodney W. Grimes if (prev_object == NULL) { 1244df8bae1dSRodney W. Grimes return (TRUE); 1245df8bae1dSRodney W. Grimes } 1246df8bae1dSRodney W. Grimes 1247df8bae1dSRodney W. Grimes /* 1248df8bae1dSRodney W. Grimes * Try to collapse the object first 1249df8bae1dSRodney W. Grimes */ 1250df8bae1dSRodney W. Grimes vm_object_collapse(prev_object); 1251df8bae1dSRodney W. Grimes 1252df8bae1dSRodney W. Grimes /* 12530d94caffSDavid Greenman * Can't coalesce if: . more than one reference . paged out . shadows 12540d94caffSDavid Greenman * another object . has a copy elsewhere (any of which mean that the 12550d94caffSDavid Greenman * pages not mapped to prev_entry may be in use anyway) 1256df8bae1dSRodney W. Grimes */ 1257df8bae1dSRodney W. Grimes 1258df8bae1dSRodney W. Grimes if (prev_object->ref_count > 1 || 125924a1cce3SDavid Greenman prev_object->type != OBJT_DEFAULT || 126024a1cce3SDavid Greenman prev_object->backing_object != NULL) { 1261df8bae1dSRodney W. Grimes return (FALSE); 1262df8bae1dSRodney W. Grimes } 1263a316d390SJohn Dyson 1264a316d390SJohn Dyson prev_size >>= PAGE_SHIFT; 1265a316d390SJohn Dyson next_size >>= PAGE_SHIFT; 1266df8bae1dSRodney W. Grimes /* 12670d94caffSDavid Greenman * Remove any pages that may still be in the object from a previous 12680d94caffSDavid Greenman * deallocation. 1269df8bae1dSRodney W. Grimes */ 1270df8bae1dSRodney W. Grimes 1271df8bae1dSRodney W. Grimes vm_object_page_remove(prev_object, 1272a316d390SJohn Dyson prev_pindex + prev_size, 1273a316d390SJohn Dyson prev_pindex + prev_size + next_size, FALSE); 1274df8bae1dSRodney W. Grimes 1275df8bae1dSRodney W. Grimes /* 1276df8bae1dSRodney W. Grimes * Extend the object if necessary. 1277df8bae1dSRodney W. Grimes */ 1278a316d390SJohn Dyson newsize = prev_pindex + prev_size + next_size; 1279df8bae1dSRodney W. Grimes if (newsize > prev_object->size) 1280df8bae1dSRodney W. Grimes prev_object->size = newsize; 1281df8bae1dSRodney W. Grimes 1282df8bae1dSRodney W. Grimes return (TRUE); 1283df8bae1dSRodney W. Grimes } 1284df8bae1dSRodney W. Grimes 1285c3cb3e12SDavid Greenman #ifdef DDB 1286c3cb3e12SDavid Greenman 1287cac597e4SBruce Evans static int 1288a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry) 1289a1f6d91cSDavid Greenman vm_map_t map; 1290a1f6d91cSDavid Greenman vm_object_t object; 1291a1f6d91cSDavid Greenman vm_map_entry_t entry; 1292a1f6d91cSDavid Greenman { 1293a1f6d91cSDavid Greenman vm_map_t tmpm; 1294a1f6d91cSDavid Greenman vm_map_entry_t tmpe; 1295a1f6d91cSDavid Greenman vm_object_t obj; 1296a1f6d91cSDavid Greenman int entcount; 1297a1f6d91cSDavid Greenman 1298a1f6d91cSDavid Greenman if (map == 0) 1299a1f6d91cSDavid Greenman return 0; 1300a1f6d91cSDavid Greenman 1301a1f6d91cSDavid Greenman if (entry == 0) { 1302a1f6d91cSDavid Greenman tmpe = map->header.next; 1303a1f6d91cSDavid Greenman entcount = map->nentries; 1304a1f6d91cSDavid Greenman while (entcount-- && (tmpe != &map->header)) { 1305a1f6d91cSDavid Greenman if( _vm_object_in_map(map, object, tmpe)) { 1306a1f6d91cSDavid Greenman return 1; 1307a1f6d91cSDavid Greenman } 1308a1f6d91cSDavid Greenman tmpe = tmpe->next; 1309a1f6d91cSDavid Greenman } 1310a1f6d91cSDavid Greenman } else if (entry->is_sub_map || entry->is_a_map) { 1311a1f6d91cSDavid Greenman tmpm = entry->object.share_map; 1312a1f6d91cSDavid Greenman tmpe = tmpm->header.next; 1313a1f6d91cSDavid Greenman entcount = tmpm->nentries; 1314a1f6d91cSDavid Greenman while (entcount-- && tmpe != &tmpm->header) { 1315a1f6d91cSDavid Greenman if( _vm_object_in_map(tmpm, object, tmpe)) { 1316a1f6d91cSDavid Greenman return 1; 1317a1f6d91cSDavid Greenman } 1318a1f6d91cSDavid Greenman tmpe = tmpe->next; 1319a1f6d91cSDavid Greenman } 1320a1f6d91cSDavid Greenman } else if (obj = entry->object.vm_object) { 132124a1cce3SDavid Greenman for(; obj; obj=obj->backing_object) 1322a1f6d91cSDavid Greenman if( obj == object) { 1323a1f6d91cSDavid Greenman return 1; 1324a1f6d91cSDavid Greenman } 1325a1f6d91cSDavid Greenman } 1326a1f6d91cSDavid Greenman return 0; 1327a1f6d91cSDavid Greenman } 1328a1f6d91cSDavid Greenman 1329cac597e4SBruce Evans static int 1330a1f6d91cSDavid Greenman vm_object_in_map( object) 1331a1f6d91cSDavid Greenman vm_object_t object; 1332a1f6d91cSDavid Greenman { 1333a1f6d91cSDavid Greenman struct proc *p; 1334a1f6d91cSDavid Greenman for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 1335a1f6d91cSDavid Greenman if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 1336a1f6d91cSDavid Greenman continue; 1337a1f6d91cSDavid Greenman /* 1338a1f6d91cSDavid Greenman if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 1339a1f6d91cSDavid Greenman continue; 1340a1f6d91cSDavid Greenman } 1341a1f6d91cSDavid Greenman */ 1342a1f6d91cSDavid Greenman if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) 1343a1f6d91cSDavid Greenman return 1; 1344a1f6d91cSDavid Greenman } 1345a1f6d91cSDavid Greenman if( _vm_object_in_map( kernel_map, object, 0)) 1346a1f6d91cSDavid Greenman return 1; 1347a1f6d91cSDavid Greenman if( _vm_object_in_map( kmem_map, object, 0)) 1348a1f6d91cSDavid Greenman return 1; 1349a1f6d91cSDavid Greenman if( _vm_object_in_map( pager_map, object, 0)) 1350a1f6d91cSDavid Greenman return 1; 1351a1f6d91cSDavid Greenman if( _vm_object_in_map( buffer_map, object, 0)) 1352a1f6d91cSDavid Greenman return 1; 1353a1f6d91cSDavid Greenman if( _vm_object_in_map( io_map, object, 0)) 1354a1f6d91cSDavid Greenman return 1; 1355a1f6d91cSDavid Greenman if( _vm_object_in_map( phys_map, object, 0)) 1356a1f6d91cSDavid Greenman return 1; 1357a1f6d91cSDavid Greenman if( _vm_object_in_map( mb_map, object, 0)) 1358a1f6d91cSDavid Greenman return 1; 1359a1f6d91cSDavid Greenman if( _vm_object_in_map( u_map, object, 0)) 1360a1f6d91cSDavid Greenman return 1; 1361a1f6d91cSDavid Greenman return 0; 1362a1f6d91cSDavid Greenman } 1363a1f6d91cSDavid Greenman 1364c3cb3e12SDavid Greenman 1365f708ef1bSPoul-Henning Kamp #ifdef DDB 1366f708ef1bSPoul-Henning Kamp static void 1367f708ef1bSPoul-Henning Kamp DDB_vm_object_check() 1368f708ef1bSPoul-Henning Kamp { 1369a1f6d91cSDavid Greenman vm_object_t object; 1370a1f6d91cSDavid Greenman 1371a1f6d91cSDavid Greenman /* 1372a1f6d91cSDavid Greenman * make sure that internal objs are in a map somewhere 1373a1f6d91cSDavid Greenman * and none have zero ref counts. 1374a1f6d91cSDavid Greenman */ 1375a1f6d91cSDavid Greenman for (object = vm_object_list.tqh_first; 1376a1f6d91cSDavid Greenman object != NULL; 1377a1f6d91cSDavid Greenman object = object->object_list.tqe_next) { 137824a1cce3SDavid Greenman if (object->handle == NULL && 137924a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 1380a1f6d91cSDavid Greenman if (object->ref_count == 0) { 1381a1f6d91cSDavid Greenman printf("vmochk: internal obj has zero ref count: %d\n", 1382a1f6d91cSDavid Greenman object->size); 1383a1f6d91cSDavid Greenman } 1384a1f6d91cSDavid Greenman if (!vm_object_in_map(object)) { 13853af76890SPoul-Henning Kamp printf("vmochk: internal obj is not in a map: " 13863af76890SPoul-Henning Kamp "ref: %d, size: %d: 0x%x, backing_object: 0x%x\n", 13873af76890SPoul-Henning Kamp object->ref_count, object->size, 13883af76890SPoul-Henning Kamp object->size, object->backing_object); 1389a1f6d91cSDavid Greenman } 1390a1f6d91cSDavid Greenman } 1391a1f6d91cSDavid Greenman } 1392a1f6d91cSDavid Greenman } 1393f708ef1bSPoul-Henning Kamp #endif /* DDB */ 1394a1f6d91cSDavid Greenman 139526f9a767SRodney W. Grimes /* 1396df8bae1dSRodney W. Grimes * vm_object_print: [ debug ] 1397df8bae1dSRodney W. Grimes */ 13980d94caffSDavid Greenman void 1399187f0238SBruce Evans vm_object_print(iobject, full, dummy3, dummy4) 1400187f0238SBruce Evans /* db_expr_t */ int iobject; 1401df8bae1dSRodney W. Grimes boolean_t full; 1402187f0238SBruce Evans /* db_expr_t */ int dummy3; 1403187f0238SBruce Evans char *dummy4; 1404df8bae1dSRodney W. Grimes { 1405187f0238SBruce Evans vm_object_t object = (vm_object_t)iobject; /* XXX */ 1406df8bae1dSRodney W. Grimes register vm_page_t p; 1407df8bae1dSRodney W. Grimes 1408df8bae1dSRodney W. Grimes register int count; 1409df8bae1dSRodney W. Grimes 1410df8bae1dSRodney W. Grimes if (object == NULL) 1411df8bae1dSRodney W. Grimes return; 1412df8bae1dSRodney W. Grimes 1413df8bae1dSRodney W. Grimes iprintf("Object 0x%x: size=0x%x, res=%d, ref=%d, ", 1414df8bae1dSRodney W. Grimes (int) object, (int) object->size, 1415df8bae1dSRodney W. Grimes object->resident_page_count, object->ref_count); 14162a4895f4SDavid Greenman printf("offset=0x%x, backing_object=(0x%x)+0x%x\n", 14172a4895f4SDavid Greenman (int) object->paging_offset, 141824a1cce3SDavid Greenman (int) object->backing_object, (int) object->backing_object_offset); 141905f0fdd2SPoul-Henning Kamp printf("cache: next=%p, prev=%p\n", 1420df8bae1dSRodney W. Grimes object->cached_list.tqe_next, object->cached_list.tqe_prev); 1421df8bae1dSRodney W. Grimes 1422df8bae1dSRodney W. Grimes if (!full) 1423df8bae1dSRodney W. Grimes return; 1424df8bae1dSRodney W. Grimes 1425df8bae1dSRodney W. Grimes indent += 2; 1426df8bae1dSRodney W. Grimes count = 0; 1427df8bae1dSRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) { 1428df8bae1dSRodney W. Grimes if (count == 0) 1429df8bae1dSRodney W. Grimes iprintf("memory:="); 1430df8bae1dSRodney W. Grimes else if (count == 6) { 1431df8bae1dSRodney W. Grimes printf("\n"); 1432df8bae1dSRodney W. Grimes iprintf(" ..."); 1433df8bae1dSRodney W. Grimes count = 0; 1434df8bae1dSRodney W. Grimes } else 1435df8bae1dSRodney W. Grimes printf(","); 1436df8bae1dSRodney W. Grimes count++; 1437df8bae1dSRodney W. Grimes 143805f0fdd2SPoul-Henning Kamp printf("(off=0x%lx,page=0x%lx)", 1439a316d390SJohn Dyson (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p)); 1440df8bae1dSRodney W. Grimes } 1441df8bae1dSRodney W. Grimes if (count != 0) 1442df8bae1dSRodney W. Grimes printf("\n"); 1443df8bae1dSRodney W. Grimes indent -= 2; 1444df8bae1dSRodney W. Grimes } 1445c3cb3e12SDavid Greenman #endif /* DDB */ 1446