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 * 6424a1cce3SDavid Greenman * $Id: vm_object.c,v 1.48 1995/06/11 19:31:53 rgrimes Exp $ 65df8bae1dSRodney W. Grimes */ 66df8bae1dSRodney W. Grimes 67df8bae1dSRodney W. Grimes /* 68df8bae1dSRodney W. Grimes * Virtual memory object module. 69df8bae1dSRodney W. Grimes */ 70df8bae1dSRodney W. Grimes 71df8bae1dSRodney W. Grimes #include <sys/param.h> 72df8bae1dSRodney W. Grimes #include <sys/systm.h> 73f23b4c91SGarrett Wollman #include <sys/kernel.h> 74f23b4c91SGarrett Wollman #include <sys/proc.h> /* for curproc, pageproc */ 75df8bae1dSRodney W. Grimes #include <sys/malloc.h> 760d94caffSDavid Greenman #include <sys/vnode.h> 770d94caffSDavid Greenman #include <sys/mount.h> 78df8bae1dSRodney W. Grimes 79df8bae1dSRodney W. Grimes #include <vm/vm.h> 80df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 8126f9a767SRodney W. Grimes #include <vm/vm_pageout.h> 820d94caffSDavid Greenman #include <vm/vm_pager.h> 8305f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 84a1f6d91cSDavid Greenman #include <vm/vm_kern.h> 8526f9a767SRodney W. Grimes 8624a1cce3SDavid Greenman static void _vm_object_allocate(objtype_t, vm_size_t, vm_object_t); 87df8bae1dSRodney W. Grimes 88f6b04d2bSDavid Greenman 89df8bae1dSRodney W. Grimes /* 90df8bae1dSRodney W. Grimes * Virtual memory objects maintain the actual data 91df8bae1dSRodney W. Grimes * associated with allocated virtual memory. A given 92df8bae1dSRodney W. Grimes * page of memory exists within exactly one object. 93df8bae1dSRodney W. Grimes * 94df8bae1dSRodney W. Grimes * An object is only deallocated when all "references" 95df8bae1dSRodney W. Grimes * are given up. Only one "reference" to a given 96df8bae1dSRodney W. Grimes * region of an object should be writeable. 97df8bae1dSRodney W. Grimes * 98df8bae1dSRodney W. Grimes * Associated with each object is a list of all resident 99df8bae1dSRodney W. Grimes * memory pages belonging to that object; this list is 100df8bae1dSRodney W. Grimes * maintained by the "vm_page" module, and locked by the object's 101df8bae1dSRodney W. Grimes * lock. 102df8bae1dSRodney W. Grimes * 103df8bae1dSRodney W. Grimes * Each object also records a "pager" routine which is 104df8bae1dSRodney W. Grimes * used to retrieve (and store) pages to the proper backing 105df8bae1dSRodney W. Grimes * storage. In addition, objects may be backed by other 106df8bae1dSRodney W. Grimes * objects from which they were virtual-copied. 107df8bae1dSRodney W. Grimes * 108df8bae1dSRodney W. Grimes * The only items within the object structure which are 109df8bae1dSRodney W. Grimes * modified after time of creation are: 110df8bae1dSRodney W. Grimes * reference count locked by object's lock 111df8bae1dSRodney W. Grimes * pager routine locked by object's lock 112df8bae1dSRodney W. Grimes * 113df8bae1dSRodney W. Grimes */ 114df8bae1dSRodney W. Grimes 11526f9a767SRodney W. Grimes 116df8bae1dSRodney W. Grimes struct vm_object kernel_object_store; 117df8bae1dSRodney W. Grimes struct vm_object kmem_object_store; 118df8bae1dSRodney W. Grimes 1190d94caffSDavid Greenman int vm_object_cache_max; 1200d94caffSDavid Greenman 121c3cb3e12SDavid Greenman long object_collapses; 122c3cb3e12SDavid Greenman long object_bypasses; 123df8bae1dSRodney W. Grimes 124df8bae1dSRodney W. Grimes static void 12524a1cce3SDavid Greenman _vm_object_allocate(type, size, object) 12624a1cce3SDavid Greenman objtype_t type; 127df8bae1dSRodney W. Grimes vm_size_t size; 128df8bae1dSRodney W. Grimes register vm_object_t object; 129df8bae1dSRodney W. Grimes { 130df8bae1dSRodney W. Grimes TAILQ_INIT(&object->memq); 13124a1cce3SDavid Greenman TAILQ_INIT(&object->shadow_head); 132a1f6d91cSDavid Greenman 13324a1cce3SDavid Greenman object->type = type; 134df8bae1dSRodney W. Grimes object->size = size; 135a1f6d91cSDavid Greenman object->ref_count = 1; 13624a1cce3SDavid Greenman object->flags = 0; 137df8bae1dSRodney W. Grimes object->paging_in_progress = 0; 138a1f6d91cSDavid Greenman object->resident_page_count = 0; 13924a1cce3SDavid Greenman object->pg_data = NULL; 14024a1cce3SDavid Greenman object->handle = NULL; 141df8bae1dSRodney W. Grimes object->paging_offset = 0; 14224a1cce3SDavid Greenman object->backing_object = NULL; 14324a1cce3SDavid Greenman object->backing_object_offset = (vm_offset_t) 0; 144a1f6d91cSDavid Greenman 145a1f6d91cSDavid Greenman object->last_read = 0; 146df8bae1dSRodney W. Grimes 147df8bae1dSRodney W. Grimes TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 148df8bae1dSRodney W. Grimes vm_object_count++; 149df8bae1dSRodney W. Grimes } 150df8bae1dSRodney W. Grimes 151df8bae1dSRodney W. Grimes /* 15226f9a767SRodney W. Grimes * vm_object_init: 15326f9a767SRodney W. Grimes * 15426f9a767SRodney W. Grimes * Initialize the VM objects module. 15526f9a767SRodney W. Grimes */ 15626f9a767SRodney W. Grimes void 15726f9a767SRodney W. Grimes vm_object_init(vm_offset_t nothing) 15826f9a767SRodney W. Grimes { 15926f9a767SRodney W. Grimes register int i; 16026f9a767SRodney W. Grimes 16126f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_cached_list); 16226f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_list); 16326f9a767SRodney W. Grimes vm_object_count = 0; 1640217125fSDavid Greenman 1650217125fSDavid Greenman vm_object_cache_max = 84; 1660217125fSDavid Greenman if (cnt.v_page_count > 1000) 1670217125fSDavid Greenman vm_object_cache_max += (cnt.v_page_count - 1000) / 4; 16826f9a767SRodney W. Grimes 16926f9a767SRodney W. Grimes kernel_object = &kernel_object_store; 17024a1cce3SDavid Greenman _vm_object_allocate(OBJT_DEFAULT, VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS, 17126f9a767SRodney W. Grimes kernel_object); 17226f9a767SRodney W. Grimes 17326f9a767SRodney W. Grimes kmem_object = &kmem_object_store; 17424a1cce3SDavid Greenman _vm_object_allocate(OBJT_DEFAULT, VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS, 17526f9a767SRodney W. Grimes kmem_object); 17626f9a767SRodney W. Grimes } 17726f9a767SRodney W. Grimes 17826f9a767SRodney W. Grimes /* 17926f9a767SRodney W. Grimes * vm_object_allocate: 18026f9a767SRodney W. Grimes * 18126f9a767SRodney W. Grimes * Returns a new object with the given size. 18226f9a767SRodney W. Grimes */ 18326f9a767SRodney W. Grimes 18426f9a767SRodney W. Grimes vm_object_t 18524a1cce3SDavid Greenman vm_object_allocate(type, size) 18624a1cce3SDavid Greenman objtype_t type; 18726f9a767SRodney W. Grimes vm_size_t size; 18826f9a767SRodney W. Grimes { 18926f9a767SRodney W. Grimes register vm_object_t result; 19026f9a767SRodney W. Grimes 19126f9a767SRodney W. Grimes result = (vm_object_t) 19226f9a767SRodney W. Grimes malloc((u_long) sizeof *result, M_VMOBJ, M_WAITOK); 19326f9a767SRodney W. Grimes 19426f9a767SRodney W. Grimes 19524a1cce3SDavid Greenman _vm_object_allocate(type, size, result); 19626f9a767SRodney W. Grimes 19726f9a767SRodney W. Grimes return (result); 19826f9a767SRodney W. Grimes } 19926f9a767SRodney W. Grimes 20026f9a767SRodney W. Grimes 20126f9a767SRodney W. Grimes /* 202df8bae1dSRodney W. Grimes * vm_object_reference: 203df8bae1dSRodney W. Grimes * 204df8bae1dSRodney W. Grimes * Gets another reference to the given object. 205df8bae1dSRodney W. Grimes */ 20626f9a767SRodney W. Grimes inline void 20726f9a767SRodney W. Grimes vm_object_reference(object) 208df8bae1dSRodney W. Grimes register vm_object_t object; 209df8bae1dSRodney W. Grimes { 210df8bae1dSRodney W. Grimes if (object == NULL) 211df8bae1dSRodney W. Grimes return; 212df8bae1dSRodney W. Grimes 21324a1cce3SDavid Greenman if (object->ref_count == 0) { 21424a1cce3SDavid Greenman if ((object->flags & OBJ_CANPERSIST) == 0) 21524a1cce3SDavid Greenman panic("vm_object_reference: non-persistent object with 0 ref_count"); 21624a1cce3SDavid Greenman TAILQ_REMOVE(&vm_object_cached_list, object, cached_list); 21724a1cce3SDavid Greenman vm_object_cached--; 21824a1cce3SDavid Greenman } 219df8bae1dSRodney W. Grimes object->ref_count++; 220df8bae1dSRodney W. Grimes } 221df8bae1dSRodney W. Grimes 222df8bae1dSRodney W. Grimes /* 223df8bae1dSRodney W. Grimes * vm_object_deallocate: 224df8bae1dSRodney W. Grimes * 225df8bae1dSRodney W. Grimes * Release a reference to the specified object, 226df8bae1dSRodney W. Grimes * gained either through a vm_object_allocate 227df8bae1dSRodney W. Grimes * or a vm_object_reference call. When all references 228df8bae1dSRodney W. Grimes * are gone, storage associated with this object 229df8bae1dSRodney W. Grimes * may be relinquished. 230df8bae1dSRodney W. Grimes * 231df8bae1dSRodney W. Grimes * No object may be locked. 232df8bae1dSRodney W. Grimes */ 23326f9a767SRodney W. Grimes void 23426f9a767SRodney W. Grimes vm_object_deallocate(object) 23526f9a767SRodney W. Grimes vm_object_t object; 236df8bae1dSRodney W. Grimes { 237df8bae1dSRodney W. Grimes vm_object_t temp; 238df8bae1dSRodney W. Grimes 239df8bae1dSRodney W. Grimes while (object != NULL) { 240df8bae1dSRodney W. Grimes 241ba8da839SDavid Greenman if (object->ref_count == 0) 242ba8da839SDavid Greenman panic("vm_object_deallocate: object deallocated too many times"); 243ba8da839SDavid Greenman 244df8bae1dSRodney W. Grimes /* 245df8bae1dSRodney W. Grimes * Lose the reference 246df8bae1dSRodney W. Grimes */ 247be6d5bfaSDavid Greenman object->ref_count--; 248be6d5bfaSDavid Greenman 249be6d5bfaSDavid Greenman if (object->ref_count != 0) { 250be6d5bfaSDavid Greenman if ((object->ref_count == 1) && 25124a1cce3SDavid Greenman (object->handle == NULL) && 25224a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || 25324a1cce3SDavid Greenman object->type == OBJT_SWAP)) { 254a1f6d91cSDavid Greenman vm_object_t robject; 25524a1cce3SDavid Greenman robject = object->shadow_head.tqh_first; 256be6d5bfaSDavid Greenman if ((robject != NULL) && 25724a1cce3SDavid Greenman (robject->handle == NULL) && 25824a1cce3SDavid Greenman (robject->type == OBJT_DEFAULT || 25924a1cce3SDavid Greenman robject->type == OBJT_SWAP)) { 260a1f6d91cSDavid Greenman int s; 261a1f6d91cSDavid Greenman robject->ref_count += 2; 2629b4814bbSDavid Greenman object->ref_count += 2; 263a1f6d91cSDavid Greenman 264a1f6d91cSDavid Greenman do { 265a1f6d91cSDavid Greenman s = splhigh(); 266a1f6d91cSDavid Greenman while (robject->paging_in_progress) { 267c0503609SDavid Greenman robject->flags |= OBJ_PIPWNT; 268a1f6d91cSDavid Greenman tsleep(robject, PVM, "objde1", 0); 2690d94caffSDavid Greenman } 270a1f6d91cSDavid Greenman 271a1f6d91cSDavid Greenman while (object->paging_in_progress) { 272c0503609SDavid Greenman object->flags |= OBJ_PIPWNT; 273a1f6d91cSDavid Greenman tsleep(object, PVM, "objde2", 0); 274a1f6d91cSDavid Greenman } 275a1f6d91cSDavid Greenman splx(s); 276a1f6d91cSDavid Greenman 277a1f6d91cSDavid Greenman } while( object->paging_in_progress || robject->paging_in_progress); 278a1f6d91cSDavid Greenman 2799b4814bbSDavid Greenman object->ref_count -= 2; 280a1f6d91cSDavid Greenman robject->ref_count -= 2; 281a1f6d91cSDavid Greenman if( robject->ref_count == 0) { 282a1f6d91cSDavid Greenman robject->ref_count += 1; 283ba8da839SDavid Greenman object = robject; 284ba8da839SDavid Greenman continue; 285a1f6d91cSDavid Greenman } 286a1f6d91cSDavid Greenman vm_object_collapse(robject); 287a1f6d91cSDavid Greenman return; 288010cf3b9SDavid Greenman } 289010cf3b9SDavid Greenman } 290df8bae1dSRodney W. Grimes /* 2910d94caffSDavid Greenman * If there are still references, then we are done. 292df8bae1dSRodney W. Grimes */ 293df8bae1dSRodney W. Grimes return; 294df8bae1dSRodney W. Grimes } 295df8bae1dSRodney W. Grimes 29624a1cce3SDavid Greenman if (object->type == OBJT_VNODE) { 29724a1cce3SDavid Greenman struct vnode *vp = object->handle; 298f5cf85d4SDavid Greenman 29924a1cce3SDavid Greenman vp->v_flag &= ~VTEXT; 300f5cf85d4SDavid Greenman } 301f5cf85d4SDavid Greenman 302f919ebdeSDavid Greenman /* 303f919ebdeSDavid Greenman * See if this object can persist and has some resident 304f919ebdeSDavid Greenman * pages. If so, enter it in the cache. 305f919ebdeSDavid Greenman */ 306f5cf85d4SDavid Greenman if (object->flags & OBJ_CANPERSIST) { 307f5cf85d4SDavid Greenman if (object->resident_page_count != 0) { 30824a1cce3SDavid Greenman vm_object_page_clean(object, 0, 0 ,TRUE, TRUE); 309df8bae1dSRodney W. Grimes TAILQ_INSERT_TAIL(&vm_object_cached_list, object, 310df8bae1dSRodney W. Grimes cached_list); 311df8bae1dSRodney W. Grimes vm_object_cached++; 312df8bae1dSRodney W. Grimes 313df8bae1dSRodney W. Grimes vm_object_cache_trim(); 314df8bae1dSRodney W. Grimes return; 315f5cf85d4SDavid Greenman } else { 316f5cf85d4SDavid Greenman object->flags &= ~OBJ_CANPERSIST; 317f5cf85d4SDavid Greenman } 318df8bae1dSRodney W. Grimes } 319f919ebdeSDavid Greenman 320df8bae1dSRodney W. Grimes /* 32124a1cce3SDavid Greenman * Make sure no one uses us. 322df8bae1dSRodney W. Grimes */ 3230d94caffSDavid Greenman object->flags |= OBJ_DEAD; 324df8bae1dSRodney W. Grimes 32524a1cce3SDavid Greenman temp = object->backing_object; 3262fe6e4d7SDavid Greenman if (temp) 32724a1cce3SDavid Greenman TAILQ_REMOVE(&temp->shadow_head, object, shadow_list); 328df8bae1dSRodney W. Grimes vm_object_terminate(object); 329df8bae1dSRodney W. Grimes /* unlocks and deallocates object */ 330df8bae1dSRodney W. Grimes object = temp; 331df8bae1dSRodney W. Grimes } 332df8bae1dSRodney W. Grimes } 333df8bae1dSRodney W. Grimes 334df8bae1dSRodney W. Grimes /* 335df8bae1dSRodney W. Grimes * vm_object_terminate actually destroys the specified object, freeing 336df8bae1dSRodney W. Grimes * up all previously used resources. 337df8bae1dSRodney W. Grimes * 338df8bae1dSRodney W. Grimes * The object must be locked. 339df8bae1dSRodney W. Grimes */ 34026f9a767SRodney W. Grimes void 34126f9a767SRodney W. Grimes vm_object_terminate(object) 342df8bae1dSRodney W. Grimes register vm_object_t object; 343df8bae1dSRodney W. Grimes { 3440d94caffSDavid Greenman register vm_page_t p, next; 34524a1cce3SDavid Greenman vm_object_t backing_object; 34626f9a767SRodney W. Grimes int s; 347df8bae1dSRodney W. Grimes 348df8bae1dSRodney W. Grimes /* 349f6b04d2bSDavid Greenman * wait for the pageout daemon to be done with the object 350df8bae1dSRodney W. Grimes */ 3510d94caffSDavid Greenman s = splhigh(); 352df8bae1dSRodney W. Grimes while (object->paging_in_progress) { 353c0503609SDavid Greenman object->flags |= OBJ_PIPWNT; 35424a1cce3SDavid Greenman tsleep(object, PVM, "objtrm", 0); 355df8bae1dSRodney W. Grimes } 3560d94caffSDavid Greenman splx(s); 357df8bae1dSRodney W. Grimes 35826f9a767SRodney W. Grimes if (object->paging_in_progress != 0) 35926f9a767SRodney W. Grimes panic("vm_object_deallocate: pageout in progress"); 36026f9a767SRodney W. Grimes 36126f9a767SRodney W. Grimes /* 3620d94caffSDavid Greenman * Clean and free the pages, as appropriate. All references to the 3630d94caffSDavid Greenman * object are gone, so we don't need to lock it. 36426f9a767SRodney W. Grimes */ 36524a1cce3SDavid Greenman if (object->type == OBJT_VNODE) { 36624a1cce3SDavid Greenman struct vnode *vp = object->handle; 367f6b04d2bSDavid Greenman 3680426122fSDavid Greenman VOP_LOCK(vp); 36924a1cce3SDavid Greenman vm_object_page_clean(object, 0, 0, TRUE, FALSE); 370f6b04d2bSDavid Greenman vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0); 3710426122fSDavid Greenman VOP_UNLOCK(vp); 372df8bae1dSRodney W. Grimes } 37383edfd47SDavid Greenman 3740d94caffSDavid Greenman /* 3750d94caffSDavid Greenman * Now free the pages. For internal objects, this also removes them 3760d94caffSDavid Greenman * from paging queues. 377df8bae1dSRodney W. Grimes */ 378df8bae1dSRodney W. Grimes while ((p = object->memq.tqh_first) != NULL) { 379be6d5bfaSDavid Greenman if (p->flags & PG_BUSY) 380be6d5bfaSDavid Greenman printf("vm_object_terminate: freeing busy page\n"); 3810d94caffSDavid Greenman PAGE_WAKEUP(p); 382df8bae1dSRodney W. Grimes vm_page_free(p); 383df8bae1dSRodney W. Grimes cnt.v_pfree++; 384df8bae1dSRodney W. Grimes } 385df8bae1dSRodney W. Grimes 386df8bae1dSRodney W. Grimes /* 387df8bae1dSRodney W. Grimes * Let the pager know object is dead. 388df8bae1dSRodney W. Grimes */ 38924a1cce3SDavid Greenman vm_pager_deallocate(object); 390df8bae1dSRodney W. Grimes 391df8bae1dSRodney W. Grimes TAILQ_REMOVE(&vm_object_list, object, object_list); 392df8bae1dSRodney W. Grimes vm_object_count--; 393df8bae1dSRodney W. Grimes 3940426122fSDavid Greenman wakeup(object); 3950426122fSDavid Greenman 396df8bae1dSRodney W. Grimes /* 397df8bae1dSRodney W. Grimes * Free the space for the object. 398df8bae1dSRodney W. Grimes */ 399df8bae1dSRodney W. Grimes free((caddr_t) object, M_VMOBJ); 400df8bae1dSRodney W. Grimes } 401df8bae1dSRodney W. Grimes 402df8bae1dSRodney W. Grimes /* 403df8bae1dSRodney W. Grimes * vm_object_page_clean 404df8bae1dSRodney W. Grimes * 405df8bae1dSRodney W. Grimes * Clean all dirty pages in the specified range of object. 40626f9a767SRodney W. Grimes * Leaves page on whatever queue it is currently on. 40726f9a767SRodney W. Grimes * 40826f9a767SRodney W. Grimes * Odd semantics: if start == end, we clean everything. 40926f9a767SRodney W. Grimes * 41026f9a767SRodney W. Grimes * The object must be locked. 41126f9a767SRodney W. Grimes */ 412f6b04d2bSDavid Greenman 413f6b04d2bSDavid Greenman void 41424a1cce3SDavid Greenman vm_object_page_clean(object, start, end, syncio, lockflag) 415f6b04d2bSDavid Greenman vm_object_t object; 416f6b04d2bSDavid Greenman vm_offset_t start; 417f6b04d2bSDavid Greenman vm_offset_t end; 418f6b04d2bSDavid Greenman boolean_t syncio; 41924a1cce3SDavid Greenman boolean_t lockflag; 420f6b04d2bSDavid Greenman { 421f6b04d2bSDavid Greenman register vm_page_t p; 422f6b04d2bSDavid Greenman register vm_offset_t tstart, tend; 423f6b04d2bSDavid Greenman int pass; 424f6b04d2bSDavid Greenman int pgcount, s; 425f6b04d2bSDavid Greenman int allclean; 426ec4f9fb0SDavid Greenman int entireobj; 42724a1cce3SDavid Greenman struct vnode *vp; 428f6b04d2bSDavid Greenman 42924a1cce3SDavid Greenman if (object->type != OBJT_VNODE || (object->flags & OBJ_WRITEABLE) == 0) 430f6b04d2bSDavid Greenman return; 431f6b04d2bSDavid Greenman 43224a1cce3SDavid Greenman vp = object->handle; 43324a1cce3SDavid Greenman 43424a1cce3SDavid Greenman if (lockflag) 43524a1cce3SDavid Greenman VOP_LOCK(vp); 43624a1cce3SDavid Greenman 437f6b04d2bSDavid Greenman if (start != end) { 438f6b04d2bSDavid Greenman start = trunc_page(start); 439f6b04d2bSDavid Greenman end = round_page(end); 440f6b04d2bSDavid Greenman } 441f6b04d2bSDavid Greenman 442f6b04d2bSDavid Greenman pass = 0; 443f6b04d2bSDavid Greenman startover: 444f6b04d2bSDavid Greenman tstart = start; 445f6b04d2bSDavid Greenman if (end == 0) { 446f6b04d2bSDavid Greenman tend = object->size; 447f6b04d2bSDavid Greenman } else { 448f6b04d2bSDavid Greenman tend = end; 449f6b04d2bSDavid Greenman } 450ec4f9fb0SDavid Greenman entireobj = 0; 451ec4f9fb0SDavid Greenman if (tstart == 0 && tend == object->size) { 452ec4f9fb0SDavid Greenman object->flags &= ~OBJ_WRITEABLE; 453ec4f9fb0SDavid Greenman entireobj = 1; 454ec4f9fb0SDavid Greenman } 455f6b04d2bSDavid Greenman 456f6b04d2bSDavid Greenman pgcount = object->resident_page_count; 457f6b04d2bSDavid Greenman 458f6b04d2bSDavid Greenman if (pass == 0 && 459f6b04d2bSDavid Greenman (pgcount < 128 || pgcount > (object->size / (8 * PAGE_SIZE)))) { 460f6b04d2bSDavid Greenman allclean = 1; 461f6b04d2bSDavid Greenman for(; pgcount && (tstart < tend); tstart += PAGE_SIZE) { 462f6b04d2bSDavid Greenman p = vm_page_lookup(object, tstart); 463f6b04d2bSDavid Greenman if (!p) 464f6b04d2bSDavid Greenman continue; 465f6b04d2bSDavid Greenman --pgcount; 466f6b04d2bSDavid Greenman s = splhigh(); 467f6b04d2bSDavid Greenman TAILQ_REMOVE(&object->memq, p, listq); 468f6b04d2bSDavid Greenman TAILQ_INSERT_TAIL(&object->memq, p, listq); 469f6b04d2bSDavid Greenman splx(s); 470ec4f9fb0SDavid Greenman if (entireobj) 471ec4f9fb0SDavid Greenman vm_page_protect(p, VM_PROT_READ); 472ec4f9fb0SDavid Greenman if ((p->flags & (PG_BUSY|PG_CACHE)) || p->busy || 473ec4f9fb0SDavid Greenman p->valid == 0) { 474f6b04d2bSDavid Greenman continue; 475f6b04d2bSDavid Greenman } 476f6b04d2bSDavid Greenman vm_page_test_dirty(p); 477f6b04d2bSDavid Greenman if ((p->valid & p->dirty) != 0) { 478f6b04d2bSDavid Greenman vm_offset_t tincr; 479f6b04d2bSDavid Greenman tincr = vm_pageout_clean(p, VM_PAGEOUT_FORCE); 4807e15fd27SJohn Dyson if( tincr) { 481f6b04d2bSDavid Greenman pgcount -= (tincr - 1); 482f6b04d2bSDavid Greenman tincr *= PAGE_SIZE; 483f6b04d2bSDavid Greenman tstart += tincr - PAGE_SIZE; 4847e15fd27SJohn Dyson } 485f6b04d2bSDavid Greenman allclean = 0; 486f6b04d2bSDavid Greenman } 487f6b04d2bSDavid Greenman } 488f6b04d2bSDavid Greenman if (!allclean) { 489f6b04d2bSDavid Greenman pass = 1; 490f6b04d2bSDavid Greenman goto startover; 491f6b04d2bSDavid Greenman } 49224a1cce3SDavid Greenman if (lockflag) 49324a1cce3SDavid Greenman VOP_UNLOCK(vp); 494f6b04d2bSDavid Greenman return; 495f6b04d2bSDavid Greenman } 496f6b04d2bSDavid Greenman 497f6b04d2bSDavid Greenman allclean = 1; 498f6b04d2bSDavid Greenman while ((p = object->memq.tqh_first) != NULL && pgcount > 0) { 499f6b04d2bSDavid Greenman 500f6b04d2bSDavid Greenman if (p->flags & PG_CACHE) { 501f6b04d2bSDavid Greenman goto donext; 502f6b04d2bSDavid Greenman } 503f6b04d2bSDavid Greenman 504ec4f9fb0SDavid Greenman if (entireobj || (p->offset >= tstart && p->offset < tend)) { 505ec4f9fb0SDavid Greenman if (entireobj) 506ec4f9fb0SDavid Greenman vm_page_protect(p, VM_PROT_READ); 507ec4f9fb0SDavid Greenman 508ec4f9fb0SDavid Greenman if (p->valid == 0) { 509f6b04d2bSDavid Greenman goto donext; 510f6b04d2bSDavid Greenman } 511f6b04d2bSDavid Greenman 512f6b04d2bSDavid Greenman s = splhigh(); 513f6b04d2bSDavid Greenman if ((p->flags & PG_BUSY) || p->busy) { 514f6b04d2bSDavid Greenman allclean = 0; 515f6b04d2bSDavid Greenman if (pass > 0) { 516f6b04d2bSDavid Greenman p->flags |= PG_WANTED; 517f6b04d2bSDavid Greenman tsleep(p, PVM, "objpcn", 0); 518f6b04d2bSDavid Greenman splx(s); 519f6b04d2bSDavid Greenman continue; 520f6b04d2bSDavid Greenman } else { 521f6b04d2bSDavid Greenman splx(s); 522f6b04d2bSDavid Greenman goto donext; 523f6b04d2bSDavid Greenman } 524f6b04d2bSDavid Greenman } 525f6b04d2bSDavid Greenman 526f6b04d2bSDavid Greenman TAILQ_REMOVE(&object->memq, p, listq); 527f6b04d2bSDavid Greenman TAILQ_INSERT_TAIL(&object->memq, p, listq); 528f6b04d2bSDavid Greenman splx(s); 529f6b04d2bSDavid Greenman 530f6b04d2bSDavid Greenman pgcount--; 531f6b04d2bSDavid Greenman vm_page_test_dirty(p); 532f6b04d2bSDavid Greenman if ((p->valid & p->dirty) != 0) { 533f6b04d2bSDavid Greenman vm_pageout_clean(p, VM_PAGEOUT_FORCE); 534f6b04d2bSDavid Greenman allclean = 0; 535f6b04d2bSDavid Greenman } 536f6b04d2bSDavid Greenman continue; 537f6b04d2bSDavid Greenman } 538f6b04d2bSDavid Greenman donext: 539f6b04d2bSDavid Greenman TAILQ_REMOVE(&object->memq, p, listq); 540f6b04d2bSDavid Greenman TAILQ_INSERT_TAIL(&object->memq, p, listq); 541f6b04d2bSDavid Greenman pgcount--; 542f6b04d2bSDavid Greenman } 543ec4f9fb0SDavid Greenman if ((!allclean && (pass == 0)) || 544ec4f9fb0SDavid Greenman (entireobj && (object->flags & OBJ_WRITEABLE))) { 545f6b04d2bSDavid Greenman pass = 1; 546ec4f9fb0SDavid Greenman if (entireobj) 547f6b04d2bSDavid Greenman object->flags &= ~OBJ_WRITEABLE; 548f6b04d2bSDavid Greenman goto startover; 549f6b04d2bSDavid Greenman } 55024a1cce3SDavid Greenman if (lockflag) 551d3628763SRodney W. Grimes VOP_UNLOCK(vp); 552f5cf85d4SDavid Greenman return; 55326f9a767SRodney W. Grimes } 554df8bae1dSRodney W. Grimes 555df8bae1dSRodney W. Grimes /* 556df8bae1dSRodney W. Grimes * vm_object_deactivate_pages 557df8bae1dSRodney W. Grimes * 558df8bae1dSRodney W. Grimes * Deactivate all pages in the specified object. (Keep its pages 559df8bae1dSRodney W. Grimes * in memory even though it is no longer referenced.) 560df8bae1dSRodney W. Grimes * 561df8bae1dSRodney W. Grimes * The object must be locked. 562df8bae1dSRodney W. Grimes */ 563df8bae1dSRodney W. Grimes void 564df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object) 565df8bae1dSRodney W. Grimes register vm_object_t object; 566df8bae1dSRodney W. Grimes { 567df8bae1dSRodney W. Grimes register vm_page_t p, next; 568df8bae1dSRodney W. Grimes 569df8bae1dSRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = next) { 570df8bae1dSRodney W. Grimes next = p->listq.tqe_next; 571df8bae1dSRodney W. Grimes vm_page_deactivate(p); 572df8bae1dSRodney W. Grimes } 573df8bae1dSRodney W. Grimes } 574df8bae1dSRodney W. Grimes 575df8bae1dSRodney W. Grimes /* 576df8bae1dSRodney W. Grimes * Trim the object cache to size. 577df8bae1dSRodney W. Grimes */ 578df8bae1dSRodney W. Grimes void 579df8bae1dSRodney W. Grimes vm_object_cache_trim() 580df8bae1dSRodney W. Grimes { 581df8bae1dSRodney W. Grimes register vm_object_t object; 582df8bae1dSRodney W. Grimes 5830d94caffSDavid Greenman while (vm_object_cached > vm_object_cache_max) { 584df8bae1dSRodney W. Grimes object = vm_object_cached_list.tqh_first; 585df8bae1dSRodney W. Grimes 58624a1cce3SDavid Greenman vm_object_reference(object); 587df8bae1dSRodney W. Grimes pager_cache(object, FALSE); 588df8bae1dSRodney W. Grimes } 589df8bae1dSRodney W. Grimes } 590df8bae1dSRodney W. Grimes 59126f9a767SRodney W. Grimes 592df8bae1dSRodney W. Grimes /* 593df8bae1dSRodney W. Grimes * vm_object_pmap_copy: 594df8bae1dSRodney W. Grimes * 595df8bae1dSRodney W. Grimes * Makes all physical pages in the specified 596df8bae1dSRodney W. Grimes * object range copy-on-write. No writeable 597df8bae1dSRodney W. Grimes * references to these pages should remain. 598df8bae1dSRodney W. Grimes * 599df8bae1dSRodney W. Grimes * The object must *not* be locked. 600df8bae1dSRodney W. Grimes */ 6010d94caffSDavid Greenman void 6020d94caffSDavid Greenman vm_object_pmap_copy(object, start, end) 603df8bae1dSRodney W. Grimes register vm_object_t object; 604df8bae1dSRodney W. Grimes register vm_offset_t start; 605df8bae1dSRodney W. Grimes register vm_offset_t end; 606df8bae1dSRodney W. Grimes { 607df8bae1dSRodney W. Grimes register vm_page_t p; 608df8bae1dSRodney W. Grimes 609df8bae1dSRodney W. Grimes if (object == NULL) 610df8bae1dSRodney W. Grimes return; 611df8bae1dSRodney W. Grimes 612df8bae1dSRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) { 613df8bae1dSRodney W. Grimes if ((start <= p->offset) && (p->offset < end)) { 614f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_READ); 615df8bae1dSRodney W. Grimes p->flags |= PG_COPYONWRITE; 616df8bae1dSRodney W. Grimes } 617df8bae1dSRodney W. Grimes } 618df8bae1dSRodney W. Grimes } 619df8bae1dSRodney W. Grimes 620df8bae1dSRodney W. Grimes /* 621df8bae1dSRodney W. Grimes * vm_object_pmap_remove: 622df8bae1dSRodney W. Grimes * 623df8bae1dSRodney W. Grimes * Removes all physical pages in the specified 624df8bae1dSRodney W. Grimes * object range from all physical maps. 625df8bae1dSRodney W. Grimes * 626df8bae1dSRodney W. Grimes * The object must *not* be locked. 627df8bae1dSRodney W. Grimes */ 62826f9a767SRodney W. Grimes void 62926f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end) 630df8bae1dSRodney W. Grimes register vm_object_t object; 631df8bae1dSRodney W. Grimes register vm_offset_t start; 632df8bae1dSRodney W. Grimes register vm_offset_t end; 633df8bae1dSRodney W. Grimes { 634df8bae1dSRodney W. Grimes register vm_page_t p; 635a481f200SDavid Greenman int s; 636df8bae1dSRodney W. Grimes 637df8bae1dSRodney W. Grimes if (object == NULL) 638df8bae1dSRodney W. Grimes return; 6392fe6e4d7SDavid Greenman ++object->paging_in_progress; 640df8bae1dSRodney W. Grimes 64126f9a767SRodney W. Grimes again: 64226f9a767SRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) { 64326f9a767SRodney W. Grimes if ((start <= p->offset) && (p->offset < end)) { 644a481f200SDavid Greenman s = splhigh(); 6450d94caffSDavid Greenman if ((p->flags & PG_BUSY) || p->busy) { 64626f9a767SRodney W. Grimes p->flags |= PG_WANTED; 64724a1cce3SDavid Greenman tsleep(p, PVM, "vmopmr", 0); 648a481f200SDavid Greenman splx(s); 64926f9a767SRodney W. Grimes goto again; 65026f9a767SRodney W. Grimes } 651a481f200SDavid Greenman splx(s); 652f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 65326f9a767SRodney W. Grimes } 65426f9a767SRodney W. Grimes } 655f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 656c0503609SDavid Greenman } 657df8bae1dSRodney W. Grimes 658df8bae1dSRodney W. Grimes /* 659df8bae1dSRodney W. Grimes * vm_object_copy: 660df8bae1dSRodney W. Grimes * 661df8bae1dSRodney W. Grimes * Create a new object which is a copy of an existing 662df8bae1dSRodney W. Grimes * object, and mark all of the pages in the existing 663df8bae1dSRodney W. Grimes * object 'copy-on-write'. The new object has one reference. 664df8bae1dSRodney W. Grimes * Returns the new object. 665df8bae1dSRodney W. Grimes * 666df8bae1dSRodney W. Grimes * May defer the copy until later if the object is not backed 667df8bae1dSRodney W. Grimes * up by a non-default pager. 668df8bae1dSRodney W. Grimes */ 6690d94caffSDavid Greenman void 6700d94caffSDavid Greenman vm_object_copy(src_object, src_offset, size, 671df8bae1dSRodney W. Grimes dst_object, dst_offset, src_needs_copy) 672df8bae1dSRodney W. Grimes register vm_object_t src_object; 673df8bae1dSRodney W. Grimes vm_offset_t src_offset; 674df8bae1dSRodney W. Grimes vm_size_t size; 675df8bae1dSRodney W. Grimes vm_object_t *dst_object;/* OUT */ 676df8bae1dSRodney W. Grimes vm_offset_t *dst_offset;/* OUT */ 677df8bae1dSRodney W. Grimes boolean_t *src_needs_copy; /* OUT */ 678df8bae1dSRodney W. Grimes { 679df8bae1dSRodney W. Grimes register vm_object_t new_copy; 680df8bae1dSRodney W. Grimes register vm_object_t old_copy; 681df8bae1dSRodney W. Grimes vm_offset_t new_start, new_end; 682df8bae1dSRodney W. Grimes 683df8bae1dSRodney W. Grimes register vm_page_t p; 684df8bae1dSRodney W. Grimes 685df8bae1dSRodney W. Grimes if (src_object == NULL) { 686df8bae1dSRodney W. Grimes /* 687df8bae1dSRodney W. Grimes * Nothing to copy 688df8bae1dSRodney W. Grimes */ 689df8bae1dSRodney W. Grimes *dst_object = NULL; 690df8bae1dSRodney W. Grimes *dst_offset = 0; 691df8bae1dSRodney W. Grimes *src_needs_copy = FALSE; 692df8bae1dSRodney W. Grimes return; 693df8bae1dSRodney W. Grimes } 69426f9a767SRodney W. Grimes 69526f9a767SRodney W. Grimes /* 69626f9a767SRodney W. Grimes * Try to collapse the object before copying it. 69726f9a767SRodney W. Grimes */ 69824a1cce3SDavid Greenman if (src_object->handle == NULL && 69924a1cce3SDavid Greenman (src_object->type == OBJT_DEFAULT || 70024a1cce3SDavid Greenman src_object->type == OBJT_SWAP)) 70126f9a767SRodney W. Grimes vm_object_collapse(src_object); 70226f9a767SRodney W. Grimes 703df8bae1dSRodney W. Grimes 704df8bae1dSRodney W. Grimes /* 705df8bae1dSRodney W. Grimes * Make another reference to the object 706df8bae1dSRodney W. Grimes */ 707df8bae1dSRodney W. Grimes src_object->ref_count++; 708df8bae1dSRodney W. Grimes 709df8bae1dSRodney W. Grimes /* 710df8bae1dSRodney W. Grimes * Mark all of the pages copy-on-write. 711df8bae1dSRodney W. Grimes */ 712df8bae1dSRodney W. Grimes for (p = src_object->memq.tqh_first; p; p = p->listq.tqe_next) 713df8bae1dSRodney W. Grimes if (src_offset <= p->offset && 714df8bae1dSRodney W. Grimes p->offset < src_offset + size) 715df8bae1dSRodney W. Grimes p->flags |= PG_COPYONWRITE; 716df8bae1dSRodney W. Grimes 717df8bae1dSRodney W. Grimes *dst_object = src_object; 718df8bae1dSRodney W. Grimes *dst_offset = src_offset; 719df8bae1dSRodney W. Grimes 720df8bae1dSRodney W. Grimes /* 721df8bae1dSRodney W. Grimes * Must make a shadow when write is desired 722df8bae1dSRodney W. Grimes */ 723df8bae1dSRodney W. Grimes *src_needs_copy = TRUE; 724df8bae1dSRodney W. Grimes return; 725df8bae1dSRodney W. Grimes } 726df8bae1dSRodney W. Grimes 727df8bae1dSRodney W. Grimes /* 728df8bae1dSRodney W. Grimes * vm_object_shadow: 729df8bae1dSRodney W. Grimes * 730df8bae1dSRodney W. Grimes * Create a new object which is backed by the 731df8bae1dSRodney W. Grimes * specified existing object range. The source 732df8bae1dSRodney W. Grimes * object reference is deallocated. 733df8bae1dSRodney W. Grimes * 734df8bae1dSRodney W. Grimes * The new object and offset into that object 735df8bae1dSRodney W. Grimes * are returned in the source parameters. 736df8bae1dSRodney W. Grimes */ 737df8bae1dSRodney W. Grimes 73826f9a767SRodney W. Grimes void 73926f9a767SRodney W. Grimes vm_object_shadow(object, offset, length) 740df8bae1dSRodney W. Grimes vm_object_t *object; /* IN/OUT */ 741df8bae1dSRodney W. Grimes vm_offset_t *offset; /* IN/OUT */ 742df8bae1dSRodney W. Grimes vm_size_t length; 743df8bae1dSRodney W. Grimes { 744df8bae1dSRodney W. Grimes register vm_object_t source; 745df8bae1dSRodney W. Grimes register vm_object_t result; 746df8bae1dSRodney W. Grimes 747df8bae1dSRodney W. Grimes source = *object; 748df8bae1dSRodney W. Grimes 749df8bae1dSRodney W. Grimes /* 750df8bae1dSRodney W. Grimes * Allocate a new object with the given length 751df8bae1dSRodney W. Grimes */ 752df8bae1dSRodney W. Grimes 75324a1cce3SDavid Greenman if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL) 754df8bae1dSRodney W. Grimes panic("vm_object_shadow: no object for shadowing"); 755df8bae1dSRodney W. Grimes 756df8bae1dSRodney W. Grimes /* 7570d94caffSDavid Greenman * The new object shadows the source object, adding a reference to it. 7580d94caffSDavid Greenman * Our caller changes his reference to point to the new object, 7590d94caffSDavid Greenman * removing a reference to the source object. Net result: no change 7600d94caffSDavid Greenman * of reference count. 761df8bae1dSRodney W. Grimes */ 76224a1cce3SDavid Greenman result->backing_object = source; 7637b18a718SDavid Greenman if (source) 76424a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&result->backing_object->shadow_head, result, shadow_list); 765df8bae1dSRodney W. Grimes 766df8bae1dSRodney W. Grimes /* 7670d94caffSDavid Greenman * Store the offset into the source object, and fix up the offset into 7680d94caffSDavid Greenman * the new object. 769df8bae1dSRodney W. Grimes */ 770df8bae1dSRodney W. Grimes 77124a1cce3SDavid Greenman result->backing_object_offset = *offset; 772df8bae1dSRodney W. Grimes 773df8bae1dSRodney W. Grimes /* 774df8bae1dSRodney W. Grimes * Return the new things 775df8bae1dSRodney W. Grimes */ 776df8bae1dSRodney W. Grimes 777df8bae1dSRodney W. Grimes *offset = 0; 778df8bae1dSRodney W. Grimes *object = result; 779df8bae1dSRodney W. Grimes } 780df8bae1dSRodney W. Grimes 781df8bae1dSRodney W. Grimes 782df8bae1dSRodney W. Grimes /* 7832fe6e4d7SDavid Greenman * this version of collapse allows the operation to occur earlier and 7842fe6e4d7SDavid Greenman * when paging_in_progress is true for an object... This is not a complete 7852fe6e4d7SDavid Greenman * operation, but should plug 99.9% of the rest of the leaks. 7862fe6e4d7SDavid Greenman */ 7872fe6e4d7SDavid Greenman static void 7882fe6e4d7SDavid Greenman vm_object_qcollapse(object) 7892fe6e4d7SDavid Greenman register vm_object_t object; 7902fe6e4d7SDavid Greenman { 7912fe6e4d7SDavid Greenman register vm_object_t backing_object; 7922fe6e4d7SDavid Greenman register vm_offset_t backing_offset, new_offset; 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 80224a1cce3SDavid Greenman backing_offset = object->backing_object_offset; 8032fe6e4d7SDavid Greenman size = object->size; 8042fe6e4d7SDavid Greenman p = backing_object->memq.tqh_first; 8052fe6e4d7SDavid Greenman while (p) { 8062fe6e4d7SDavid Greenman vm_page_t next; 8070d94caffSDavid Greenman 8082fe6e4d7SDavid Greenman next = p->listq.tqe_next; 8090d94caffSDavid Greenman if ((p->flags & (PG_BUSY | PG_FICTITIOUS | PG_CACHE)) || 810f6b04d2bSDavid Greenman !p->valid || p->hold_count || p->wire_count || p->busy) { 8112fe6e4d7SDavid Greenman p = next; 8122fe6e4d7SDavid Greenman continue; 8132fe6e4d7SDavid Greenman } 814f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 8152fe6e4d7SDavid Greenman new_offset = (p->offset - backing_offset); 8162fe6e4d7SDavid Greenman if (p->offset < backing_offset || 8172fe6e4d7SDavid Greenman new_offset >= size) { 81824a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) 81924a1cce3SDavid Greenman swap_pager_freespace(backing_object, 8200d94caffSDavid Greenman backing_object->paging_offset + p->offset, PAGE_SIZE); 8212fe6e4d7SDavid Greenman vm_page_free(p); 8222fe6e4d7SDavid Greenman } else { 8232fe6e4d7SDavid Greenman pp = vm_page_lookup(object, new_offset); 82424a1cce3SDavid Greenman if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object, 82524a1cce3SDavid Greenman object->paging_offset + new_offset, NULL, NULL))) { 82624a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) 82724a1cce3SDavid Greenman swap_pager_freespace(backing_object, 8280d94caffSDavid Greenman backing_object->paging_offset + p->offset, PAGE_SIZE); 8292fe6e4d7SDavid Greenman vm_page_free(p); 8302fe6e4d7SDavid Greenman } else { 83124a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) 83224a1cce3SDavid Greenman swap_pager_freespace(backing_object, 833a1f6d91cSDavid Greenman backing_object->paging_offset + p->offset, PAGE_SIZE); 8342fe6e4d7SDavid Greenman vm_page_rename(p, object, new_offset); 8359b4814bbSDavid Greenman p->dirty = VM_PAGE_BITS_ALL; 8362fe6e4d7SDavid Greenman } 8372fe6e4d7SDavid Greenman } 8382fe6e4d7SDavid Greenman p = next; 8392fe6e4d7SDavid Greenman } 840010cf3b9SDavid Greenman backing_object->ref_count -= 2; 8412fe6e4d7SDavid Greenman } 8422fe6e4d7SDavid Greenman 843df8bae1dSRodney W. Grimes /* 844df8bae1dSRodney W. Grimes * vm_object_collapse: 845df8bae1dSRodney W. Grimes * 846df8bae1dSRodney W. Grimes * Collapse an object with the object backing it. 847df8bae1dSRodney W. Grimes * Pages in the backing object are moved into the 848df8bae1dSRodney W. Grimes * parent, and the backing object is deallocated. 849df8bae1dSRodney W. Grimes */ 85026f9a767SRodney W. Grimes void 85126f9a767SRodney W. Grimes vm_object_collapse(object) 85224a1cce3SDavid Greenman vm_object_t object; 853df8bae1dSRodney W. Grimes 854df8bae1dSRodney W. Grimes { 85524a1cce3SDavid Greenman vm_object_t backing_object; 85624a1cce3SDavid Greenman vm_offset_t backing_offset; 85724a1cce3SDavid Greenman vm_size_t size; 85824a1cce3SDavid Greenman vm_offset_t new_offset; 85924a1cce3SDavid Greenman vm_page_t p, pp; 860df8bae1dSRodney W. Grimes 861df8bae1dSRodney W. Grimes while (TRUE) { 862df8bae1dSRodney W. Grimes /* 863df8bae1dSRodney W. Grimes * Verify that the conditions are right for collapse: 864df8bae1dSRodney W. Grimes * 8650d94caffSDavid Greenman * The object exists and no pages in it are currently being paged 8660d94caffSDavid Greenman * out. 867df8bae1dSRodney W. Grimes */ 8682fe6e4d7SDavid Greenman if (object == NULL) 869df8bae1dSRodney W. Grimes return; 870df8bae1dSRodney W. Grimes 871b9921222SDavid Greenman /* 872b9921222SDavid Greenman * Make sure there is a backing object. 873b9921222SDavid Greenman */ 87424a1cce3SDavid Greenman if ((backing_object = object->backing_object) == NULL) 875df8bae1dSRodney W. Grimes return; 876df8bae1dSRodney W. Grimes 877f919ebdeSDavid Greenman /* 878f919ebdeSDavid Greenman * we check the backing object first, because it is most likely 87924a1cce3SDavid Greenman * not collapsable. 880f919ebdeSDavid Greenman */ 88124a1cce3SDavid Greenman if (backing_object->handle != NULL || 88224a1cce3SDavid Greenman (backing_object->type != OBJT_DEFAULT && 88324a1cce3SDavid Greenman backing_object->type != OBJT_SWAP) || 884f919ebdeSDavid Greenman (backing_object->flags & OBJ_DEAD) || 88524a1cce3SDavid Greenman object->handle != NULL || 88624a1cce3SDavid Greenman (object->type != OBJT_DEFAULT && 88724a1cce3SDavid Greenman object->type != OBJT_SWAP) || 88824a1cce3SDavid Greenman (object->flags & OBJ_DEAD)) { 8899b4814bbSDavid Greenman return; 89024a1cce3SDavid Greenman } 8919b4814bbSDavid Greenman 892f919ebdeSDavid Greenman if (object->paging_in_progress != 0 || 893f919ebdeSDavid Greenman backing_object->paging_in_progress != 0) { 894b9921222SDavid Greenman vm_object_qcollapse(object); 895df8bae1dSRodney W. Grimes return; 896df8bae1dSRodney W. Grimes } 897f919ebdeSDavid Greenman 89826f9a767SRodney W. Grimes /* 8990d94caffSDavid Greenman * We know that we can either collapse the backing object (if 9000d94caffSDavid Greenman * the parent is the only reference to it) or (perhaps) remove 9010d94caffSDavid Greenman * the parent's reference to it. 902df8bae1dSRodney W. Grimes */ 903df8bae1dSRodney W. Grimes 90424a1cce3SDavid Greenman backing_offset = object->backing_object_offset; 905df8bae1dSRodney W. Grimes size = object->size; 906df8bae1dSRodney W. Grimes 907df8bae1dSRodney W. Grimes /* 9080d94caffSDavid Greenman * If there is exactly one reference to the backing object, we 9090d94caffSDavid Greenman * can collapse it into the parent. 910df8bae1dSRodney W. Grimes */ 911df8bae1dSRodney W. Grimes 912df8bae1dSRodney W. Grimes if (backing_object->ref_count == 1) { 913df8bae1dSRodney W. Grimes 914a1f6d91cSDavid Greenman backing_object->flags |= OBJ_DEAD; 915df8bae1dSRodney W. Grimes /* 916df8bae1dSRodney W. Grimes * We can collapse the backing object. 917df8bae1dSRodney W. Grimes * 9180d94caffSDavid Greenman * Move all in-memory pages from backing_object to the 9190d94caffSDavid Greenman * parent. Pages that have been paged out will be 9200d94caffSDavid Greenman * overwritten by any of the parent's pages that 9210d94caffSDavid Greenman * shadow them. 922df8bae1dSRodney W. Grimes */ 923df8bae1dSRodney W. Grimes 92405f0fdd2SPoul-Henning Kamp while ((p = backing_object->memq.tqh_first) != 0) { 92526f9a767SRodney W. Grimes 926df8bae1dSRodney W. Grimes new_offset = (p->offset - backing_offset); 927df8bae1dSRodney W. Grimes 928df8bae1dSRodney W. Grimes /* 9290d94caffSDavid Greenman * If the parent has a page here, or if this 9300d94caffSDavid Greenman * page falls outside the parent, dispose of 9310d94caffSDavid Greenman * it. 932df8bae1dSRodney W. Grimes * 933df8bae1dSRodney W. Grimes * Otherwise, move it as planned. 934df8bae1dSRodney W. Grimes */ 935df8bae1dSRodney W. Grimes 936df8bae1dSRodney W. Grimes if (p->offset < backing_offset || 937df8bae1dSRodney W. Grimes new_offset >= size) { 938f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 9390d94caffSDavid Greenman PAGE_WAKEUP(p); 940df8bae1dSRodney W. Grimes vm_page_free(p); 941df8bae1dSRodney W. Grimes } else { 942df8bae1dSRodney W. Grimes pp = vm_page_lookup(object, new_offset); 94324a1cce3SDavid Greenman if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object, 94424a1cce3SDavid Greenman object->paging_offset + new_offset, NULL, NULL))) { 945f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 9460d94caffSDavid Greenman PAGE_WAKEUP(p); 947df8bae1dSRodney W. Grimes vm_page_free(p); 94826f9a767SRodney W. Grimes } else { 949df8bae1dSRodney W. Grimes vm_page_rename(p, object, new_offset); 950df8bae1dSRodney W. Grimes } 951df8bae1dSRodney W. Grimes } 952df8bae1dSRodney W. Grimes } 953df8bae1dSRodney W. Grimes 954df8bae1dSRodney W. Grimes /* 955df8bae1dSRodney W. Grimes * Move the pager from backing_object to object. 956df8bae1dSRodney W. Grimes */ 957df8bae1dSRodney W. Grimes 95824a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) { 95926f9a767SRodney W. Grimes backing_object->paging_in_progress++; 96024a1cce3SDavid Greenman if (object->type == OBJT_SWAP) { 96126f9a767SRodney W. Grimes object->paging_in_progress++; 96226f9a767SRodney W. Grimes /* 96326f9a767SRodney W. Grimes * copy shadow object pages into ours 9640d94caffSDavid Greenman * and destroy unneeded pages in 9650d94caffSDavid Greenman * shadow object. 96626f9a767SRodney W. Grimes */ 96726f9a767SRodney W. Grimes swap_pager_copy( 96824a1cce3SDavid Greenman backing_object, backing_object->paging_offset, 96924a1cce3SDavid Greenman object, object->paging_offset, 97024a1cce3SDavid Greenman object->backing_object_offset); 971f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 97226f9a767SRodney W. Grimes } else { 97324a1cce3SDavid Greenman extern struct pagerlst swap_pager_un_object_list; 97424a1cce3SDavid Greenman 97526f9a767SRodney W. Grimes object->paging_in_progress++; 97626f9a767SRodney W. Grimes /* 97724a1cce3SDavid Greenman * move the shadow backing_object's pager data to 97824a1cce3SDavid Greenman * "object" and convert "object" type to OBJT_SWAP. 97926f9a767SRodney W. Grimes */ 98024a1cce3SDavid Greenman object->type = OBJT_SWAP; 98124a1cce3SDavid Greenman object->pg_data = backing_object->pg_data; 98226f9a767SRodney W. Grimes object->paging_offset = backing_object->paging_offset + backing_offset; 98324a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&swap_pager_un_object_list, object, pager_object_list); 98424a1cce3SDavid Greenman 98524a1cce3SDavid Greenman /* 98624a1cce3SDavid Greenman * Convert backing object from OBJT_SWAP to 98724a1cce3SDavid Greenman * OBJT_DEFAULT. XXX - only the TAILQ_REMOVE is 98824a1cce3SDavid Greenman * actually necessary. 98924a1cce3SDavid Greenman */ 99024a1cce3SDavid Greenman backing_object->type = OBJT_DEFAULT; 99124a1cce3SDavid Greenman backing_object->pg_data = NULL; 99224a1cce3SDavid Greenman TAILQ_REMOVE(&swap_pager_un_object_list, backing_object, pager_object_list); 99326f9a767SRodney W. Grimes /* 99426f9a767SRodney W. Grimes * free unnecessary blocks 99526f9a767SRodney W. Grimes */ 99624a1cce3SDavid Greenman swap_pager_freespace(object, 0, object->paging_offset); 997f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 998c0503609SDavid Greenman } 999c0503609SDavid Greenman 1000f919ebdeSDavid Greenman vm_object_pip_wakeup(backing_object); 1001c0503609SDavid Greenman } 1002df8bae1dSRodney W. Grimes /* 1003df8bae1dSRodney W. Grimes * Object now shadows whatever backing_object did. 100424a1cce3SDavid Greenman * Note that the reference to backing_object->backing_object 1005df8bae1dSRodney W. Grimes * moves from within backing_object to within object. 1006df8bae1dSRodney W. Grimes */ 1007df8bae1dSRodney W. Grimes 100824a1cce3SDavid Greenman TAILQ_REMOVE(&object->backing_object->shadow_head, object, 100924a1cce3SDavid Greenman shadow_list); 101024a1cce3SDavid Greenman if (backing_object->backing_object) 101124a1cce3SDavid Greenman TAILQ_REMOVE(&backing_object->backing_object->shadow_head, 101224a1cce3SDavid Greenman backing_object, shadow_list); 101324a1cce3SDavid Greenman object->backing_object = backing_object->backing_object; 101424a1cce3SDavid Greenman if (object->backing_object) 101524a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&object->backing_object->shadow_head, 101624a1cce3SDavid Greenman object, shadow_list); 10172fe6e4d7SDavid Greenman 101824a1cce3SDavid Greenman object->backing_object_offset += backing_object->backing_object_offset; 1019df8bae1dSRodney W. Grimes /* 1020df8bae1dSRodney W. Grimes * Discard backing_object. 1021df8bae1dSRodney W. Grimes * 10220d94caffSDavid Greenman * Since the backing object has no pages, no pager left, 10230d94caffSDavid Greenman * and no object references within it, all that is 10240d94caffSDavid Greenman * necessary is to dispose of it. 1025df8bae1dSRodney W. Grimes */ 1026df8bae1dSRodney W. Grimes 1027df8bae1dSRodney W. Grimes TAILQ_REMOVE(&vm_object_list, backing_object, 1028df8bae1dSRodney W. Grimes object_list); 1029df8bae1dSRodney W. Grimes vm_object_count--; 1030df8bae1dSRodney W. Grimes 1031df8bae1dSRodney W. Grimes free((caddr_t) backing_object, M_VMOBJ); 1032df8bae1dSRodney W. Grimes 1033df8bae1dSRodney W. Grimes object_collapses++; 10340d94caffSDavid Greenman } else { 1035df8bae1dSRodney W. Grimes /* 1036df8bae1dSRodney W. Grimes * If all of the pages in the backing object are 10370d94caffSDavid Greenman * shadowed by the parent object, the parent object no 10380d94caffSDavid Greenman * longer has to shadow the backing object; it can 10390d94caffSDavid Greenman * shadow the next one in the chain. 1040df8bae1dSRodney W. Grimes * 10410d94caffSDavid Greenman * The backing object must not be paged out - we'd have 10420d94caffSDavid Greenman * to check all of the paged-out pages, as well. 1043df8bae1dSRodney W. Grimes */ 1044df8bae1dSRodney W. Grimes 104524a1cce3SDavid Greenman if (backing_object->type != OBJT_DEFAULT) { 1046df8bae1dSRodney W. Grimes return; 1047df8bae1dSRodney W. Grimes } 1048df8bae1dSRodney W. Grimes /* 10490d94caffSDavid Greenman * Should have a check for a 'small' number of pages 10500d94caffSDavid Greenman * here. 1051df8bae1dSRodney W. Grimes */ 1052df8bae1dSRodney W. Grimes 105326f9a767SRodney W. Grimes for (p = backing_object->memq.tqh_first; p; p = p->listq.tqe_next) { 1054df8bae1dSRodney W. Grimes new_offset = (p->offset - backing_offset); 1055df8bae1dSRodney W. Grimes 1056df8bae1dSRodney W. Grimes /* 10570d94caffSDavid Greenman * If the parent has a page here, or if this 10580d94caffSDavid Greenman * page falls outside the parent, keep going. 1059df8bae1dSRodney W. Grimes * 10600d94caffSDavid Greenman * Otherwise, the backing_object must be left in 10610d94caffSDavid Greenman * the chain. 1062df8bae1dSRodney W. Grimes */ 1063df8bae1dSRodney W. Grimes 106424a1cce3SDavid Greenman if (p->offset >= backing_offset && new_offset <= size) { 106524a1cce3SDavid Greenman 106624a1cce3SDavid Greenman pp = vm_page_lookup(object, new_offset); 106724a1cce3SDavid Greenman 106824a1cce3SDavid Greenman if ((pp == NULL || pp->valid == 0) && 106924a1cce3SDavid Greenman !vm_pager_has_page(object, object->paging_offset + new_offset, NULL, NULL)) { 107024a1cce3SDavid Greenman 1071df8bae1dSRodney W. Grimes /* 10720d94caffSDavid Greenman * Page still needed. Can't go any 10730d94caffSDavid Greenman * further. 1074df8bae1dSRodney W. Grimes */ 1075df8bae1dSRodney W. Grimes return; 1076df8bae1dSRodney W. Grimes } 1077df8bae1dSRodney W. Grimes } 107824a1cce3SDavid Greenman } 1079df8bae1dSRodney W. Grimes 1080df8bae1dSRodney W. Grimes /* 10810d94caffSDavid Greenman * Make the parent shadow the next object in the 10820d94caffSDavid Greenman * chain. Deallocating backing_object will not remove 10830d94caffSDavid Greenman * it, since its reference count is at least 2. 1084df8bae1dSRodney W. Grimes */ 1085df8bae1dSRodney W. Grimes 108624a1cce3SDavid Greenman TAILQ_REMOVE(&object->backing_object->shadow_head, 108724a1cce3SDavid Greenman object, shadow_list); 108824a1cce3SDavid Greenman vm_object_reference(object->backing_object = backing_object->backing_object); 108924a1cce3SDavid Greenman if (object->backing_object) 109024a1cce3SDavid Greenman TAILQ_INSERT_TAIL(&object->backing_object->shadow_head, 109124a1cce3SDavid Greenman object, shadow_list); 109224a1cce3SDavid Greenman object->backing_object_offset += backing_object->backing_object_offset; 1093df8bae1dSRodney W. Grimes 1094df8bae1dSRodney W. Grimes /* 10950d94caffSDavid Greenman * Drop the reference count on backing_object. Since 10960d94caffSDavid Greenman * its ref_count was at least 2, it will not vanish; 10970d94caffSDavid Greenman * so we don't need to call vm_object_deallocate. 1098df8bae1dSRodney W. Grimes */ 109926f9a767SRodney W. Grimes if (backing_object->ref_count == 1) 110026f9a767SRodney W. Grimes printf("should have called obj deallocate\n"); 1101df8bae1dSRodney W. Grimes backing_object->ref_count--; 1102df8bae1dSRodney W. Grimes 1103df8bae1dSRodney W. Grimes object_bypasses++; 1104df8bae1dSRodney W. Grimes 1105df8bae1dSRodney W. Grimes } 1106df8bae1dSRodney W. Grimes 1107df8bae1dSRodney W. Grimes /* 1108df8bae1dSRodney W. Grimes * Try again with this object's new backing object. 1109df8bae1dSRodney W. Grimes */ 1110df8bae1dSRodney W. Grimes } 1111df8bae1dSRodney W. Grimes } 1112df8bae1dSRodney W. Grimes 1113df8bae1dSRodney W. Grimes /* 1114df8bae1dSRodney W. Grimes * vm_object_page_remove: [internal] 1115df8bae1dSRodney W. Grimes * 1116df8bae1dSRodney W. Grimes * Removes all physical pages in the specified 1117df8bae1dSRodney W. Grimes * object range from the object's list of pages. 1118df8bae1dSRodney W. Grimes * 1119df8bae1dSRodney W. Grimes * The object must be locked. 1120df8bae1dSRodney W. Grimes */ 112126f9a767SRodney W. Grimes void 11227c1f6cedSDavid Greenman vm_object_page_remove(object, start, end, clean_only) 1123df8bae1dSRodney W. Grimes register vm_object_t object; 1124df8bae1dSRodney W. Grimes register vm_offset_t start; 1125df8bae1dSRodney W. Grimes register vm_offset_t end; 11267c1f6cedSDavid Greenman boolean_t clean_only; 1127df8bae1dSRodney W. Grimes { 1128df8bae1dSRodney W. Grimes register vm_page_t p, next; 112926f9a767SRodney W. Grimes vm_offset_t size; 113026f9a767SRodney W. Grimes int s; 1131df8bae1dSRodney W. Grimes 1132df8bae1dSRodney W. Grimes if (object == NULL) 1133df8bae1dSRodney W. Grimes return; 1134df8bae1dSRodney W. Grimes 11352fe6e4d7SDavid Greenman object->paging_in_progress++; 113626f9a767SRodney W. Grimes start = trunc_page(start); 113726f9a767SRodney W. Grimes end = round_page(end); 113826f9a767SRodney W. Grimes again: 113926f9a767SRodney W. Grimes size = end - start; 114026f9a767SRodney W. Grimes if (size > 4 * PAGE_SIZE || size >= object->size / 4) { 11410d94caffSDavid Greenman for (p = object->memq.tqh_first; p != NULL; p = next) { 1142df8bae1dSRodney W. Grimes next = p->listq.tqe_next; 1143df8bae1dSRodney W. Grimes if ((start <= p->offset) && (p->offset < end)) { 1144a481f200SDavid Greenman s = splhigh(); 11450d94caffSDavid Greenman if (p->bmapped) { 11460d94caffSDavid Greenman splx(s); 11470d94caffSDavid Greenman continue; 11480d94caffSDavid Greenman } 11490d94caffSDavid Greenman if ((p->flags & PG_BUSY) || p->busy) { 115026f9a767SRodney W. Grimes p->flags |= PG_WANTED; 115124a1cce3SDavid Greenman tsleep(p, PVM, "vmopar", 0); 1152a481f200SDavid Greenman splx(s); 115326f9a767SRodney W. Grimes goto again; 115426f9a767SRodney W. Grimes } 1155a481f200SDavid Greenman splx(s); 11567c1f6cedSDavid Greenman if (clean_only) { 11577c1f6cedSDavid Greenman vm_page_test_dirty(p); 11587c1f6cedSDavid Greenman if (p->valid & p->dirty) 11597c1f6cedSDavid Greenman continue; 11607c1f6cedSDavid Greenman } 1161f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 11620d94caffSDavid Greenman PAGE_WAKEUP(p); 1163df8bae1dSRodney W. Grimes vm_page_free(p); 116426f9a767SRodney W. Grimes } 116526f9a767SRodney W. Grimes } 116626f9a767SRodney W. Grimes } else { 116726f9a767SRodney W. Grimes while (size > 0) { 116805f0fdd2SPoul-Henning Kamp while ((p = vm_page_lookup(object, start)) != 0) { 1169a481f200SDavid Greenman s = splhigh(); 11700d94caffSDavid Greenman if (p->bmapped) { 11710d94caffSDavid Greenman splx(s); 11720d94caffSDavid Greenman break; 11730d94caffSDavid Greenman } 11740d94caffSDavid Greenman if ((p->flags & PG_BUSY) || p->busy) { 117526f9a767SRodney W. Grimes p->flags |= PG_WANTED; 117624a1cce3SDavid Greenman tsleep(p, PVM, "vmopar", 0); 1177a481f200SDavid Greenman splx(s); 117826f9a767SRodney W. Grimes goto again; 117926f9a767SRodney W. Grimes } 1180a481f200SDavid Greenman splx(s); 11817c1f6cedSDavid Greenman if (clean_only) { 11827c1f6cedSDavid Greenman vm_page_test_dirty(p); 11837c1f6cedSDavid Greenman if (p->valid & p->dirty) 11847c1f6cedSDavid Greenman continue; 11857c1f6cedSDavid Greenman } 1186f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 11870d94caffSDavid Greenman PAGE_WAKEUP(p); 118826f9a767SRodney W. Grimes vm_page_free(p); 118926f9a767SRodney W. Grimes } 119026f9a767SRodney W. Grimes start += PAGE_SIZE; 119126f9a767SRodney W. Grimes size -= PAGE_SIZE; 1192df8bae1dSRodney W. Grimes } 1193df8bae1dSRodney W. Grimes } 1194f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 1195c0503609SDavid Greenman } 1196df8bae1dSRodney W. Grimes 1197df8bae1dSRodney W. Grimes /* 1198df8bae1dSRodney W. Grimes * Routine: vm_object_coalesce 1199df8bae1dSRodney W. Grimes * Function: Coalesces two objects backing up adjoining 1200df8bae1dSRodney W. Grimes * regions of memory into a single object. 1201df8bae1dSRodney W. Grimes * 1202df8bae1dSRodney W. Grimes * returns TRUE if objects were combined. 1203df8bae1dSRodney W. Grimes * 1204df8bae1dSRodney W. Grimes * NOTE: Only works at the moment if the second object is NULL - 1205df8bae1dSRodney W. Grimes * if it's not, which object do we lock first? 1206df8bae1dSRodney W. Grimes * 1207df8bae1dSRodney W. Grimes * Parameters: 1208df8bae1dSRodney W. Grimes * prev_object First object to coalesce 1209df8bae1dSRodney W. Grimes * prev_offset Offset into prev_object 1210df8bae1dSRodney W. Grimes * next_object Second object into coalesce 1211df8bae1dSRodney W. Grimes * next_offset Offset into next_object 1212df8bae1dSRodney W. Grimes * 1213df8bae1dSRodney W. Grimes * prev_size Size of reference to prev_object 1214df8bae1dSRodney W. Grimes * next_size Size of reference to next_object 1215df8bae1dSRodney W. Grimes * 1216df8bae1dSRodney W. Grimes * Conditions: 1217df8bae1dSRodney W. Grimes * The object must *not* be locked. 1218df8bae1dSRodney W. Grimes */ 12190d94caffSDavid Greenman boolean_t 12200d94caffSDavid Greenman vm_object_coalesce(prev_object, next_object, 1221df8bae1dSRodney W. Grimes prev_offset, next_offset, 1222df8bae1dSRodney W. Grimes prev_size, next_size) 1223df8bae1dSRodney W. Grimes register vm_object_t prev_object; 1224df8bae1dSRodney W. Grimes vm_object_t next_object; 1225df8bae1dSRodney W. Grimes vm_offset_t prev_offset, next_offset; 1226df8bae1dSRodney W. Grimes vm_size_t prev_size, next_size; 1227df8bae1dSRodney W. Grimes { 1228df8bae1dSRodney W. Grimes vm_size_t newsize; 1229df8bae1dSRodney W. Grimes 1230df8bae1dSRodney W. Grimes if (next_object != NULL) { 1231df8bae1dSRodney W. Grimes return (FALSE); 1232df8bae1dSRodney W. Grimes } 1233df8bae1dSRodney W. Grimes if (prev_object == NULL) { 1234df8bae1dSRodney W. Grimes return (TRUE); 1235df8bae1dSRodney W. Grimes } 1236df8bae1dSRodney W. Grimes 1237df8bae1dSRodney W. Grimes /* 1238df8bae1dSRodney W. Grimes * Try to collapse the object first 1239df8bae1dSRodney W. Grimes */ 1240df8bae1dSRodney W. Grimes vm_object_collapse(prev_object); 1241df8bae1dSRodney W. Grimes 1242df8bae1dSRodney W. Grimes /* 12430d94caffSDavid Greenman * Can't coalesce if: . more than one reference . paged out . shadows 12440d94caffSDavid Greenman * another object . has a copy elsewhere (any of which mean that the 12450d94caffSDavid Greenman * pages not mapped to prev_entry may be in use anyway) 1246df8bae1dSRodney W. Grimes */ 1247df8bae1dSRodney W. Grimes 1248df8bae1dSRodney W. Grimes if (prev_object->ref_count > 1 || 124924a1cce3SDavid Greenman prev_object->type != OBJT_DEFAULT || 125024a1cce3SDavid Greenman prev_object->backing_object != NULL) { 1251df8bae1dSRodney W. Grimes return (FALSE); 1252df8bae1dSRodney W. Grimes } 1253df8bae1dSRodney W. Grimes /* 12540d94caffSDavid Greenman * Remove any pages that may still be in the object from a previous 12550d94caffSDavid Greenman * deallocation. 1256df8bae1dSRodney W. Grimes */ 1257df8bae1dSRodney W. Grimes 1258df8bae1dSRodney W. Grimes vm_object_page_remove(prev_object, 1259df8bae1dSRodney W. Grimes prev_offset + prev_size, 12607c1f6cedSDavid Greenman prev_offset + prev_size + next_size, FALSE); 1261df8bae1dSRodney W. Grimes 1262df8bae1dSRodney W. Grimes /* 1263df8bae1dSRodney W. Grimes * Extend the object if necessary. 1264df8bae1dSRodney W. Grimes */ 1265df8bae1dSRodney W. Grimes newsize = prev_offset + prev_size + next_size; 1266df8bae1dSRodney W. Grimes if (newsize > prev_object->size) 1267df8bae1dSRodney W. Grimes prev_object->size = newsize; 1268df8bae1dSRodney W. Grimes 1269df8bae1dSRodney W. Grimes return (TRUE); 1270df8bae1dSRodney W. Grimes } 1271df8bae1dSRodney W. Grimes 1272df8bae1dSRodney W. Grimes /* 127326f9a767SRodney W. Grimes * returns page after looking up in shadow chain 127426f9a767SRodney W. Grimes */ 127526f9a767SRodney W. Grimes 127626f9a767SRodney W. Grimes vm_page_t 127726f9a767SRodney W. Grimes vm_object_page_lookup(object, offset) 127826f9a767SRodney W. Grimes vm_object_t object; 127926f9a767SRodney W. Grimes vm_offset_t offset; 128026f9a767SRodney W. Grimes { 128126f9a767SRodney W. Grimes vm_page_t m; 12820d94caffSDavid Greenman 128326f9a767SRodney W. Grimes if (!(m = vm_page_lookup(object, offset))) { 128424a1cce3SDavid Greenman if (!object->backing_object) 128526f9a767SRodney W. Grimes return 0; 128626f9a767SRodney W. Grimes else 128724a1cce3SDavid Greenman return vm_object_page_lookup(object->backing_object, offset + object->backing_object_offset); 128826f9a767SRodney W. Grimes } 128926f9a767SRodney W. Grimes return m; 129026f9a767SRodney W. Grimes } 129126f9a767SRodney W. Grimes 1292c3cb3e12SDavid Greenman #ifdef DDB 1293c3cb3e12SDavid Greenman 1294a1f6d91cSDavid Greenman int 1295a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry) 1296a1f6d91cSDavid Greenman vm_map_t map; 1297a1f6d91cSDavid Greenman vm_object_t object; 1298a1f6d91cSDavid Greenman vm_map_entry_t entry; 1299a1f6d91cSDavid Greenman { 1300a1f6d91cSDavid Greenman vm_map_t tmpm; 1301a1f6d91cSDavid Greenman vm_map_entry_t tmpe; 1302a1f6d91cSDavid Greenman vm_object_t obj; 1303a1f6d91cSDavid Greenman int entcount; 1304a1f6d91cSDavid Greenman 1305a1f6d91cSDavid Greenman if (map == 0) 1306a1f6d91cSDavid Greenman return 0; 1307a1f6d91cSDavid Greenman 1308a1f6d91cSDavid Greenman if (entry == 0) { 1309a1f6d91cSDavid Greenman tmpe = map->header.next; 1310a1f6d91cSDavid Greenman entcount = map->nentries; 1311a1f6d91cSDavid Greenman while (entcount-- && (tmpe != &map->header)) { 1312a1f6d91cSDavid Greenman if( _vm_object_in_map(map, object, tmpe)) { 1313a1f6d91cSDavid Greenman return 1; 1314a1f6d91cSDavid Greenman } 1315a1f6d91cSDavid Greenman tmpe = tmpe->next; 1316a1f6d91cSDavid Greenman } 1317a1f6d91cSDavid Greenman } else if (entry->is_sub_map || entry->is_a_map) { 1318a1f6d91cSDavid Greenman tmpm = entry->object.share_map; 1319a1f6d91cSDavid Greenman tmpe = tmpm->header.next; 1320a1f6d91cSDavid Greenman entcount = tmpm->nentries; 1321a1f6d91cSDavid Greenman while (entcount-- && tmpe != &tmpm->header) { 1322a1f6d91cSDavid Greenman if( _vm_object_in_map(tmpm, object, tmpe)) { 1323a1f6d91cSDavid Greenman return 1; 1324a1f6d91cSDavid Greenman } 1325a1f6d91cSDavid Greenman tmpe = tmpe->next; 1326a1f6d91cSDavid Greenman } 1327a1f6d91cSDavid Greenman } else if (obj = entry->object.vm_object) { 132824a1cce3SDavid Greenman for(; obj; obj=obj->backing_object) 1329a1f6d91cSDavid Greenman if( obj == object) { 1330a1f6d91cSDavid Greenman return 1; 1331a1f6d91cSDavid Greenman } 1332a1f6d91cSDavid Greenman } 1333a1f6d91cSDavid Greenman return 0; 1334a1f6d91cSDavid Greenman } 1335a1f6d91cSDavid Greenman 1336a1f6d91cSDavid Greenman int 1337a1f6d91cSDavid Greenman vm_object_in_map( object) 1338a1f6d91cSDavid Greenman vm_object_t object; 1339a1f6d91cSDavid Greenman { 1340a1f6d91cSDavid Greenman struct proc *p; 1341a1f6d91cSDavid Greenman for (p = (struct proc *) allproc; p != NULL; p = p->p_next) { 1342a1f6d91cSDavid Greenman if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 1343a1f6d91cSDavid Greenman continue; 1344a1f6d91cSDavid Greenman /* 1345a1f6d91cSDavid Greenman if (p->p_stat != SRUN && p->p_stat != SSLEEP) { 1346a1f6d91cSDavid Greenman continue; 1347a1f6d91cSDavid Greenman } 1348a1f6d91cSDavid Greenman */ 1349a1f6d91cSDavid Greenman if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) 1350a1f6d91cSDavid Greenman return 1; 1351a1f6d91cSDavid Greenman } 1352a1f6d91cSDavid Greenman if( _vm_object_in_map( kernel_map, object, 0)) 1353a1f6d91cSDavid Greenman return 1; 1354a1f6d91cSDavid Greenman if( _vm_object_in_map( kmem_map, object, 0)) 1355a1f6d91cSDavid Greenman return 1; 1356a1f6d91cSDavid Greenman if( _vm_object_in_map( pager_map, object, 0)) 1357a1f6d91cSDavid Greenman return 1; 1358a1f6d91cSDavid Greenman if( _vm_object_in_map( buffer_map, object, 0)) 1359a1f6d91cSDavid Greenman return 1; 1360a1f6d91cSDavid Greenman if( _vm_object_in_map( io_map, object, 0)) 1361a1f6d91cSDavid Greenman return 1; 1362a1f6d91cSDavid Greenman if( _vm_object_in_map( phys_map, object, 0)) 1363a1f6d91cSDavid Greenman return 1; 1364a1f6d91cSDavid Greenman if( _vm_object_in_map( mb_map, object, 0)) 1365a1f6d91cSDavid Greenman return 1; 1366a1f6d91cSDavid Greenman if( _vm_object_in_map( u_map, object, 0)) 1367a1f6d91cSDavid Greenman return 1; 1368a1f6d91cSDavid Greenman return 0; 1369a1f6d91cSDavid Greenman } 1370a1f6d91cSDavid Greenman 1371c3cb3e12SDavid Greenman 1372a1f6d91cSDavid Greenman void 1373a1f6d91cSDavid Greenman vm_object_check() { 1374a1f6d91cSDavid Greenman int i; 1375a1f6d91cSDavid Greenman int maxhash = 0; 1376a1f6d91cSDavid Greenman vm_object_t object; 1377a1f6d91cSDavid Greenman 1378a1f6d91cSDavid Greenman /* 1379a1f6d91cSDavid Greenman * make sure that internal objs are in a map somewhere 1380a1f6d91cSDavid Greenman * and none have zero ref counts. 1381a1f6d91cSDavid Greenman */ 1382a1f6d91cSDavid Greenman for (object = vm_object_list.tqh_first; 1383a1f6d91cSDavid Greenman object != NULL; 1384a1f6d91cSDavid Greenman object = object->object_list.tqe_next) { 138524a1cce3SDavid Greenman if (object->handle == NULL && 138624a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 1387a1f6d91cSDavid Greenman if (object->ref_count == 0) { 1388a1f6d91cSDavid Greenman printf("vmochk: internal obj has zero ref count: %d\n", 1389a1f6d91cSDavid Greenman object->size); 1390a1f6d91cSDavid Greenman } 1391a1f6d91cSDavid Greenman if (!vm_object_in_map(object)) { 139224a1cce3SDavid Greenman printf("vmochk: internal obj is not in a map: ref: %d, size: %d, pg_data: 0x%x, backing_object: 0x%x\n", 139324a1cce3SDavid Greenman object->ref_count, object->size, object->pg_data, object->backing_object); 1394a1f6d91cSDavid Greenman } 1395a1f6d91cSDavid Greenman } 1396a1f6d91cSDavid Greenman } 1397a1f6d91cSDavid Greenman } 1398a1f6d91cSDavid Greenman 139926f9a767SRodney W. Grimes /* 1400df8bae1dSRodney W. Grimes * vm_object_print: [ debug ] 1401df8bae1dSRodney W. Grimes */ 14020d94caffSDavid Greenman void 14030d94caffSDavid Greenman vm_object_print(object, full) 1404df8bae1dSRodney W. Grimes vm_object_t object; 1405df8bae1dSRodney W. Grimes boolean_t full; 1406df8bae1dSRodney W. Grimes { 1407df8bae1dSRodney W. Grimes register vm_page_t p; 1408df8bae1dSRodney W. Grimes 1409df8bae1dSRodney W. Grimes register int count; 1410df8bae1dSRodney W. Grimes 1411df8bae1dSRodney W. Grimes if (object == NULL) 1412df8bae1dSRodney W. Grimes return; 1413df8bae1dSRodney W. Grimes 1414df8bae1dSRodney W. Grimes iprintf("Object 0x%x: size=0x%x, res=%d, ref=%d, ", 1415df8bae1dSRodney W. Grimes (int) object, (int) object->size, 1416df8bae1dSRodney W. Grimes object->resident_page_count, object->ref_count); 141724a1cce3SDavid Greenman printf("pg_data=0x%x+0x%x, backing_object=(0x%x)+0x%x\n", 141824a1cce3SDavid Greenman (int) object->pg_data, (int) object->paging_offset, 141924a1cce3SDavid Greenman (int) object->backing_object, (int) object->backing_object_offset); 142005f0fdd2SPoul-Henning Kamp printf("cache: next=%p, prev=%p\n", 1421df8bae1dSRodney W. Grimes object->cached_list.tqe_next, object->cached_list.tqe_prev); 1422df8bae1dSRodney W. Grimes 1423df8bae1dSRodney W. Grimes if (!full) 1424df8bae1dSRodney W. Grimes return; 1425df8bae1dSRodney W. Grimes 1426df8bae1dSRodney W. Grimes indent += 2; 1427df8bae1dSRodney W. Grimes count = 0; 1428df8bae1dSRodney W. Grimes for (p = object->memq.tqh_first; p != NULL; p = p->listq.tqe_next) { 1429df8bae1dSRodney W. Grimes if (count == 0) 1430df8bae1dSRodney W. Grimes iprintf("memory:="); 1431df8bae1dSRodney W. Grimes else if (count == 6) { 1432df8bae1dSRodney W. Grimes printf("\n"); 1433df8bae1dSRodney W. Grimes iprintf(" ..."); 1434df8bae1dSRodney W. Grimes count = 0; 1435df8bae1dSRodney W. Grimes } else 1436df8bae1dSRodney W. Grimes printf(","); 1437df8bae1dSRodney W. Grimes count++; 1438df8bae1dSRodney W. Grimes 143905f0fdd2SPoul-Henning Kamp printf("(off=0x%lx,page=0x%lx)", 144005f0fdd2SPoul-Henning Kamp (u_long) p->offset, (u_long) VM_PAGE_TO_PHYS(p)); 1441df8bae1dSRodney W. Grimes } 1442df8bae1dSRodney W. Grimes if (count != 0) 1443df8bae1dSRodney W. Grimes printf("\n"); 1444df8bae1dSRodney W. Grimes indent -= 2; 1445df8bae1dSRodney W. Grimes } 1446c3cb3e12SDavid Greenman #endif /* DDB */ 1447