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 175929bcfaSPhilippe Charnier * 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 * 64c3aac50fSPeter Wemm * $FreeBSD$ 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> 73fb919e4dSMark Murray #include <sys/lock.h> 74867a482dSJohn Dyson #include <sys/mman.h> 75cf2819ccSJohn Dyson #include <sys/mount.h> 761b367556SJason Evans #include <sys/mutex.h> 77fb919e4dSMark Murray #include <sys/proc.h> /* for curproc, pageproc */ 78fb919e4dSMark Murray #include <sys/socket.h> 79fb919e4dSMark Murray #include <sys/vnode.h> 80fb919e4dSMark Murray #include <sys/vmmeter.h> 811005a129SJohn Baldwin #include <sys/sx.h> 82df8bae1dSRodney W. Grimes 83df8bae1dSRodney W. Grimes #include <vm/vm.h> 84efeaf95aSDavid Greenman #include <vm/vm_param.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> 9121cd6e62SSeigo Tanimura #include <vm/vm_zone.h> 9205f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 93a1f6d91cSDavid Greenman #include <vm/vm_kern.h> 94efeaf95aSDavid Greenman #include <vm/vm_extern.h> 9526f9a767SRodney W. Grimes 96cac597e4SBruce Evans static void vm_object_qcollapse __P((vm_object_t object)); 97f6b04d2bSDavid Greenman 98df8bae1dSRodney W. Grimes /* 99df8bae1dSRodney W. Grimes * Virtual memory objects maintain the actual data 100df8bae1dSRodney W. Grimes * associated with allocated virtual memory. A given 101df8bae1dSRodney W. Grimes * page of memory exists within exactly one object. 102df8bae1dSRodney W. Grimes * 103df8bae1dSRodney W. Grimes * An object is only deallocated when all "references" 104df8bae1dSRodney W. Grimes * are given up. Only one "reference" to a given 105df8bae1dSRodney W. Grimes * region of an object should be writeable. 106df8bae1dSRodney W. Grimes * 107df8bae1dSRodney W. Grimes * Associated with each object is a list of all resident 108df8bae1dSRodney W. Grimes * memory pages belonging to that object; this list is 109df8bae1dSRodney W. Grimes * maintained by the "vm_page" module, and locked by the object's 110df8bae1dSRodney W. Grimes * lock. 111df8bae1dSRodney W. Grimes * 112df8bae1dSRodney W. Grimes * Each object also records a "pager" routine which is 113df8bae1dSRodney W. Grimes * used to retrieve (and store) pages to the proper backing 114df8bae1dSRodney W. Grimes * storage. In addition, objects may be backed by other 115df8bae1dSRodney W. Grimes * objects from which they were virtual-copied. 116df8bae1dSRodney W. Grimes * 117df8bae1dSRodney W. Grimes * The only items within the object structure which are 118df8bae1dSRodney W. Grimes * modified after time of creation are: 119df8bae1dSRodney W. Grimes * reference count locked by object's lock 120df8bae1dSRodney W. Grimes * pager routine locked by object's lock 121df8bae1dSRodney W. Grimes * 122df8bae1dSRodney W. Grimes */ 123df8bae1dSRodney W. Grimes 12428f8db14SBruce Evans struct object_q vm_object_list; 125cc64b484SAlfred Perlstein static struct mtx vm_object_list_mtx; /* lock for object list and count */ 1264de628deSBruce Evans static long vm_object_count; /* count of all objects */ 12728f8db14SBruce Evans vm_object_t kernel_object; 12828f8db14SBruce Evans vm_object_t kmem_object; 129f708ef1bSPoul-Henning Kamp static struct vm_object kernel_object_store; 130f708ef1bSPoul-Henning Kamp static struct vm_object kmem_object_store; 131aef922f5SJohn Dyson extern int vm_pageout_page_count; 132df8bae1dSRodney W. Grimes 133f708ef1bSPoul-Henning Kamp static long object_collapses; 134f708ef1bSPoul-Henning Kamp static long object_bypasses; 1355070c7f8SJohn Dyson static int next_index; 13699448ed1SJohn Dyson static vm_zone_t obj_zone; 13799448ed1SJohn Dyson static struct vm_zone obj_zone_store; 1381c7c3c6aSMatthew Dillon static int object_hash_rand; 13999448ed1SJohn Dyson #define VM_OBJECTS_INIT 256 140303b270bSEivind Eklund static struct vm_object vm_objects_init[VM_OBJECTS_INIT]; 141df8bae1dSRodney W. Grimes 1423075778bSJohn Dyson void 1431b40f8c0SMatthew Dillon _vm_object_allocate(objtype_t type, vm_size_t size, vm_object_t object) 144df8bae1dSRodney W. Grimes { 14599448ed1SJohn Dyson int incr; 14623955314SAlfred Perlstein 1470cddd8f0SMatthew Dillon GIANT_REQUIRED; 1480cddd8f0SMatthew Dillon 149df8bae1dSRodney W. Grimes TAILQ_INIT(&object->memq); 15024a1cce3SDavid Greenman TAILQ_INIT(&object->shadow_head); 151a1f6d91cSDavid Greenman 15224a1cce3SDavid Greenman object->type = type; 153df8bae1dSRodney W. Grimes object->size = size; 154a1f6d91cSDavid Greenman object->ref_count = 1; 15524a1cce3SDavid Greenman object->flags = 0; 15660517fd1SJohn Baldwin if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP)) 15760517fd1SJohn Baldwin vm_object_set_flag(object, OBJ_ONEMAPPING); 158df8bae1dSRodney W. Grimes object->paging_in_progress = 0; 159a1f6d91cSDavid Greenman object->resident_page_count = 0; 160de5f6a77SJohn Dyson object->shadow_count = 0; 1615070c7f8SJohn Dyson object->pg_color = next_index; 16299448ed1SJohn Dyson if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1)) 16399448ed1SJohn Dyson incr = PQ_L2_SIZE / 3 + PQ_PRIME1; 16499448ed1SJohn Dyson else 16599448ed1SJohn Dyson incr = size; 16699448ed1SJohn Dyson next_index = (next_index + incr) & PQ_L2_MASK; 16724a1cce3SDavid Greenman object->handle = NULL; 16824a1cce3SDavid Greenman object->backing_object = NULL; 169a316d390SJohn Dyson object->backing_object_offset = (vm_ooffset_t) 0; 1701c7c3c6aSMatthew Dillon /* 1711c7c3c6aSMatthew Dillon * Try to generate a number that will spread objects out in the 1721c7c3c6aSMatthew Dillon * hash table. We 'wipe' new objects across the hash in 128 page 1731c7c3c6aSMatthew Dillon * increments plus 1 more to offset it a little more by the time 1741c7c3c6aSMatthew Dillon * it wraps around. 1751c7c3c6aSMatthew Dillon */ 1761c7c3c6aSMatthew Dillon object->hash_rand = object_hash_rand - 129; 177a1f6d91cSDavid Greenman 17860517fd1SJohn Baldwin object->generation++; 17960517fd1SJohn Baldwin 18060517fd1SJohn Baldwin TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 18160517fd1SJohn Baldwin vm_object_count++; 1821c7c3c6aSMatthew Dillon object_hash_rand = object->hash_rand; 183df8bae1dSRodney W. Grimes } 184df8bae1dSRodney W. Grimes 185df8bae1dSRodney W. Grimes /* 18626f9a767SRodney W. Grimes * vm_object_init: 18726f9a767SRodney W. Grimes * 18826f9a767SRodney W. Grimes * Initialize the VM objects module. 18926f9a767SRodney W. Grimes */ 19026f9a767SRodney W. Grimes void 1911b40f8c0SMatthew Dillon vm_object_init(void) 19226f9a767SRodney W. Grimes { 1930cddd8f0SMatthew Dillon GIANT_REQUIRED; 19460517fd1SJohn Baldwin 19526f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_list); 1961b367556SJason Evans mtx_init(&vm_object_list_mtx, "vm object_list", MTX_DEF); 19726f9a767SRodney W. Grimes vm_object_count = 0; 1980217125fSDavid Greenman 19926f9a767SRodney W. Grimes kernel_object = &kernel_object_store; 200a316d390SJohn Dyson _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 20126f9a767SRodney W. Grimes kernel_object); 20226f9a767SRodney W. Grimes 20326f9a767SRodney W. Grimes kmem_object = &kmem_object_store; 204a316d390SJohn Dyson _vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 20526f9a767SRodney W. Grimes kmem_object); 20699448ed1SJohn Dyson 20799448ed1SJohn Dyson obj_zone = &obj_zone_store; 20899448ed1SJohn Dyson zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object), 20999448ed1SJohn Dyson vm_objects_init, VM_OBJECTS_INIT); 21099448ed1SJohn Dyson } 21199448ed1SJohn Dyson 21299448ed1SJohn Dyson void 2131b40f8c0SMatthew Dillon vm_object_init2(void) 21460517fd1SJohn Baldwin { 2150a80f406SJohn Dyson zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1); 21626f9a767SRodney W. Grimes } 21726f9a767SRodney W. Grimes 2181b40f8c0SMatthew Dillon void 2191b40f8c0SMatthew Dillon vm_object_set_flag(vm_object_t object, u_short bits) 2201b40f8c0SMatthew Dillon { 2211b40f8c0SMatthew Dillon GIANT_REQUIRED; 222b06805adSJake Burkholder object->flags |= bits; 2231b40f8c0SMatthew Dillon } 2241b40f8c0SMatthew Dillon 2251b40f8c0SMatthew Dillon void 2261b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits) 2271b40f8c0SMatthew Dillon { 2281b40f8c0SMatthew Dillon GIANT_REQUIRED; 229b06805adSJake Burkholder object->flags &= ~bits; 2301b40f8c0SMatthew Dillon } 2311b40f8c0SMatthew Dillon 2321b40f8c0SMatthew Dillon void 2331b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i) 2341b40f8c0SMatthew Dillon { 2351b40f8c0SMatthew Dillon GIANT_REQUIRED; 236b06805adSJake Burkholder object->paging_in_progress += i; 2371b40f8c0SMatthew Dillon } 2381b40f8c0SMatthew Dillon 2391b40f8c0SMatthew Dillon void 2401b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i) 2411b40f8c0SMatthew Dillon { 2421b40f8c0SMatthew Dillon GIANT_REQUIRED; 243b06805adSJake Burkholder object->paging_in_progress -= i; 2441b40f8c0SMatthew Dillon } 2451b40f8c0SMatthew Dillon 2461b40f8c0SMatthew Dillon void 2471b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object) 2481b40f8c0SMatthew Dillon { 2491b40f8c0SMatthew Dillon GIANT_REQUIRED; 250b06805adSJake Burkholder object->paging_in_progress--; 2511b40f8c0SMatthew Dillon if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) { 2521b40f8c0SMatthew Dillon vm_object_clear_flag(object, OBJ_PIPWNT); 2531b40f8c0SMatthew Dillon wakeup(object); 2541b40f8c0SMatthew Dillon } 2551b40f8c0SMatthew Dillon } 2561b40f8c0SMatthew Dillon 2571b40f8c0SMatthew Dillon void 2581b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i) 2591b40f8c0SMatthew Dillon { 2601b40f8c0SMatthew Dillon GIANT_REQUIRED; 2611b40f8c0SMatthew Dillon if (i) 262b06805adSJake Burkholder object->paging_in_progress -= i; 2631b40f8c0SMatthew Dillon if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) { 2641b40f8c0SMatthew Dillon vm_object_clear_flag(object, OBJ_PIPWNT); 2651b40f8c0SMatthew Dillon wakeup(object); 2661b40f8c0SMatthew Dillon } 2671b40f8c0SMatthew Dillon } 2681b40f8c0SMatthew Dillon 2691b40f8c0SMatthew Dillon void 2701b40f8c0SMatthew Dillon vm_object_pip_sleep(vm_object_t object, char *waitid) 2711b40f8c0SMatthew Dillon { 2721b40f8c0SMatthew Dillon GIANT_REQUIRED; 2731b40f8c0SMatthew Dillon if (object->paging_in_progress) { 2741b40f8c0SMatthew Dillon int s = splvm(); 2751b40f8c0SMatthew Dillon if (object->paging_in_progress) { 2761b40f8c0SMatthew Dillon vm_object_set_flag(object, OBJ_PIPWNT); 2771b40f8c0SMatthew Dillon tsleep(object, PVM, waitid, 0); 2781b40f8c0SMatthew Dillon } 2791b40f8c0SMatthew Dillon splx(s); 2801b40f8c0SMatthew Dillon } 2811b40f8c0SMatthew Dillon } 2821b40f8c0SMatthew Dillon 2831b40f8c0SMatthew Dillon void 2841b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid) 2851b40f8c0SMatthew Dillon { 2861b40f8c0SMatthew Dillon GIANT_REQUIRED; 2871b40f8c0SMatthew Dillon while (object->paging_in_progress) 2881b40f8c0SMatthew Dillon vm_object_pip_sleep(object, waitid); 2891b40f8c0SMatthew Dillon } 2901b40f8c0SMatthew Dillon 29126f9a767SRodney W. Grimes /* 29226f9a767SRodney W. Grimes * vm_object_allocate: 29326f9a767SRodney W. Grimes * 29426f9a767SRodney W. Grimes * Returns a new object with the given size. 29526f9a767SRodney W. Grimes */ 29626f9a767SRodney W. Grimes 29726f9a767SRodney W. Grimes vm_object_t 2981b40f8c0SMatthew Dillon vm_object_allocate(objtype_t type, vm_size_t size) 29926f9a767SRodney W. Grimes { 300d031cff1SMatthew Dillon vm_object_t result; 301d031cff1SMatthew Dillon 3020cddd8f0SMatthew Dillon GIANT_REQUIRED; 3030cddd8f0SMatthew Dillon 30499448ed1SJohn Dyson result = (vm_object_t) zalloc(obj_zone); 30524a1cce3SDavid Greenman _vm_object_allocate(type, size, result); 30626f9a767SRodney W. Grimes 30726f9a767SRodney W. Grimes return (result); 30826f9a767SRodney W. Grimes } 30926f9a767SRodney W. Grimes 31026f9a767SRodney W. Grimes 31126f9a767SRodney W. Grimes /* 312df8bae1dSRodney W. Grimes * vm_object_reference: 313df8bae1dSRodney W. Grimes * 314df8bae1dSRodney W. Grimes * Gets another reference to the given object. 315df8bae1dSRodney W. Grimes */ 3166476c0d2SJohn Dyson void 3171b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object) 318df8bae1dSRodney W. Grimes { 3190cddd8f0SMatthew Dillon GIANT_REQUIRED; 32060517fd1SJohn Baldwin 321df8bae1dSRodney W. Grimes if (object == NULL) 322df8bae1dSRodney W. Grimes return; 32395e5e988SJohn Dyson 3245526d2d9SEivind Eklund KASSERT(!(object->flags & OBJ_DEAD), 3255526d2d9SEivind Eklund ("vm_object_reference: attempting to reference dead obj")); 32695e5e988SJohn Dyson 327df8bae1dSRodney W. Grimes object->ref_count++; 32847221757SJohn Dyson if (object->type == OBJT_VNODE) { 32947221757SJohn Dyson while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) { 33047221757SJohn Dyson printf("vm_object_reference: delay in getting object\n"); 33147221757SJohn Dyson } 33247221757SJohn Dyson } 33395e5e988SJohn Dyson } 33495e5e988SJohn Dyson 33523955314SAlfred Perlstein /* 33623955314SAlfred Perlstein * handle deallocating a object of type OBJT_VNODE 33723955314SAlfred Perlstein */ 338bf27292bSJohn Dyson void 3391b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object) 34095e5e988SJohn Dyson { 34195e5e988SJohn Dyson struct vnode *vp = (struct vnode *) object->handle; 342219cbf59SEivind Eklund 3430cddd8f0SMatthew Dillon GIANT_REQUIRED; 3445526d2d9SEivind Eklund KASSERT(object->type == OBJT_VNODE, 3455526d2d9SEivind Eklund ("vm_object_vndeallocate: not a vnode object")); 346219cbf59SEivind Eklund KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp")); 347219cbf59SEivind Eklund #ifdef INVARIANTS 34895e5e988SJohn Dyson if (object->ref_count == 0) { 34995e5e988SJohn Dyson vprint("vm_object_vndeallocate", vp); 35095e5e988SJohn Dyson panic("vm_object_vndeallocate: bad object reference count"); 35195e5e988SJohn Dyson } 35295e5e988SJohn Dyson #endif 35395e5e988SJohn Dyson 35495e5e988SJohn Dyson object->ref_count--; 35547221757SJohn Dyson if (object->ref_count == 0) { 356bf27292bSJohn Dyson vp->v_flag &= ~VTEXT; 357069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_OPT); 3582be70f79SJohn Dyson } 35923955314SAlfred Perlstein /* 36023955314SAlfred Perlstein * vrele may need a vop lock 36123955314SAlfred Perlstein */ 36247221757SJohn Dyson vrele(vp); 363df8bae1dSRodney W. Grimes } 364df8bae1dSRodney W. Grimes 365df8bae1dSRodney W. Grimes /* 366df8bae1dSRodney W. Grimes * vm_object_deallocate: 367df8bae1dSRodney W. Grimes * 368df8bae1dSRodney W. Grimes * Release a reference to the specified object, 369df8bae1dSRodney W. Grimes * gained either through a vm_object_allocate 370df8bae1dSRodney W. Grimes * or a vm_object_reference call. When all references 371df8bae1dSRodney W. Grimes * are gone, storage associated with this object 372df8bae1dSRodney W. Grimes * may be relinquished. 373df8bae1dSRodney W. Grimes * 374df8bae1dSRodney W. Grimes * No object may be locked. 375df8bae1dSRodney W. Grimes */ 37626f9a767SRodney W. Grimes void 3771b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object) 378df8bae1dSRodney W. Grimes { 379df8bae1dSRodney W. Grimes vm_object_t temp; 380df8bae1dSRodney W. Grimes 3810cddd8f0SMatthew Dillon GIANT_REQUIRED; 3820cddd8f0SMatthew Dillon 383df8bae1dSRodney W. Grimes while (object != NULL) { 384df8bae1dSRodney W. Grimes 38595e5e988SJohn Dyson if (object->type == OBJT_VNODE) { 38695e5e988SJohn Dyson vm_object_vndeallocate(object); 38795e5e988SJohn Dyson return; 38895e5e988SJohn Dyson } 38995e5e988SJohn Dyson 3908125b1e6SAlfred Perlstein KASSERT(object->ref_count != 0, 3918125b1e6SAlfred Perlstein ("vm_object_deallocate: object deallocated too many times: %d", object->type)); 3922be70f79SJohn Dyson 3932be70f79SJohn Dyson /* 3948125b1e6SAlfred Perlstein * If the reference count goes to 0 we start calling 3958125b1e6SAlfred Perlstein * vm_object_terminate() on the object chain. 3968125b1e6SAlfred Perlstein * A ref count of 1 may be a special case depending on the 3978125b1e6SAlfred Perlstein * shadow count being 0 or 1. 3982be70f79SJohn Dyson */ 399c0877f10SJohn Dyson object->ref_count--; 4008125b1e6SAlfred Perlstein if (object->ref_count > 1) { 401c0877f10SJohn Dyson return; 4028125b1e6SAlfred Perlstein } else if (object->ref_count == 1) { 4038125b1e6SAlfred Perlstein if (object->shadow_count == 0) { 4048125b1e6SAlfred Perlstein vm_object_set_flag(object, OBJ_ONEMAPPING); 4058125b1e6SAlfred Perlstein } else if ((object->shadow_count == 1) && 4068125b1e6SAlfred Perlstein (object->handle == NULL) && 40724a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || 40824a1cce3SDavid Greenman object->type == OBJT_SWAP)) { 409a1f6d91cSDavid Greenman vm_object_t robject; 41095e5e988SJohn Dyson 411b18bfc3dSJohn Dyson robject = TAILQ_FIRST(&object->shadow_head); 4125526d2d9SEivind Eklund KASSERT(robject != NULL, 413219cbf59SEivind Eklund ("vm_object_deallocate: ref_count: %d, shadow_count: %d", 4145526d2d9SEivind Eklund object->ref_count, 4155526d2d9SEivind Eklund object->shadow_count)); 41695e5e988SJohn Dyson if ((robject->handle == NULL) && 41724a1cce3SDavid Greenman (robject->type == OBJT_DEFAULT || 41824a1cce3SDavid Greenman robject->type == OBJT_SWAP)) { 419a1f6d91cSDavid Greenman 42095e5e988SJohn Dyson robject->ref_count++; 42195e5e988SJohn Dyson 4221c7c3c6aSMatthew Dillon while ( 4231c7c3c6aSMatthew Dillon robject->paging_in_progress || 4241c7c3c6aSMatthew Dillon object->paging_in_progress 4251c7c3c6aSMatthew Dillon ) { 42666095752SJohn Dyson vm_object_pip_sleep(robject, "objde1"); 42766095752SJohn Dyson vm_object_pip_sleep(object, "objde2"); 428a1f6d91cSDavid Greenman } 429a1f6d91cSDavid Greenman 43095e5e988SJohn Dyson if (robject->ref_count == 1) { 43195e5e988SJohn Dyson robject->ref_count--; 432ba8da839SDavid Greenman object = robject; 43395e5e988SJohn Dyson goto doterm; 43495e5e988SJohn Dyson } 43595e5e988SJohn Dyson 43695e5e988SJohn Dyson object = robject; 43795e5e988SJohn Dyson vm_object_collapse(object); 438ba8da839SDavid Greenman continue; 439a1f6d91cSDavid Greenman } 44095e5e988SJohn Dyson } 44195e5e988SJohn Dyson 442a1f6d91cSDavid Greenman return; 443df8bae1dSRodney W. Grimes 44495e5e988SJohn Dyson } 44595e5e988SJohn Dyson 44695e5e988SJohn Dyson doterm: 4471efb74fbSJohn Dyson 44824a1cce3SDavid Greenman temp = object->backing_object; 449de5f6a77SJohn Dyson if (temp) { 45024a1cce3SDavid Greenman TAILQ_REMOVE(&temp->shadow_head, object, shadow_list); 45195e5e988SJohn Dyson temp->shadow_count--; 4522d8acc0fSJohn Dyson if (temp->ref_count == 0) 453069e9bc1SDoug Rabson vm_object_clear_flag(temp, OBJ_OPT); 454eaf13dd7SJohn Dyson temp->generation++; 45595461b45SJohn Dyson object->backing_object = NULL; 456de5f6a77SJohn Dyson } 457df8bae1dSRodney W. Grimes vm_object_terminate(object); 458df8bae1dSRodney W. Grimes /* unlocks and deallocates object */ 459df8bae1dSRodney W. Grimes object = temp; 460df8bae1dSRodney W. Grimes } 461df8bae1dSRodney W. Grimes } 462df8bae1dSRodney W. Grimes 463df8bae1dSRodney W. Grimes /* 464df8bae1dSRodney W. Grimes * vm_object_terminate actually destroys the specified object, freeing 465df8bae1dSRodney W. Grimes * up all previously used resources. 466df8bae1dSRodney W. Grimes * 467df8bae1dSRodney W. Grimes * The object must be locked. 4681c7c3c6aSMatthew Dillon * This routine may block. 469df8bae1dSRodney W. Grimes */ 47095e5e988SJohn Dyson void 4711b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object) 472df8bae1dSRodney W. Grimes { 473d031cff1SMatthew Dillon vm_page_t p; 474e4b7635dSDavid Greenman int s; 475df8bae1dSRodney W. Grimes 4760cddd8f0SMatthew Dillon GIANT_REQUIRED; 4770cddd8f0SMatthew Dillon 47895e5e988SJohn Dyson /* 47995e5e988SJohn Dyson * Make sure no one uses us. 48095e5e988SJohn Dyson */ 481069e9bc1SDoug Rabson vm_object_set_flag(object, OBJ_DEAD); 4823c631446SJohn Dyson 483df8bae1dSRodney W. Grimes /* 484f6b04d2bSDavid Greenman * wait for the pageout daemon to be done with the object 485df8bae1dSRodney W. Grimes */ 48666095752SJohn Dyson vm_object_pip_wait(object, "objtrm"); 487df8bae1dSRodney W. Grimes 4885526d2d9SEivind Eklund KASSERT(!object->paging_in_progress, 4895526d2d9SEivind Eklund ("vm_object_terminate: pageout in progress")); 49026f9a767SRodney W. Grimes 49126f9a767SRodney W. Grimes /* 4920d94caffSDavid Greenman * Clean and free the pages, as appropriate. All references to the 4930d94caffSDavid Greenman * object are gone, so we don't need to lock it. 49426f9a767SRodney W. Grimes */ 49524a1cce3SDavid Greenman if (object->type == OBJT_VNODE) { 49695e5e988SJohn Dyson struct vnode *vp; 49795e5e988SJohn Dyson 49895e5e988SJohn Dyson /* 49995e5e988SJohn Dyson * Freeze optimized copies. 50095e5e988SJohn Dyson */ 50195e5e988SJohn Dyson vm_freeze_copyopts(object, 0, object->size); 50295e5e988SJohn Dyson 50395e5e988SJohn Dyson /* 50495e5e988SJohn Dyson * Clean pages and flush buffers. 50595e5e988SJohn Dyson */ 5068f9110f6SJohn Dyson vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 50795e5e988SJohn Dyson 50895e5e988SJohn Dyson vp = (struct vnode *) object->handle; 509f6b04d2bSDavid Greenman vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0); 510bef608bdSJohn Dyson } 511bef608bdSJohn Dyson 512971dd342SAlfred Perlstein KASSERT(object->ref_count == 0, 513971dd342SAlfred Perlstein ("vm_object_terminate: object with references, ref_count=%d", 514971dd342SAlfred Perlstein object->ref_count)); 515996c772fSJohn Dyson 5160d94caffSDavid Greenman /* 517356863ebSDavid Greenman * Now free any remaining pages. For internal objects, this also 518356863ebSDavid Greenman * removes them from paging queues. Don't free wired pages, just 519356863ebSDavid Greenman * remove them from the object. 520df8bae1dSRodney W. Grimes */ 521e4b7635dSDavid Greenman s = splvm(); 522b18bfc3dSJohn Dyson while ((p = TAILQ_FIRST(&object->memq)) != NULL) { 523971dd342SAlfred Perlstein KASSERT(!p->busy && (p->flags & PG_BUSY) == 0, 524971dd342SAlfred Perlstein ("vm_object_terminate: freeing busy page %p " 525971dd342SAlfred Perlstein "p->busy = %d, p->flags %x\n", p, p->busy, p->flags)); 5260b10ba98SDavid Greenman if (p->wire_count == 0) { 527e69763a3SDoug Rabson vm_page_busy(p); 528df8bae1dSRodney W. Grimes vm_page_free(p); 529df8bae1dSRodney W. Grimes cnt.v_pfree++; 5300b10ba98SDavid Greenman } else { 531356863ebSDavid Greenman vm_page_busy(p); 5320b10ba98SDavid Greenman vm_page_remove(p); 5330b10ba98SDavid Greenman } 534df8bae1dSRodney W. Grimes } 535e4b7635dSDavid Greenman splx(s); 536bef608bdSJohn Dyson 5372d8acc0fSJohn Dyson /* 5389fcfb650SDavid Greenman * Let the pager know object is dead. 5399fcfb650SDavid Greenman */ 5409fcfb650SDavid Greenman vm_pager_deallocate(object); 5419fcfb650SDavid Greenman 5429fcfb650SDavid Greenman /* 5430b10ba98SDavid Greenman * Remove the object from the global object list. 5442d8acc0fSJohn Dyson */ 5459ed346baSBosko Milekic mtx_lock(&vm_object_list_mtx); 546df8bae1dSRodney W. Grimes TAILQ_REMOVE(&vm_object_list, object, object_list); 5479ed346baSBosko Milekic mtx_unlock(&vm_object_list_mtx); 5480b10ba98SDavid Greenman 5490b10ba98SDavid Greenman wakeup(object); 5500b10ba98SDavid Greenman 551df8bae1dSRodney W. Grimes /* 552df8bae1dSRodney W. Grimes * Free the space for the object. 553df8bae1dSRodney W. Grimes */ 55499448ed1SJohn Dyson zfree(obj_zone, object); 55547221757SJohn Dyson } 556df8bae1dSRodney W. Grimes 557df8bae1dSRodney W. Grimes /* 558df8bae1dSRodney W. Grimes * vm_object_page_clean 559df8bae1dSRodney W. Grimes * 5604f79d873SMatthew Dillon * Clean all dirty pages in the specified range of object. Leaves page 5614f79d873SMatthew Dillon * on whatever queue it is currently on. If NOSYNC is set then do not 5624f79d873SMatthew Dillon * write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC), 5634f79d873SMatthew Dillon * leaving the object dirty. 56426f9a767SRodney W. Grimes * 56526f9a767SRodney W. Grimes * Odd semantics: if start == end, we clean everything. 56626f9a767SRodney W. Grimes * 56726f9a767SRodney W. Grimes * The object must be locked. 56826f9a767SRodney W. Grimes */ 569f6b04d2bSDavid Greenman 570f6b04d2bSDavid Greenman void 5711b40f8c0SMatthew Dillon vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags) 572f6b04d2bSDavid Greenman { 573d031cff1SMatthew Dillon vm_page_t p, np, tp; 574d031cff1SMatthew Dillon vm_offset_t tstart, tend; 575bd7e5f99SJohn Dyson vm_pindex_t pi; 576aef922f5SJohn Dyson int s; 57724a1cce3SDavid Greenman struct vnode *vp; 578aef922f5SJohn Dyson int runlen; 579bd7e5f99SJohn Dyson int maxf; 580bd7e5f99SJohn Dyson int chkb; 581bd7e5f99SJohn Dyson int maxb; 582bd7e5f99SJohn Dyson int i; 5834f79d873SMatthew Dillon int clearobjflags; 5848f9110f6SJohn Dyson int pagerflags; 585bd7e5f99SJohn Dyson vm_page_t maf[vm_pageout_page_count]; 586bd7e5f99SJohn Dyson vm_page_t mab[vm_pageout_page_count]; 587aef922f5SJohn Dyson vm_page_t ma[vm_pageout_page_count]; 5882d8acc0fSJohn Dyson int curgeneration; 589f6b04d2bSDavid Greenman 5900cddd8f0SMatthew Dillon GIANT_REQUIRED; 5910cddd8f0SMatthew Dillon 592aef922f5SJohn Dyson if (object->type != OBJT_VNODE || 593aef922f5SJohn Dyson (object->flags & OBJ_MIGHTBEDIRTY) == 0) 594f6b04d2bSDavid Greenman return; 595f6b04d2bSDavid Greenman 5968f9110f6SJohn Dyson pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0; 5978f9110f6SJohn Dyson pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0; 5988f9110f6SJohn Dyson 59924a1cce3SDavid Greenman vp = object->handle; 60024a1cce3SDavid Greenman 601069e9bc1SDoug Rabson vm_object_set_flag(object, OBJ_CLEANING); 60224a1cce3SDavid Greenman 603f6b04d2bSDavid Greenman tstart = start; 604f6b04d2bSDavid Greenman if (end == 0) { 605f6b04d2bSDavid Greenman tend = object->size; 606f6b04d2bSDavid Greenman } else { 607f6b04d2bSDavid Greenman tend = end; 608f6b04d2bSDavid Greenman } 609eaf13dd7SJohn Dyson 6104f79d873SMatthew Dillon /* 6114f79d873SMatthew Dillon * Generally set CLEANCHK interlock and make the page read-only so 6124f79d873SMatthew Dillon * we can then clear the object flags. 6134f79d873SMatthew Dillon * 6144f79d873SMatthew Dillon * However, if this is a nosync mmap then the object is likely to 6154f79d873SMatthew Dillon * stay dirty so do not mess with the page and do not clear the 6164f79d873SMatthew Dillon * object flags. 6174f79d873SMatthew Dillon */ 6184f79d873SMatthew Dillon 6194f79d873SMatthew Dillon clearobjflags = 1; 6204f79d873SMatthew Dillon 621fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(p, &object->memq, listq) { 622e69763a3SDoug Rabson vm_page_flag_set(p, PG_CLEANCHK); 6234f79d873SMatthew Dillon if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) 6244f79d873SMatthew Dillon clearobjflags = 0; 6254f79d873SMatthew Dillon else 626eaf13dd7SJohn Dyson vm_page_protect(p, VM_PROT_READ); 627eaf13dd7SJohn Dyson } 628eaf13dd7SJohn Dyson 6294f79d873SMatthew Dillon if (clearobjflags && (tstart == 0) && (tend == object->size)) { 630069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY); 631ec4f9fb0SDavid Greenman } 632f6b04d2bSDavid Greenman 633bd7e5f99SJohn Dyson rescan: 6342d8acc0fSJohn Dyson curgeneration = object->generation; 6352d8acc0fSJohn Dyson 636b18bfc3dSJohn Dyson for (p = TAILQ_FIRST(&object->memq); p; p = np) { 637b18bfc3dSJohn Dyson np = TAILQ_NEXT(p, listq); 638bd7e5f99SJohn Dyson 639bd7e5f99SJohn Dyson pi = p->pindex; 640bd7e5f99SJohn Dyson if (((p->flags & PG_CLEANCHK) == 0) || 641bd7e5f99SJohn Dyson (pi < tstart) || (pi >= tend) || 6425070c7f8SJohn Dyson (p->valid == 0) || 6435070c7f8SJohn Dyson ((p->queue - p->pc) == PQ_CACHE)) { 644e69763a3SDoug Rabson vm_page_flag_clear(p, PG_CLEANCHK); 645aef922f5SJohn Dyson continue; 646f6b04d2bSDavid Greenman } 647f6b04d2bSDavid Greenman 648bd7e5f99SJohn Dyson vm_page_test_dirty(p); 649bd7e5f99SJohn Dyson if ((p->dirty & p->valid) == 0) { 650e69763a3SDoug Rabson vm_page_flag_clear(p, PG_CLEANCHK); 651bd7e5f99SJohn Dyson continue; 652bd7e5f99SJohn Dyson } 653ec4f9fb0SDavid Greenman 6544f79d873SMatthew Dillon /* 6554f79d873SMatthew Dillon * If we have been asked to skip nosync pages and this is a 6564f79d873SMatthew Dillon * nosync page, skip it. Note that the object flags were 6574f79d873SMatthew Dillon * not cleared in this case so we do not have to set them. 6584f79d873SMatthew Dillon */ 6594f79d873SMatthew Dillon if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) { 6604f79d873SMatthew Dillon vm_page_flag_clear(p, PG_CLEANCHK); 6614f79d873SMatthew Dillon continue; 6624f79d873SMatthew Dillon } 6634f79d873SMatthew Dillon 664b18bfc3dSJohn Dyson s = splvm(); 6651c7c3c6aSMatthew Dillon while (vm_page_sleep_busy(p, TRUE, "vpcwai")) { 6662d8acc0fSJohn Dyson if (object->generation != curgeneration) { 667f6b04d2bSDavid Greenman splx(s); 668bd7e5f99SJohn Dyson goto rescan; 669f6b04d2bSDavid Greenman } 6702d8acc0fSJohn Dyson } 671f6b04d2bSDavid Greenman 672bd7e5f99SJohn Dyson maxf = 0; 673bd7e5f99SJohn Dyson for (i = 1; i < vm_pageout_page_count; i++) { 6748aef1712SMatthew Dillon if ((tp = vm_page_lookup(object, pi + i)) != NULL) { 675bd7e5f99SJohn Dyson if ((tp->flags & PG_BUSY) || 676ffc82b0aSJohn Dyson (tp->flags & PG_CLEANCHK) == 0 || 677ffc82b0aSJohn Dyson (tp->busy != 0)) 678bd7e5f99SJohn Dyson break; 6795070c7f8SJohn Dyson if((tp->queue - tp->pc) == PQ_CACHE) { 680e69763a3SDoug Rabson vm_page_flag_clear(tp, PG_CLEANCHK); 6813077a9c2SJohn Dyson break; 6823077a9c2SJohn Dyson } 683bd7e5f99SJohn Dyson vm_page_test_dirty(tp); 684bd7e5f99SJohn Dyson if ((tp->dirty & tp->valid) == 0) { 685e69763a3SDoug Rabson vm_page_flag_clear(tp, PG_CLEANCHK); 686bd7e5f99SJohn Dyson break; 687bd7e5f99SJohn Dyson } 688bd7e5f99SJohn Dyson maf[ i - 1 ] = tp; 689bd7e5f99SJohn Dyson maxf++; 690bd7e5f99SJohn Dyson continue; 691bd7e5f99SJohn Dyson } 692bd7e5f99SJohn Dyson break; 693bd7e5f99SJohn Dyson } 694aef922f5SJohn Dyson 695bd7e5f99SJohn Dyson maxb = 0; 696bd7e5f99SJohn Dyson chkb = vm_pageout_page_count - maxf; 697bd7e5f99SJohn Dyson if (chkb) { 698bd7e5f99SJohn Dyson for (i = 1; i < chkb; i++) { 6998aef1712SMatthew Dillon if ((tp = vm_page_lookup(object, pi - i)) != NULL) { 700bd7e5f99SJohn Dyson if ((tp->flags & PG_BUSY) || 701ffc82b0aSJohn Dyson (tp->flags & PG_CLEANCHK) == 0 || 702ffc82b0aSJohn Dyson (tp->busy != 0)) 703bd7e5f99SJohn Dyson break; 7045070c7f8SJohn Dyson if((tp->queue - tp->pc) == PQ_CACHE) { 705e69763a3SDoug Rabson vm_page_flag_clear(tp, PG_CLEANCHK); 7063077a9c2SJohn Dyson break; 7073077a9c2SJohn Dyson } 708bd7e5f99SJohn Dyson vm_page_test_dirty(tp); 709bd7e5f99SJohn Dyson if ((tp->dirty & tp->valid) == 0) { 710e69763a3SDoug Rabson vm_page_flag_clear(tp, PG_CLEANCHK); 711bd7e5f99SJohn Dyson break; 712bd7e5f99SJohn Dyson } 713bd7e5f99SJohn Dyson mab[ i - 1 ] = tp; 714bd7e5f99SJohn Dyson maxb++; 715bd7e5f99SJohn Dyson continue; 716bd7e5f99SJohn Dyson } 717bd7e5f99SJohn Dyson break; 718bd7e5f99SJohn Dyson } 719bd7e5f99SJohn Dyson } 720bd7e5f99SJohn Dyson 721bd7e5f99SJohn Dyson for (i = 0; i < maxb; i++) { 722bd7e5f99SJohn Dyson int index = (maxb - i) - 1; 723bd7e5f99SJohn Dyson ma[index] = mab[i]; 724e69763a3SDoug Rabson vm_page_flag_clear(ma[index], PG_CLEANCHK); 725bd7e5f99SJohn Dyson } 726e69763a3SDoug Rabson vm_page_flag_clear(p, PG_CLEANCHK); 727bd7e5f99SJohn Dyson ma[maxb] = p; 728bd7e5f99SJohn Dyson for (i = 0 ; i < maxf; i++) { 729bd7e5f99SJohn Dyson int index = (maxb + i) + 1; 730bd7e5f99SJohn Dyson ma[index] = maf[i]; 731e69763a3SDoug Rabson vm_page_flag_clear(ma[index], PG_CLEANCHK); 732f6b04d2bSDavid Greenman } 733bd7e5f99SJohn Dyson runlen = maxb + maxf + 1; 734cf2819ccSJohn Dyson 735f35329acSJohn Dyson splx(s); 7368f9110f6SJohn Dyson vm_pageout_flush(ma, runlen, pagerflags); 737cf2819ccSJohn Dyson for (i = 0; i < runlen; i++) { 738cf2819ccSJohn Dyson if (ma[i]->valid & ma[i]->dirty) { 739cf2819ccSJohn Dyson vm_page_protect(ma[i], VM_PROT_READ); 740e69763a3SDoug Rabson vm_page_flag_set(ma[i], PG_CLEANCHK); 741cf2819ccSJohn Dyson } 742cf2819ccSJohn Dyson } 7432d8acc0fSJohn Dyson if (object->generation != curgeneration) 744bd7e5f99SJohn Dyson goto rescan; 745f6b04d2bSDavid Greenman } 746aef922f5SJohn Dyson 747479112dfSMatthew Dillon #if 0 748cf2819ccSJohn Dyson VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc); 749479112dfSMatthew Dillon #endif 750aef922f5SJohn Dyson 751069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_CLEANING); 752f5cf85d4SDavid Greenman return; 75326f9a767SRodney W. Grimes } 754df8bae1dSRodney W. Grimes 755df8bae1dSRodney W. Grimes /* 7568e3ad7c9SMatthew Dillon * Same as vm_object_pmap_copy, except range checking really 7571efb74fbSJohn Dyson * works, and is meant for small sections of an object. 7588e3ad7c9SMatthew Dillon * 7598e3ad7c9SMatthew Dillon * This code protects resident pages by making them read-only 7608e3ad7c9SMatthew Dillon * and is typically called on a fork or split when a page 7618e3ad7c9SMatthew Dillon * is converted to copy-on-write. 7628e3ad7c9SMatthew Dillon * 7638e3ad7c9SMatthew Dillon * NOTE: If the page is already at VM_PROT_NONE, calling 7648e3ad7c9SMatthew Dillon * vm_page_protect will have no effect. 7651efb74fbSJohn Dyson */ 7668e3ad7c9SMatthew Dillon 7671efb74fbSJohn Dyson void 7681b40f8c0SMatthew Dillon vm_object_pmap_copy_1(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 7691efb74fbSJohn Dyson { 7701efb74fbSJohn Dyson vm_pindex_t idx; 771d031cff1SMatthew Dillon vm_page_t p; 7721efb74fbSJohn Dyson 7730cddd8f0SMatthew Dillon GIANT_REQUIRED; 7740cddd8f0SMatthew Dillon 7751efb74fbSJohn Dyson if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0) 7761efb74fbSJohn Dyson return; 7771efb74fbSJohn Dyson 7781efb74fbSJohn Dyson for (idx = start; idx < end; idx++) { 7791efb74fbSJohn Dyson p = vm_page_lookup(object, idx); 7801efb74fbSJohn Dyson if (p == NULL) 7811efb74fbSJohn Dyson continue; 7821efb74fbSJohn Dyson vm_page_protect(p, VM_PROT_READ); 7831efb74fbSJohn Dyson } 7841efb74fbSJohn Dyson } 7851efb74fbSJohn Dyson 7861efb74fbSJohn Dyson /* 787df8bae1dSRodney W. Grimes * vm_object_pmap_remove: 788df8bae1dSRodney W. Grimes * 789df8bae1dSRodney W. Grimes * Removes all physical pages in the specified 790df8bae1dSRodney W. Grimes * object range from all physical maps. 791df8bae1dSRodney W. Grimes * 792df8bae1dSRodney W. Grimes * The object must *not* be locked. 793df8bae1dSRodney W. Grimes */ 79426f9a767SRodney W. Grimes void 7951b40f8c0SMatthew Dillon vm_object_pmap_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 796df8bae1dSRodney W. Grimes { 797d031cff1SMatthew Dillon vm_page_t p; 798d031cff1SMatthew Dillon 7990cddd8f0SMatthew Dillon GIANT_REQUIRED; 800df8bae1dSRodney W. Grimes if (object == NULL) 801df8bae1dSRodney W. Grimes return; 802cc64b484SAlfred Perlstein TAILQ_FOREACH(p, &object->memq, listq) { 803bd7e5f99SJohn Dyson if (p->pindex >= start && p->pindex < end) 804f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 80526f9a767SRodney W. Grimes } 8066e20a165SJohn Dyson if ((start == 0) && (object->size == end)) 807069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_WRITEABLE); 80826f9a767SRodney W. Grimes } 809df8bae1dSRodney W. Grimes 810df8bae1dSRodney W. Grimes /* 811867a482dSJohn Dyson * vm_object_madvise: 812867a482dSJohn Dyson * 813867a482dSJohn Dyson * Implements the madvise function at the object/page level. 8141c7c3c6aSMatthew Dillon * 815193b9358SAlan Cox * MADV_WILLNEED (any object) 816193b9358SAlan Cox * 817193b9358SAlan Cox * Activate the specified pages if they are resident. 818193b9358SAlan Cox * 819193b9358SAlan Cox * MADV_DONTNEED (any object) 820193b9358SAlan Cox * 821193b9358SAlan Cox * Deactivate the specified pages if they are resident. 822193b9358SAlan Cox * 823193b9358SAlan Cox * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, 824193b9358SAlan Cox * OBJ_ONEMAPPING only) 825193b9358SAlan Cox * 826193b9358SAlan Cox * Deactivate and clean the specified pages if they are 827193b9358SAlan Cox * resident. This permits the process to reuse the pages 828193b9358SAlan Cox * without faulting or the kernel to reclaim the pages 829193b9358SAlan Cox * without I/O. 830867a482dSJohn Dyson */ 831867a482dSJohn Dyson void 8321b40f8c0SMatthew Dillon vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise) 833867a482dSJohn Dyson { 8346e20a165SJohn Dyson vm_pindex_t end, tpindex; 8356e20a165SJohn Dyson vm_object_t tobject; 836867a482dSJohn Dyson vm_page_t m; 837867a482dSJohn Dyson 8380cddd8f0SMatthew Dillon GIANT_REQUIRED; 839867a482dSJohn Dyson if (object == NULL) 840867a482dSJohn Dyson return; 841867a482dSJohn Dyson 842867a482dSJohn Dyson end = pindex + count; 843867a482dSJohn Dyson 8441c7c3c6aSMatthew Dillon /* 8451c7c3c6aSMatthew Dillon * Locate and adjust resident pages 8461c7c3c6aSMatthew Dillon */ 8471c7c3c6aSMatthew Dillon 8481c7c3c6aSMatthew Dillon for (; pindex < end; pindex += 1) { 8496e20a165SJohn Dyson relookup: 8506e20a165SJohn Dyson tobject = object; 8516e20a165SJohn Dyson tpindex = pindex; 8526e20a165SJohn Dyson shadowlookup: 85358b4e6ccSAlan Cox /* 85458b4e6ccSAlan Cox * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages 85558b4e6ccSAlan Cox * and those pages must be OBJ_ONEMAPPING. 85658b4e6ccSAlan Cox */ 85758b4e6ccSAlan Cox if (advise == MADV_FREE) { 85858b4e6ccSAlan Cox if ((tobject->type != OBJT_DEFAULT && 85958b4e6ccSAlan Cox tobject->type != OBJT_SWAP) || 86058b4e6ccSAlan Cox (tobject->flags & OBJ_ONEMAPPING) == 0) { 8616e20a165SJohn Dyson continue; 8626e20a165SJohn Dyson } 86358b4e6ccSAlan Cox } 8641c7c3c6aSMatthew Dillon 8651c7c3c6aSMatthew Dillon m = vm_page_lookup(tobject, tpindex); 8661c7c3c6aSMatthew Dillon 8671c7c3c6aSMatthew Dillon if (m == NULL) { 8681ce137beSMatthew Dillon /* 8691ce137beSMatthew Dillon * There may be swap even if there is no backing page 8701ce137beSMatthew Dillon */ 8711ce137beSMatthew Dillon if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 8721ce137beSMatthew Dillon swap_pager_freespace(tobject, tpindex, 1); 8731ce137beSMatthew Dillon 8741ce137beSMatthew Dillon /* 8751ce137beSMatthew Dillon * next object 8761ce137beSMatthew Dillon */ 8776e20a165SJohn Dyson tobject = tobject->backing_object; 8781c7c3c6aSMatthew Dillon if (tobject == NULL) 8791c7c3c6aSMatthew Dillon continue; 8806e20a165SJohn Dyson tpindex += OFF_TO_IDX(tobject->backing_object_offset); 8816e20a165SJohn Dyson goto shadowlookup; 8826e20a165SJohn Dyson } 883867a482dSJohn Dyson 884867a482dSJohn Dyson /* 885867a482dSJohn Dyson * If the page is busy or not in a normal active state, 8868b03c8edSMatthew Dillon * we skip it. If the page is not managed there are no 8878b03c8edSMatthew Dillon * page queues to mess with. Things can break if we mess 8888b03c8edSMatthew Dillon * with pages in any of the below states. 889867a482dSJohn Dyson */ 8901c7c3c6aSMatthew Dillon if ( 8911c7c3c6aSMatthew Dillon m->hold_count || 8921c7c3c6aSMatthew Dillon m->wire_count || 8938b03c8edSMatthew Dillon (m->flags & PG_UNMANAGED) || 8941c7c3c6aSMatthew Dillon m->valid != VM_PAGE_BITS_ALL 8951c7c3c6aSMatthew Dillon ) { 896867a482dSJohn Dyson continue; 8976e20a165SJohn Dyson } 8986e20a165SJohn Dyson 8991c7c3c6aSMatthew Dillon if (vm_page_sleep_busy(m, TRUE, "madvpo")) 9006e20a165SJohn Dyson goto relookup; 901867a482dSJohn Dyson 902867a482dSJohn Dyson if (advise == MADV_WILLNEED) { 903867a482dSJohn Dyson vm_page_activate(m); 9046e20a165SJohn Dyson } else if (advise == MADV_DONTNEED) { 905479112dfSMatthew Dillon vm_page_dontneed(m); 9060a47b48bSJohn Dyson } else if (advise == MADV_FREE) { 9071c7c3c6aSMatthew Dillon /* 9082aaeadf8SMatthew Dillon * Mark the page clean. This will allow the page 9092aaeadf8SMatthew Dillon * to be freed up by the system. However, such pages 9102aaeadf8SMatthew Dillon * are often reused quickly by malloc()/free() 9112aaeadf8SMatthew Dillon * so we do not do anything that would cause 9122aaeadf8SMatthew Dillon * a page fault if we can help it. 9132aaeadf8SMatthew Dillon * 9142aaeadf8SMatthew Dillon * Specifically, we do not try to actually free 9152aaeadf8SMatthew Dillon * the page now nor do we try to put it in the 9162aaeadf8SMatthew Dillon * cache (which would cause a page fault on reuse). 91741c67e12SMatthew Dillon * 91841c67e12SMatthew Dillon * But we do make the page is freeable as we 91941c67e12SMatthew Dillon * can without actually taking the step of unmapping 92041c67e12SMatthew Dillon * it. 9211c7c3c6aSMatthew Dillon */ 9220385347cSPeter Wemm pmap_clear_modify(m); 9236e20a165SJohn Dyson m->dirty = 0; 92441c67e12SMatthew Dillon m->act_count = 0; 925479112dfSMatthew Dillon vm_page_dontneed(m); 9261ce137beSMatthew Dillon if (tobject->type == OBJT_SWAP) 9271ce137beSMatthew Dillon swap_pager_freespace(tobject, tpindex, 1); 928867a482dSJohn Dyson } 929867a482dSJohn Dyson } 930867a482dSJohn Dyson } 931867a482dSJohn Dyson 932867a482dSJohn Dyson /* 933df8bae1dSRodney W. Grimes * vm_object_shadow: 934df8bae1dSRodney W. Grimes * 935df8bae1dSRodney W. Grimes * Create a new object which is backed by the 936df8bae1dSRodney W. Grimes * specified existing object range. The source 937df8bae1dSRodney W. Grimes * object reference is deallocated. 938df8bae1dSRodney W. Grimes * 939df8bae1dSRodney W. Grimes * The new object and offset into that object 940df8bae1dSRodney W. Grimes * are returned in the source parameters. 941df8bae1dSRodney W. Grimes */ 942df8bae1dSRodney W. Grimes 94326f9a767SRodney W. Grimes void 9441b40f8c0SMatthew Dillon vm_object_shadow( 9451b40f8c0SMatthew Dillon vm_object_t *object, /* IN/OUT */ 9461b40f8c0SMatthew Dillon vm_ooffset_t *offset, /* IN/OUT */ 9471b40f8c0SMatthew Dillon vm_size_t length) 948df8bae1dSRodney W. Grimes { 949d031cff1SMatthew Dillon vm_object_t source; 950d031cff1SMatthew Dillon vm_object_t result; 951df8bae1dSRodney W. Grimes 9520cddd8f0SMatthew Dillon GIANT_REQUIRED; 953df8bae1dSRodney W. Grimes source = *object; 954df8bae1dSRodney W. Grimes 955df8bae1dSRodney W. Grimes /* 9569a2f6362SAlan Cox * Don't create the new object if the old object isn't shared. 9579a2f6362SAlan Cox */ 9589a2f6362SAlan Cox 959c7997d57SAlan Cox if (source != NULL && 960c7997d57SAlan Cox source->ref_count == 1 && 9619a2f6362SAlan Cox source->handle == NULL && 9629a2f6362SAlan Cox (source->type == OBJT_DEFAULT || 9639a2f6362SAlan Cox source->type == OBJT_SWAP)) 9649a2f6362SAlan Cox return; 9659a2f6362SAlan Cox 9669a2f6362SAlan Cox /* 967df8bae1dSRodney W. Grimes * Allocate a new object with the given length 968df8bae1dSRodney W. Grimes */ 969971dd342SAlfred Perlstein result = vm_object_allocate(OBJT_DEFAULT, length); 970971dd342SAlfred Perlstein KASSERT(result != NULL, ("vm_object_shadow: no object for shadowing")); 971df8bae1dSRodney W. Grimes 972df8bae1dSRodney W. Grimes /* 9730d94caffSDavid Greenman * The new object shadows the source object, adding a reference to it. 9740d94caffSDavid Greenman * Our caller changes his reference to point to the new object, 9750d94caffSDavid Greenman * removing a reference to the source object. Net result: no change 9760d94caffSDavid Greenman * of reference count. 9779b09fe24SMatthew Dillon * 9789b09fe24SMatthew Dillon * Try to optimize the result object's page color when shadowing 979956f3135SPhilippe Charnier * in order to maintain page coloring consistency in the combined 9809b09fe24SMatthew Dillon * shadowed object. 981df8bae1dSRodney W. Grimes */ 98224a1cce3SDavid Greenman result->backing_object = source; 983de5f6a77SJohn Dyson if (source) { 984de5f6a77SJohn Dyson TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list); 985eaf13dd7SJohn Dyson source->shadow_count++; 986eaf13dd7SJohn Dyson source->generation++; 9879b09fe24SMatthew Dillon result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & PQ_L2_MASK; 988de5f6a77SJohn Dyson } 989df8bae1dSRodney W. Grimes 990df8bae1dSRodney W. Grimes /* 9910d94caffSDavid Greenman * Store the offset into the source object, and fix up the offset into 9920d94caffSDavid Greenman * the new object. 993df8bae1dSRodney W. Grimes */ 994df8bae1dSRodney W. Grimes 99524a1cce3SDavid Greenman result->backing_object_offset = *offset; 996df8bae1dSRodney W. Grimes 997df8bae1dSRodney W. Grimes /* 998df8bae1dSRodney W. Grimes * Return the new things 999df8bae1dSRodney W. Grimes */ 1000df8bae1dSRodney W. Grimes 1001df8bae1dSRodney W. Grimes *offset = 0; 1002df8bae1dSRodney W. Grimes *object = result; 1003df8bae1dSRodney W. Grimes } 1004df8bae1dSRodney W. Grimes 10052ad1a3f7SMatthew Dillon #define OBSC_TEST_ALL_SHADOWED 0x0001 10062ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_NOWAIT 0x0002 10072ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_WAIT 0x0004 10082ad1a3f7SMatthew Dillon 10092ad1a3f7SMatthew Dillon static __inline int 10102ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op) 10112ad1a3f7SMatthew Dillon { 10122ad1a3f7SMatthew Dillon int s; 10132ad1a3f7SMatthew Dillon int r = 1; 10142ad1a3f7SMatthew Dillon vm_page_t p; 10152ad1a3f7SMatthew Dillon vm_object_t backing_object; 10162ad1a3f7SMatthew Dillon vm_pindex_t backing_offset_index; 10172ad1a3f7SMatthew Dillon 10182ad1a3f7SMatthew Dillon s = splvm(); 10190cddd8f0SMatthew Dillon GIANT_REQUIRED; 10202ad1a3f7SMatthew Dillon 10212ad1a3f7SMatthew Dillon backing_object = object->backing_object; 10222ad1a3f7SMatthew Dillon backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 10232ad1a3f7SMatthew Dillon 10242ad1a3f7SMatthew Dillon /* 10252ad1a3f7SMatthew Dillon * Initial conditions 10262ad1a3f7SMatthew Dillon */ 10272ad1a3f7SMatthew Dillon 10282ad1a3f7SMatthew Dillon if (op & OBSC_TEST_ALL_SHADOWED) { 10292ad1a3f7SMatthew Dillon /* 1030956f3135SPhilippe Charnier * We do not want to have to test for the existence of 10312ad1a3f7SMatthew Dillon * swap pages in the backing object. XXX but with the 10322ad1a3f7SMatthew Dillon * new swapper this would be pretty easy to do. 10332ad1a3f7SMatthew Dillon * 10342ad1a3f7SMatthew Dillon * XXX what about anonymous MAP_SHARED memory that hasn't 10352ad1a3f7SMatthew Dillon * been ZFOD faulted yet? If we do not test for this, the 10362ad1a3f7SMatthew Dillon * shadow test may succeed! XXX 10372ad1a3f7SMatthew Dillon */ 10382ad1a3f7SMatthew Dillon if (backing_object->type != OBJT_DEFAULT) { 10392ad1a3f7SMatthew Dillon splx(s); 10402ad1a3f7SMatthew Dillon return(0); 10412ad1a3f7SMatthew Dillon } 10422ad1a3f7SMatthew Dillon } 10432ad1a3f7SMatthew Dillon if (op & OBSC_COLLAPSE_WAIT) { 10442ad1a3f7SMatthew Dillon vm_object_set_flag(backing_object, OBJ_DEAD); 10452ad1a3f7SMatthew Dillon } 10462ad1a3f7SMatthew Dillon 10472ad1a3f7SMatthew Dillon /* 10482ad1a3f7SMatthew Dillon * Our scan 10492ad1a3f7SMatthew Dillon */ 10502ad1a3f7SMatthew Dillon 10512ad1a3f7SMatthew Dillon p = TAILQ_FIRST(&backing_object->memq); 10522ad1a3f7SMatthew Dillon while (p) { 10532ad1a3f7SMatthew Dillon vm_page_t next = TAILQ_NEXT(p, listq); 10542ad1a3f7SMatthew Dillon vm_pindex_t new_pindex = p->pindex - backing_offset_index; 10552ad1a3f7SMatthew Dillon 10562ad1a3f7SMatthew Dillon if (op & OBSC_TEST_ALL_SHADOWED) { 10572ad1a3f7SMatthew Dillon vm_page_t pp; 10582ad1a3f7SMatthew Dillon 10592ad1a3f7SMatthew Dillon /* 10602ad1a3f7SMatthew Dillon * Ignore pages outside the parent object's range 10612ad1a3f7SMatthew Dillon * and outside the parent object's mapping of the 10622ad1a3f7SMatthew Dillon * backing object. 10632ad1a3f7SMatthew Dillon * 10642ad1a3f7SMatthew Dillon * note that we do not busy the backing object's 10652ad1a3f7SMatthew Dillon * page. 10662ad1a3f7SMatthew Dillon */ 10672ad1a3f7SMatthew Dillon 10682ad1a3f7SMatthew Dillon if ( 10692ad1a3f7SMatthew Dillon p->pindex < backing_offset_index || 10702ad1a3f7SMatthew Dillon new_pindex >= object->size 10712ad1a3f7SMatthew Dillon ) { 10722ad1a3f7SMatthew Dillon p = next; 10732ad1a3f7SMatthew Dillon continue; 10742ad1a3f7SMatthew Dillon } 10752ad1a3f7SMatthew Dillon 10762ad1a3f7SMatthew Dillon /* 10772ad1a3f7SMatthew Dillon * See if the parent has the page or if the parent's 10782ad1a3f7SMatthew Dillon * object pager has the page. If the parent has the 10792ad1a3f7SMatthew Dillon * page but the page is not valid, the parent's 10802ad1a3f7SMatthew Dillon * object pager must have the page. 10812ad1a3f7SMatthew Dillon * 10822ad1a3f7SMatthew Dillon * If this fails, the parent does not completely shadow 10832ad1a3f7SMatthew Dillon * the object and we might as well give up now. 10842ad1a3f7SMatthew Dillon */ 10852ad1a3f7SMatthew Dillon 10862ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 10872ad1a3f7SMatthew Dillon if ( 10882ad1a3f7SMatthew Dillon (pp == NULL || pp->valid == 0) && 10892ad1a3f7SMatthew Dillon !vm_pager_has_page(object, new_pindex, NULL, NULL) 10902ad1a3f7SMatthew Dillon ) { 10912ad1a3f7SMatthew Dillon r = 0; 10922ad1a3f7SMatthew Dillon break; 10932ad1a3f7SMatthew Dillon } 10942ad1a3f7SMatthew Dillon } 10952ad1a3f7SMatthew Dillon 10962ad1a3f7SMatthew Dillon /* 10972ad1a3f7SMatthew Dillon * Check for busy page 10982ad1a3f7SMatthew Dillon */ 10992ad1a3f7SMatthew Dillon 11002ad1a3f7SMatthew Dillon if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) { 11012ad1a3f7SMatthew Dillon vm_page_t pp; 11022ad1a3f7SMatthew Dillon 11032ad1a3f7SMatthew Dillon if (op & OBSC_COLLAPSE_NOWAIT) { 11042ad1a3f7SMatthew Dillon if ( 11052ad1a3f7SMatthew Dillon (p->flags & PG_BUSY) || 11062ad1a3f7SMatthew Dillon !p->valid || 11072ad1a3f7SMatthew Dillon p->hold_count || 11082ad1a3f7SMatthew Dillon p->wire_count || 11092ad1a3f7SMatthew Dillon p->busy 11102ad1a3f7SMatthew Dillon ) { 11112ad1a3f7SMatthew Dillon p = next; 11122ad1a3f7SMatthew Dillon continue; 11132ad1a3f7SMatthew Dillon } 11142ad1a3f7SMatthew Dillon } else if (op & OBSC_COLLAPSE_WAIT) { 11152ad1a3f7SMatthew Dillon if (vm_page_sleep_busy(p, TRUE, "vmocol")) { 11162ad1a3f7SMatthew Dillon /* 11172ad1a3f7SMatthew Dillon * If we slept, anything could have 11182ad1a3f7SMatthew Dillon * happened. Since the object is 11192ad1a3f7SMatthew Dillon * marked dead, the backing offset 11202ad1a3f7SMatthew Dillon * should not have changed so we 11212ad1a3f7SMatthew Dillon * just restart our scan. 11222ad1a3f7SMatthew Dillon */ 11232ad1a3f7SMatthew Dillon p = TAILQ_FIRST(&backing_object->memq); 11242ad1a3f7SMatthew Dillon continue; 11252ad1a3f7SMatthew Dillon } 11262ad1a3f7SMatthew Dillon } 11272ad1a3f7SMatthew Dillon 11282ad1a3f7SMatthew Dillon /* 11292ad1a3f7SMatthew Dillon * Busy the page 11302ad1a3f7SMatthew Dillon */ 11312ad1a3f7SMatthew Dillon vm_page_busy(p); 11322ad1a3f7SMatthew Dillon 11332ad1a3f7SMatthew Dillon KASSERT( 11342ad1a3f7SMatthew Dillon p->object == backing_object, 11352ad1a3f7SMatthew Dillon ("vm_object_qcollapse(): object mismatch") 11362ad1a3f7SMatthew Dillon ); 11372ad1a3f7SMatthew Dillon 11382ad1a3f7SMatthew Dillon /* 11392ad1a3f7SMatthew Dillon * Destroy any associated swap 11402ad1a3f7SMatthew Dillon */ 11412ad1a3f7SMatthew Dillon if (backing_object->type == OBJT_SWAP) { 11422ad1a3f7SMatthew Dillon swap_pager_freespace( 11432ad1a3f7SMatthew Dillon backing_object, 11442ad1a3f7SMatthew Dillon p->pindex, 11452ad1a3f7SMatthew Dillon 1 11462ad1a3f7SMatthew Dillon ); 11472ad1a3f7SMatthew Dillon } 11482ad1a3f7SMatthew Dillon 11492ad1a3f7SMatthew Dillon if ( 11502ad1a3f7SMatthew Dillon p->pindex < backing_offset_index || 11512ad1a3f7SMatthew Dillon new_pindex >= object->size 11522ad1a3f7SMatthew Dillon ) { 11532ad1a3f7SMatthew Dillon /* 11542ad1a3f7SMatthew Dillon * Page is out of the parent object's range, we 11552ad1a3f7SMatthew Dillon * can simply destroy it. 11562ad1a3f7SMatthew Dillon */ 11572ad1a3f7SMatthew Dillon vm_page_protect(p, VM_PROT_NONE); 11582ad1a3f7SMatthew Dillon vm_page_free(p); 11592ad1a3f7SMatthew Dillon p = next; 11602ad1a3f7SMatthew Dillon continue; 11612ad1a3f7SMatthew Dillon } 11622ad1a3f7SMatthew Dillon 11632ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 11642ad1a3f7SMatthew Dillon if ( 11652ad1a3f7SMatthew Dillon pp != NULL || 11662ad1a3f7SMatthew Dillon vm_pager_has_page(object, new_pindex, NULL, NULL) 11672ad1a3f7SMatthew Dillon ) { 11682ad1a3f7SMatthew Dillon /* 11692ad1a3f7SMatthew Dillon * page already exists in parent OR swap exists 11702ad1a3f7SMatthew Dillon * for this location in the parent. Destroy 11712ad1a3f7SMatthew Dillon * the original page from the backing object. 11722ad1a3f7SMatthew Dillon * 11732ad1a3f7SMatthew Dillon * Leave the parent's page alone 11742ad1a3f7SMatthew Dillon */ 11752ad1a3f7SMatthew Dillon vm_page_protect(p, VM_PROT_NONE); 11762ad1a3f7SMatthew Dillon vm_page_free(p); 11772ad1a3f7SMatthew Dillon p = next; 11782ad1a3f7SMatthew Dillon continue; 11792ad1a3f7SMatthew Dillon } 11802ad1a3f7SMatthew Dillon 11812ad1a3f7SMatthew Dillon /* 11822ad1a3f7SMatthew Dillon * Page does not exist in parent, rename the 11832ad1a3f7SMatthew Dillon * page from the backing object to the main object. 1184d1bf5d56SMatthew Dillon * 1185d1bf5d56SMatthew Dillon * If the page was mapped to a process, it can remain 1186d1bf5d56SMatthew Dillon * mapped through the rename. 11872ad1a3f7SMatthew Dillon */ 11882ad1a3f7SMatthew Dillon if ((p->queue - p->pc) == PQ_CACHE) 11892ad1a3f7SMatthew Dillon vm_page_deactivate(p); 11902ad1a3f7SMatthew Dillon 11912ad1a3f7SMatthew Dillon vm_page_rename(p, object, new_pindex); 11922ad1a3f7SMatthew Dillon /* page automatically made dirty by rename */ 11932ad1a3f7SMatthew Dillon } 11942ad1a3f7SMatthew Dillon p = next; 11952ad1a3f7SMatthew Dillon } 11962ad1a3f7SMatthew Dillon splx(s); 11972ad1a3f7SMatthew Dillon return(r); 11982ad1a3f7SMatthew Dillon } 11992ad1a3f7SMatthew Dillon 1200df8bae1dSRodney W. Grimes 1201df8bae1dSRodney W. Grimes /* 12022fe6e4d7SDavid Greenman * this version of collapse allows the operation to occur earlier and 12032fe6e4d7SDavid Greenman * when paging_in_progress is true for an object... This is not a complete 12042fe6e4d7SDavid Greenman * operation, but should plug 99.9% of the rest of the leaks. 12052fe6e4d7SDavid Greenman */ 12062fe6e4d7SDavid Greenman static void 12071b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object) 12082fe6e4d7SDavid Greenman { 12092ad1a3f7SMatthew Dillon vm_object_t backing_object = object->backing_object; 12102fe6e4d7SDavid Greenman 12111b40f8c0SMatthew Dillon GIANT_REQUIRED; 12121b40f8c0SMatthew Dillon 12132fe6e4d7SDavid Greenman if (backing_object->ref_count != 1) 12142fe6e4d7SDavid Greenman return; 12152fe6e4d7SDavid Greenman 1216010cf3b9SDavid Greenman backing_object->ref_count += 2; 1217010cf3b9SDavid Greenman 12182ad1a3f7SMatthew Dillon vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT); 12191c7c3c6aSMatthew Dillon 1220010cf3b9SDavid Greenman backing_object->ref_count -= 2; 12212fe6e4d7SDavid Greenman } 12222fe6e4d7SDavid Greenman 1223df8bae1dSRodney W. Grimes /* 1224df8bae1dSRodney W. Grimes * vm_object_collapse: 1225df8bae1dSRodney W. Grimes * 1226df8bae1dSRodney W. Grimes * Collapse an object with the object backing it. 1227df8bae1dSRodney W. Grimes * Pages in the backing object are moved into the 1228df8bae1dSRodney W. Grimes * parent, and the backing object is deallocated. 1229df8bae1dSRodney W. Grimes */ 123026f9a767SRodney W. Grimes void 12311b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object) 1232df8bae1dSRodney W. Grimes { 12330cddd8f0SMatthew Dillon GIANT_REQUIRED; 123423955314SAlfred Perlstein 1235df8bae1dSRodney W. Grimes while (TRUE) { 12362ad1a3f7SMatthew Dillon vm_object_t backing_object; 12372ad1a3f7SMatthew Dillon 1238df8bae1dSRodney W. Grimes /* 1239df8bae1dSRodney W. Grimes * Verify that the conditions are right for collapse: 1240df8bae1dSRodney W. Grimes * 12412ad1a3f7SMatthew Dillon * The object exists and the backing object exists. 1242df8bae1dSRodney W. Grimes */ 12432fe6e4d7SDavid Greenman if (object == NULL) 12442ad1a3f7SMatthew Dillon break; 1245df8bae1dSRodney W. Grimes 124624a1cce3SDavid Greenman if ((backing_object = object->backing_object) == NULL) 12472ad1a3f7SMatthew Dillon break; 1248df8bae1dSRodney W. Grimes 1249f919ebdeSDavid Greenman /* 1250f919ebdeSDavid Greenman * we check the backing object first, because it is most likely 125124a1cce3SDavid Greenman * not collapsable. 1252f919ebdeSDavid Greenman */ 125324a1cce3SDavid Greenman if (backing_object->handle != NULL || 125424a1cce3SDavid Greenman (backing_object->type != OBJT_DEFAULT && 125524a1cce3SDavid Greenman backing_object->type != OBJT_SWAP) || 1256f919ebdeSDavid Greenman (backing_object->flags & OBJ_DEAD) || 125724a1cce3SDavid Greenman object->handle != NULL || 125824a1cce3SDavid Greenman (object->type != OBJT_DEFAULT && 125924a1cce3SDavid Greenman object->type != OBJT_SWAP) || 126024a1cce3SDavid Greenman (object->flags & OBJ_DEAD)) { 12612ad1a3f7SMatthew Dillon break; 126224a1cce3SDavid Greenman } 12639b4814bbSDavid Greenman 12642ad1a3f7SMatthew Dillon if ( 12652ad1a3f7SMatthew Dillon object->paging_in_progress != 0 || 12662ad1a3f7SMatthew Dillon backing_object->paging_in_progress != 0 12672ad1a3f7SMatthew Dillon ) { 1268b9921222SDavid Greenman vm_object_qcollapse(object); 12692ad1a3f7SMatthew Dillon break; 1270df8bae1dSRodney W. Grimes } 1271f919ebdeSDavid Greenman 127226f9a767SRodney W. Grimes /* 12730d94caffSDavid Greenman * We know that we can either collapse the backing object (if 12742ad1a3f7SMatthew Dillon * the parent is the only reference to it) or (perhaps) have 12752ad1a3f7SMatthew Dillon * the parent bypass the object if the parent happens to shadow 12762ad1a3f7SMatthew Dillon * all the resident pages in the entire backing object. 12772ad1a3f7SMatthew Dillon * 12782ad1a3f7SMatthew Dillon * This is ignoring pager-backed pages such as swap pages. 12792ad1a3f7SMatthew Dillon * vm_object_backing_scan fails the shadowing test in this 12802ad1a3f7SMatthew Dillon * case. 1281df8bae1dSRodney W. Grimes */ 1282df8bae1dSRodney W. Grimes 1283df8bae1dSRodney W. Grimes if (backing_object->ref_count == 1) { 1284df8bae1dSRodney W. Grimes /* 12852ad1a3f7SMatthew Dillon * If there is exactly one reference to the backing 12862ad1a3f7SMatthew Dillon * object, we can collapse it into the parent. 1287df8bae1dSRodney W. Grimes */ 1288df8bae1dSRodney W. Grimes 12892ad1a3f7SMatthew Dillon vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT); 1290df8bae1dSRodney W. Grimes 1291df8bae1dSRodney W. Grimes /* 1292df8bae1dSRodney W. Grimes * Move the pager from backing_object to object. 1293df8bae1dSRodney W. Grimes */ 1294df8bae1dSRodney W. Grimes 129524a1cce3SDavid Greenman if (backing_object->type == OBJT_SWAP) { 1296d474eaaaSDoug Rabson vm_object_pip_add(backing_object, 1); 129724a1cce3SDavid Greenman 129824a1cce3SDavid Greenman /* 12991c7c3c6aSMatthew Dillon * scrap the paging_offset junk and do a 13001c7c3c6aSMatthew Dillon * discrete copy. This also removes major 13011c7c3c6aSMatthew Dillon * assumptions about how the swap-pager 13021c7c3c6aSMatthew Dillon * works from where it doesn't belong. The 13031c7c3c6aSMatthew Dillon * new swapper is able to optimize the 13041c7c3c6aSMatthew Dillon * destroy-source case. 130524a1cce3SDavid Greenman */ 13061c7c3c6aSMatthew Dillon 13071c7c3c6aSMatthew Dillon vm_object_pip_add(object, 1); 13081c7c3c6aSMatthew Dillon swap_pager_copy( 13091c7c3c6aSMatthew Dillon backing_object, 13101c7c3c6aSMatthew Dillon object, 13111c7c3c6aSMatthew Dillon OFF_TO_IDX(object->backing_object_offset), TRUE); 1312f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 1313c0503609SDavid Greenman 1314f919ebdeSDavid Greenman vm_object_pip_wakeup(backing_object); 1315c0503609SDavid Greenman } 1316df8bae1dSRodney W. Grimes /* 1317df8bae1dSRodney W. Grimes * Object now shadows whatever backing_object did. 13182ad1a3f7SMatthew Dillon * Note that the reference to 13192ad1a3f7SMatthew Dillon * backing_object->backing_object moves from within 13202ad1a3f7SMatthew Dillon * backing_object to within object. 1321df8bae1dSRodney W. Grimes */ 1322df8bae1dSRodney W. Grimes 13232ad1a3f7SMatthew Dillon TAILQ_REMOVE( 13242ad1a3f7SMatthew Dillon &object->backing_object->shadow_head, 13252ad1a3f7SMatthew Dillon object, 13262ad1a3f7SMatthew Dillon shadow_list 13272ad1a3f7SMatthew Dillon ); 1328eaf13dd7SJohn Dyson object->backing_object->shadow_count--; 1329eaf13dd7SJohn Dyson object->backing_object->generation++; 1330de5f6a77SJohn Dyson if (backing_object->backing_object) { 13312ad1a3f7SMatthew Dillon TAILQ_REMOVE( 13322ad1a3f7SMatthew Dillon &backing_object->backing_object->shadow_head, 13332ad1a3f7SMatthew Dillon backing_object, 13342ad1a3f7SMatthew Dillon shadow_list 13352ad1a3f7SMatthew Dillon ); 1336eaf13dd7SJohn Dyson backing_object->backing_object->shadow_count--; 1337eaf13dd7SJohn Dyson backing_object->backing_object->generation++; 1338de5f6a77SJohn Dyson } 133924a1cce3SDavid Greenman object->backing_object = backing_object->backing_object; 1340de5f6a77SJohn Dyson if (object->backing_object) { 13412ad1a3f7SMatthew Dillon TAILQ_INSERT_TAIL( 13422ad1a3f7SMatthew Dillon &object->backing_object->shadow_head, 13432ad1a3f7SMatthew Dillon object, 13442ad1a3f7SMatthew Dillon shadow_list 13452ad1a3f7SMatthew Dillon ); 1346eaf13dd7SJohn Dyson object->backing_object->shadow_count++; 1347eaf13dd7SJohn Dyson object->backing_object->generation++; 1348de5f6a77SJohn Dyson } 13492fe6e4d7SDavid Greenman 13502ad1a3f7SMatthew Dillon object->backing_object_offset += 13512ad1a3f7SMatthew Dillon backing_object->backing_object_offset; 13522ad1a3f7SMatthew Dillon 1353df8bae1dSRodney W. Grimes /* 1354df8bae1dSRodney W. Grimes * Discard backing_object. 1355df8bae1dSRodney W. Grimes * 13560d94caffSDavid Greenman * Since the backing object has no pages, no pager left, 13570d94caffSDavid Greenman * and no object references within it, all that is 13580d94caffSDavid Greenman * necessary is to dispose of it. 1359df8bae1dSRodney W. Grimes */ 1360df8bae1dSRodney W. Grimes 13612ad1a3f7SMatthew Dillon TAILQ_REMOVE( 13622ad1a3f7SMatthew Dillon &vm_object_list, 13632ad1a3f7SMatthew Dillon backing_object, 13642ad1a3f7SMatthew Dillon object_list 13652ad1a3f7SMatthew Dillon ); 1366df8bae1dSRodney W. Grimes vm_object_count--; 1367df8bae1dSRodney W. Grimes 136899448ed1SJohn Dyson zfree(obj_zone, backing_object); 1369df8bae1dSRodney W. Grimes 1370df8bae1dSRodney W. Grimes object_collapses++; 13710d94caffSDavid Greenman } else { 137295e5e988SJohn Dyson vm_object_t new_backing_object; 1373df8bae1dSRodney W. Grimes 1374df8bae1dSRodney W. Grimes /* 13752ad1a3f7SMatthew Dillon * If we do not entirely shadow the backing object, 13762ad1a3f7SMatthew Dillon * there is nothing we can do so we give up. 1377df8bae1dSRodney W. Grimes */ 1378df8bae1dSRodney W. Grimes 13792ad1a3f7SMatthew Dillon if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) { 13802ad1a3f7SMatthew Dillon break; 138124a1cce3SDavid Greenman } 1382df8bae1dSRodney W. Grimes 1383df8bae1dSRodney W. Grimes /* 13840d94caffSDavid Greenman * Make the parent shadow the next object in the 13850d94caffSDavid Greenman * chain. Deallocating backing_object will not remove 13860d94caffSDavid Greenman * it, since its reference count is at least 2. 1387df8bae1dSRodney W. Grimes */ 1388df8bae1dSRodney W. Grimes 13892ad1a3f7SMatthew Dillon TAILQ_REMOVE( 13902ad1a3f7SMatthew Dillon &backing_object->shadow_head, 13912ad1a3f7SMatthew Dillon object, 13922ad1a3f7SMatthew Dillon shadow_list 13932ad1a3f7SMatthew Dillon ); 1394eaf13dd7SJohn Dyson backing_object->shadow_count--; 1395eaf13dd7SJohn Dyson backing_object->generation++; 139695e5e988SJohn Dyson 139795e5e988SJohn Dyson new_backing_object = backing_object->backing_object; 13988aef1712SMatthew Dillon if ((object->backing_object = new_backing_object) != NULL) { 139995e5e988SJohn Dyson vm_object_reference(new_backing_object); 14002ad1a3f7SMatthew Dillon TAILQ_INSERT_TAIL( 14012ad1a3f7SMatthew Dillon &new_backing_object->shadow_head, 14022ad1a3f7SMatthew Dillon object, 14032ad1a3f7SMatthew Dillon shadow_list 14042ad1a3f7SMatthew Dillon ); 1405eaf13dd7SJohn Dyson new_backing_object->shadow_count++; 1406eaf13dd7SJohn Dyson new_backing_object->generation++; 140795e5e988SJohn Dyson object->backing_object_offset += 140895e5e988SJohn Dyson backing_object->backing_object_offset; 1409de5f6a77SJohn Dyson } 1410df8bae1dSRodney W. Grimes 1411df8bae1dSRodney W. Grimes /* 14120d94caffSDavid Greenman * Drop the reference count on backing_object. Since 14130d94caffSDavid Greenman * its ref_count was at least 2, it will not vanish; 1414eaf13dd7SJohn Dyson * so we don't need to call vm_object_deallocate, but 1415eaf13dd7SJohn Dyson * we do anyway. 1416df8bae1dSRodney W. Grimes */ 141795e5e988SJohn Dyson vm_object_deallocate(backing_object); 1418df8bae1dSRodney W. Grimes object_bypasses++; 1419df8bae1dSRodney W. Grimes } 1420df8bae1dSRodney W. Grimes 1421df8bae1dSRodney W. Grimes /* 1422df8bae1dSRodney W. Grimes * Try again with this object's new backing object. 1423df8bae1dSRodney W. Grimes */ 1424df8bae1dSRodney W. Grimes } 1425df8bae1dSRodney W. Grimes } 1426df8bae1dSRodney W. Grimes 1427df8bae1dSRodney W. Grimes /* 1428df8bae1dSRodney W. Grimes * vm_object_page_remove: [internal] 1429df8bae1dSRodney W. Grimes * 1430df8bae1dSRodney W. Grimes * Removes all physical pages in the specified 1431df8bae1dSRodney W. Grimes * object range from the object's list of pages. 1432df8bae1dSRodney W. Grimes * 1433df8bae1dSRodney W. Grimes * The object must be locked. 1434df8bae1dSRodney W. Grimes */ 143526f9a767SRodney W. Grimes void 14361b40f8c0SMatthew Dillon vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, boolean_t clean_only) 1437df8bae1dSRodney W. Grimes { 1438d031cff1SMatthew Dillon vm_page_t p, next; 1439a316d390SJohn Dyson unsigned int size; 1440f5ef029eSPoul-Henning Kamp int all; 1441df8bae1dSRodney W. Grimes 14420cddd8f0SMatthew Dillon GIANT_REQUIRED; 144323955314SAlfred Perlstein 1444a1a54e9fSAlan Cox if (object == NULL || 1445a1a54e9fSAlan Cox object->resident_page_count == 0) 1446df8bae1dSRodney W. Grimes return; 1447df8bae1dSRodney W. Grimes 144895e5e988SJohn Dyson all = ((end == 0) && (start == 0)); 144995e5e988SJohn Dyson 14508b03c8edSMatthew Dillon /* 14518b03c8edSMatthew Dillon * Since physically-backed objects do not use managed pages, we can't 14528b03c8edSMatthew Dillon * remove pages from the object (we must instead remove the page 14538b03c8edSMatthew Dillon * references, and then destroy the object). 14548b03c8edSMatthew Dillon */ 14558b03c8edSMatthew Dillon KASSERT(object->type != OBJT_PHYS, ("attempt to remove pages from a physical object")); 14568b03c8edSMatthew Dillon 1457d474eaaaSDoug Rabson vm_object_pip_add(object, 1); 145826f9a767SRodney W. Grimes again: 145926f9a767SRodney W. Grimes size = end - start; 1460a1a54e9fSAlan Cox if (all || size > object->resident_page_count / 4) { 1461b18bfc3dSJohn Dyson for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) { 1462b18bfc3dSJohn Dyson next = TAILQ_NEXT(p, listq); 146395e5e988SJohn Dyson if (all || ((start <= p->pindex) && (p->pindex < end))) { 1464bd7e5f99SJohn Dyson if (p->wire_count != 0) { 1465bd7e5f99SJohn Dyson vm_page_protect(p, VM_PROT_NONE); 1466ce65e68cSDavid Greenman if (!clean_only) 1467bd7e5f99SJohn Dyson p->valid = 0; 14680d94caffSDavid Greenman continue; 14690d94caffSDavid Greenman } 14700891ef4cSJohn Dyson 1471b18bfc3dSJohn Dyson /* 1472b18bfc3dSJohn Dyson * The busy flags are only cleared at 1473b18bfc3dSJohn Dyson * interrupt -- minimize the spl transitions 1474b18bfc3dSJohn Dyson */ 1475ffc82b0aSJohn Dyson 14761c7c3c6aSMatthew Dillon if (vm_page_sleep_busy(p, TRUE, "vmopar")) 147726f9a767SRodney W. Grimes goto again; 14780891ef4cSJohn Dyson 14798f9110f6SJohn Dyson if (clean_only && p->valid) { 14807c1f6cedSDavid Greenman vm_page_test_dirty(p); 14817c1f6cedSDavid Greenman if (p->valid & p->dirty) 14827c1f6cedSDavid Greenman continue; 14837c1f6cedSDavid Greenman } 1484eaf13dd7SJohn Dyson 1485e69763a3SDoug Rabson vm_page_busy(p); 1486f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 1487df8bae1dSRodney W. Grimes vm_page_free(p); 148826f9a767SRodney W. Grimes } 148926f9a767SRodney W. Grimes } 149026f9a767SRodney W. Grimes } else { 149126f9a767SRodney W. Grimes while (size > 0) { 1492bd7e5f99SJohn Dyson if ((p = vm_page_lookup(object, start)) != 0) { 1493eaf13dd7SJohn Dyson 1494bd7e5f99SJohn Dyson if (p->wire_count != 0) { 1495bd7e5f99SJohn Dyson vm_page_protect(p, VM_PROT_NONE); 1496ce65e68cSDavid Greenman if (!clean_only) 1497ce65e68cSDavid Greenman p->valid = 0; 1498bd7e5f99SJohn Dyson start += 1; 1499bd7e5f99SJohn Dyson size -= 1; 1500bd7e5f99SJohn Dyson continue; 15010d94caffSDavid Greenman } 1502eaf13dd7SJohn Dyson 1503b18bfc3dSJohn Dyson /* 1504b18bfc3dSJohn Dyson * The busy flags are only cleared at 1505b18bfc3dSJohn Dyson * interrupt -- minimize the spl transitions 1506b18bfc3dSJohn Dyson */ 15071c7c3c6aSMatthew Dillon if (vm_page_sleep_busy(p, TRUE, "vmopar")) 150826f9a767SRodney W. Grimes goto again; 1509eaf13dd7SJohn Dyson 15108f9110f6SJohn Dyson if (clean_only && p->valid) { 15117c1f6cedSDavid Greenman vm_page_test_dirty(p); 1512bd7e5f99SJohn Dyson if (p->valid & p->dirty) { 1513bd7e5f99SJohn Dyson start += 1; 1514bd7e5f99SJohn Dyson size -= 1; 15157c1f6cedSDavid Greenman continue; 15167c1f6cedSDavid Greenman } 1517bd7e5f99SJohn Dyson } 1518eaf13dd7SJohn Dyson 1519e69763a3SDoug Rabson vm_page_busy(p); 1520f919ebdeSDavid Greenman vm_page_protect(p, VM_PROT_NONE); 152126f9a767SRodney W. Grimes vm_page_free(p); 152226f9a767SRodney W. Grimes } 1523a316d390SJohn Dyson start += 1; 1524a316d390SJohn Dyson size -= 1; 1525df8bae1dSRodney W. Grimes } 1526df8bae1dSRodney W. Grimes } 1527f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 1528c0503609SDavid Greenman } 1529df8bae1dSRodney W. Grimes 1530df8bae1dSRodney W. Grimes /* 1531df8bae1dSRodney W. Grimes * Routine: vm_object_coalesce 1532df8bae1dSRodney W. Grimes * Function: Coalesces two objects backing up adjoining 1533df8bae1dSRodney W. Grimes * regions of memory into a single object. 1534df8bae1dSRodney W. Grimes * 1535df8bae1dSRodney W. Grimes * returns TRUE if objects were combined. 1536df8bae1dSRodney W. Grimes * 1537df8bae1dSRodney W. Grimes * NOTE: Only works at the moment if the second object is NULL - 1538df8bae1dSRodney W. Grimes * if it's not, which object do we lock first? 1539df8bae1dSRodney W. Grimes * 1540df8bae1dSRodney W. Grimes * Parameters: 1541df8bae1dSRodney W. Grimes * prev_object First object to coalesce 1542df8bae1dSRodney W. Grimes * prev_offset Offset into prev_object 1543df8bae1dSRodney W. Grimes * next_object Second object into coalesce 1544df8bae1dSRodney W. Grimes * next_offset Offset into next_object 1545df8bae1dSRodney W. Grimes * 1546df8bae1dSRodney W. Grimes * prev_size Size of reference to prev_object 1547df8bae1dSRodney W. Grimes * next_size Size of reference to next_object 1548df8bae1dSRodney W. Grimes * 1549df8bae1dSRodney W. Grimes * Conditions: 1550df8bae1dSRodney W. Grimes * The object must *not* be locked. 1551df8bae1dSRodney W. Grimes */ 15520d94caffSDavid Greenman boolean_t 15531b40f8c0SMatthew Dillon vm_object_coalesce(vm_object_t prev_object, vm_pindex_t prev_pindex, vm_size_t prev_size, vm_size_t next_size) 1554df8bae1dSRodney W. Grimes { 1555ea41812fSAlan Cox vm_pindex_t next_pindex; 1556df8bae1dSRodney W. Grimes 15570cddd8f0SMatthew Dillon GIANT_REQUIRED; 155823955314SAlfred Perlstein 1559df8bae1dSRodney W. Grimes if (prev_object == NULL) { 1560df8bae1dSRodney W. Grimes return (TRUE); 1561df8bae1dSRodney W. Grimes } 1562df8bae1dSRodney W. Grimes 15634112823fSMatthew Dillon if (prev_object->type != OBJT_DEFAULT && 15644112823fSMatthew Dillon prev_object->type != OBJT_SWAP) { 156530dcfc09SJohn Dyson return (FALSE); 156630dcfc09SJohn Dyson } 156730dcfc09SJohn Dyson 1568df8bae1dSRodney W. Grimes /* 1569df8bae1dSRodney W. Grimes * Try to collapse the object first 1570df8bae1dSRodney W. Grimes */ 1571df8bae1dSRodney W. Grimes vm_object_collapse(prev_object); 1572df8bae1dSRodney W. Grimes 1573df8bae1dSRodney W. Grimes /* 15740d94caffSDavid Greenman * Can't coalesce if: . more than one reference . paged out . shadows 15750d94caffSDavid Greenman * another object . has a copy elsewhere (any of which mean that the 15760d94caffSDavid Greenman * pages not mapped to prev_entry may be in use anyway) 1577df8bae1dSRodney W. Grimes */ 1578df8bae1dSRodney W. Grimes 15798cc7e047SJohn Dyson if (prev_object->backing_object != NULL) { 1580df8bae1dSRodney W. Grimes return (FALSE); 1581df8bae1dSRodney W. Grimes } 1582a316d390SJohn Dyson 1583a316d390SJohn Dyson prev_size >>= PAGE_SHIFT; 1584a316d390SJohn Dyson next_size >>= PAGE_SHIFT; 1585ea41812fSAlan Cox next_pindex = prev_pindex + prev_size; 15868cc7e047SJohn Dyson 15878cc7e047SJohn Dyson if ((prev_object->ref_count > 1) && 1588ea41812fSAlan Cox (prev_object->size != next_pindex)) { 15898cc7e047SJohn Dyson return (FALSE); 15908cc7e047SJohn Dyson } 15918cc7e047SJohn Dyson 1592df8bae1dSRodney W. Grimes /* 15930d94caffSDavid Greenman * Remove any pages that may still be in the object from a previous 15940d94caffSDavid Greenman * deallocation. 1595df8bae1dSRodney W. Grimes */ 1596ea41812fSAlan Cox if (next_pindex < prev_object->size) { 1597df8bae1dSRodney W. Grimes vm_object_page_remove(prev_object, 1598ea41812fSAlan Cox next_pindex, 1599ea41812fSAlan Cox next_pindex + next_size, FALSE); 1600ea41812fSAlan Cox if (prev_object->type == OBJT_SWAP) 1601ea41812fSAlan Cox swap_pager_freespace(prev_object, 1602ea41812fSAlan Cox next_pindex, next_size); 1603ea41812fSAlan Cox } 1604df8bae1dSRodney W. Grimes 1605df8bae1dSRodney W. Grimes /* 1606df8bae1dSRodney W. Grimes * Extend the object if necessary. 1607df8bae1dSRodney W. Grimes */ 1608ea41812fSAlan Cox if (next_pindex + next_size > prev_object->size) 1609ea41812fSAlan Cox prev_object->size = next_pindex + next_size; 1610df8bae1dSRodney W. Grimes 1611df8bae1dSRodney W. Grimes return (TRUE); 1612df8bae1dSRodney W. Grimes } 1613df8bae1dSRodney W. Grimes 1614c7c34a24SBruce Evans #include "opt_ddb.h" 1615c3cb3e12SDavid Greenman #ifdef DDB 1616c7c34a24SBruce Evans #include <sys/kernel.h> 1617c7c34a24SBruce Evans 1618ce9edcf5SPoul-Henning Kamp #include <sys/cons.h> 1619c7c34a24SBruce Evans 1620c7c34a24SBruce Evans #include <ddb/ddb.h> 1621c7c34a24SBruce Evans 1622cac597e4SBruce Evans static int 16231b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry) 1624a1f6d91cSDavid Greenman { 1625a1f6d91cSDavid Greenman vm_map_t tmpm; 1626a1f6d91cSDavid Greenman vm_map_entry_t tmpe; 1627a1f6d91cSDavid Greenman vm_object_t obj; 1628a1f6d91cSDavid Greenman int entcount; 1629a1f6d91cSDavid Greenman 1630a1f6d91cSDavid Greenman if (map == 0) 1631a1f6d91cSDavid Greenman return 0; 1632a1f6d91cSDavid Greenman 1633a1f6d91cSDavid Greenman if (entry == 0) { 1634a1f6d91cSDavid Greenman tmpe = map->header.next; 1635a1f6d91cSDavid Greenman entcount = map->nentries; 1636a1f6d91cSDavid Greenman while (entcount-- && (tmpe != &map->header)) { 1637a1f6d91cSDavid Greenman if( _vm_object_in_map(map, object, tmpe)) { 1638a1f6d91cSDavid Greenman return 1; 1639a1f6d91cSDavid Greenman } 1640a1f6d91cSDavid Greenman tmpe = tmpe->next; 1641a1f6d91cSDavid Greenman } 16429fdfe602SMatthew Dillon } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 16439fdfe602SMatthew Dillon tmpm = entry->object.sub_map; 1644a1f6d91cSDavid Greenman tmpe = tmpm->header.next; 1645a1f6d91cSDavid Greenman entcount = tmpm->nentries; 1646a1f6d91cSDavid Greenman while (entcount-- && tmpe != &tmpm->header) { 1647a1f6d91cSDavid Greenman if( _vm_object_in_map(tmpm, object, tmpe)) { 1648a1f6d91cSDavid Greenman return 1; 1649a1f6d91cSDavid Greenman } 1650a1f6d91cSDavid Greenman tmpe = tmpe->next; 1651a1f6d91cSDavid Greenman } 16528aef1712SMatthew Dillon } else if ((obj = entry->object.vm_object) != NULL) { 165324a1cce3SDavid Greenman for (; obj; obj = obj->backing_object) 1654a1f6d91cSDavid Greenman if( obj == object) { 1655a1f6d91cSDavid Greenman return 1; 1656a1f6d91cSDavid Greenman } 1657a1f6d91cSDavid Greenman } 1658a1f6d91cSDavid Greenman return 0; 1659a1f6d91cSDavid Greenman } 1660a1f6d91cSDavid Greenman 1661cac597e4SBruce Evans static int 16621b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object) 1663a1f6d91cSDavid Greenman { 1664a1f6d91cSDavid Greenman struct proc *p; 16651005a129SJohn Baldwin 166660517fd1SJohn Baldwin /* sx_slock(&allproc_lock); */ 1667fc2ffbe6SPoul-Henning Kamp LIST_FOREACH(p, &allproc, p_list) { 1668a1f6d91cSDavid Greenman if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 1669a1f6d91cSDavid Greenman continue; 1670553629ebSJake Burkholder if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) { 167160517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 1672a1f6d91cSDavid Greenman return 1; 1673a1f6d91cSDavid Greenman } 1674553629ebSJake Burkholder } 167560517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 1676a1f6d91cSDavid Greenman if( _vm_object_in_map( kernel_map, object, 0)) 1677a1f6d91cSDavid Greenman return 1; 1678a1f6d91cSDavid Greenman if( _vm_object_in_map( kmem_map, object, 0)) 1679a1f6d91cSDavid Greenman return 1; 1680a1f6d91cSDavid Greenman if( _vm_object_in_map( pager_map, object, 0)) 1681a1f6d91cSDavid Greenman return 1; 1682a1f6d91cSDavid Greenman if( _vm_object_in_map( buffer_map, object, 0)) 1683a1f6d91cSDavid Greenman return 1; 1684a1f6d91cSDavid Greenman return 0; 1685a1f6d91cSDavid Greenman } 1686a1f6d91cSDavid Greenman 1687c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check) 1688f708ef1bSPoul-Henning Kamp { 1689a1f6d91cSDavid Greenman vm_object_t object; 1690a1f6d91cSDavid Greenman 1691a1f6d91cSDavid Greenman /* 1692a1f6d91cSDavid Greenman * make sure that internal objs are in a map somewhere 1693a1f6d91cSDavid Greenman * and none have zero ref counts. 1694a1f6d91cSDavid Greenman */ 1695cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 169624a1cce3SDavid Greenman if (object->handle == NULL && 169724a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 1698a1f6d91cSDavid Greenman if (object->ref_count == 0) { 16993efc015bSPeter Wemm db_printf("vmochk: internal obj has zero ref count: %ld\n", 17003efc015bSPeter Wemm (long)object->size); 1701a1f6d91cSDavid Greenman } 1702a1f6d91cSDavid Greenman if (!vm_object_in_map(object)) { 1703fc62ef1fSBruce Evans db_printf( 1704fc62ef1fSBruce Evans "vmochk: internal obj is not in a map: " 1705fc62ef1fSBruce Evans "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 1706fc62ef1fSBruce Evans object->ref_count, (u_long)object->size, 1707fc62ef1fSBruce Evans (u_long)object->size, 1708fc62ef1fSBruce Evans (void *)object->backing_object); 1709a1f6d91cSDavid Greenman } 1710a1f6d91cSDavid Greenman } 1711a1f6d91cSDavid Greenman } 1712a1f6d91cSDavid Greenman } 1713a1f6d91cSDavid Greenman 171426f9a767SRodney W. Grimes /* 1715df8bae1dSRodney W. Grimes * vm_object_print: [ debug ] 1716df8bae1dSRodney W. Grimes */ 1717c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static) 1718df8bae1dSRodney W. Grimes { 1719c7c34a24SBruce Evans /* XXX convert args. */ 1720c7c34a24SBruce Evans vm_object_t object = (vm_object_t)addr; 1721c7c34a24SBruce Evans boolean_t full = have_addr; 1722c7c34a24SBruce Evans 1723d031cff1SMatthew Dillon vm_page_t p; 1724df8bae1dSRodney W. Grimes 1725c7c34a24SBruce Evans /* XXX count is an (unused) arg. Avoid shadowing it. */ 1726c7c34a24SBruce Evans #define count was_count 1727c7c34a24SBruce Evans 1728d031cff1SMatthew Dillon int count; 1729df8bae1dSRodney W. Grimes 1730df8bae1dSRodney W. Grimes if (object == NULL) 1731df8bae1dSRodney W. Grimes return; 1732df8bae1dSRodney W. Grimes 1733eb95adefSBruce Evans db_iprintf( 1734eb95adefSBruce Evans "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n", 1735eb95adefSBruce Evans object, (int)object->type, (u_long)object->size, 1736eb95adefSBruce Evans object->resident_page_count, object->ref_count, object->flags); 1737eb95adefSBruce Evans /* 17381c7c3c6aSMatthew Dillon * XXX no %qd in kernel. Truncate object->backing_object_offset. 1739eb95adefSBruce Evans */ 17401c7c3c6aSMatthew Dillon db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n", 17411c7c3c6aSMatthew Dillon object->shadow_count, 1742eb95adefSBruce Evans object->backing_object ? object->backing_object->ref_count : 0, 1743eb95adefSBruce Evans object->backing_object, (long)object->backing_object_offset); 1744df8bae1dSRodney W. Grimes 1745df8bae1dSRodney W. Grimes if (!full) 1746df8bae1dSRodney W. Grimes return; 1747df8bae1dSRodney W. Grimes 1748c7c34a24SBruce Evans db_indent += 2; 1749df8bae1dSRodney W. Grimes count = 0; 1750fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(p, &object->memq, listq) { 1751df8bae1dSRodney W. Grimes if (count == 0) 1752c7c34a24SBruce Evans db_iprintf("memory:="); 1753df8bae1dSRodney W. Grimes else if (count == 6) { 1754c7c34a24SBruce Evans db_printf("\n"); 1755c7c34a24SBruce Evans db_iprintf(" ..."); 1756df8bae1dSRodney W. Grimes count = 0; 1757df8bae1dSRodney W. Grimes } else 1758c7c34a24SBruce Evans db_printf(","); 1759df8bae1dSRodney W. Grimes count++; 1760df8bae1dSRodney W. Grimes 1761c7c34a24SBruce Evans db_printf("(off=0x%lx,page=0x%lx)", 1762a316d390SJohn Dyson (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p)); 1763df8bae1dSRodney W. Grimes } 1764df8bae1dSRodney W. Grimes if (count != 0) 1765c7c34a24SBruce Evans db_printf("\n"); 1766c7c34a24SBruce Evans db_indent -= 2; 1767df8bae1dSRodney W. Grimes } 17685070c7f8SJohn Dyson 1769c7c34a24SBruce Evans /* XXX. */ 1770c7c34a24SBruce Evans #undef count 1771c7c34a24SBruce Evans 1772c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */ 17735070c7f8SJohn Dyson void 17741b40f8c0SMatthew Dillon vm_object_print( 17751b40f8c0SMatthew Dillon /* db_expr_t */ long addr, 17761b40f8c0SMatthew Dillon boolean_t have_addr, 17771b40f8c0SMatthew Dillon /* db_expr_t */ long count, 17781b40f8c0SMatthew Dillon char *modif) 1779c7c34a24SBruce Evans { 1780c7c34a24SBruce Evans vm_object_print_static(addr, have_addr, count, modif); 1781c7c34a24SBruce Evans } 1782c7c34a24SBruce Evans 1783c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages) 17845070c7f8SJohn Dyson { 17855070c7f8SJohn Dyson vm_object_t object; 17865070c7f8SJohn Dyson int nl = 0; 17875070c7f8SJohn Dyson int c; 1788cc64b484SAlfred Perlstein 1789cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 17905070c7f8SJohn Dyson vm_pindex_t idx, fidx; 17915070c7f8SJohn Dyson vm_pindex_t osize; 17925070c7f8SJohn Dyson vm_offset_t pa = -1, padiff; 17935070c7f8SJohn Dyson int rcount; 17945070c7f8SJohn Dyson vm_page_t m; 17955070c7f8SJohn Dyson 1796fc62ef1fSBruce Evans db_printf("new object: %p\n", (void *)object); 17975070c7f8SJohn Dyson if ( nl > 18) { 17985070c7f8SJohn Dyson c = cngetc(); 17995070c7f8SJohn Dyson if (c != ' ') 18005070c7f8SJohn Dyson return; 18015070c7f8SJohn Dyson nl = 0; 18025070c7f8SJohn Dyson } 18035070c7f8SJohn Dyson nl++; 18045070c7f8SJohn Dyson rcount = 0; 18055070c7f8SJohn Dyson fidx = 0; 18065070c7f8SJohn Dyson osize = object->size; 18075070c7f8SJohn Dyson if (osize > 128) 18085070c7f8SJohn Dyson osize = 128; 18095070c7f8SJohn Dyson for (idx = 0; idx < osize; idx++) { 18105070c7f8SJohn Dyson m = vm_page_lookup(object, idx); 18115070c7f8SJohn Dyson if (m == NULL) { 18125070c7f8SJohn Dyson if (rcount) { 18133efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 18143efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 18155070c7f8SJohn Dyson if ( nl > 18) { 18165070c7f8SJohn Dyson c = cngetc(); 18175070c7f8SJohn Dyson if (c != ' ') 18185070c7f8SJohn Dyson return; 18195070c7f8SJohn Dyson nl = 0; 18205070c7f8SJohn Dyson } 18215070c7f8SJohn Dyson nl++; 18225070c7f8SJohn Dyson rcount = 0; 18235070c7f8SJohn Dyson } 18245070c7f8SJohn Dyson continue; 18255070c7f8SJohn Dyson } 18265070c7f8SJohn Dyson 18275070c7f8SJohn Dyson 18285070c7f8SJohn Dyson if (rcount && 18295070c7f8SJohn Dyson (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) { 18305070c7f8SJohn Dyson ++rcount; 18315070c7f8SJohn Dyson continue; 18325070c7f8SJohn Dyson } 18335070c7f8SJohn Dyson if (rcount) { 18345070c7f8SJohn Dyson padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m); 18355070c7f8SJohn Dyson padiff >>= PAGE_SHIFT; 18365070c7f8SJohn Dyson padiff &= PQ_L2_MASK; 18375070c7f8SJohn Dyson if (padiff == 0) { 18385070c7f8SJohn Dyson pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE; 18395070c7f8SJohn Dyson ++rcount; 18405070c7f8SJohn Dyson continue; 18415070c7f8SJohn Dyson } 18423efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)", 18433efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 18443efc015bSPeter Wemm db_printf("pd(%ld)\n", (long)padiff); 18455070c7f8SJohn Dyson if ( nl > 18) { 18465070c7f8SJohn Dyson c = cngetc(); 18475070c7f8SJohn Dyson if (c != ' ') 18485070c7f8SJohn Dyson return; 18495070c7f8SJohn Dyson nl = 0; 18505070c7f8SJohn Dyson } 18515070c7f8SJohn Dyson nl++; 18525070c7f8SJohn Dyson } 18535070c7f8SJohn Dyson fidx = idx; 18545070c7f8SJohn Dyson pa = VM_PAGE_TO_PHYS(m); 18555070c7f8SJohn Dyson rcount = 1; 18565070c7f8SJohn Dyson } 18575070c7f8SJohn Dyson if (rcount) { 18583efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 18593efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 18605070c7f8SJohn Dyson if ( nl > 18) { 18615070c7f8SJohn Dyson c = cngetc(); 18625070c7f8SJohn Dyson if (c != ' ') 18635070c7f8SJohn Dyson return; 18645070c7f8SJohn Dyson nl = 0; 18655070c7f8SJohn Dyson } 18665070c7f8SJohn Dyson nl++; 18675070c7f8SJohn Dyson } 18685070c7f8SJohn Dyson } 18695070c7f8SJohn Dyson } 1870c3cb3e12SDavid Greenman #endif /* DDB */ 1871