160727d8bSWarner Losh /*- 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 * 4. Neither the name of the University nor the names of its contributors 17df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 18df8bae1dSRodney W. Grimes * without specific prior written permission. 19df8bae1dSRodney W. Grimes * 20df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30df8bae1dSRodney W. Grimes * SUCH DAMAGE. 31df8bae1dSRodney W. Grimes * 323c4dd356SDavid Greenman * from: @(#)vm_object.c 8.5 (Berkeley) 3/22/94 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * 35df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 36df8bae1dSRodney W. Grimes * All rights reserved. 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 39df8bae1dSRodney W. Grimes * 40df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 41df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 42df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 43df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 44df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 45df8bae1dSRodney W. Grimes * 46df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 47df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 48df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 51df8bae1dSRodney W. Grimes * 52df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 53df8bae1dSRodney W. Grimes * School of Computer Science 54df8bae1dSRodney W. Grimes * Carnegie Mellon University 55df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 56df8bae1dSRodney W. Grimes * 57df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 58df8bae1dSRodney W. Grimes * rights to redistribute these changes. 59df8bae1dSRodney W. Grimes */ 60df8bae1dSRodney W. Grimes 61df8bae1dSRodney W. Grimes /* 62df8bae1dSRodney W. Grimes * Virtual memory object module. 63df8bae1dSRodney W. Grimes */ 64df8bae1dSRodney W. Grimes 65874651b1SDavid E. O'Brien #include <sys/cdefs.h> 66874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 67874651b1SDavid E. O'Brien 68df8bae1dSRodney W. Grimes #include <sys/param.h> 69df8bae1dSRodney W. Grimes #include <sys/systm.h> 70fb919e4dSMark Murray #include <sys/lock.h> 71867a482dSJohn Dyson #include <sys/mman.h> 72cf2819ccSJohn Dyson #include <sys/mount.h> 73b9b7a4beSMatthew Dillon #include <sys/kernel.h> 74b9b7a4beSMatthew Dillon #include <sys/sysctl.h> 751b367556SJason Evans #include <sys/mutex.h> 76fb919e4dSMark Murray #include <sys/proc.h> /* for curproc, pageproc */ 77fb919e4dSMark Murray #include <sys/socket.h> 78fb919e4dSMark Murray #include <sys/vnode.h> 79fb919e4dSMark Murray #include <sys/vmmeter.h> 801005a129SJohn Baldwin #include <sys/sx.h> 81df8bae1dSRodney W. Grimes 82df8bae1dSRodney W. Grimes #include <vm/vm.h> 83efeaf95aSDavid Greenman #include <vm/vm_param.h> 84efeaf95aSDavid Greenman #include <vm/pmap.h> 85efeaf95aSDavid Greenman #include <vm/vm_map.h> 86efeaf95aSDavid Greenman #include <vm/vm_object.h> 87df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 8826f9a767SRodney W. Grimes #include <vm/vm_pageout.h> 890d94caffSDavid Greenman #include <vm/vm_pager.h> 9005f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 91a1f6d91cSDavid Greenman #include <vm/vm_kern.h> 92efeaf95aSDavid Greenman #include <vm/vm_extern.h> 93670d17b5SJeff Roberson #include <vm/uma.h> 9426f9a767SRodney W. Grimes 95b9b7a4beSMatthew Dillon #define EASY_SCAN_FACTOR 8 96b9b7a4beSMatthew Dillon 97b9b7a4beSMatthew Dillon #define MSYNC_FLUSH_HARDSEQ 0x01 98b9b7a4beSMatthew Dillon #define MSYNC_FLUSH_SOFTSEQ 0x02 99b9b7a4beSMatthew Dillon 100b9b7a4beSMatthew Dillon /* 101b9b7a4beSMatthew Dillon * msync / VM object flushing optimizations 102b9b7a4beSMatthew Dillon */ 103b9b7a4beSMatthew Dillon static int msync_flush_flags = MSYNC_FLUSH_HARDSEQ | MSYNC_FLUSH_SOFTSEQ; 104b9b7a4beSMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, msync_flush_flags, 105b9b7a4beSMatthew Dillon CTLFLAG_RW, &msync_flush_flags, 0, ""); 106b9b7a4beSMatthew Dillon 107c53f7aceSDag-Erling Smørgrav static int old_msync; 108c53f7aceSDag-Erling Smørgrav SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0, 109c53f7aceSDag-Erling Smørgrav "Use old (insecure) msync behavior"); 110c53f7aceSDag-Erling Smørgrav 111b9b7a4beSMatthew Dillon static void vm_object_qcollapse(vm_object_t object); 112b9b7a4beSMatthew Dillon static int vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags); 11302dd8331SAlan Cox static void vm_object_vndeallocate(vm_object_t object); 114f6b04d2bSDavid Greenman 115df8bae1dSRodney W. Grimes /* 116df8bae1dSRodney W. Grimes * Virtual memory objects maintain the actual data 117df8bae1dSRodney W. Grimes * associated with allocated virtual memory. A given 118df8bae1dSRodney W. Grimes * page of memory exists within exactly one object. 119df8bae1dSRodney W. Grimes * 120df8bae1dSRodney W. Grimes * An object is only deallocated when all "references" 121df8bae1dSRodney W. Grimes * are given up. Only one "reference" to a given 122df8bae1dSRodney W. Grimes * region of an object should be writeable. 123df8bae1dSRodney W. Grimes * 124df8bae1dSRodney W. Grimes * Associated with each object is a list of all resident 125df8bae1dSRodney W. Grimes * memory pages belonging to that object; this list is 126df8bae1dSRodney W. Grimes * maintained by the "vm_page" module, and locked by the object's 127df8bae1dSRodney W. Grimes * lock. 128df8bae1dSRodney W. Grimes * 129df8bae1dSRodney W. Grimes * Each object also records a "pager" routine which is 130df8bae1dSRodney W. Grimes * used to retrieve (and store) pages to the proper backing 131df8bae1dSRodney W. Grimes * storage. In addition, objects may be backed by other 132df8bae1dSRodney W. Grimes * objects from which they were virtual-copied. 133df8bae1dSRodney W. Grimes * 134df8bae1dSRodney W. Grimes * The only items within the object structure which are 135df8bae1dSRodney W. Grimes * modified after time of creation are: 136df8bae1dSRodney W. Grimes * reference count locked by object's lock 137df8bae1dSRodney W. Grimes * pager routine locked by object's lock 138df8bae1dSRodney W. Grimes * 139df8bae1dSRodney W. Grimes */ 140df8bae1dSRodney W. Grimes 14128f8db14SBruce Evans struct object_q vm_object_list; 142a5698387SAlan Cox struct mtx vm_object_list_mtx; /* lock for object list and count */ 143cccf11b8SAlan Cox 144cccf11b8SAlan Cox struct vm_object kernel_object_store; 145cccf11b8SAlan Cox struct vm_object kmem_object_store; 146df8bae1dSRodney W. Grimes 147604c2bbcSAlan Cox SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0, "VM object stats"); 148604c2bbcSAlan Cox 149f708ef1bSPoul-Henning Kamp static long object_collapses; 150604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD, 151604c2bbcSAlan Cox &object_collapses, 0, "VM object collapses"); 152604c2bbcSAlan Cox 153f708ef1bSPoul-Henning Kamp static long object_bypasses; 154604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD, 155604c2bbcSAlan Cox &object_bypasses, 0, "VM object bypasses"); 156dad740e9SAlan Cox 157670d17b5SJeff Roberson static uma_zone_t obj_zone; 1588355f576SJeff Roberson 159b23f72e9SBrian Feldman static int vm_object_zinit(void *mem, int size, int flags); 1608355f576SJeff Roberson 1618355f576SJeff Roberson #ifdef INVARIANTS 1628355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg); 1638355f576SJeff Roberson 1648355f576SJeff Roberson static void 1658355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg) 1668355f576SJeff Roberson { 1678355f576SJeff Roberson vm_object_t object; 1688355f576SJeff Roberson 1698355f576SJeff Roberson object = (vm_object_t)mem; 17043186e53SAlan Cox KASSERT(TAILQ_EMPTY(&object->memq), 17143186e53SAlan Cox ("object %p has resident pages", 17243186e53SAlan Cox object)); 1737bfda801SAlan Cox KASSERT(object->cache == NULL, 1747bfda801SAlan Cox ("object %p has cached pages", 1757bfda801SAlan Cox object)); 1768355f576SJeff Roberson KASSERT(object->paging_in_progress == 0, 1778355f576SJeff Roberson ("object %p paging_in_progress = %d", 1788355f576SJeff Roberson object, object->paging_in_progress)); 1798355f576SJeff Roberson KASSERT(object->resident_page_count == 0, 1808355f576SJeff Roberson ("object %p resident_page_count = %d", 1818355f576SJeff Roberson object, object->resident_page_count)); 1828355f576SJeff Roberson KASSERT(object->shadow_count == 0, 1838355f576SJeff Roberson ("object %p shadow_count = %d", 1848355f576SJeff Roberson object, object->shadow_count)); 1858355f576SJeff Roberson } 1868355f576SJeff Roberson #endif 1878355f576SJeff Roberson 188b23f72e9SBrian Feldman static int 189b23f72e9SBrian Feldman vm_object_zinit(void *mem, int size, int flags) 1908355f576SJeff Roberson { 1918355f576SJeff Roberson vm_object_t object; 1928355f576SJeff Roberson 1938355f576SJeff Roberson object = (vm_object_t)mem; 194e3a9e1b2SAlan Cox bzero(&object->mtx, sizeof(object->mtx)); 1955285558aSAlan Cox VM_OBJECT_LOCK_INIT(object, "standard object"); 1968355f576SJeff Roberson 1978355f576SJeff Roberson /* These are true for any object that has been freed */ 1988355f576SJeff Roberson object->paging_in_progress = 0; 1998355f576SJeff Roberson object->resident_page_count = 0; 2008355f576SJeff Roberson object->shadow_count = 0; 201b23f72e9SBrian Feldman return (0); 2028355f576SJeff Roberson } 203df8bae1dSRodney W. Grimes 2043075778bSJohn Dyson void 2056395da54SIan Dowse _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object) 206df8bae1dSRodney W. Grimes { 2070cddd8f0SMatthew Dillon 208df8bae1dSRodney W. Grimes TAILQ_INIT(&object->memq); 2091c500307SAlan Cox LIST_INIT(&object->shadow_head); 210a1f6d91cSDavid Greenman 211b86ec922SMatthew Dillon object->root = NULL; 21224a1cce3SDavid Greenman object->type = type; 213df8bae1dSRodney W. Grimes object->size = size; 214b881da26SAlan Cox object->generation = 1; 215a1f6d91cSDavid Greenman object->ref_count = 1; 21624a1cce3SDavid Greenman object->flags = 0; 21760517fd1SJohn Baldwin if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP)) 2183471677cSAlan Cox object->flags = OBJ_ONEMAPPING; 2192446e4f0SAlan Cox object->pg_color = 0; 22024a1cce3SDavid Greenman object->handle = NULL; 22124a1cce3SDavid Greenman object->backing_object = NULL; 222a316d390SJohn Dyson object->backing_object_offset = (vm_ooffset_t) 0; 2237bfda801SAlan Cox object->cache = NULL; 224a1f6d91cSDavid Greenman 225a5698387SAlan Cox mtx_lock(&vm_object_list_mtx); 22660517fd1SJohn Baldwin TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 227a5698387SAlan Cox mtx_unlock(&vm_object_list_mtx); 228df8bae1dSRodney W. Grimes } 229df8bae1dSRodney W. Grimes 230df8bae1dSRodney W. Grimes /* 23126f9a767SRodney W. Grimes * vm_object_init: 23226f9a767SRodney W. Grimes * 23326f9a767SRodney W. Grimes * Initialize the VM objects module. 23426f9a767SRodney W. Grimes */ 23526f9a767SRodney W. Grimes void 2361b40f8c0SMatthew Dillon vm_object_init(void) 23726f9a767SRodney W. Grimes { 23826f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_list); 2396008862bSJohn Baldwin mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF); 2400217125fSDavid Greenman 2415285558aSAlan Cox VM_OBJECT_LOCK_INIT(&kernel_object_store, "kernel object"); 2429f5c801bSAlan Cox _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 24326f9a767SRodney W. Grimes kernel_object); 24426f9a767SRodney W. Grimes 2455285558aSAlan Cox VM_OBJECT_LOCK_INIT(&kmem_object_store, "kmem object"); 2469f5c801bSAlan Cox _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 24726f9a767SRodney W. Grimes kmem_object); 248ed6a7863SAlan Cox 2498dbca793STor Egge /* 2508dbca793STor Egge * The lock portion of struct vm_object must be type stable due 2518dbca793STor Egge * to vm_pageout_fallback_object_lock locking a vm object 2528dbca793STor Egge * without holding any references to it. 2538dbca793STor Egge */ 2548355f576SJeff Roberson obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL, 2558355f576SJeff Roberson #ifdef INVARIANTS 2568355f576SJeff Roberson vm_object_zdtor, 2578355f576SJeff Roberson #else 2588355f576SJeff Roberson NULL, 2598355f576SJeff Roberson #endif 260f3c625e4SJeff Roberson vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM|UMA_ZONE_NOFREE); 26199448ed1SJohn Dyson } 26299448ed1SJohn Dyson 26399448ed1SJohn Dyson void 2641b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits) 2651b40f8c0SMatthew Dillon { 2665440b5a9SAlan Cox 267d647a0edSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 268b06805adSJake Burkholder object->flags &= ~bits; 2691b40f8c0SMatthew Dillon } 2701b40f8c0SMatthew Dillon 2711b40f8c0SMatthew Dillon void 2721b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i) 2731b40f8c0SMatthew Dillon { 274f279b88dSAlan Cox 275d647a0edSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 276b06805adSJake Burkholder object->paging_in_progress += i; 2771b40f8c0SMatthew Dillon } 2781b40f8c0SMatthew Dillon 2791b40f8c0SMatthew Dillon void 2801b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i) 2811b40f8c0SMatthew Dillon { 282d647a0edSAlan Cox 2830fa05eaeSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 284b06805adSJake Burkholder object->paging_in_progress -= i; 2851b40f8c0SMatthew Dillon } 2861b40f8c0SMatthew Dillon 2871b40f8c0SMatthew Dillon void 2881b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object) 2891b40f8c0SMatthew Dillon { 290f279b88dSAlan Cox 291d647a0edSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 292b06805adSJake Burkholder object->paging_in_progress--; 2931b40f8c0SMatthew Dillon if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) { 2941b40f8c0SMatthew Dillon vm_object_clear_flag(object, OBJ_PIPWNT); 2951b40f8c0SMatthew Dillon wakeup(object); 2961b40f8c0SMatthew Dillon } 2971b40f8c0SMatthew Dillon } 2981b40f8c0SMatthew Dillon 2991b40f8c0SMatthew Dillon void 3001b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i) 3011b40f8c0SMatthew Dillon { 302d647a0edSAlan Cox 3030d420ad3SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 3041b40f8c0SMatthew Dillon if (i) 305b06805adSJake Burkholder object->paging_in_progress -= i; 3061b40f8c0SMatthew Dillon if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) { 3071b40f8c0SMatthew Dillon vm_object_clear_flag(object, OBJ_PIPWNT); 3081b40f8c0SMatthew Dillon wakeup(object); 3091b40f8c0SMatthew Dillon } 3101b40f8c0SMatthew Dillon } 3111b40f8c0SMatthew Dillon 3121b40f8c0SMatthew Dillon void 3131b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid) 3141b40f8c0SMatthew Dillon { 3151ca58953SAlan Cox 3161ca58953SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 3171ca58953SAlan Cox while (object->paging_in_progress) { 3181ca58953SAlan Cox object->flags |= OBJ_PIPWNT; 3191ca58953SAlan Cox msleep(object, VM_OBJECT_MTX(object), PVM, waitid, 0); 3201ca58953SAlan Cox } 3211b40f8c0SMatthew Dillon } 3221b40f8c0SMatthew Dillon 32326f9a767SRodney W. Grimes /* 32426f9a767SRodney W. Grimes * vm_object_allocate: 32526f9a767SRodney W. Grimes * 32626f9a767SRodney W. Grimes * Returns a new object with the given size. 32726f9a767SRodney W. Grimes */ 32826f9a767SRodney W. Grimes vm_object_t 3296395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size) 33026f9a767SRodney W. Grimes { 33190688d13SAlan Cox vm_object_t object; 33290688d13SAlan Cox 33390688d13SAlan Cox object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK); 33490688d13SAlan Cox _vm_object_allocate(type, size, object); 33590688d13SAlan Cox return (object); 33626f9a767SRodney W. Grimes } 33726f9a767SRodney W. Grimes 33826f9a767SRodney W. Grimes 33926f9a767SRodney W. Grimes /* 340df8bae1dSRodney W. Grimes * vm_object_reference: 341df8bae1dSRodney W. Grimes * 34215347817SAlan Cox * Gets another reference to the given object. Note: OBJ_DEAD 34315347817SAlan Cox * objects can be referenced during final cleaning. 344df8bae1dSRodney W. Grimes */ 3456476c0d2SJohn Dyson void 3461b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object) 347df8bae1dSRodney W. Grimes { 34882f9defeSAlan Cox struct vnode *vp; 34982f9defeSAlan Cox 350df8bae1dSRodney W. Grimes if (object == NULL) 351df8bae1dSRodney W. Grimes return; 35215347817SAlan Cox VM_OBJECT_LOCK(object); 353df8bae1dSRodney W. Grimes object->ref_count++; 35447221757SJohn Dyson if (object->type == OBJT_VNODE) { 355c05e22d4SJeff Roberson int vfslocked; 356c05e22d4SJeff Roberson 35782f9defeSAlan Cox vp = object->handle; 35882f9defeSAlan Cox VM_OBJECT_UNLOCK(object); 359c05e22d4SJeff Roberson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 360c05e22d4SJeff Roberson vget(vp, LK_RETRY, curthread); 361c05e22d4SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 36282f9defeSAlan Cox } else 36382f9defeSAlan Cox VM_OBJECT_UNLOCK(object); 36495e5e988SJohn Dyson } 36595e5e988SJohn Dyson 36623955314SAlfred Perlstein /* 367b921a12bSAlan Cox * vm_object_reference_locked: 368b921a12bSAlan Cox * 369b921a12bSAlan Cox * Gets another reference to the given object. 370b921a12bSAlan Cox * 371b921a12bSAlan Cox * The object must be locked. 372b921a12bSAlan Cox */ 373b921a12bSAlan Cox void 374b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object) 375b921a12bSAlan Cox { 376b921a12bSAlan Cox struct vnode *vp; 377b921a12bSAlan Cox 378b921a12bSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 379b921a12bSAlan Cox KASSERT((object->flags & OBJ_DEAD) == 0, 380b921a12bSAlan Cox ("vm_object_reference_locked: dead object referenced")); 381b921a12bSAlan Cox object->ref_count++; 382b921a12bSAlan Cox if (object->type == OBJT_VNODE) { 383b921a12bSAlan Cox vp = object->handle; 384b921a12bSAlan Cox vref(vp); 385b921a12bSAlan Cox } 386b921a12bSAlan Cox } 387b921a12bSAlan Cox 388b921a12bSAlan Cox /* 3899d5abbddSJens Schweikhardt * Handle deallocating an object of type OBJT_VNODE. 39023955314SAlfred Perlstein */ 39102dd8331SAlan Cox static void 3921b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object) 39395e5e988SJohn Dyson { 39495e5e988SJohn Dyson struct vnode *vp = (struct vnode *) object->handle; 395219cbf59SEivind Eklund 396ae51ff11SJeff Roberson VFS_ASSERT_GIANT(vp->v_mount); 397ad682c48SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 3985526d2d9SEivind Eklund KASSERT(object->type == OBJT_VNODE, 3995526d2d9SEivind Eklund ("vm_object_vndeallocate: not a vnode object")); 400219cbf59SEivind Eklund KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp")); 401219cbf59SEivind Eklund #ifdef INVARIANTS 40295e5e988SJohn Dyson if (object->ref_count == 0) { 40395e5e988SJohn Dyson vprint("vm_object_vndeallocate", vp); 40495e5e988SJohn Dyson panic("vm_object_vndeallocate: bad object reference count"); 40595e5e988SJohn Dyson } 40695e5e988SJohn Dyson #endif 40795e5e988SJohn Dyson 40895e5e988SJohn Dyson object->ref_count--; 40947221757SJohn Dyson if (object->ref_count == 0) { 410e6e370a7SJeff Roberson mp_fixme("Unlocked vflag access."); 411e6e370a7SJeff Roberson vp->v_vflag &= ~VV_TEXT; 4122be70f79SJohn Dyson } 413ad682c48SAlan Cox VM_OBJECT_UNLOCK(object); 41423955314SAlfred Perlstein /* 41523955314SAlfred Perlstein * vrele may need a vop lock 41623955314SAlfred Perlstein */ 41747221757SJohn Dyson vrele(vp); 418df8bae1dSRodney W. Grimes } 419df8bae1dSRodney W. Grimes 420df8bae1dSRodney W. Grimes /* 421df8bae1dSRodney W. Grimes * vm_object_deallocate: 422df8bae1dSRodney W. Grimes * 423df8bae1dSRodney W. Grimes * Release a reference to the specified object, 424df8bae1dSRodney W. Grimes * gained either through a vm_object_allocate 425df8bae1dSRodney W. Grimes * or a vm_object_reference call. When all references 426df8bae1dSRodney W. Grimes * are gone, storage associated with this object 427df8bae1dSRodney W. Grimes * may be relinquished. 428df8bae1dSRodney W. Grimes * 429df8bae1dSRodney W. Grimes * No object may be locked. 430df8bae1dSRodney W. Grimes */ 43126f9a767SRodney W. Grimes void 4321b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object) 433df8bae1dSRodney W. Grimes { 434df8bae1dSRodney W. Grimes vm_object_t temp; 435df8bae1dSRodney W. Grimes 436df8bae1dSRodney W. Grimes while (object != NULL) { 437ae51ff11SJeff Roberson int vfslocked; 438ca95b514SJohn Baldwin 439ae51ff11SJeff Roberson vfslocked = 0; 440ca95b514SJohn Baldwin restart: 441ad682c48SAlan Cox VM_OBJECT_LOCK(object); 4423b68228cSAlan Cox if (object->type == OBJT_VNODE) { 443ca95b514SJohn Baldwin struct vnode *vp = (struct vnode *) object->handle; 444ca95b514SJohn Baldwin 445ca95b514SJohn Baldwin /* 446ca95b514SJohn Baldwin * Conditionally acquire Giant for a vnode-backed 447ca95b514SJohn Baldwin * object. We have to be careful since the type of 448ca95b514SJohn Baldwin * a vnode object can change while the object is 449ca95b514SJohn Baldwin * unlocked. 450ca95b514SJohn Baldwin */ 451ca95b514SJohn Baldwin if (VFS_NEEDSGIANT(vp->v_mount) && !vfslocked) { 452ca95b514SJohn Baldwin vfslocked = 1; 453ca95b514SJohn Baldwin if (!mtx_trylock(&Giant)) { 454ca95b514SJohn Baldwin VM_OBJECT_UNLOCK(object); 455ca95b514SJohn Baldwin mtx_lock(&Giant); 456ca95b514SJohn Baldwin goto restart; 457ca95b514SJohn Baldwin } 458ca95b514SJohn Baldwin } 45995e5e988SJohn Dyson vm_object_vndeallocate(object); 460ae51ff11SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 46123b186d3SAlan Cox return; 462ca95b514SJohn Baldwin } else 463ca95b514SJohn Baldwin /* 464ca95b514SJohn Baldwin * This is to handle the case that the object 465ca95b514SJohn Baldwin * changed type while we dropped its lock to 466ca95b514SJohn Baldwin * obtain Giant. 467ca95b514SJohn Baldwin */ 468ca95b514SJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 46995e5e988SJohn Dyson 4708125b1e6SAlfred Perlstein KASSERT(object->ref_count != 0, 4718125b1e6SAlfred Perlstein ("vm_object_deallocate: object deallocated too many times: %d", object->type)); 4722be70f79SJohn Dyson 4732be70f79SJohn Dyson /* 4748125b1e6SAlfred Perlstein * If the reference count goes to 0 we start calling 4758125b1e6SAlfred Perlstein * vm_object_terminate() on the object chain. 4768125b1e6SAlfred Perlstein * A ref count of 1 may be a special case depending on the 4778125b1e6SAlfred Perlstein * shadow count being 0 or 1. 4782be70f79SJohn Dyson */ 479c0877f10SJohn Dyson object->ref_count--; 4808125b1e6SAlfred Perlstein if (object->ref_count > 1) { 4813b68228cSAlan Cox VM_OBJECT_UNLOCK(object); 48223b186d3SAlan Cox return; 4838125b1e6SAlfred Perlstein } else if (object->ref_count == 1) { 4848125b1e6SAlfred Perlstein if (object->shadow_count == 0) { 4858125b1e6SAlfred Perlstein vm_object_set_flag(object, OBJ_ONEMAPPING); 4868125b1e6SAlfred Perlstein } else if ((object->shadow_count == 1) && 4878125b1e6SAlfred Perlstein (object->handle == NULL) && 48824a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || 48924a1cce3SDavid Greenman object->type == OBJT_SWAP)) { 490a1f6d91cSDavid Greenman vm_object_t robject; 49195e5e988SJohn Dyson 4921c500307SAlan Cox robject = LIST_FIRST(&object->shadow_head); 4935526d2d9SEivind Eklund KASSERT(robject != NULL, 494219cbf59SEivind Eklund ("vm_object_deallocate: ref_count: %d, shadow_count: %d", 4955526d2d9SEivind Eklund object->ref_count, 4965526d2d9SEivind Eklund object->shadow_count)); 497b72b0115SAlan Cox if (!VM_OBJECT_TRYLOCK(robject)) { 498b72b0115SAlan Cox /* 499b72b0115SAlan Cox * Avoid a potential deadlock. 500b72b0115SAlan Cox */ 501b72b0115SAlan Cox object->ref_count++; 502b72b0115SAlan Cox VM_OBJECT_UNLOCK(object); 503a7d86121SAlan Cox /* 504a7d86121SAlan Cox * More likely than not the thread 505a7d86121SAlan Cox * holding robject's lock has lower 506a7d86121SAlan Cox * priority than the current thread. 507a7d86121SAlan Cox * Let the lower priority thread run. 508a7d86121SAlan Cox */ 5098db5fc58SJohn Baldwin pause("vmo_de", 1); 510b72b0115SAlan Cox continue; 511b72b0115SAlan Cox } 512d936694fSAlan Cox /* 513d936694fSAlan Cox * Collapse object into its shadow unless its 514d936694fSAlan Cox * shadow is dead. In that case, object will 515d936694fSAlan Cox * be deallocated by the thread that is 516d936694fSAlan Cox * deallocating its shadow. 517d936694fSAlan Cox */ 518d936694fSAlan Cox if ((robject->flags & OBJ_DEAD) == 0 && 519d936694fSAlan Cox (robject->handle == NULL) && 52024a1cce3SDavid Greenman (robject->type == OBJT_DEFAULT || 52124a1cce3SDavid Greenman robject->type == OBJT_SWAP)) { 522a1f6d91cSDavid Greenman 52395e5e988SJohn Dyson robject->ref_count++; 524138449dcSAlan Cox retry: 525138449dcSAlan Cox if (robject->paging_in_progress) { 526138449dcSAlan Cox VM_OBJECT_UNLOCK(object); 527138449dcSAlan Cox vm_object_pip_wait(robject, 528138449dcSAlan Cox "objde1"); 5292e9f4a69SAlan Cox temp = robject->backing_object; 5302e9f4a69SAlan Cox if (object == temp) { 531138449dcSAlan Cox VM_OBJECT_LOCK(object); 532138449dcSAlan Cox goto retry; 5332e9f4a69SAlan Cox } 534138449dcSAlan Cox } else if (object->paging_in_progress) { 535138449dcSAlan Cox VM_OBJECT_UNLOCK(robject); 536138449dcSAlan Cox object->flags |= OBJ_PIPWNT; 537138449dcSAlan Cox msleep(object, 538138449dcSAlan Cox VM_OBJECT_MTX(object), 539138449dcSAlan Cox PDROP | PVM, "objde2", 0); 540138449dcSAlan Cox VM_OBJECT_LOCK(robject); 5412e9f4a69SAlan Cox temp = robject->backing_object; 5422e9f4a69SAlan Cox if (object == temp) { 543138449dcSAlan Cox VM_OBJECT_LOCK(object); 544138449dcSAlan Cox goto retry; 545a1f6d91cSDavid Greenman } 5462e9f4a69SAlan Cox } else 5473b68228cSAlan Cox VM_OBJECT_UNLOCK(object); 5482e9f4a69SAlan Cox 54995e5e988SJohn Dyson if (robject->ref_count == 1) { 55095e5e988SJohn Dyson robject->ref_count--; 551ba8da839SDavid Greenman object = robject; 55295e5e988SJohn Dyson goto doterm; 55395e5e988SJohn Dyson } 55495e5e988SJohn Dyson object = robject; 55595e5e988SJohn Dyson vm_object_collapse(object); 556d7fc2210SAlan Cox VM_OBJECT_UNLOCK(object); 557ba8da839SDavid Greenman continue; 558a1f6d91cSDavid Greenman } 559b72b0115SAlan Cox VM_OBJECT_UNLOCK(robject); 56095e5e988SJohn Dyson } 5613b68228cSAlan Cox VM_OBJECT_UNLOCK(object); 56223b186d3SAlan Cox return; 56395e5e988SJohn Dyson } 56495e5e988SJohn Dyson doterm: 56524a1cce3SDavid Greenman temp = object->backing_object; 566c9917419SAlan Cox if (temp != NULL) { 567c9917419SAlan Cox VM_OBJECT_LOCK(temp); 5681c500307SAlan Cox LIST_REMOVE(object, shadow_list); 56995e5e988SJohn Dyson temp->shadow_count--; 570eaf13dd7SJohn Dyson temp->generation++; 571c9917419SAlan Cox VM_OBJECT_UNLOCK(temp); 57295461b45SJohn Dyson object->backing_object = NULL; 573de5f6a77SJohn Dyson } 574245df27cSMatthew Dillon /* 575245df27cSMatthew Dillon * Don't double-terminate, we could be in a termination 576245df27cSMatthew Dillon * recursion due to the terminate having to sync data 577245df27cSMatthew Dillon * to disk. 578245df27cSMatthew Dillon */ 579245df27cSMatthew Dillon if ((object->flags & OBJ_DEAD) == 0) 580df8bae1dSRodney W. Grimes vm_object_terminate(object); 581c829b9d0SAlan Cox else 582c829b9d0SAlan Cox VM_OBJECT_UNLOCK(object); 583df8bae1dSRodney W. Grimes object = temp; 584df8bae1dSRodney W. Grimes } 585df8bae1dSRodney W. Grimes } 586df8bae1dSRodney W. Grimes 587df8bae1dSRodney W. Grimes /* 588df8bae1dSRodney W. Grimes * vm_object_terminate actually destroys the specified object, freeing 589df8bae1dSRodney W. Grimes * up all previously used resources. 590df8bae1dSRodney W. Grimes * 591df8bae1dSRodney W. Grimes * The object must be locked. 5921c7c3c6aSMatthew Dillon * This routine may block. 593df8bae1dSRodney W. Grimes */ 59495e5e988SJohn Dyson void 5951b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object) 596df8bae1dSRodney W. Grimes { 597d031cff1SMatthew Dillon vm_page_t p; 598df8bae1dSRodney W. Grimes 599c829b9d0SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 6000cddd8f0SMatthew Dillon 60195e5e988SJohn Dyson /* 60295e5e988SJohn Dyson * Make sure no one uses us. 60395e5e988SJohn Dyson */ 604069e9bc1SDoug Rabson vm_object_set_flag(object, OBJ_DEAD); 6053c631446SJohn Dyson 606df8bae1dSRodney W. Grimes /* 607f6b04d2bSDavid Greenman * wait for the pageout daemon to be done with the object 608df8bae1dSRodney W. Grimes */ 60966095752SJohn Dyson vm_object_pip_wait(object, "objtrm"); 610df8bae1dSRodney W. Grimes 6115526d2d9SEivind Eklund KASSERT(!object->paging_in_progress, 6125526d2d9SEivind Eklund ("vm_object_terminate: pageout in progress")); 61326f9a767SRodney W. Grimes 61426f9a767SRodney W. Grimes /* 6150d94caffSDavid Greenman * Clean and free the pages, as appropriate. All references to the 6160d94caffSDavid Greenman * object are gone, so we don't need to lock it. 61726f9a767SRodney W. Grimes */ 61824a1cce3SDavid Greenman if (object->type == OBJT_VNODE) { 619f7dd7b63SAlan Cox struct vnode *vp = (struct vnode *)object->handle; 62095e5e988SJohn Dyson 62195e5e988SJohn Dyson /* 62295e5e988SJohn Dyson * Clean pages and flush buffers. 62395e5e988SJohn Dyson */ 6248f9110f6SJohn Dyson vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 625b6e48e03SAlan Cox VM_OBJECT_UNLOCK(object); 62695e5e988SJohn Dyson 6277c0745eeSPoul-Henning Kamp vinvalbuf(vp, V_SAVE, NULL, 0, 0); 628f7dd7b63SAlan Cox 629f7dd7b63SAlan Cox VM_OBJECT_LOCK(object); 630bef608bdSJohn Dyson } 631bef608bdSJohn Dyson 632971dd342SAlfred Perlstein KASSERT(object->ref_count == 0, 633971dd342SAlfred Perlstein ("vm_object_terminate: object with references, ref_count=%d", 634971dd342SAlfred Perlstein object->ref_count)); 635996c772fSJohn Dyson 6360d94caffSDavid Greenman /* 637356863ebSDavid Greenman * Now free any remaining pages. For internal objects, this also 638356863ebSDavid Greenman * removes them from paging queues. Don't free wired pages, just 639356863ebSDavid Greenman * remove them from the object. 640df8bae1dSRodney W. Grimes */ 64156030358SAlan Cox vm_page_lock_queues(); 642b18bfc3dSJohn Dyson while ((p = TAILQ_FIRST(&object->memq)) != NULL) { 6439af80719SAlan Cox KASSERT(!p->busy && (p->oflags & VPO_BUSY) == 0, 644971dd342SAlfred Perlstein ("vm_object_terminate: freeing busy page %p " 645971dd342SAlfred Perlstein "p->busy = %d, p->flags %x\n", p, p->busy, p->flags)); 6460b10ba98SDavid Greenman if (p->wire_count == 0) { 647df8bae1dSRodney W. Grimes vm_page_free(p); 648393a081dSAttilio Rao cnt.v_pfree++; 6490b10ba98SDavid Greenman } else { 6500b10ba98SDavid Greenman vm_page_remove(p); 6510b10ba98SDavid Greenman } 652df8bae1dSRodney W. Grimes } 65356030358SAlan Cox vm_page_unlock_queues(); 654bef608bdSJohn Dyson 6557bfda801SAlan Cox if (__predict_false(object->cache != NULL)) 656c9444914SAlan Cox vm_page_cache_free(object, 0, 0); 6577bfda801SAlan Cox 6582d8acc0fSJohn Dyson /* 6599fcfb650SDavid Greenman * Let the pager know object is dead. 6609fcfb650SDavid Greenman */ 6619fcfb650SDavid Greenman vm_pager_deallocate(object); 662658ad5ffSAlan Cox VM_OBJECT_UNLOCK(object); 6639fcfb650SDavid Greenman 6649fcfb650SDavid Greenman /* 6650b10ba98SDavid Greenman * Remove the object from the global object list. 6662d8acc0fSJohn Dyson */ 6679ed346baSBosko Milekic mtx_lock(&vm_object_list_mtx); 668df8bae1dSRodney W. Grimes TAILQ_REMOVE(&vm_object_list, object, object_list); 6699ed346baSBosko Milekic mtx_unlock(&vm_object_list_mtx); 6700b10ba98SDavid Greenman 671df8bae1dSRodney W. Grimes /* 672df8bae1dSRodney W. Grimes * Free the space for the object. 673df8bae1dSRodney W. Grimes */ 674670d17b5SJeff Roberson uma_zfree(obj_zone, object); 67547221757SJohn Dyson } 676df8bae1dSRodney W. Grimes 677df8bae1dSRodney W. Grimes /* 678df8bae1dSRodney W. Grimes * vm_object_page_clean 679df8bae1dSRodney W. Grimes * 6804f79d873SMatthew Dillon * Clean all dirty pages in the specified range of object. Leaves page 6814f79d873SMatthew Dillon * on whatever queue it is currently on. If NOSYNC is set then do not 682b146f9e5SAlan Cox * write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC), 6834f79d873SMatthew Dillon * leaving the object dirty. 68426f9a767SRodney W. Grimes * 68543b7990eSMatthew Dillon * When stuffing pages asynchronously, allow clustering. XXX we need a 68643b7990eSMatthew Dillon * synchronous clustering mode implementation. 68743b7990eSMatthew Dillon * 68826f9a767SRodney W. Grimes * Odd semantics: if start == end, we clean everything. 68926f9a767SRodney W. Grimes * 69026f9a767SRodney W. Grimes * The object must be locked. 69126f9a767SRodney W. Grimes */ 692f6b04d2bSDavid Greenman void 6931b40f8c0SMatthew Dillon vm_object_page_clean(vm_object_t object, vm_pindex_t start, vm_pindex_t end, int flags) 694f6b04d2bSDavid Greenman { 695b9b7a4beSMatthew Dillon vm_page_t p, np; 6966395da54SIan Dowse vm_pindex_t tstart, tend; 697bd7e5f99SJohn Dyson vm_pindex_t pi; 6984f79d873SMatthew Dillon int clearobjflags; 6998f9110f6SJohn Dyson int pagerflags; 7002d8acc0fSJohn Dyson int curgeneration; 701f6b04d2bSDavid Greenman 702b6e48e03SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 703aef922f5SJohn Dyson if (object->type != OBJT_VNODE || 704aef922f5SJohn Dyson (object->flags & OBJ_MIGHTBEDIRTY) == 0) 705f6b04d2bSDavid Greenman return; 706f6b04d2bSDavid Greenman 70743b7990eSMatthew Dillon pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK; 7088f9110f6SJohn Dyson pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0; 7098f9110f6SJohn Dyson 710069e9bc1SDoug Rabson vm_object_set_flag(object, OBJ_CLEANING); 71124a1cce3SDavid Greenman 712f6b04d2bSDavid Greenman tstart = start; 713f6b04d2bSDavid Greenman if (end == 0) { 714f6b04d2bSDavid Greenman tend = object->size; 715f6b04d2bSDavid Greenman } else { 716f6b04d2bSDavid Greenman tend = end; 717f6b04d2bSDavid Greenman } 718eaf13dd7SJohn Dyson 71935c01631SAlan Cox vm_page_lock_queues(); 7204f79d873SMatthew Dillon /* 721b9b7a4beSMatthew Dillon * If the caller is smart and only msync()s a range he knows is 722b9b7a4beSMatthew Dillon * dirty, we may be able to avoid an object scan. This results in 723b9b7a4beSMatthew Dillon * a phenominal improvement in performance. We cannot do this 724b9b7a4beSMatthew Dillon * as a matter of course because the object may be huge - e.g. 725b9b7a4beSMatthew Dillon * the size might be in the gigabytes or terrabytes. 726b9b7a4beSMatthew Dillon */ 727b9b7a4beSMatthew Dillon if (msync_flush_flags & MSYNC_FLUSH_HARDSEQ) { 728300b96acSIan Dowse vm_pindex_t tscan; 729b9b7a4beSMatthew Dillon int scanlimit; 730b9b7a4beSMatthew Dillon int scanreset; 731b9b7a4beSMatthew Dillon 732b9b7a4beSMatthew Dillon scanreset = object->resident_page_count / EASY_SCAN_FACTOR; 733b9b7a4beSMatthew Dillon if (scanreset < 16) 734b9b7a4beSMatthew Dillon scanreset = 16; 73543b7990eSMatthew Dillon pagerflags |= VM_PAGER_IGNORE_CLEANCHK; 736b9b7a4beSMatthew Dillon 737b9b7a4beSMatthew Dillon scanlimit = scanreset; 738b9b7a4beSMatthew Dillon tscan = tstart; 739b9b7a4beSMatthew Dillon while (tscan < tend) { 740b9b7a4beSMatthew Dillon curgeneration = object->generation; 741b9b7a4beSMatthew Dillon p = vm_page_lookup(object, tscan); 7427bfda801SAlan Cox if (p == NULL || p->valid == 0) { 743b9b7a4beSMatthew Dillon if (--scanlimit == 0) 744b9b7a4beSMatthew Dillon break; 745b9b7a4beSMatthew Dillon ++tscan; 746b9b7a4beSMatthew Dillon continue; 747b9b7a4beSMatthew Dillon } 748b9b7a4beSMatthew Dillon vm_page_test_dirty(p); 749b9b7a4beSMatthew Dillon if ((p->dirty & p->valid) == 0) { 750b9b7a4beSMatthew Dillon if (--scanlimit == 0) 751b9b7a4beSMatthew Dillon break; 752b9b7a4beSMatthew Dillon ++tscan; 753b9b7a4beSMatthew Dillon continue; 754b9b7a4beSMatthew Dillon } 755b9b7a4beSMatthew Dillon /* 756b9b7a4beSMatthew Dillon * If we have been asked to skip nosync pages and 757b9b7a4beSMatthew Dillon * this is a nosync page, we can't continue. 758b9b7a4beSMatthew Dillon */ 759b146f9e5SAlan Cox if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) { 760b9b7a4beSMatthew Dillon if (--scanlimit == 0) 761b9b7a4beSMatthew Dillon break; 762b9b7a4beSMatthew Dillon ++tscan; 763b9b7a4beSMatthew Dillon continue; 764b9b7a4beSMatthew Dillon } 765b9b7a4beSMatthew Dillon scanlimit = scanreset; 766b9b7a4beSMatthew Dillon 767b9b7a4beSMatthew Dillon /* 768b9b7a4beSMatthew Dillon * This returns 0 if it was unable to busy the first 769b9b7a4beSMatthew Dillon * page (i.e. had to sleep). 770b9b7a4beSMatthew Dillon */ 771b9b7a4beSMatthew Dillon tscan += vm_object_page_collect_flush(object, p, curgeneration, pagerflags); 772b9b7a4beSMatthew Dillon } 773b9b7a4beSMatthew Dillon 774b9b7a4beSMatthew Dillon /* 775b9b7a4beSMatthew Dillon * If everything was dirty and we flushed it successfully, 776b9b7a4beSMatthew Dillon * and the requested range is not the entire object, we 777b9b7a4beSMatthew Dillon * don't have to mess with CLEANCHK or MIGHTBEDIRTY and can 778b9b7a4beSMatthew Dillon * return immediately. 779b9b7a4beSMatthew Dillon */ 780b9b7a4beSMatthew Dillon if (tscan >= tend && (tstart || tend < object->size)) { 78135c01631SAlan Cox vm_page_unlock_queues(); 782b9b7a4beSMatthew Dillon vm_object_clear_flag(object, OBJ_CLEANING); 783b9b7a4beSMatthew Dillon return; 784b9b7a4beSMatthew Dillon } 78543b7990eSMatthew Dillon pagerflags &= ~VM_PAGER_IGNORE_CLEANCHK; 786b9b7a4beSMatthew Dillon } 787b9b7a4beSMatthew Dillon 788b9b7a4beSMatthew Dillon /* 7894f79d873SMatthew Dillon * Generally set CLEANCHK interlock and make the page read-only so 7904f79d873SMatthew Dillon * we can then clear the object flags. 7914f79d873SMatthew Dillon * 7924f79d873SMatthew Dillon * However, if this is a nosync mmap then the object is likely to 7934f79d873SMatthew Dillon * stay dirty so do not mess with the page and do not clear the 7944f79d873SMatthew Dillon * object flags. 7954f79d873SMatthew Dillon */ 7964f79d873SMatthew Dillon clearobjflags = 1; 797fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(p, &object->memq, listq) { 7980cd31a0dSAlan Cox p->oflags |= VPO_CLEANCHK; 799b146f9e5SAlan Cox if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) 8004f79d873SMatthew Dillon clearobjflags = 0; 8014f79d873SMatthew Dillon else 80278985e42SAlan Cox pmap_remove_write(p); 803eaf13dd7SJohn Dyson } 804eaf13dd7SJohn Dyson 8054f79d873SMatthew Dillon if (clearobjflags && (tstart == 0) && (tend == object->size)) { 806245df27cSMatthew Dillon struct vnode *vp; 807245df27cSMatthew Dillon 808af51d7bfSAlan Cox vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY); 809245df27cSMatthew Dillon if (object->type == OBJT_VNODE && 810245df27cSMatthew Dillon (vp = (struct vnode *)object->handle) != NULL) { 811e6e370a7SJeff Roberson VI_LOCK(vp); 812e6e370a7SJeff Roberson if (vp->v_iflag & VI_OBJDIRTY) 813e6e370a7SJeff Roberson vp->v_iflag &= ~VI_OBJDIRTY; 814e6e370a7SJeff Roberson VI_UNLOCK(vp); 815245df27cSMatthew Dillon } 816ec4f9fb0SDavid Greenman } 817f6b04d2bSDavid Greenman 818bd7e5f99SJohn Dyson rescan: 8192d8acc0fSJohn Dyson curgeneration = object->generation; 8202d8acc0fSJohn Dyson 821b18bfc3dSJohn Dyson for (p = TAILQ_FIRST(&object->memq); p; p = np) { 822b9b7a4beSMatthew Dillon int n; 823b9b7a4beSMatthew Dillon 824b18bfc3dSJohn Dyson np = TAILQ_NEXT(p, listq); 825bd7e5f99SJohn Dyson 826b9b7a4beSMatthew Dillon again: 827bd7e5f99SJohn Dyson pi = p->pindex; 8280cd31a0dSAlan Cox if ((p->oflags & VPO_CLEANCHK) == 0 || 829bd7e5f99SJohn Dyson (pi < tstart) || (pi >= tend) || 8307bfda801SAlan Cox p->valid == 0) { 8310cd31a0dSAlan Cox p->oflags &= ~VPO_CLEANCHK; 832aef922f5SJohn Dyson continue; 833f6b04d2bSDavid Greenman } 834f6b04d2bSDavid Greenman 835bd7e5f99SJohn Dyson vm_page_test_dirty(p); 836bd7e5f99SJohn Dyson if ((p->dirty & p->valid) == 0) { 8370cd31a0dSAlan Cox p->oflags &= ~VPO_CLEANCHK; 838bd7e5f99SJohn Dyson continue; 839bd7e5f99SJohn Dyson } 840ec4f9fb0SDavid Greenman 8414f79d873SMatthew Dillon /* 8424f79d873SMatthew Dillon * If we have been asked to skip nosync pages and this is a 8434f79d873SMatthew Dillon * nosync page, skip it. Note that the object flags were 8444f79d873SMatthew Dillon * not cleared in this case so we do not have to set them. 8454f79d873SMatthew Dillon */ 846b146f9e5SAlan Cox if ((flags & OBJPC_NOSYNC) && (p->oflags & VPO_NOSYNC)) { 8470cd31a0dSAlan Cox p->oflags &= ~VPO_CLEANCHK; 8484f79d873SMatthew Dillon continue; 8494f79d873SMatthew Dillon } 8504f79d873SMatthew Dillon 851b9b7a4beSMatthew Dillon n = vm_object_page_collect_flush(object, p, 852b9b7a4beSMatthew Dillon curgeneration, pagerflags); 853b9b7a4beSMatthew Dillon if (n == 0) 854b9b7a4beSMatthew Dillon goto rescan; 855b9b7a4beSMatthew Dillon 856b9b7a4beSMatthew Dillon if (object->generation != curgeneration) 857b9b7a4beSMatthew Dillon goto rescan; 858b9b7a4beSMatthew Dillon 859b9b7a4beSMatthew Dillon /* 860b9b7a4beSMatthew Dillon * Try to optimize the next page. If we can't we pick up 861b9b7a4beSMatthew Dillon * our (random) scan where we left off. 862b9b7a4beSMatthew Dillon */ 863b9b7a4beSMatthew Dillon if (msync_flush_flags & MSYNC_FLUSH_SOFTSEQ) { 864b9b7a4beSMatthew Dillon if ((p = vm_page_lookup(object, pi + n)) != NULL) 865b9b7a4beSMatthew Dillon goto again; 866b9b7a4beSMatthew Dillon } 867b9b7a4beSMatthew Dillon } 86835c01631SAlan Cox vm_page_unlock_queues(); 869b9b7a4beSMatthew Dillon #if 0 8708df6bac4SPoul-Henning Kamp VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc); 871b9b7a4beSMatthew Dillon #endif 872b9b7a4beSMatthew Dillon 873b9b7a4beSMatthew Dillon vm_object_clear_flag(object, OBJ_CLEANING); 874b9b7a4beSMatthew Dillon return; 875b9b7a4beSMatthew Dillon } 876b9b7a4beSMatthew Dillon 877b9b7a4beSMatthew Dillon static int 878b9b7a4beSMatthew Dillon vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int curgeneration, int pagerflags) 879b9b7a4beSMatthew Dillon { 880b9b7a4beSMatthew Dillon int runlen; 881b9b7a4beSMatthew Dillon int maxf; 882b9b7a4beSMatthew Dillon int chkb; 883b9b7a4beSMatthew Dillon int maxb; 884b9b7a4beSMatthew Dillon int i; 885b9b7a4beSMatthew Dillon vm_pindex_t pi; 886b9b7a4beSMatthew Dillon vm_page_t maf[vm_pageout_page_count]; 887b9b7a4beSMatthew Dillon vm_page_t mab[vm_pageout_page_count]; 888b9b7a4beSMatthew Dillon vm_page_t ma[vm_pageout_page_count]; 889b9b7a4beSMatthew Dillon 89035c01631SAlan Cox mtx_assert(&vm_page_queue_mtx, MA_OWNED); 891b9b7a4beSMatthew Dillon pi = p->pindex; 892bd82dc74SAlan Cox while (vm_page_sleep_if_busy(p, TRUE, "vpcwai")) { 89335c01631SAlan Cox vm_page_lock_queues(); 8942d8acc0fSJohn Dyson if (object->generation != curgeneration) { 895b9b7a4beSMatthew Dillon return(0); 896f6b04d2bSDavid Greenman } 897bd82dc74SAlan Cox } 898bd7e5f99SJohn Dyson maxf = 0; 899bd7e5f99SJohn Dyson for(i = 1; i < vm_pageout_page_count; i++) { 900b9b7a4beSMatthew Dillon vm_page_t tp; 901b9b7a4beSMatthew Dillon 9028aef1712SMatthew Dillon if ((tp = vm_page_lookup(object, pi + i)) != NULL) { 9039af80719SAlan Cox if ((tp->oflags & VPO_BUSY) || 90443b7990eSMatthew Dillon ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 && 9050cd31a0dSAlan Cox (tp->oflags & VPO_CLEANCHK) == 0) || 906ffc82b0aSJohn Dyson (tp->busy != 0)) 907bd7e5f99SJohn Dyson break; 908bd7e5f99SJohn Dyson vm_page_test_dirty(tp); 909bd7e5f99SJohn Dyson if ((tp->dirty & tp->valid) == 0) { 9100cd31a0dSAlan Cox tp->oflags &= ~VPO_CLEANCHK; 911bd7e5f99SJohn Dyson break; 912bd7e5f99SJohn Dyson } 913bd7e5f99SJohn Dyson maf[ i - 1 ] = tp; 914bd7e5f99SJohn Dyson maxf++; 915bd7e5f99SJohn Dyson continue; 916bd7e5f99SJohn Dyson } 917bd7e5f99SJohn Dyson break; 918bd7e5f99SJohn Dyson } 919aef922f5SJohn Dyson 920bd7e5f99SJohn Dyson maxb = 0; 921bd7e5f99SJohn Dyson chkb = vm_pageout_page_count - maxf; 922bd7e5f99SJohn Dyson if (chkb) { 923bd7e5f99SJohn Dyson for(i = 1; i < chkb;i++) { 924b9b7a4beSMatthew Dillon vm_page_t tp; 925b9b7a4beSMatthew Dillon 9268aef1712SMatthew Dillon if ((tp = vm_page_lookup(object, pi - i)) != NULL) { 9279af80719SAlan Cox if ((tp->oflags & VPO_BUSY) || 92843b7990eSMatthew Dillon ((pagerflags & VM_PAGER_IGNORE_CLEANCHK) == 0 && 9290cd31a0dSAlan Cox (tp->oflags & VPO_CLEANCHK) == 0) || 930ffc82b0aSJohn Dyson (tp->busy != 0)) 931bd7e5f99SJohn Dyson break; 932bd7e5f99SJohn Dyson vm_page_test_dirty(tp); 933bd7e5f99SJohn Dyson if ((tp->dirty & tp->valid) == 0) { 9340cd31a0dSAlan Cox tp->oflags &= ~VPO_CLEANCHK; 935bd7e5f99SJohn Dyson break; 936bd7e5f99SJohn Dyson } 937bd7e5f99SJohn Dyson mab[ i - 1 ] = tp; 938bd7e5f99SJohn Dyson maxb++; 939bd7e5f99SJohn Dyson continue; 940bd7e5f99SJohn Dyson } 941bd7e5f99SJohn Dyson break; 942bd7e5f99SJohn Dyson } 943bd7e5f99SJohn Dyson } 944bd7e5f99SJohn Dyson 945bd7e5f99SJohn Dyson for(i = 0; i < maxb; i++) { 946bd7e5f99SJohn Dyson int index = (maxb - i) - 1; 947bd7e5f99SJohn Dyson ma[index] = mab[i]; 9480cd31a0dSAlan Cox ma[index]->oflags &= ~VPO_CLEANCHK; 949bd7e5f99SJohn Dyson } 9500cd31a0dSAlan Cox p->oflags &= ~VPO_CLEANCHK; 951bd7e5f99SJohn Dyson ma[maxb] = p; 952bd7e5f99SJohn Dyson for(i = 0; i < maxf; i++) { 953bd7e5f99SJohn Dyson int index = (maxb + i) + 1; 954bd7e5f99SJohn Dyson ma[index] = maf[i]; 9550cd31a0dSAlan Cox ma[index]->oflags &= ~VPO_CLEANCHK; 956f6b04d2bSDavid Greenman } 957bd7e5f99SJohn Dyson runlen = maxb + maxf + 1; 958cf2819ccSJohn Dyson 9597a935082SAlan Cox vm_pageout_flush(ma, runlen, pagerflags); 960cf2819ccSJohn Dyson for (i = 0; i < runlen; i++) { 961cf2819ccSJohn Dyson if (ma[i]->valid & ma[i]->dirty) { 96278985e42SAlan Cox pmap_remove_write(ma[i]); 9630cd31a0dSAlan Cox ma[i]->oflags |= VPO_CLEANCHK; 964aef922f5SJohn Dyson 965b9b7a4beSMatthew Dillon /* 966b9b7a4beSMatthew Dillon * maxf will end up being the actual number of pages 967b9b7a4beSMatthew Dillon * we wrote out contiguously, non-inclusive of the 968b9b7a4beSMatthew Dillon * first page. We do not count look-behind pages. 969b9b7a4beSMatthew Dillon */ 970b9b7a4beSMatthew Dillon if (i >= maxb + 1 && (maxf > i - maxb - 1)) 971b9b7a4beSMatthew Dillon maxf = i - maxb - 1; 972b9b7a4beSMatthew Dillon } 973b9b7a4beSMatthew Dillon } 974b9b7a4beSMatthew Dillon return(maxf + 1); 97526f9a767SRodney W. Grimes } 976df8bae1dSRodney W. Grimes 9771efb74fbSJohn Dyson /* 978950f8459SAlan Cox * Note that there is absolutely no sense in writing out 979950f8459SAlan Cox * anonymous objects, so we track down the vnode object 980950f8459SAlan Cox * to write out. 981950f8459SAlan Cox * We invalidate (remove) all pages from the address space 982950f8459SAlan Cox * for semantic correctness. 983950f8459SAlan Cox * 984950f8459SAlan Cox * Note: certain anonymous maps, such as MAP_NOSYNC maps, 985950f8459SAlan Cox * may start out with a NULL object. 986950f8459SAlan Cox */ 987950f8459SAlan Cox void 988950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size, 989950f8459SAlan Cox boolean_t syncio, boolean_t invalidate) 990950f8459SAlan Cox { 991950f8459SAlan Cox vm_object_t backing_object; 992950f8459SAlan Cox struct vnode *vp; 9933b582b4eSTor Egge struct mount *mp; 994950f8459SAlan Cox int flags; 995950f8459SAlan Cox 996950f8459SAlan Cox if (object == NULL) 997950f8459SAlan Cox return; 998950f8459SAlan Cox VM_OBJECT_LOCK(object); 999950f8459SAlan Cox while ((backing_object = object->backing_object) != NULL) { 1000950f8459SAlan Cox VM_OBJECT_LOCK(backing_object); 100156e0670fSAlan Cox offset += object->backing_object_offset; 1002950f8459SAlan Cox VM_OBJECT_UNLOCK(object); 1003950f8459SAlan Cox object = backing_object; 1004950f8459SAlan Cox if (object->size < OFF_TO_IDX(offset + size)) 1005950f8459SAlan Cox size = IDX_TO_OFF(object->size) - offset; 1006950f8459SAlan Cox } 1007950f8459SAlan Cox /* 1008950f8459SAlan Cox * Flush pages if writing is allowed, invalidate them 1009950f8459SAlan Cox * if invalidation requested. Pages undergoing I/O 1010950f8459SAlan Cox * will be ignored by vm_object_page_remove(). 1011950f8459SAlan Cox * 1012950f8459SAlan Cox * We cannot lock the vnode and then wait for paging 1013950f8459SAlan Cox * to complete without deadlocking against vm_fault. 1014950f8459SAlan Cox * Instead we simply call vm_object_page_remove() and 1015950f8459SAlan Cox * allow it to block internally on a page-by-page 1016950f8459SAlan Cox * basis when it encounters pages undergoing async 1017950f8459SAlan Cox * I/O. 1018950f8459SAlan Cox */ 1019950f8459SAlan Cox if (object->type == OBJT_VNODE && 1020950f8459SAlan Cox (object->flags & OBJ_MIGHTBEDIRTY) != 0) { 1021ae51ff11SJeff Roberson int vfslocked; 1022950f8459SAlan Cox vp = object->handle; 1023950f8459SAlan Cox VM_OBJECT_UNLOCK(object); 10243b582b4eSTor Egge (void) vn_start_write(vp, &mp, V_WAIT); 1025ae51ff11SJeff Roberson vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1026950f8459SAlan Cox vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread); 1027950f8459SAlan Cox flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 1028950f8459SAlan Cox flags |= invalidate ? OBJPC_INVAL : 0; 1029950f8459SAlan Cox VM_OBJECT_LOCK(object); 1030950f8459SAlan Cox vm_object_page_clean(object, 1031950f8459SAlan Cox OFF_TO_IDX(offset), 1032950f8459SAlan Cox OFF_TO_IDX(offset + size + PAGE_MASK), 1033950f8459SAlan Cox flags); 1034950f8459SAlan Cox VM_OBJECT_UNLOCK(object); 1035950f8459SAlan Cox VOP_UNLOCK(vp, 0, curthread); 1036ae51ff11SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 10373b582b4eSTor Egge vn_finished_write(mp); 1038950f8459SAlan Cox VM_OBJECT_LOCK(object); 1039950f8459SAlan Cox } 1040950f8459SAlan Cox if ((object->type == OBJT_VNODE || 1041950f8459SAlan Cox object->type == OBJT_DEVICE) && invalidate) { 1042874f0135SDoug Rabson boolean_t purge; 1043874f0135SDoug Rabson purge = old_msync || (object->type == OBJT_DEVICE); 1044950f8459SAlan Cox vm_object_page_remove(object, 1045950f8459SAlan Cox OFF_TO_IDX(offset), 1046950f8459SAlan Cox OFF_TO_IDX(offset + size + PAGE_MASK), 1047874f0135SDoug Rabson purge ? FALSE : TRUE); 1048950f8459SAlan Cox } 1049950f8459SAlan Cox VM_OBJECT_UNLOCK(object); 1050950f8459SAlan Cox } 1051950f8459SAlan Cox 1052950f8459SAlan Cox /* 1053867a482dSJohn Dyson * vm_object_madvise: 1054867a482dSJohn Dyson * 1055867a482dSJohn Dyson * Implements the madvise function at the object/page level. 10561c7c3c6aSMatthew Dillon * 1057193b9358SAlan Cox * MADV_WILLNEED (any object) 1058193b9358SAlan Cox * 1059193b9358SAlan Cox * Activate the specified pages if they are resident. 1060193b9358SAlan Cox * 1061193b9358SAlan Cox * MADV_DONTNEED (any object) 1062193b9358SAlan Cox * 1063193b9358SAlan Cox * Deactivate the specified pages if they are resident. 1064193b9358SAlan Cox * 1065193b9358SAlan Cox * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, 1066193b9358SAlan Cox * OBJ_ONEMAPPING only) 1067193b9358SAlan Cox * 1068193b9358SAlan Cox * Deactivate and clean the specified pages if they are 1069193b9358SAlan Cox * resident. This permits the process to reuse the pages 1070193b9358SAlan Cox * without faulting or the kernel to reclaim the pages 1071193b9358SAlan Cox * without I/O. 1072867a482dSJohn Dyson */ 1073867a482dSJohn Dyson void 10741b40f8c0SMatthew Dillon vm_object_madvise(vm_object_t object, vm_pindex_t pindex, int count, int advise) 1075867a482dSJohn Dyson { 10766e20a165SJohn Dyson vm_pindex_t end, tpindex; 107734567de7SAlan Cox vm_object_t backing_object, tobject; 1078867a482dSJohn Dyson vm_page_t m; 1079867a482dSJohn Dyson 1080867a482dSJohn Dyson if (object == NULL) 1081867a482dSJohn Dyson return; 10829b98b796SAlan Cox VM_OBJECT_LOCK(object); 1083867a482dSJohn Dyson end = pindex + count; 10841c7c3c6aSMatthew Dillon /* 10851c7c3c6aSMatthew Dillon * Locate and adjust resident pages 10861c7c3c6aSMatthew Dillon */ 10871c7c3c6aSMatthew Dillon for (; pindex < end; pindex += 1) { 10886e20a165SJohn Dyson relookup: 10896e20a165SJohn Dyson tobject = object; 10906e20a165SJohn Dyson tpindex = pindex; 10916e20a165SJohn Dyson shadowlookup: 109258b4e6ccSAlan Cox /* 109358b4e6ccSAlan Cox * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages 109458b4e6ccSAlan Cox * and those pages must be OBJ_ONEMAPPING. 109558b4e6ccSAlan Cox */ 109658b4e6ccSAlan Cox if (advise == MADV_FREE) { 109758b4e6ccSAlan Cox if ((tobject->type != OBJT_DEFAULT && 109858b4e6ccSAlan Cox tobject->type != OBJT_SWAP) || 109958b4e6ccSAlan Cox (tobject->flags & OBJ_ONEMAPPING) == 0) { 110034567de7SAlan Cox goto unlock_tobject; 11016e20a165SJohn Dyson } 110258b4e6ccSAlan Cox } 11031c7c3c6aSMatthew Dillon m = vm_page_lookup(tobject, tpindex); 11047bfda801SAlan Cox if (m == NULL && advise == MADV_WILLNEED) { 11057bfda801SAlan Cox /* 11067bfda801SAlan Cox * If the page is cached, reactivate it. 11077bfda801SAlan Cox */ 1108f3a2ed4bSAlan Cox m = vm_page_alloc(tobject, tpindex, VM_ALLOC_IFCACHED | 1109f3a2ed4bSAlan Cox VM_ALLOC_NOBUSY); 11107bfda801SAlan Cox } 11111c7c3c6aSMatthew Dillon if (m == NULL) { 11121ce137beSMatthew Dillon /* 11131ce137beSMatthew Dillon * There may be swap even if there is no backing page 11141ce137beSMatthew Dillon */ 11151ce137beSMatthew Dillon if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 11161ce137beSMatthew Dillon swap_pager_freespace(tobject, tpindex, 1); 11171ce137beSMatthew Dillon /* 11181ce137beSMatthew Dillon * next object 11191ce137beSMatthew Dillon */ 112034567de7SAlan Cox backing_object = tobject->backing_object; 112134567de7SAlan Cox if (backing_object == NULL) 112234567de7SAlan Cox goto unlock_tobject; 112334567de7SAlan Cox VM_OBJECT_LOCK(backing_object); 112456e0670fSAlan Cox tpindex += OFF_TO_IDX(tobject->backing_object_offset); 11259b98b796SAlan Cox if (tobject != object) 112634567de7SAlan Cox VM_OBJECT_UNLOCK(tobject); 112734567de7SAlan Cox tobject = backing_object; 11286e20a165SJohn Dyson goto shadowlookup; 11296e20a165SJohn Dyson } 1130867a482dSJohn Dyson /* 1131867a482dSJohn Dyson * If the page is busy or not in a normal active state, 11328b03c8edSMatthew Dillon * we skip it. If the page is not managed there are no 11338b03c8edSMatthew Dillon * page queues to mess with. Things can break if we mess 11348b03c8edSMatthew Dillon * with pages in any of the below states. 1135867a482dSJohn Dyson */ 113632585dd6SAlan Cox vm_page_lock_queues(); 113732585dd6SAlan Cox if (m->hold_count || 11381c7c3c6aSMatthew Dillon m->wire_count || 11398b03c8edSMatthew Dillon (m->flags & PG_UNMANAGED) || 114032585dd6SAlan Cox m->valid != VM_PAGE_BITS_ALL) { 114132585dd6SAlan Cox vm_page_unlock_queues(); 114234567de7SAlan Cox goto unlock_tobject; 11436e20a165SJohn Dyson } 11449af80719SAlan Cox if ((m->oflags & VPO_BUSY) || m->busy) { 11455786be7cSAlan Cox vm_page_flag_set(m, PG_REFERENCED); 114691449ce9SAlan Cox vm_page_unlock_queues(); 11479b98b796SAlan Cox if (object != tobject) 11489b98b796SAlan Cox VM_OBJECT_UNLOCK(object); 11495786be7cSAlan Cox m->oflags |= VPO_WANTED; 115091449ce9SAlan Cox msleep(m, VM_OBJECT_MTX(tobject), PDROP | PVM, "madvpo", 0); 11519b98b796SAlan Cox VM_OBJECT_LOCK(object); 11526e20a165SJohn Dyson goto relookup; 115334567de7SAlan Cox } 1154867a482dSJohn Dyson if (advise == MADV_WILLNEED) { 1155867a482dSJohn Dyson vm_page_activate(m); 11566e20a165SJohn Dyson } else if (advise == MADV_DONTNEED) { 1157479112dfSMatthew Dillon vm_page_dontneed(m); 11580a47b48bSJohn Dyson } else if (advise == MADV_FREE) { 11591c7c3c6aSMatthew Dillon /* 11602aaeadf8SMatthew Dillon * Mark the page clean. This will allow the page 11612aaeadf8SMatthew Dillon * to be freed up by the system. However, such pages 11622aaeadf8SMatthew Dillon * are often reused quickly by malloc()/free() 11632aaeadf8SMatthew Dillon * so we do not do anything that would cause 11642aaeadf8SMatthew Dillon * a page fault if we can help it. 11652aaeadf8SMatthew Dillon * 11662aaeadf8SMatthew Dillon * Specifically, we do not try to actually free 11672aaeadf8SMatthew Dillon * the page now nor do we try to put it in the 11682aaeadf8SMatthew Dillon * cache (which would cause a page fault on reuse). 116941c67e12SMatthew Dillon * 117041c67e12SMatthew Dillon * But we do make the page is freeable as we 117141c67e12SMatthew Dillon * can without actually taking the step of unmapping 117241c67e12SMatthew Dillon * it. 11731c7c3c6aSMatthew Dillon */ 11740385347cSPeter Wemm pmap_clear_modify(m); 11756e20a165SJohn Dyson m->dirty = 0; 117641c67e12SMatthew Dillon m->act_count = 0; 1177479112dfSMatthew Dillon vm_page_dontneed(m); 1178867a482dSJohn Dyson } 11792999e9faSAlan Cox vm_page_unlock_queues(); 11802999e9faSAlan Cox if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 11812999e9faSAlan Cox swap_pager_freespace(tobject, tpindex, 1); 118234567de7SAlan Cox unlock_tobject: 11839b98b796SAlan Cox if (tobject != object) 118434567de7SAlan Cox VM_OBJECT_UNLOCK(tobject); 1185867a482dSJohn Dyson } 11869b98b796SAlan Cox VM_OBJECT_UNLOCK(object); 1187867a482dSJohn Dyson } 1188867a482dSJohn Dyson 1189867a482dSJohn Dyson /* 1190df8bae1dSRodney W. Grimes * vm_object_shadow: 1191df8bae1dSRodney W. Grimes * 1192df8bae1dSRodney W. Grimes * Create a new object which is backed by the 1193df8bae1dSRodney W. Grimes * specified existing object range. The source 1194df8bae1dSRodney W. Grimes * object reference is deallocated. 1195df8bae1dSRodney W. Grimes * 1196df8bae1dSRodney W. Grimes * The new object and offset into that object 1197df8bae1dSRodney W. Grimes * are returned in the source parameters. 1198df8bae1dSRodney W. Grimes */ 119926f9a767SRodney W. Grimes void 12001b40f8c0SMatthew Dillon vm_object_shadow( 12011b40f8c0SMatthew Dillon vm_object_t *object, /* IN/OUT */ 12021b40f8c0SMatthew Dillon vm_ooffset_t *offset, /* IN/OUT */ 12031b40f8c0SMatthew Dillon vm_size_t length) 1204df8bae1dSRodney W. Grimes { 1205d031cff1SMatthew Dillon vm_object_t source; 1206d031cff1SMatthew Dillon vm_object_t result; 1207df8bae1dSRodney W. Grimes 1208df8bae1dSRodney W. Grimes source = *object; 1209df8bae1dSRodney W. Grimes 1210df8bae1dSRodney W. Grimes /* 12119a2f6362SAlan Cox * Don't create the new object if the old object isn't shared. 12129a2f6362SAlan Cox */ 1213570a2f4aSAlan Cox if (source != NULL) { 1214570a2f4aSAlan Cox VM_OBJECT_LOCK(source); 1215570a2f4aSAlan Cox if (source->ref_count == 1 && 12169a2f6362SAlan Cox source->handle == NULL && 12179a2f6362SAlan Cox (source->type == OBJT_DEFAULT || 12189917e010SAlan Cox source->type == OBJT_SWAP)) { 1219570a2f4aSAlan Cox VM_OBJECT_UNLOCK(source); 12209a2f6362SAlan Cox return; 12219917e010SAlan Cox } 1222570a2f4aSAlan Cox VM_OBJECT_UNLOCK(source); 1223570a2f4aSAlan Cox } 12249a2f6362SAlan Cox 12259a2f6362SAlan Cox /* 1226570a2f4aSAlan Cox * Allocate a new object with the given length. 1227df8bae1dSRodney W. Grimes */ 1228971dd342SAlfred Perlstein result = vm_object_allocate(OBJT_DEFAULT, length); 1229df8bae1dSRodney W. Grimes 1230df8bae1dSRodney W. Grimes /* 12310d94caffSDavid Greenman * The new object shadows the source object, adding a reference to it. 12320d94caffSDavid Greenman * Our caller changes his reference to point to the new object, 12330d94caffSDavid Greenman * removing a reference to the source object. Net result: no change 12340d94caffSDavid Greenman * of reference count. 12359b09fe24SMatthew Dillon * 12369b09fe24SMatthew Dillon * Try to optimize the result object's page color when shadowing 1237956f3135SPhilippe Charnier * in order to maintain page coloring consistency in the combined 12389b09fe24SMatthew Dillon * shadowed object. 1239df8bae1dSRodney W. Grimes */ 124024a1cce3SDavid Greenman result->backing_object = source; 12419174ca7bSTor Egge /* 12429174ca7bSTor Egge * Store the offset into the source object, and fix up the offset into 12439174ca7bSTor Egge * the new object. 12449174ca7bSTor Egge */ 12459174ca7bSTor Egge result->backing_object_offset = *offset; 1246570a2f4aSAlan Cox if (source != NULL) { 1247570a2f4aSAlan Cox VM_OBJECT_LOCK(source); 12481c500307SAlan Cox LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list); 1249eaf13dd7SJohn Dyson source->shadow_count++; 1250eaf13dd7SJohn Dyson source->generation++; 1251ed4fe4f4SJeff Roberson result->flags |= source->flags & OBJ_NEEDGIANT; 1252570a2f4aSAlan Cox VM_OBJECT_UNLOCK(source); 1253de5f6a77SJohn Dyson } 1254df8bae1dSRodney W. Grimes 1255df8bae1dSRodney W. Grimes 1256df8bae1dSRodney W. Grimes /* 1257df8bae1dSRodney W. Grimes * Return the new things 1258df8bae1dSRodney W. Grimes */ 1259df8bae1dSRodney W. Grimes *offset = 0; 1260df8bae1dSRodney W. Grimes *object = result; 1261df8bae1dSRodney W. Grimes } 1262df8bae1dSRodney W. Grimes 1263c5aaa06dSAlan Cox /* 1264c5aaa06dSAlan Cox * vm_object_split: 1265c5aaa06dSAlan Cox * 1266c5aaa06dSAlan Cox * Split the pages in a map entry into a new object. This affords 1267c5aaa06dSAlan Cox * easier removal of unused pages, and keeps object inheritance from 1268c5aaa06dSAlan Cox * being a negative impact on memory usage. 1269c5aaa06dSAlan Cox */ 1270c5aaa06dSAlan Cox void 1271c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry) 1272c5aaa06dSAlan Cox { 127373000556SAlan Cox vm_page_t m, m_next; 1274c5aaa06dSAlan Cox vm_object_t orig_object, new_object, source; 127573000556SAlan Cox vm_pindex_t idx, offidxstart; 127673000556SAlan Cox vm_size_t size; 1277c5aaa06dSAlan Cox 1278c5aaa06dSAlan Cox orig_object = entry->object.vm_object; 1279c5aaa06dSAlan Cox if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP) 1280c5aaa06dSAlan Cox return; 1281c5aaa06dSAlan Cox if (orig_object->ref_count <= 1) 1282c5aaa06dSAlan Cox return; 12834da9f125SAlan Cox VM_OBJECT_UNLOCK(orig_object); 1284c5aaa06dSAlan Cox 12854da9f125SAlan Cox offidxstart = OFF_TO_IDX(entry->offset); 128695442adfSAlan Cox size = atop(entry->end - entry->start); 1287c5aaa06dSAlan Cox 12884da9f125SAlan Cox /* 12894da9f125SAlan Cox * If swap_pager_copy() is later called, it will convert new_object 12904da9f125SAlan Cox * into a swap object. 12914da9f125SAlan Cox */ 12924da9f125SAlan Cox new_object = vm_object_allocate(OBJT_DEFAULT, size); 1293c5aaa06dSAlan Cox 1294c5474b8fSAlan Cox /* 1295c5474b8fSAlan Cox * At this point, the new object is still private, so the order in 1296c5474b8fSAlan Cox * which the original and new objects are locked does not matter. 1297c5474b8fSAlan Cox */ 129863f6cefcSAlan Cox VM_OBJECT_LOCK(new_object); 129963f6cefcSAlan Cox VM_OBJECT_LOCK(orig_object); 1300c5aaa06dSAlan Cox source = orig_object->backing_object; 1301c5aaa06dSAlan Cox if (source != NULL) { 13028e3a76fbSAlan Cox VM_OBJECT_LOCK(source); 130319c244d0SAlan Cox if ((source->flags & OBJ_DEAD) != 0) { 130419c244d0SAlan Cox VM_OBJECT_UNLOCK(source); 130519c244d0SAlan Cox VM_OBJECT_UNLOCK(orig_object); 130619c244d0SAlan Cox VM_OBJECT_UNLOCK(new_object); 130719c244d0SAlan Cox vm_object_deallocate(new_object); 130819c244d0SAlan Cox VM_OBJECT_LOCK(orig_object); 130919c244d0SAlan Cox return; 131019c244d0SAlan Cox } 13111c500307SAlan Cox LIST_INSERT_HEAD(&source->shadow_head, 1312c5aaa06dSAlan Cox new_object, shadow_list); 13138e3a76fbSAlan Cox source->shadow_count++; 13148e3a76fbSAlan Cox source->generation++; 1315b921a12bSAlan Cox vm_object_reference_locked(source); /* for new_object */ 1316c5aaa06dSAlan Cox vm_object_clear_flag(source, OBJ_ONEMAPPING); 1317e2479b4fSAlan Cox VM_OBJECT_UNLOCK(source); 1318c5aaa06dSAlan Cox new_object->backing_object_offset = 13194da9f125SAlan Cox orig_object->backing_object_offset + entry->offset; 1320c5aaa06dSAlan Cox new_object->backing_object = source; 1321c5aaa06dSAlan Cox } 1322b8a0b997SJeff Roberson new_object->flags |= orig_object->flags & OBJ_NEEDGIANT; 1323c5aaa06dSAlan Cox retry: 132473000556SAlan Cox if ((m = TAILQ_FIRST(&orig_object->memq)) != NULL) { 132573000556SAlan Cox if (m->pindex < offidxstart) { 132673000556SAlan Cox m = vm_page_splay(offidxstart, orig_object->root); 132773000556SAlan Cox if ((orig_object->root = m)->pindex < offidxstart) 132873000556SAlan Cox m = TAILQ_NEXT(m, listq); 132973000556SAlan Cox } 133073000556SAlan Cox } 133173000556SAlan Cox vm_page_lock_queues(); 133273000556SAlan Cox for (; m != NULL && (idx = m->pindex - offidxstart) < size; 133373000556SAlan Cox m = m_next) { 133473000556SAlan Cox m_next = TAILQ_NEXT(m, listq); 1335c5aaa06dSAlan Cox 1336c5aaa06dSAlan Cox /* 1337c5aaa06dSAlan Cox * We must wait for pending I/O to complete before we can 1338c5aaa06dSAlan Cox * rename the page. 1339c5aaa06dSAlan Cox * 1340c5aaa06dSAlan Cox * We do not have to VM_PROT_NONE the page as mappings should 1341c5aaa06dSAlan Cox * not be changed by this operation. 1342c5aaa06dSAlan Cox */ 13439af80719SAlan Cox if ((m->oflags & VPO_BUSY) || m->busy) { 13445786be7cSAlan Cox vm_page_flag_set(m, PG_REFERENCED); 134591449ce9SAlan Cox vm_page_unlock_queues(); 1346de33beddSAlan Cox VM_OBJECT_UNLOCK(new_object); 13475786be7cSAlan Cox m->oflags |= VPO_WANTED; 1348c5474b8fSAlan Cox msleep(m, VM_OBJECT_MTX(orig_object), PVM, "spltwt", 0); 1349de33beddSAlan Cox VM_OBJECT_LOCK(new_object); 1350c5aaa06dSAlan Cox goto retry; 1351de33beddSAlan Cox } 1352c5aaa06dSAlan Cox vm_page_rename(m, new_object, idx); 1353c5aaa06dSAlan Cox /* page automatically made dirty by rename and cache handled */ 1354c5aaa06dSAlan Cox vm_page_busy(m); 1355c5aaa06dSAlan Cox } 13565ba514bcSAlan Cox vm_page_unlock_queues(); 1357d7a013c3SAlan Cox if (orig_object->type == OBJT_SWAP) { 1358c5aaa06dSAlan Cox /* 1359c7c8dd7eSAlan Cox * swap_pager_copy() can sleep, in which case the orig_object's 1360c7c8dd7eSAlan Cox * and new_object's locks are released and reacquired. 1361c5aaa06dSAlan Cox */ 1362c5aaa06dSAlan Cox swap_pager_copy(orig_object, new_object, offidxstart, 0); 13637bfda801SAlan Cox 13647bfda801SAlan Cox /* 13657bfda801SAlan Cox * Transfer any cached pages from orig_object to new_object. 13667bfda801SAlan Cox */ 13677bfda801SAlan Cox if (__predict_false(orig_object->cache != NULL)) 13687bfda801SAlan Cox vm_page_cache_transfer(orig_object, offidxstart, 13697bfda801SAlan Cox new_object); 1370c5aaa06dSAlan Cox } 1371d7a013c3SAlan Cox VM_OBJECT_UNLOCK(orig_object); 1372c7118ed6SAlan Cox TAILQ_FOREACH(m, &new_object->memq, listq) 1373c5aaa06dSAlan Cox vm_page_wakeup(m); 1374c7c8dd7eSAlan Cox VM_OBJECT_UNLOCK(new_object); 1375c5aaa06dSAlan Cox entry->object.vm_object = new_object; 1376c5aaa06dSAlan Cox entry->offset = 0LL; 1377c5aaa06dSAlan Cox vm_object_deallocate(orig_object); 13784da9f125SAlan Cox VM_OBJECT_LOCK(new_object); 1379c5aaa06dSAlan Cox } 1380c5aaa06dSAlan Cox 13812ad1a3f7SMatthew Dillon #define OBSC_TEST_ALL_SHADOWED 0x0001 13822ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_NOWAIT 0x0002 13832ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_WAIT 0x0004 13842ad1a3f7SMatthew Dillon 1385b4ae4780SPoul-Henning Kamp static int 13862ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op) 13872ad1a3f7SMatthew Dillon { 13882ad1a3f7SMatthew Dillon int r = 1; 13892ad1a3f7SMatthew Dillon vm_page_t p; 13902ad1a3f7SMatthew Dillon vm_object_t backing_object; 13912ad1a3f7SMatthew Dillon vm_pindex_t backing_offset_index; 13922ad1a3f7SMatthew Dillon 13937ca33ad1SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 13947ca33ad1SAlan Cox VM_OBJECT_LOCK_ASSERT(object->backing_object, MA_OWNED); 13952ad1a3f7SMatthew Dillon 13962ad1a3f7SMatthew Dillon backing_object = object->backing_object; 13972ad1a3f7SMatthew Dillon backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 13982ad1a3f7SMatthew Dillon 13992ad1a3f7SMatthew Dillon /* 14002ad1a3f7SMatthew Dillon * Initial conditions 14012ad1a3f7SMatthew Dillon */ 14022ad1a3f7SMatthew Dillon if (op & OBSC_TEST_ALL_SHADOWED) { 14032ad1a3f7SMatthew Dillon /* 14047bfda801SAlan Cox * We do not want to have to test for the existence of cache 14057bfda801SAlan Cox * or swap pages in the backing object. XXX but with the 14062ad1a3f7SMatthew Dillon * new swapper this would be pretty easy to do. 14072ad1a3f7SMatthew Dillon * 14082ad1a3f7SMatthew Dillon * XXX what about anonymous MAP_SHARED memory that hasn't 14092ad1a3f7SMatthew Dillon * been ZFOD faulted yet? If we do not test for this, the 14102ad1a3f7SMatthew Dillon * shadow test may succeed! XXX 14112ad1a3f7SMatthew Dillon */ 14122ad1a3f7SMatthew Dillon if (backing_object->type != OBJT_DEFAULT) { 14132ad1a3f7SMatthew Dillon return (0); 14142ad1a3f7SMatthew Dillon } 14152ad1a3f7SMatthew Dillon } 14162ad1a3f7SMatthew Dillon if (op & OBSC_COLLAPSE_WAIT) { 14172ad1a3f7SMatthew Dillon vm_object_set_flag(backing_object, OBJ_DEAD); 14182ad1a3f7SMatthew Dillon } 14192ad1a3f7SMatthew Dillon 14202ad1a3f7SMatthew Dillon /* 14212ad1a3f7SMatthew Dillon * Our scan 14222ad1a3f7SMatthew Dillon */ 14232ad1a3f7SMatthew Dillon p = TAILQ_FIRST(&backing_object->memq); 14242ad1a3f7SMatthew Dillon while (p) { 14252ad1a3f7SMatthew Dillon vm_page_t next = TAILQ_NEXT(p, listq); 14262ad1a3f7SMatthew Dillon vm_pindex_t new_pindex = p->pindex - backing_offset_index; 14272ad1a3f7SMatthew Dillon 14282ad1a3f7SMatthew Dillon if (op & OBSC_TEST_ALL_SHADOWED) { 14292ad1a3f7SMatthew Dillon vm_page_t pp; 14302ad1a3f7SMatthew Dillon 14312ad1a3f7SMatthew Dillon /* 14322ad1a3f7SMatthew Dillon * Ignore pages outside the parent object's range 14332ad1a3f7SMatthew Dillon * and outside the parent object's mapping of the 14342ad1a3f7SMatthew Dillon * backing object. 14352ad1a3f7SMatthew Dillon * 14362ad1a3f7SMatthew Dillon * note that we do not busy the backing object's 14372ad1a3f7SMatthew Dillon * page. 14382ad1a3f7SMatthew Dillon */ 14392ad1a3f7SMatthew Dillon if ( 14402ad1a3f7SMatthew Dillon p->pindex < backing_offset_index || 14412ad1a3f7SMatthew Dillon new_pindex >= object->size 14422ad1a3f7SMatthew Dillon ) { 14432ad1a3f7SMatthew Dillon p = next; 14442ad1a3f7SMatthew Dillon continue; 14452ad1a3f7SMatthew Dillon } 14462ad1a3f7SMatthew Dillon 14472ad1a3f7SMatthew Dillon /* 14482ad1a3f7SMatthew Dillon * See if the parent has the page or if the parent's 14492ad1a3f7SMatthew Dillon * object pager has the page. If the parent has the 14502ad1a3f7SMatthew Dillon * page but the page is not valid, the parent's 14512ad1a3f7SMatthew Dillon * object pager must have the page. 14522ad1a3f7SMatthew Dillon * 14532ad1a3f7SMatthew Dillon * If this fails, the parent does not completely shadow 14542ad1a3f7SMatthew Dillon * the object and we might as well give up now. 14552ad1a3f7SMatthew Dillon */ 14562ad1a3f7SMatthew Dillon 14572ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 14582ad1a3f7SMatthew Dillon if ( 14592ad1a3f7SMatthew Dillon (pp == NULL || pp->valid == 0) && 14602ad1a3f7SMatthew Dillon !vm_pager_has_page(object, new_pindex, NULL, NULL) 14612ad1a3f7SMatthew Dillon ) { 14622ad1a3f7SMatthew Dillon r = 0; 14632ad1a3f7SMatthew Dillon break; 14642ad1a3f7SMatthew Dillon } 14652ad1a3f7SMatthew Dillon } 14662ad1a3f7SMatthew Dillon 14672ad1a3f7SMatthew Dillon /* 14682ad1a3f7SMatthew Dillon * Check for busy page 14692ad1a3f7SMatthew Dillon */ 14702ad1a3f7SMatthew Dillon if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) { 14712ad1a3f7SMatthew Dillon vm_page_t pp; 14722ad1a3f7SMatthew Dillon 14732ad1a3f7SMatthew Dillon if (op & OBSC_COLLAPSE_NOWAIT) { 14749af80719SAlan Cox if ((p->oflags & VPO_BUSY) || 14752ad1a3f7SMatthew Dillon !p->valid || 147600f9e8b4SAlan Cox p->busy) { 14772ad1a3f7SMatthew Dillon p = next; 14782ad1a3f7SMatthew Dillon continue; 14792ad1a3f7SMatthew Dillon } 14802ad1a3f7SMatthew Dillon } else if (op & OBSC_COLLAPSE_WAIT) { 14819af80719SAlan Cox if ((p->oflags & VPO_BUSY) || p->busy) { 1482c6ec6a7cSAlan Cox vm_page_lock_queues(); 14835786be7cSAlan Cox vm_page_flag_set(p, PG_REFERENCED); 148491449ce9SAlan Cox vm_page_unlock_queues(); 14857ca33ad1SAlan Cox VM_OBJECT_UNLOCK(object); 14865786be7cSAlan Cox p->oflags |= VPO_WANTED; 148791449ce9SAlan Cox msleep(p, VM_OBJECT_MTX(backing_object), 14887ca33ad1SAlan Cox PDROP | PVM, "vmocol", 0); 14897ca33ad1SAlan Cox VM_OBJECT_LOCK(object); 14907ca33ad1SAlan Cox VM_OBJECT_LOCK(backing_object); 14912ad1a3f7SMatthew Dillon /* 14922ad1a3f7SMatthew Dillon * If we slept, anything could have 14932ad1a3f7SMatthew Dillon * happened. Since the object is 14942ad1a3f7SMatthew Dillon * marked dead, the backing offset 14952ad1a3f7SMatthew Dillon * should not have changed so we 14962ad1a3f7SMatthew Dillon * just restart our scan. 14972ad1a3f7SMatthew Dillon */ 14982ad1a3f7SMatthew Dillon p = TAILQ_FIRST(&backing_object->memq); 14992ad1a3f7SMatthew Dillon continue; 15002ad1a3f7SMatthew Dillon } 15012ad1a3f7SMatthew Dillon } 15022ad1a3f7SMatthew Dillon 15032ad1a3f7SMatthew Dillon KASSERT( 15042ad1a3f7SMatthew Dillon p->object == backing_object, 15058e99783bSAlan Cox ("vm_object_backing_scan: object mismatch") 15062ad1a3f7SMatthew Dillon ); 15072ad1a3f7SMatthew Dillon 15082ad1a3f7SMatthew Dillon /* 15092ad1a3f7SMatthew Dillon * Destroy any associated swap 15102ad1a3f7SMatthew Dillon */ 15112ad1a3f7SMatthew Dillon if (backing_object->type == OBJT_SWAP) { 15122ad1a3f7SMatthew Dillon swap_pager_freespace( 15132ad1a3f7SMatthew Dillon backing_object, 15142ad1a3f7SMatthew Dillon p->pindex, 15152ad1a3f7SMatthew Dillon 1 15162ad1a3f7SMatthew Dillon ); 15172ad1a3f7SMatthew Dillon } 15182ad1a3f7SMatthew Dillon 15192ad1a3f7SMatthew Dillon if ( 15202ad1a3f7SMatthew Dillon p->pindex < backing_offset_index || 15212ad1a3f7SMatthew Dillon new_pindex >= object->size 15222ad1a3f7SMatthew Dillon ) { 15232ad1a3f7SMatthew Dillon /* 15242ad1a3f7SMatthew Dillon * Page is out of the parent object's range, we 15252ad1a3f7SMatthew Dillon * can simply destroy it. 15262ad1a3f7SMatthew Dillon */ 15276a684ecfSAlan Cox vm_page_lock_queues(); 1528f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1529f6d89838SAlan Cox ("freeing mapped page %p", p)); 1530f6d89838SAlan Cox if (p->wire_count == 0) 15312ad1a3f7SMatthew Dillon vm_page_free(p); 1532f6d89838SAlan Cox else 1533f6d89838SAlan Cox vm_page_remove(p); 15346a684ecfSAlan Cox vm_page_unlock_queues(); 15352ad1a3f7SMatthew Dillon p = next; 15362ad1a3f7SMatthew Dillon continue; 15372ad1a3f7SMatthew Dillon } 15382ad1a3f7SMatthew Dillon 15392ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 15402ad1a3f7SMatthew Dillon if ( 15412ad1a3f7SMatthew Dillon pp != NULL || 15422ad1a3f7SMatthew Dillon vm_pager_has_page(object, new_pindex, NULL, NULL) 15432ad1a3f7SMatthew Dillon ) { 15442ad1a3f7SMatthew Dillon /* 15452ad1a3f7SMatthew Dillon * page already exists in parent OR swap exists 15462ad1a3f7SMatthew Dillon * for this location in the parent. Destroy 15472ad1a3f7SMatthew Dillon * the original page from the backing object. 15482ad1a3f7SMatthew Dillon * 15492ad1a3f7SMatthew Dillon * Leave the parent's page alone 15502ad1a3f7SMatthew Dillon */ 15516a684ecfSAlan Cox vm_page_lock_queues(); 1552f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1553f6d89838SAlan Cox ("freeing mapped page %p", p)); 1554f6d89838SAlan Cox if (p->wire_count == 0) 15552ad1a3f7SMatthew Dillon vm_page_free(p); 1556f6d89838SAlan Cox else 1557f6d89838SAlan Cox vm_page_remove(p); 15586a684ecfSAlan Cox vm_page_unlock_queues(); 15592ad1a3f7SMatthew Dillon p = next; 15602ad1a3f7SMatthew Dillon continue; 15612ad1a3f7SMatthew Dillon } 15622ad1a3f7SMatthew Dillon 15632ad1a3f7SMatthew Dillon /* 15642ad1a3f7SMatthew Dillon * Page does not exist in parent, rename the 15652ad1a3f7SMatthew Dillon * page from the backing object to the main object. 1566d1bf5d56SMatthew Dillon * 1567d1bf5d56SMatthew Dillon * If the page was mapped to a process, it can remain 1568d1bf5d56SMatthew Dillon * mapped through the rename. 15692ad1a3f7SMatthew Dillon */ 1570a28cc55eSAlan Cox vm_page_lock_queues(); 15712ad1a3f7SMatthew Dillon vm_page_rename(p, object, new_pindex); 1572a28cc55eSAlan Cox vm_page_unlock_queues(); 15732ad1a3f7SMatthew Dillon /* page automatically made dirty by rename */ 15742ad1a3f7SMatthew Dillon } 15752ad1a3f7SMatthew Dillon p = next; 15762ad1a3f7SMatthew Dillon } 15772ad1a3f7SMatthew Dillon return (r); 15782ad1a3f7SMatthew Dillon } 15792ad1a3f7SMatthew Dillon 1580df8bae1dSRodney W. Grimes 1581df8bae1dSRodney W. Grimes /* 15822fe6e4d7SDavid Greenman * this version of collapse allows the operation to occur earlier and 15832fe6e4d7SDavid Greenman * when paging_in_progress is true for an object... This is not a complete 15842fe6e4d7SDavid Greenman * operation, but should plug 99.9% of the rest of the leaks. 15852fe6e4d7SDavid Greenman */ 15862fe6e4d7SDavid Greenman static void 15871b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object) 15882fe6e4d7SDavid Greenman { 15892ad1a3f7SMatthew Dillon vm_object_t backing_object = object->backing_object; 15902fe6e4d7SDavid Greenman 159106ecade7SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 159206ecade7SAlan Cox VM_OBJECT_LOCK_ASSERT(backing_object, MA_OWNED); 15931b40f8c0SMatthew Dillon 15942fe6e4d7SDavid Greenman if (backing_object->ref_count != 1) 15952fe6e4d7SDavid Greenman return; 15962fe6e4d7SDavid Greenman 15972ad1a3f7SMatthew Dillon vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT); 15982fe6e4d7SDavid Greenman } 15992fe6e4d7SDavid Greenman 1600df8bae1dSRodney W. Grimes /* 1601df8bae1dSRodney W. Grimes * vm_object_collapse: 1602df8bae1dSRodney W. Grimes * 1603df8bae1dSRodney W. Grimes * Collapse an object with the object backing it. 1604df8bae1dSRodney W. Grimes * Pages in the backing object are moved into the 1605df8bae1dSRodney W. Grimes * parent, and the backing object is deallocated. 1606df8bae1dSRodney W. Grimes */ 160726f9a767SRodney W. Grimes void 16081b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object) 1609df8bae1dSRodney W. Grimes { 1610d7fc2210SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 161123955314SAlfred Perlstein 1612df8bae1dSRodney W. Grimes while (TRUE) { 16132ad1a3f7SMatthew Dillon vm_object_t backing_object; 16142ad1a3f7SMatthew Dillon 1615df8bae1dSRodney W. Grimes /* 1616df8bae1dSRodney W. Grimes * Verify that the conditions are right for collapse: 1617df8bae1dSRodney W. Grimes * 16182ad1a3f7SMatthew Dillon * The object exists and the backing object exists. 1619df8bae1dSRodney W. Grimes */ 162024a1cce3SDavid Greenman if ((backing_object = object->backing_object) == NULL) 16212ad1a3f7SMatthew Dillon break; 1622df8bae1dSRodney W. Grimes 1623f919ebdeSDavid Greenman /* 1624f919ebdeSDavid Greenman * we check the backing object first, because it is most likely 162524a1cce3SDavid Greenman * not collapsable. 1626f919ebdeSDavid Greenman */ 162740b808a8SAlan Cox VM_OBJECT_LOCK(backing_object); 162824a1cce3SDavid Greenman if (backing_object->handle != NULL || 162924a1cce3SDavid Greenman (backing_object->type != OBJT_DEFAULT && 163024a1cce3SDavid Greenman backing_object->type != OBJT_SWAP) || 1631f919ebdeSDavid Greenman (backing_object->flags & OBJ_DEAD) || 163224a1cce3SDavid Greenman object->handle != NULL || 163324a1cce3SDavid Greenman (object->type != OBJT_DEFAULT && 163424a1cce3SDavid Greenman object->type != OBJT_SWAP) || 163524a1cce3SDavid Greenman (object->flags & OBJ_DEAD)) { 163640b808a8SAlan Cox VM_OBJECT_UNLOCK(backing_object); 16372ad1a3f7SMatthew Dillon break; 163824a1cce3SDavid Greenman } 16399b4814bbSDavid Greenman 16402ad1a3f7SMatthew Dillon if ( 16412ad1a3f7SMatthew Dillon object->paging_in_progress != 0 || 16422ad1a3f7SMatthew Dillon backing_object->paging_in_progress != 0 16432ad1a3f7SMatthew Dillon ) { 1644b9921222SDavid Greenman vm_object_qcollapse(object); 164540b808a8SAlan Cox VM_OBJECT_UNLOCK(backing_object); 16462ad1a3f7SMatthew Dillon break; 1647df8bae1dSRodney W. Grimes } 164826f9a767SRodney W. Grimes /* 16490d94caffSDavid Greenman * We know that we can either collapse the backing object (if 16502ad1a3f7SMatthew Dillon * the parent is the only reference to it) or (perhaps) have 16512ad1a3f7SMatthew Dillon * the parent bypass the object if the parent happens to shadow 16522ad1a3f7SMatthew Dillon * all the resident pages in the entire backing object. 16532ad1a3f7SMatthew Dillon * 16542ad1a3f7SMatthew Dillon * This is ignoring pager-backed pages such as swap pages. 16552ad1a3f7SMatthew Dillon * vm_object_backing_scan fails the shadowing test in this 16562ad1a3f7SMatthew Dillon * case. 1657df8bae1dSRodney W. Grimes */ 1658df8bae1dSRodney W. Grimes if (backing_object->ref_count == 1) { 1659df8bae1dSRodney W. Grimes /* 16602ad1a3f7SMatthew Dillon * If there is exactly one reference to the backing 16612ad1a3f7SMatthew Dillon * object, we can collapse it into the parent. 1662df8bae1dSRodney W. Grimes */ 16632ad1a3f7SMatthew Dillon vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT); 1664df8bae1dSRodney W. Grimes 1665df8bae1dSRodney W. Grimes /* 1666df8bae1dSRodney W. Grimes * Move the pager from backing_object to object. 1667df8bae1dSRodney W. Grimes */ 16686be36525SAlan Cox if (backing_object->type == OBJT_SWAP) { 166924a1cce3SDavid Greenman /* 1670c7c8dd7eSAlan Cox * swap_pager_copy() can sleep, in which case 1671c7c8dd7eSAlan Cox * the backing_object's and object's locks are 1672c7c8dd7eSAlan Cox * released and reacquired. 167324a1cce3SDavid Greenman */ 16741c7c3c6aSMatthew Dillon swap_pager_copy( 16751c7c3c6aSMatthew Dillon backing_object, 16761c7c3c6aSMatthew Dillon object, 16771c7c3c6aSMatthew Dillon OFF_TO_IDX(object->backing_object_offset), TRUE); 16787bfda801SAlan Cox 16797bfda801SAlan Cox /* 16807bfda801SAlan Cox * Free any cached pages from backing_object. 16817bfda801SAlan Cox */ 16827bfda801SAlan Cox if (__predict_false(backing_object->cache != NULL)) 1683c9444914SAlan Cox vm_page_cache_free(backing_object, 0, 0); 1684c0503609SDavid Greenman } 1685df8bae1dSRodney W. Grimes /* 1686df8bae1dSRodney W. Grimes * Object now shadows whatever backing_object did. 16872ad1a3f7SMatthew Dillon * Note that the reference to 16882ad1a3f7SMatthew Dillon * backing_object->backing_object moves from within 16892ad1a3f7SMatthew Dillon * backing_object to within object. 1690df8bae1dSRodney W. Grimes */ 16911c500307SAlan Cox LIST_REMOVE(object, shadow_list); 16924f7c7f6eSAlan Cox backing_object->shadow_count--; 16934f7c7f6eSAlan Cox backing_object->generation++; 1694de5f6a77SJohn Dyson if (backing_object->backing_object) { 16956be36525SAlan Cox VM_OBJECT_LOCK(backing_object->backing_object); 16961c500307SAlan Cox LIST_REMOVE(backing_object, shadow_list); 169743186e53SAlan Cox LIST_INSERT_HEAD( 169843186e53SAlan Cox &backing_object->backing_object->shadow_head, 169943186e53SAlan Cox object, shadow_list); 170043186e53SAlan Cox /* 170143186e53SAlan Cox * The shadow_count has not changed. 170243186e53SAlan Cox */ 1703eaf13dd7SJohn Dyson backing_object->backing_object->generation++; 17046be36525SAlan Cox VM_OBJECT_UNLOCK(backing_object->backing_object); 1705de5f6a77SJohn Dyson } 170624a1cce3SDavid Greenman object->backing_object = backing_object->backing_object; 17072ad1a3f7SMatthew Dillon object->backing_object_offset += 17082ad1a3f7SMatthew Dillon backing_object->backing_object_offset; 17092ad1a3f7SMatthew Dillon 1710df8bae1dSRodney W. Grimes /* 1711df8bae1dSRodney W. Grimes * Discard backing_object. 1712df8bae1dSRodney W. Grimes * 17130d94caffSDavid Greenman * Since the backing object has no pages, no pager left, 17140d94caffSDavid Greenman * and no object references within it, all that is 17150d94caffSDavid Greenman * necessary is to dispose of it. 1716df8bae1dSRodney W. Grimes */ 1717245df27cSMatthew Dillon KASSERT(backing_object->ref_count == 1, ("backing_object %p was somehow re-referenced during collapse!", backing_object)); 17186be36525SAlan Cox VM_OBJECT_UNLOCK(backing_object); 1719df8bae1dSRodney W. Grimes 1720a5698387SAlan Cox mtx_lock(&vm_object_list_mtx); 17212ad1a3f7SMatthew Dillon TAILQ_REMOVE( 17222ad1a3f7SMatthew Dillon &vm_object_list, 17232ad1a3f7SMatthew Dillon backing_object, 17242ad1a3f7SMatthew Dillon object_list 17252ad1a3f7SMatthew Dillon ); 1726a5698387SAlan Cox mtx_unlock(&vm_object_list_mtx); 1727df8bae1dSRodney W. Grimes 1728670d17b5SJeff Roberson uma_zfree(obj_zone, backing_object); 1729df8bae1dSRodney W. Grimes 1730df8bae1dSRodney W. Grimes object_collapses++; 17310d94caffSDavid Greenman } else { 173295e5e988SJohn Dyson vm_object_t new_backing_object; 1733df8bae1dSRodney W. Grimes 1734df8bae1dSRodney W. Grimes /* 17352ad1a3f7SMatthew Dillon * If we do not entirely shadow the backing object, 17362ad1a3f7SMatthew Dillon * there is nothing we can do so we give up. 1737df8bae1dSRodney W. Grimes */ 1738df59a0feSJeff Roberson if (object->resident_page_count != object->size && 1739df59a0feSJeff Roberson vm_object_backing_scan(object, 1740df59a0feSJeff Roberson OBSC_TEST_ALL_SHADOWED) == 0) { 174140b808a8SAlan Cox VM_OBJECT_UNLOCK(backing_object); 17422ad1a3f7SMatthew Dillon break; 174324a1cce3SDavid Greenman } 1744df8bae1dSRodney W. Grimes 1745df8bae1dSRodney W. Grimes /* 17460d94caffSDavid Greenman * Make the parent shadow the next object in the 17470d94caffSDavid Greenman * chain. Deallocating backing_object will not remove 17480d94caffSDavid Greenman * it, since its reference count is at least 2. 1749df8bae1dSRodney W. Grimes */ 17501c500307SAlan Cox LIST_REMOVE(object, shadow_list); 1751eaf13dd7SJohn Dyson backing_object->shadow_count--; 1752eaf13dd7SJohn Dyson backing_object->generation++; 175395e5e988SJohn Dyson 175495e5e988SJohn Dyson new_backing_object = backing_object->backing_object; 17558aef1712SMatthew Dillon if ((object->backing_object = new_backing_object) != NULL) { 17566be36525SAlan Cox VM_OBJECT_LOCK(new_backing_object); 17571c500307SAlan Cox LIST_INSERT_HEAD( 17582ad1a3f7SMatthew Dillon &new_backing_object->shadow_head, 17592ad1a3f7SMatthew Dillon object, 17602ad1a3f7SMatthew Dillon shadow_list 17612ad1a3f7SMatthew Dillon ); 1762eaf13dd7SJohn Dyson new_backing_object->shadow_count++; 1763eaf13dd7SJohn Dyson new_backing_object->generation++; 1764b921a12bSAlan Cox vm_object_reference_locked(new_backing_object); 17656be36525SAlan Cox VM_OBJECT_UNLOCK(new_backing_object); 176695e5e988SJohn Dyson object->backing_object_offset += 176795e5e988SJohn Dyson backing_object->backing_object_offset; 1768de5f6a77SJohn Dyson } 1769df8bae1dSRodney W. Grimes 1770df8bae1dSRodney W. Grimes /* 17710d94caffSDavid Greenman * Drop the reference count on backing_object. Since 177222ec553fSAlan Cox * its ref_count was at least 2, it will not vanish. 1773df8bae1dSRodney W. Grimes */ 177422ec553fSAlan Cox backing_object->ref_count--; 177522ec553fSAlan Cox VM_OBJECT_UNLOCK(backing_object); 1776df8bae1dSRodney W. Grimes object_bypasses++; 1777df8bae1dSRodney W. Grimes } 1778df8bae1dSRodney W. Grimes 1779df8bae1dSRodney W. Grimes /* 1780df8bae1dSRodney W. Grimes * Try again with this object's new backing object. 1781df8bae1dSRodney W. Grimes */ 1782df8bae1dSRodney W. Grimes } 1783df8bae1dSRodney W. Grimes } 1784df8bae1dSRodney W. Grimes 1785df8bae1dSRodney W. Grimes /* 1786bff99f0dSAlan Cox * vm_object_page_remove: 1787df8bae1dSRodney W. Grimes * 1788bff99f0dSAlan Cox * Removes all physical pages in the given range from the 1789bff99f0dSAlan Cox * object's list of pages. If the range's end is zero, all 1790bff99f0dSAlan Cox * physical pages from the range's start to the end of the object 1791bff99f0dSAlan Cox * are deleted. 1792df8bae1dSRodney W. Grimes * 1793df8bae1dSRodney W. Grimes * The object must be locked. 1794df8bae1dSRodney W. Grimes */ 179526f9a767SRodney W. Grimes void 1796ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, 1797ecde4b32SAlan Cox boolean_t clean_only) 1798df8bae1dSRodney W. Grimes { 1799d031cff1SMatthew Dillon vm_page_t p, next; 180059677d3cSAlan Cox int wirings; 1801df8bae1dSRodney W. Grimes 1802ecde4b32SAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1803ecde4b32SAlan Cox if (object->resident_page_count == 0) 180425732691SAlan Cox goto skipmemq; 180595e5e988SJohn Dyson 18068b03c8edSMatthew Dillon /* 18078b03c8edSMatthew Dillon * Since physically-backed objects do not use managed pages, we can't 18088b03c8edSMatthew Dillon * remove pages from the object (we must instead remove the page 18098b03c8edSMatthew Dillon * references, and then destroy the object). 18108b03c8edSMatthew Dillon */ 18119f5c801bSAlan Cox KASSERT(object->type != OBJT_PHYS || object == kernel_object || 18129f5c801bSAlan Cox object == kmem_object, 1813ecde4b32SAlan Cox ("attempt to remove pages from a physical object")); 18148b03c8edSMatthew Dillon 1815d474eaaaSDoug Rabson vm_object_pip_add(object, 1); 181626f9a767SRodney W. Grimes again: 181732585dd6SAlan Cox vm_page_lock_queues(); 181875741c04SAlan Cox if ((p = TAILQ_FIRST(&object->memq)) != NULL) { 181975741c04SAlan Cox if (p->pindex < start) { 182075741c04SAlan Cox p = vm_page_splay(start, object->root); 182175741c04SAlan Cox if ((object->root = p)->pindex < start) 182275741c04SAlan Cox p = TAILQ_NEXT(p, listq); 182375741c04SAlan Cox } 182475741c04SAlan Cox } 182575741c04SAlan Cox /* 182675741c04SAlan Cox * Assert: the variable p is either (1) the page with the 182775741c04SAlan Cox * least pindex greater than or equal to the parameter pindex 182875741c04SAlan Cox * or (2) NULL. 182975741c04SAlan Cox */ 183075741c04SAlan Cox for (; 1831bff99f0dSAlan Cox p != NULL && (p->pindex < end || end == 0); 183275741c04SAlan Cox p = next) { 1833b18bfc3dSJohn Dyson next = TAILQ_NEXT(p, listq); 183475741c04SAlan Cox 183559677d3cSAlan Cox /* 183659677d3cSAlan Cox * If the page is wired for any reason besides the 183759677d3cSAlan Cox * existence of managed, wired mappings, then it cannot 183859677d3cSAlan Cox * be freed. 183959677d3cSAlan Cox */ 184059677d3cSAlan Cox if ((wirings = p->wire_count) != 0 && 184159677d3cSAlan Cox (wirings = pmap_page_wired_mappings(p)) != p->wire_count) { 18424fec79beSAlan Cox pmap_remove_all(p); 184359677d3cSAlan Cox /* Account for removal of managed, wired mappings. */ 184459677d3cSAlan Cox p->wire_count -= wirings; 1845ce65e68cSDavid Greenman if (!clean_only) 1846bd7e5f99SJohn Dyson p->valid = 0; 18470d94caffSDavid Greenman continue; 18480d94caffSDavid Greenman } 184932585dd6SAlan Cox if (vm_page_sleep_if_busy(p, TRUE, "vmopar")) 185026f9a767SRodney W. Grimes goto again; 18518f9110f6SJohn Dyson if (clean_only && p->valid) { 185278985e42SAlan Cox pmap_remove_write(p); 18537c1f6cedSDavid Greenman if (p->valid & p->dirty) 18547c1f6cedSDavid Greenman continue; 18557c1f6cedSDavid Greenman } 18564fec79beSAlan Cox pmap_remove_all(p); 185759677d3cSAlan Cox /* Account for removal of managed, wired mappings. */ 185859677d3cSAlan Cox if (wirings != 0) 185959677d3cSAlan Cox p->wire_count -= wirings; 1860df8bae1dSRodney W. Grimes vm_page_free(p); 186126f9a767SRodney W. Grimes } 186232585dd6SAlan Cox vm_page_unlock_queues(); 1863f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 186425732691SAlan Cox skipmemq: 1865c9444914SAlan Cox if (__predict_false(object->cache != NULL)) 1866c9444914SAlan Cox vm_page_cache_free(object, start, end); 1867c0503609SDavid Greenman } 1868df8bae1dSRodney W. Grimes 1869df8bae1dSRodney W. Grimes /* 1870df8bae1dSRodney W. Grimes * Routine: vm_object_coalesce 1871df8bae1dSRodney W. Grimes * Function: Coalesces two objects backing up adjoining 1872df8bae1dSRodney W. Grimes * regions of memory into a single object. 1873df8bae1dSRodney W. Grimes * 1874df8bae1dSRodney W. Grimes * returns TRUE if objects were combined. 1875df8bae1dSRodney W. Grimes * 1876df8bae1dSRodney W. Grimes * NOTE: Only works at the moment if the second object is NULL - 1877df8bae1dSRodney W. Grimes * if it's not, which object do we lock first? 1878df8bae1dSRodney W. Grimes * 1879df8bae1dSRodney W. Grimes * Parameters: 1880df8bae1dSRodney W. Grimes * prev_object First object to coalesce 1881df8bae1dSRodney W. Grimes * prev_offset Offset into prev_object 1882df8bae1dSRodney W. Grimes * prev_size Size of reference to prev_object 188357a21abaSAlan Cox * next_size Size of reference to the second object 1884df8bae1dSRodney W. Grimes * 1885df8bae1dSRodney W. Grimes * Conditions: 1886df8bae1dSRodney W. Grimes * The object must *not* be locked. 1887df8bae1dSRodney W. Grimes */ 18880d94caffSDavid Greenman boolean_t 188957a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset, 189000e1854aSAlan Cox vm_size_t prev_size, vm_size_t next_size) 1891df8bae1dSRodney W. Grimes { 1892ea41812fSAlan Cox vm_pindex_t next_pindex; 1893df8bae1dSRodney W. Grimes 189400e1854aSAlan Cox if (prev_object == NULL) 1895df8bae1dSRodney W. Grimes return (TRUE); 1896bdbfbaafSAlan Cox VM_OBJECT_LOCK(prev_object); 18974112823fSMatthew Dillon if (prev_object->type != OBJT_DEFAULT && 18984112823fSMatthew Dillon prev_object->type != OBJT_SWAP) { 1899bdbfbaafSAlan Cox VM_OBJECT_UNLOCK(prev_object); 190030dcfc09SJohn Dyson return (FALSE); 190130dcfc09SJohn Dyson } 190230dcfc09SJohn Dyson 1903df8bae1dSRodney W. Grimes /* 1904df8bae1dSRodney W. Grimes * Try to collapse the object first 1905df8bae1dSRodney W. Grimes */ 1906df8bae1dSRodney W. Grimes vm_object_collapse(prev_object); 1907df8bae1dSRodney W. Grimes 1908df8bae1dSRodney W. Grimes /* 19090d94caffSDavid Greenman * Can't coalesce if: . more than one reference . paged out . shadows 19100d94caffSDavid Greenman * another object . has a copy elsewhere (any of which mean that the 19110d94caffSDavid Greenman * pages not mapped to prev_entry may be in use anyway) 1912df8bae1dSRodney W. Grimes */ 19138cc7e047SJohn Dyson if (prev_object->backing_object != NULL) { 1914bdbfbaafSAlan Cox VM_OBJECT_UNLOCK(prev_object); 1915df8bae1dSRodney W. Grimes return (FALSE); 1916df8bae1dSRodney W. Grimes } 1917a316d390SJohn Dyson 1918a316d390SJohn Dyson prev_size >>= PAGE_SHIFT; 1919a316d390SJohn Dyson next_size >>= PAGE_SHIFT; 192057a21abaSAlan Cox next_pindex = OFF_TO_IDX(prev_offset) + prev_size; 19218cc7e047SJohn Dyson 19228cc7e047SJohn Dyson if ((prev_object->ref_count > 1) && 1923ea41812fSAlan Cox (prev_object->size != next_pindex)) { 1924bdbfbaafSAlan Cox VM_OBJECT_UNLOCK(prev_object); 19258cc7e047SJohn Dyson return (FALSE); 19268cc7e047SJohn Dyson } 19278cc7e047SJohn Dyson 1928df8bae1dSRodney W. Grimes /* 19290d94caffSDavid Greenman * Remove any pages that may still be in the object from a previous 19300d94caffSDavid Greenman * deallocation. 1931df8bae1dSRodney W. Grimes */ 1932ea41812fSAlan Cox if (next_pindex < prev_object->size) { 1933df8bae1dSRodney W. Grimes vm_object_page_remove(prev_object, 1934ea41812fSAlan Cox next_pindex, 1935ea41812fSAlan Cox next_pindex + next_size, FALSE); 1936ea41812fSAlan Cox if (prev_object->type == OBJT_SWAP) 1937ea41812fSAlan Cox swap_pager_freespace(prev_object, 1938ea41812fSAlan Cox next_pindex, next_size); 1939ea41812fSAlan Cox } 1940df8bae1dSRodney W. Grimes 1941df8bae1dSRodney W. Grimes /* 1942df8bae1dSRodney W. Grimes * Extend the object if necessary. 1943df8bae1dSRodney W. Grimes */ 1944ea41812fSAlan Cox if (next_pindex + next_size > prev_object->size) 1945ea41812fSAlan Cox prev_object->size = next_pindex + next_size; 1946df8bae1dSRodney W. Grimes 1947bdbfbaafSAlan Cox VM_OBJECT_UNLOCK(prev_object); 1948df8bae1dSRodney W. Grimes return (TRUE); 1949df8bae1dSRodney W. Grimes } 1950df8bae1dSRodney W. Grimes 19517a5a6352SMatthew Dillon void 19527a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object) 19537a5a6352SMatthew Dillon { 19547a5a6352SMatthew Dillon struct vnode *vp; 19557a5a6352SMatthew Dillon 1956de33beddSAlan Cox VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 1957af51d7bfSAlan Cox if ((object->flags & OBJ_MIGHTBEDIRTY) != 0) 1958ee39666aSJeff Roberson return; 1959af51d7bfSAlan Cox vm_object_set_flag(object, OBJ_MIGHTBEDIRTY); 19607a5a6352SMatthew Dillon if (object->type == OBJT_VNODE && 19617a5a6352SMatthew Dillon (vp = (struct vnode *)object->handle) != NULL) { 1962e6e370a7SJeff Roberson VI_LOCK(vp); 1963e6e370a7SJeff Roberson vp->v_iflag |= VI_OBJDIRTY; 1964e6e370a7SJeff Roberson VI_UNLOCK(vp); 19657a5a6352SMatthew Dillon } 19667a5a6352SMatthew Dillon } 19677a5a6352SMatthew Dillon 1968c7c34a24SBruce Evans #include "opt_ddb.h" 1969c3cb3e12SDavid Greenman #ifdef DDB 1970c7c34a24SBruce Evans #include <sys/kernel.h> 1971c7c34a24SBruce Evans 1972ce9edcf5SPoul-Henning Kamp #include <sys/cons.h> 1973c7c34a24SBruce Evans 1974c7c34a24SBruce Evans #include <ddb/ddb.h> 1975c7c34a24SBruce Evans 1976cac597e4SBruce Evans static int 19771b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry) 1978a1f6d91cSDavid Greenman { 1979a1f6d91cSDavid Greenman vm_map_t tmpm; 1980a1f6d91cSDavid Greenman vm_map_entry_t tmpe; 1981a1f6d91cSDavid Greenman vm_object_t obj; 1982a1f6d91cSDavid Greenman int entcount; 1983a1f6d91cSDavid Greenman 1984a1f6d91cSDavid Greenman if (map == 0) 1985a1f6d91cSDavid Greenman return 0; 1986a1f6d91cSDavid Greenman 1987a1f6d91cSDavid Greenman if (entry == 0) { 1988a1f6d91cSDavid Greenman tmpe = map->header.next; 1989a1f6d91cSDavid Greenman entcount = map->nentries; 1990a1f6d91cSDavid Greenman while (entcount-- && (tmpe != &map->header)) { 1991a1f6d91cSDavid Greenman if (_vm_object_in_map(map, object, tmpe)) { 1992a1f6d91cSDavid Greenman return 1; 1993a1f6d91cSDavid Greenman } 1994a1f6d91cSDavid Greenman tmpe = tmpe->next; 1995a1f6d91cSDavid Greenman } 19969fdfe602SMatthew Dillon } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 19979fdfe602SMatthew Dillon tmpm = entry->object.sub_map; 1998a1f6d91cSDavid Greenman tmpe = tmpm->header.next; 1999a1f6d91cSDavid Greenman entcount = tmpm->nentries; 2000a1f6d91cSDavid Greenman while (entcount-- && tmpe != &tmpm->header) { 2001a1f6d91cSDavid Greenman if (_vm_object_in_map(tmpm, object, tmpe)) { 2002a1f6d91cSDavid Greenman return 1; 2003a1f6d91cSDavid Greenman } 2004a1f6d91cSDavid Greenman tmpe = tmpe->next; 2005a1f6d91cSDavid Greenman } 20068aef1712SMatthew Dillon } else if ((obj = entry->object.vm_object) != NULL) { 200724a1cce3SDavid Greenman for (; obj; obj = obj->backing_object) 2008a1f6d91cSDavid Greenman if (obj == object) { 2009a1f6d91cSDavid Greenman return 1; 2010a1f6d91cSDavid Greenman } 2011a1f6d91cSDavid Greenman } 2012a1f6d91cSDavid Greenman return 0; 2013a1f6d91cSDavid Greenman } 2014a1f6d91cSDavid Greenman 2015cac597e4SBruce Evans static int 20161b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object) 2017a1f6d91cSDavid Greenman { 2018a1f6d91cSDavid Greenman struct proc *p; 20191005a129SJohn Baldwin 202060517fd1SJohn Baldwin /* sx_slock(&allproc_lock); */ 2021f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) { 2022a1f6d91cSDavid Greenman if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 2023a1f6d91cSDavid Greenman continue; 2024553629ebSJake Burkholder if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) { 202560517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2026a1f6d91cSDavid Greenman return 1; 2027a1f6d91cSDavid Greenman } 2028553629ebSJake Burkholder } 202960517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2030a1f6d91cSDavid Greenman if (_vm_object_in_map(kernel_map, object, 0)) 2031a1f6d91cSDavid Greenman return 1; 2032a1f6d91cSDavid Greenman if (_vm_object_in_map(kmem_map, object, 0)) 2033a1f6d91cSDavid Greenman return 1; 2034a1f6d91cSDavid Greenman if (_vm_object_in_map(pager_map, object, 0)) 2035a1f6d91cSDavid Greenman return 1; 2036a1f6d91cSDavid Greenman if (_vm_object_in_map(buffer_map, object, 0)) 2037a1f6d91cSDavid Greenman return 1; 2038a1f6d91cSDavid Greenman return 0; 2039a1f6d91cSDavid Greenman } 2040a1f6d91cSDavid Greenman 2041c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check) 2042f708ef1bSPoul-Henning Kamp { 2043a1f6d91cSDavid Greenman vm_object_t object; 2044a1f6d91cSDavid Greenman 2045a1f6d91cSDavid Greenman /* 2046a1f6d91cSDavid Greenman * make sure that internal objs are in a map somewhere 2047a1f6d91cSDavid Greenman * and none have zero ref counts. 2048a1f6d91cSDavid Greenman */ 2049cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 205024a1cce3SDavid Greenman if (object->handle == NULL && 205124a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 2052a1f6d91cSDavid Greenman if (object->ref_count == 0) { 20533efc015bSPeter Wemm db_printf("vmochk: internal obj has zero ref count: %ld\n", 20543efc015bSPeter Wemm (long)object->size); 2055a1f6d91cSDavid Greenman } 2056a1f6d91cSDavid Greenman if (!vm_object_in_map(object)) { 2057fc62ef1fSBruce Evans db_printf( 2058fc62ef1fSBruce Evans "vmochk: internal obj is not in a map: " 2059fc62ef1fSBruce Evans "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 2060fc62ef1fSBruce Evans object->ref_count, (u_long)object->size, 2061fc62ef1fSBruce Evans (u_long)object->size, 2062fc62ef1fSBruce Evans (void *)object->backing_object); 2063a1f6d91cSDavid Greenman } 2064a1f6d91cSDavid Greenman } 2065a1f6d91cSDavid Greenman } 2066a1f6d91cSDavid Greenman } 2067a1f6d91cSDavid Greenman 206826f9a767SRodney W. Grimes /* 2069df8bae1dSRodney W. Grimes * vm_object_print: [ debug ] 2070df8bae1dSRodney W. Grimes */ 2071c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static) 2072df8bae1dSRodney W. Grimes { 2073c7c34a24SBruce Evans /* XXX convert args. */ 2074c7c34a24SBruce Evans vm_object_t object = (vm_object_t)addr; 2075c7c34a24SBruce Evans boolean_t full = have_addr; 2076c7c34a24SBruce Evans 2077d031cff1SMatthew Dillon vm_page_t p; 2078df8bae1dSRodney W. Grimes 2079c7c34a24SBruce Evans /* XXX count is an (unused) arg. Avoid shadowing it. */ 2080c7c34a24SBruce Evans #define count was_count 2081c7c34a24SBruce Evans 2082d031cff1SMatthew Dillon int count; 2083df8bae1dSRodney W. Grimes 2084df8bae1dSRodney W. Grimes if (object == NULL) 2085df8bae1dSRodney W. Grimes return; 2086df8bae1dSRodney W. Grimes 2087eb95adefSBruce Evans db_iprintf( 2088e47cd172SMaxime Henrion "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x\n", 2089e47cd172SMaxime Henrion object, (int)object->type, (uintmax_t)object->size, 2090eb95adefSBruce Evans object->resident_page_count, object->ref_count, object->flags); 2091e47cd172SMaxime Henrion db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n", 20921c7c3c6aSMatthew Dillon object->shadow_count, 2093eb95adefSBruce Evans object->backing_object ? object->backing_object->ref_count : 0, 2094e47cd172SMaxime Henrion object->backing_object, (uintmax_t)object->backing_object_offset); 2095df8bae1dSRodney W. Grimes 2096df8bae1dSRodney W. Grimes if (!full) 2097df8bae1dSRodney W. Grimes return; 2098df8bae1dSRodney W. Grimes 2099c7c34a24SBruce Evans db_indent += 2; 2100df8bae1dSRodney W. Grimes count = 0; 2101fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(p, &object->memq, listq) { 2102df8bae1dSRodney W. Grimes if (count == 0) 2103c7c34a24SBruce Evans db_iprintf("memory:="); 2104df8bae1dSRodney W. Grimes else if (count == 6) { 2105c7c34a24SBruce Evans db_printf("\n"); 2106c7c34a24SBruce Evans db_iprintf(" ..."); 2107df8bae1dSRodney W. Grimes count = 0; 2108df8bae1dSRodney W. Grimes } else 2109c7c34a24SBruce Evans db_printf(","); 2110df8bae1dSRodney W. Grimes count++; 2111df8bae1dSRodney W. Grimes 2112e47cd172SMaxime Henrion db_printf("(off=0x%jx,page=0x%jx)", 2113e47cd172SMaxime Henrion (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p)); 2114df8bae1dSRodney W. Grimes } 2115df8bae1dSRodney W. Grimes if (count != 0) 2116c7c34a24SBruce Evans db_printf("\n"); 2117c7c34a24SBruce Evans db_indent -= 2; 2118df8bae1dSRodney W. Grimes } 21195070c7f8SJohn Dyson 2120c7c34a24SBruce Evans /* XXX. */ 2121c7c34a24SBruce Evans #undef count 2122c7c34a24SBruce Evans 2123c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */ 21245070c7f8SJohn Dyson void 21251b40f8c0SMatthew Dillon vm_object_print( 21261b40f8c0SMatthew Dillon /* db_expr_t */ long addr, 21271b40f8c0SMatthew Dillon boolean_t have_addr, 21281b40f8c0SMatthew Dillon /* db_expr_t */ long count, 21291b40f8c0SMatthew Dillon char *modif) 2130c7c34a24SBruce Evans { 2131c7c34a24SBruce Evans vm_object_print_static(addr, have_addr, count, modif); 2132c7c34a24SBruce Evans } 2133c7c34a24SBruce Evans 2134c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages) 21355070c7f8SJohn Dyson { 21365070c7f8SJohn Dyson vm_object_t object; 21375070c7f8SJohn Dyson int nl = 0; 21385070c7f8SJohn Dyson int c; 2139cc64b484SAlfred Perlstein 2140cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 21415070c7f8SJohn Dyson vm_pindex_t idx, fidx; 21425070c7f8SJohn Dyson vm_pindex_t osize; 21432446e4f0SAlan Cox vm_paddr_t pa = -1; 21445070c7f8SJohn Dyson int rcount; 21455070c7f8SJohn Dyson vm_page_t m; 21465070c7f8SJohn Dyson 2147fc62ef1fSBruce Evans db_printf("new object: %p\n", (void *)object); 21485070c7f8SJohn Dyson if (nl > 18) { 21495070c7f8SJohn Dyson c = cngetc(); 21505070c7f8SJohn Dyson if (c != ' ') 21515070c7f8SJohn Dyson return; 21525070c7f8SJohn Dyson nl = 0; 21535070c7f8SJohn Dyson } 21545070c7f8SJohn Dyson nl++; 21555070c7f8SJohn Dyson rcount = 0; 21565070c7f8SJohn Dyson fidx = 0; 21575070c7f8SJohn Dyson osize = object->size; 21585070c7f8SJohn Dyson if (osize > 128) 21595070c7f8SJohn Dyson osize = 128; 21605070c7f8SJohn Dyson for (idx = 0; idx < osize; idx++) { 21615070c7f8SJohn Dyson m = vm_page_lookup(object, idx); 21625070c7f8SJohn Dyson if (m == NULL) { 21635070c7f8SJohn Dyson if (rcount) { 21643efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 21653efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 21665070c7f8SJohn Dyson if (nl > 18) { 21675070c7f8SJohn Dyson c = cngetc(); 21685070c7f8SJohn Dyson if (c != ' ') 21695070c7f8SJohn Dyson return; 21705070c7f8SJohn Dyson nl = 0; 21715070c7f8SJohn Dyson } 21725070c7f8SJohn Dyson nl++; 21735070c7f8SJohn Dyson rcount = 0; 21745070c7f8SJohn Dyson } 21755070c7f8SJohn Dyson continue; 21765070c7f8SJohn Dyson } 21775070c7f8SJohn Dyson 21785070c7f8SJohn Dyson 21795070c7f8SJohn Dyson if (rcount && 21805070c7f8SJohn Dyson (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) { 21815070c7f8SJohn Dyson ++rcount; 21825070c7f8SJohn Dyson continue; 21835070c7f8SJohn Dyson } 21845070c7f8SJohn Dyson if (rcount) { 21852446e4f0SAlan Cox db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 21863efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 21875070c7f8SJohn Dyson if (nl > 18) { 21885070c7f8SJohn Dyson c = cngetc(); 21895070c7f8SJohn Dyson if (c != ' ') 21905070c7f8SJohn Dyson return; 21915070c7f8SJohn Dyson nl = 0; 21925070c7f8SJohn Dyson } 21935070c7f8SJohn Dyson nl++; 21945070c7f8SJohn Dyson } 21955070c7f8SJohn Dyson fidx = idx; 21965070c7f8SJohn Dyson pa = VM_PAGE_TO_PHYS(m); 21975070c7f8SJohn Dyson rcount = 1; 21985070c7f8SJohn Dyson } 21995070c7f8SJohn Dyson if (rcount) { 22003efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 22013efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 22025070c7f8SJohn Dyson if (nl > 18) { 22035070c7f8SJohn Dyson c = cngetc(); 22045070c7f8SJohn Dyson if (c != ' ') 22055070c7f8SJohn Dyson return; 22065070c7f8SJohn Dyson nl = 0; 22075070c7f8SJohn Dyson } 22085070c7f8SJohn Dyson nl++; 22095070c7f8SJohn Dyson } 22105070c7f8SJohn Dyson } 22115070c7f8SJohn Dyson } 2212c3cb3e12SDavid Greenman #endif /* DDB */ 2213