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 68f8a47341SAlan Cox #include "opt_vm.h" 69f8a47341SAlan Cox 70df8bae1dSRodney W. Grimes #include <sys/param.h> 71df8bae1dSRodney W. Grimes #include <sys/systm.h> 72fb919e4dSMark Murray #include <sys/lock.h> 73867a482dSJohn Dyson #include <sys/mman.h> 74cf2819ccSJohn Dyson #include <sys/mount.h> 75b9b7a4beSMatthew Dillon #include <sys/kernel.h> 76b9b7a4beSMatthew Dillon #include <sys/sysctl.h> 771b367556SJason Evans #include <sys/mutex.h> 78fb919e4dSMark Murray #include <sys/proc.h> /* for curproc, pageproc */ 79fb919e4dSMark Murray #include <sys/socket.h> 803364c323SKonstantin Belousov #include <sys/resourcevar.h> 8189f6b863SAttilio Rao #include <sys/rwlock.h> 82fb919e4dSMark Murray #include <sys/vnode.h> 83fb919e4dSMark Murray #include <sys/vmmeter.h> 841005a129SJohn Baldwin #include <sys/sx.h> 85df8bae1dSRodney W. Grimes 86df8bae1dSRodney W. Grimes #include <vm/vm.h> 87efeaf95aSDavid Greenman #include <vm/vm_param.h> 88efeaf95aSDavid Greenman #include <vm/pmap.h> 89efeaf95aSDavid Greenman #include <vm/vm_map.h> 90efeaf95aSDavid Greenman #include <vm/vm_object.h> 91df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 9226f9a767SRodney W. Grimes #include <vm/vm_pageout.h> 930d94caffSDavid Greenman #include <vm/vm_pager.h> 9405f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h> 95a1f6d91cSDavid Greenman #include <vm/vm_kern.h> 96efeaf95aSDavid Greenman #include <vm/vm_extern.h> 97774d251dSAttilio Rao #include <vm/vm_radix.h> 98f8a47341SAlan Cox #include <vm/vm_reserv.h> 99670d17b5SJeff Roberson #include <vm/uma.h> 10026f9a767SRodney W. Grimes 101c53f7aceSDag-Erling Smørgrav static int old_msync; 102c53f7aceSDag-Erling Smørgrav SYSCTL_INT(_vm, OID_AUTO, old_msync, CTLFLAG_RW, &old_msync, 0, 103c53f7aceSDag-Erling Smørgrav "Use old (insecure) msync behavior"); 104c53f7aceSDag-Erling Smørgrav 105757216f3SKonstantin Belousov static int vm_object_page_collect_flush(vm_object_t object, vm_page_t p, 106126d6082SKonstantin Belousov int pagerflags, int flags, boolean_t *clearobjflags, 107126d6082SKonstantin Belousov boolean_t *eio); 1083280870dSKonstantin Belousov static boolean_t vm_object_page_remove_write(vm_page_t p, int flags, 109126d6082SKonstantin Belousov boolean_t *clearobjflags); 110b9b7a4beSMatthew Dillon static void vm_object_qcollapse(vm_object_t object); 11102dd8331SAlan Cox static void vm_object_vndeallocate(vm_object_t object); 112f6b04d2bSDavid Greenman 113df8bae1dSRodney W. Grimes /* 114df8bae1dSRodney W. Grimes * Virtual memory objects maintain the actual data 115df8bae1dSRodney W. Grimes * associated with allocated virtual memory. A given 116df8bae1dSRodney W. Grimes * page of memory exists within exactly one object. 117df8bae1dSRodney W. Grimes * 118df8bae1dSRodney W. Grimes * An object is only deallocated when all "references" 119df8bae1dSRodney W. Grimes * are given up. Only one "reference" to a given 120df8bae1dSRodney W. Grimes * region of an object should be writeable. 121df8bae1dSRodney W. Grimes * 122df8bae1dSRodney W. Grimes * Associated with each object is a list of all resident 123df8bae1dSRodney W. Grimes * memory pages belonging to that object; this list is 124df8bae1dSRodney W. Grimes * maintained by the "vm_page" module, and locked by the object's 125df8bae1dSRodney W. Grimes * lock. 126df8bae1dSRodney W. Grimes * 127df8bae1dSRodney W. Grimes * Each object also records a "pager" routine which is 128df8bae1dSRodney W. Grimes * used to retrieve (and store) pages to the proper backing 129df8bae1dSRodney W. Grimes * storage. In addition, objects may be backed by other 130df8bae1dSRodney W. Grimes * objects from which they were virtual-copied. 131df8bae1dSRodney W. Grimes * 132df8bae1dSRodney W. Grimes * The only items within the object structure which are 133df8bae1dSRodney W. Grimes * modified after time of creation are: 134df8bae1dSRodney W. Grimes * reference count locked by object's lock 135df8bae1dSRodney W. Grimes * pager routine locked by object's lock 136df8bae1dSRodney W. Grimes * 137df8bae1dSRodney W. Grimes */ 138df8bae1dSRodney W. Grimes 13928f8db14SBruce Evans struct object_q vm_object_list; 140a5698387SAlan Cox struct mtx vm_object_list_mtx; /* lock for object list and count */ 141cccf11b8SAlan Cox 142cccf11b8SAlan Cox struct vm_object kernel_object_store; 143cccf11b8SAlan Cox struct vm_object kmem_object_store; 144df8bae1dSRodney W. Grimes 1456472ac3dSEd Schouten static SYSCTL_NODE(_vm_stats, OID_AUTO, object, CTLFLAG_RD, 0, 1466472ac3dSEd Schouten "VM object stats"); 147604c2bbcSAlan Cox 148f708ef1bSPoul-Henning Kamp static long object_collapses; 149604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, collapses, CTLFLAG_RD, 150604c2bbcSAlan Cox &object_collapses, 0, "VM object collapses"); 151604c2bbcSAlan Cox 152f708ef1bSPoul-Henning Kamp static long object_bypasses; 153604c2bbcSAlan Cox SYSCTL_LONG(_vm_stats_object, OID_AUTO, bypasses, CTLFLAG_RD, 154604c2bbcSAlan Cox &object_bypasses, 0, "VM object bypasses"); 155dad740e9SAlan Cox 156670d17b5SJeff Roberson static uma_zone_t obj_zone; 1578355f576SJeff Roberson 158b23f72e9SBrian Feldman static int vm_object_zinit(void *mem, int size, int flags); 1598355f576SJeff Roberson 1608355f576SJeff Roberson #ifdef INVARIANTS 1618355f576SJeff Roberson static void vm_object_zdtor(void *mem, int size, void *arg); 1628355f576SJeff Roberson 1638355f576SJeff Roberson static void 1648355f576SJeff Roberson vm_object_zdtor(void *mem, int size, void *arg) 1658355f576SJeff Roberson { 1668355f576SJeff Roberson vm_object_t object; 1678355f576SJeff Roberson 1688355f576SJeff Roberson object = (vm_object_t)mem; 16943186e53SAlan Cox KASSERT(TAILQ_EMPTY(&object->memq), 170198da1b2SAttilio Rao ("object %p has resident pages in its memq", object)); 171774d251dSAttilio Rao KASSERT(vm_radix_is_empty(&object->rtree), 172774d251dSAttilio Rao ("object %p has resident pages in its trie", object)); 173f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 174f8a47341SAlan Cox KASSERT(LIST_EMPTY(&object->rvq), 175f8a47341SAlan Cox ("object %p has reservations", 176f8a47341SAlan Cox object)); 177f8a47341SAlan Cox #endif 178c9341161SAttilio Rao KASSERT(vm_object_cache_is_empty(object), 1797bfda801SAlan Cox ("object %p has cached pages", 1807bfda801SAlan Cox object)); 1818355f576SJeff Roberson KASSERT(object->paging_in_progress == 0, 1828355f576SJeff Roberson ("object %p paging_in_progress = %d", 1838355f576SJeff Roberson object, object->paging_in_progress)); 1848355f576SJeff Roberson KASSERT(object->resident_page_count == 0, 1858355f576SJeff Roberson ("object %p resident_page_count = %d", 1868355f576SJeff Roberson object, object->resident_page_count)); 1878355f576SJeff Roberson KASSERT(object->shadow_count == 0, 1888355f576SJeff Roberson ("object %p shadow_count = %d", 1898355f576SJeff Roberson object, object->shadow_count)); 1908355f576SJeff Roberson } 1918355f576SJeff Roberson #endif 1928355f576SJeff Roberson 193b23f72e9SBrian Feldman static int 194b23f72e9SBrian Feldman vm_object_zinit(void *mem, int size, int flags) 1958355f576SJeff Roberson { 1968355f576SJeff Roberson vm_object_t object; 1978355f576SJeff Roberson 1988355f576SJeff Roberson object = (vm_object_t)mem; 19989f6b863SAttilio Rao bzero(&object->lock, sizeof(object->lock)); 20089f6b863SAttilio Rao rw_init_flags(&object->lock, "vm object", RW_DUPOK); 2018355f576SJeff Roberson 2028355f576SJeff Roberson /* These are true for any object that has been freed */ 203774d251dSAttilio Rao object->rtree.rt_root = 0; 2048355f576SJeff Roberson object->paging_in_progress = 0; 2058355f576SJeff Roberson object->resident_page_count = 0; 2068355f576SJeff Roberson object->shadow_count = 0; 207774d251dSAttilio Rao object->cache.rt_root = 0; 208b23f72e9SBrian Feldman return (0); 2098355f576SJeff Roberson } 210df8bae1dSRodney W. Grimes 211a4915c21SAttilio Rao static void 2126395da54SIan Dowse _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object) 213df8bae1dSRodney W. Grimes { 2140cddd8f0SMatthew Dillon 215df8bae1dSRodney W. Grimes TAILQ_INIT(&object->memq); 2161c500307SAlan Cox LIST_INIT(&object->shadow_head); 217a1f6d91cSDavid Greenman 21824a1cce3SDavid Greenman object->type = type; 21928634820SAlan Cox switch (type) { 22028634820SAlan Cox case OBJT_DEAD: 22128634820SAlan Cox panic("_vm_object_allocate: can't create OBJT_DEAD"); 22228634820SAlan Cox case OBJT_DEFAULT: 22328634820SAlan Cox case OBJT_SWAP: 22428634820SAlan Cox object->flags = OBJ_ONEMAPPING; 22528634820SAlan Cox break; 22628634820SAlan Cox case OBJT_DEVICE: 22728634820SAlan Cox case OBJT_SG: 22828634820SAlan Cox object->flags = OBJ_FICTITIOUS | OBJ_UNMANAGED; 22928634820SAlan Cox break; 23028634820SAlan Cox case OBJT_MGTDEVICE: 23128634820SAlan Cox object->flags = OBJ_FICTITIOUS; 23228634820SAlan Cox break; 23328634820SAlan Cox case OBJT_PHYS: 23428634820SAlan Cox object->flags = OBJ_UNMANAGED; 23528634820SAlan Cox break; 23628634820SAlan Cox case OBJT_VNODE: 23728634820SAlan Cox object->flags = 0; 23828634820SAlan Cox break; 23928634820SAlan Cox default: 24028634820SAlan Cox panic("_vm_object_allocate: type %d is undefined", type); 24128634820SAlan Cox } 242df8bae1dSRodney W. Grimes object->size = size; 243b881da26SAlan Cox object->generation = 1; 244a1f6d91cSDavid Greenman object->ref_count = 1; 2453153e878SAlan Cox object->memattr = VM_MEMATTR_DEFAULT; 246ef694c1aSEdward Tomasz Napierala object->cred = NULL; 2473364c323SKonstantin Belousov object->charge = 0; 24824a1cce3SDavid Greenman object->handle = NULL; 24924a1cce3SDavid Greenman object->backing_object = NULL; 250a316d390SJohn Dyson object->backing_object_offset = (vm_ooffset_t) 0; 251f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 252f8a47341SAlan Cox LIST_INIT(&object->rvq); 253f8a47341SAlan Cox #endif 254a1f6d91cSDavid Greenman 255a5698387SAlan Cox mtx_lock(&vm_object_list_mtx); 25660517fd1SJohn Baldwin TAILQ_INSERT_TAIL(&vm_object_list, object, object_list); 257a5698387SAlan Cox mtx_unlock(&vm_object_list_mtx); 258df8bae1dSRodney W. Grimes } 259df8bae1dSRodney W. Grimes 260df8bae1dSRodney W. Grimes /* 26126f9a767SRodney W. Grimes * vm_object_init: 26226f9a767SRodney W. Grimes * 26326f9a767SRodney W. Grimes * Initialize the VM objects module. 26426f9a767SRodney W. Grimes */ 26526f9a767SRodney W. Grimes void 2661b40f8c0SMatthew Dillon vm_object_init(void) 26726f9a767SRodney W. Grimes { 26826f9a767SRodney W. Grimes TAILQ_INIT(&vm_object_list); 2696008862bSJohn Baldwin mtx_init(&vm_object_list_mtx, "vm object_list", NULL, MTX_DEF); 2700217125fSDavid Greenman 27189f6b863SAttilio Rao rw_init(&kernel_object->lock, "kernel vm object"); 2729f5c801bSAlan Cox _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 27326f9a767SRodney W. Grimes kernel_object); 274f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 275f8a47341SAlan Cox kernel_object->flags |= OBJ_COLORED; 276f8a47341SAlan Cox kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS); 277f8a47341SAlan Cox #endif 27826f9a767SRodney W. Grimes 27989f6b863SAttilio Rao rw_init(&kmem_object->lock, "kmem vm object"); 2809f5c801bSAlan Cox _vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS), 28126f9a767SRodney W. Grimes kmem_object); 282f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 283f8a47341SAlan Cox kmem_object->flags |= OBJ_COLORED; 284f8a47341SAlan Cox kmem_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS); 285f8a47341SAlan Cox #endif 286ed6a7863SAlan Cox 2878dbca793STor Egge /* 2888dbca793STor Egge * The lock portion of struct vm_object must be type stable due 2898dbca793STor Egge * to vm_pageout_fallback_object_lock locking a vm object 2908dbca793STor Egge * without holding any references to it. 2918dbca793STor Egge */ 2928355f576SJeff Roberson obj_zone = uma_zcreate("VM OBJECT", sizeof (struct vm_object), NULL, 2938355f576SJeff Roberson #ifdef INVARIANTS 2948355f576SJeff Roberson vm_object_zdtor, 2958355f576SJeff Roberson #else 2968355f576SJeff Roberson NULL, 2978355f576SJeff Roberson #endif 2985df87b21SJeff Roberson vm_object_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 299774d251dSAttilio Rao 300774d251dSAttilio Rao vm_radix_init(); 30199448ed1SJohn Dyson } 30299448ed1SJohn Dyson 30399448ed1SJohn Dyson void 3041b40f8c0SMatthew Dillon vm_object_clear_flag(vm_object_t object, u_short bits) 3051b40f8c0SMatthew Dillon { 3065440b5a9SAlan Cox 30789f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 308b06805adSJake Burkholder object->flags &= ~bits; 3091b40f8c0SMatthew Dillon } 3101b40f8c0SMatthew Dillon 3113153e878SAlan Cox /* 3123153e878SAlan Cox * Sets the default memory attribute for the specified object. Pages 3133153e878SAlan Cox * that are allocated to this object are by default assigned this memory 3143153e878SAlan Cox * attribute. 3153153e878SAlan Cox * 3163153e878SAlan Cox * Presently, this function must be called before any pages are allocated 3173153e878SAlan Cox * to the object. In the future, this requirement may be relaxed for 3183153e878SAlan Cox * "default" and "swap" objects. 3193153e878SAlan Cox */ 3203153e878SAlan Cox int 3213153e878SAlan Cox vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr) 3223153e878SAlan Cox { 3233153e878SAlan Cox 32489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 3253153e878SAlan Cox switch (object->type) { 3263153e878SAlan Cox case OBJT_DEFAULT: 3273153e878SAlan Cox case OBJT_DEVICE: 32896b0b92aSAlan Cox case OBJT_MGTDEVICE: 3293153e878SAlan Cox case OBJT_PHYS: 33001381811SJohn Baldwin case OBJT_SG: 3313153e878SAlan Cox case OBJT_SWAP: 3323153e878SAlan Cox case OBJT_VNODE: 3333153e878SAlan Cox if (!TAILQ_EMPTY(&object->memq)) 3343153e878SAlan Cox return (KERN_FAILURE); 3353153e878SAlan Cox break; 3363153e878SAlan Cox case OBJT_DEAD: 3373153e878SAlan Cox return (KERN_INVALID_ARGUMENT); 33896b0b92aSAlan Cox default: 33996b0b92aSAlan Cox panic("vm_object_set_memattr: object %p is of undefined type", 34096b0b92aSAlan Cox object); 3413153e878SAlan Cox } 3423153e878SAlan Cox object->memattr = memattr; 3433153e878SAlan Cox return (KERN_SUCCESS); 3443153e878SAlan Cox } 3453153e878SAlan Cox 3461b40f8c0SMatthew Dillon void 3471b40f8c0SMatthew Dillon vm_object_pip_add(vm_object_t object, short i) 3481b40f8c0SMatthew Dillon { 349f279b88dSAlan Cox 35089f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 351b06805adSJake Burkholder object->paging_in_progress += i; 3521b40f8c0SMatthew Dillon } 3531b40f8c0SMatthew Dillon 3541b40f8c0SMatthew Dillon void 3551b40f8c0SMatthew Dillon vm_object_pip_subtract(vm_object_t object, short i) 3561b40f8c0SMatthew Dillon { 357d647a0edSAlan Cox 35889f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 359b06805adSJake Burkholder object->paging_in_progress -= i; 3601b40f8c0SMatthew Dillon } 3611b40f8c0SMatthew Dillon 3621b40f8c0SMatthew Dillon void 3631b40f8c0SMatthew Dillon vm_object_pip_wakeup(vm_object_t object) 3641b40f8c0SMatthew Dillon { 365f279b88dSAlan Cox 36689f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 367b06805adSJake Burkholder object->paging_in_progress--; 3681b40f8c0SMatthew Dillon if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) { 3691b40f8c0SMatthew Dillon vm_object_clear_flag(object, OBJ_PIPWNT); 3701b40f8c0SMatthew Dillon wakeup(object); 3711b40f8c0SMatthew Dillon } 3721b40f8c0SMatthew Dillon } 3731b40f8c0SMatthew Dillon 3741b40f8c0SMatthew Dillon void 3751b40f8c0SMatthew Dillon vm_object_pip_wakeupn(vm_object_t object, short i) 3761b40f8c0SMatthew Dillon { 377d647a0edSAlan Cox 37889f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 3791b40f8c0SMatthew Dillon if (i) 380b06805adSJake Burkholder object->paging_in_progress -= i; 3811b40f8c0SMatthew Dillon if ((object->flags & OBJ_PIPWNT) && object->paging_in_progress == 0) { 3821b40f8c0SMatthew Dillon vm_object_clear_flag(object, OBJ_PIPWNT); 3831b40f8c0SMatthew Dillon wakeup(object); 3841b40f8c0SMatthew Dillon } 3851b40f8c0SMatthew Dillon } 3861b40f8c0SMatthew Dillon 3871b40f8c0SMatthew Dillon void 3881b40f8c0SMatthew Dillon vm_object_pip_wait(vm_object_t object, char *waitid) 3891b40f8c0SMatthew Dillon { 3901ca58953SAlan Cox 39189f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 3921ca58953SAlan Cox while (object->paging_in_progress) { 3931ca58953SAlan Cox object->flags |= OBJ_PIPWNT; 3940dde287bSAttilio Rao VM_OBJECT_SLEEP(object, object, PVM, waitid, 0); 3951ca58953SAlan Cox } 3961b40f8c0SMatthew Dillon } 3971b40f8c0SMatthew Dillon 39826f9a767SRodney W. Grimes /* 39926f9a767SRodney W. Grimes * vm_object_allocate: 40026f9a767SRodney W. Grimes * 40126f9a767SRodney W. Grimes * Returns a new object with the given size. 40226f9a767SRodney W. Grimes */ 40326f9a767SRodney W. Grimes vm_object_t 4046395da54SIan Dowse vm_object_allocate(objtype_t type, vm_pindex_t size) 40526f9a767SRodney W. Grimes { 40690688d13SAlan Cox vm_object_t object; 40790688d13SAlan Cox 40890688d13SAlan Cox object = (vm_object_t)uma_zalloc(obj_zone, M_WAITOK); 40990688d13SAlan Cox _vm_object_allocate(type, size, object); 41090688d13SAlan Cox return (object); 41126f9a767SRodney W. Grimes } 41226f9a767SRodney W. Grimes 41326f9a767SRodney W. Grimes 41426f9a767SRodney W. Grimes /* 415df8bae1dSRodney W. Grimes * vm_object_reference: 416df8bae1dSRodney W. Grimes * 41715347817SAlan Cox * Gets another reference to the given object. Note: OBJ_DEAD 41815347817SAlan Cox * objects can be referenced during final cleaning. 419df8bae1dSRodney W. Grimes */ 4206476c0d2SJohn Dyson void 4211b40f8c0SMatthew Dillon vm_object_reference(vm_object_t object) 422df8bae1dSRodney W. Grimes { 423df8bae1dSRodney W. Grimes if (object == NULL) 424df8bae1dSRodney W. Grimes return; 42589f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 42652481a9aSJeff Roberson vm_object_reference_locked(object); 42789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 42895e5e988SJohn Dyson } 42995e5e988SJohn Dyson 43023955314SAlfred Perlstein /* 431b921a12bSAlan Cox * vm_object_reference_locked: 432b921a12bSAlan Cox * 433b921a12bSAlan Cox * Gets another reference to the given object. 434b921a12bSAlan Cox * 435b921a12bSAlan Cox * The object must be locked. 436b921a12bSAlan Cox */ 437b921a12bSAlan Cox void 438b921a12bSAlan Cox vm_object_reference_locked(vm_object_t object) 439b921a12bSAlan Cox { 440b921a12bSAlan Cox struct vnode *vp; 441b921a12bSAlan Cox 44289f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 443b921a12bSAlan Cox object->ref_count++; 444b921a12bSAlan Cox if (object->type == OBJT_VNODE) { 445b921a12bSAlan Cox vp = object->handle; 446b921a12bSAlan Cox vref(vp); 447b921a12bSAlan Cox } 448b921a12bSAlan Cox } 449b921a12bSAlan Cox 450b921a12bSAlan Cox /* 4519d5abbddSJens Schweikhardt * Handle deallocating an object of type OBJT_VNODE. 45223955314SAlfred Perlstein */ 45302dd8331SAlan Cox static void 4541b40f8c0SMatthew Dillon vm_object_vndeallocate(vm_object_t object) 45595e5e988SJohn Dyson { 45695e5e988SJohn Dyson struct vnode *vp = (struct vnode *) object->handle; 457219cbf59SEivind Eklund 45889f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 4595526d2d9SEivind Eklund KASSERT(object->type == OBJT_VNODE, 4605526d2d9SEivind Eklund ("vm_object_vndeallocate: not a vnode object")); 461219cbf59SEivind Eklund KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp")); 462219cbf59SEivind Eklund #ifdef INVARIANTS 46395e5e988SJohn Dyson if (object->ref_count == 0) { 46495e5e988SJohn Dyson vprint("vm_object_vndeallocate", vp); 46595e5e988SJohn Dyson panic("vm_object_vndeallocate: bad object reference count"); 46695e5e988SJohn Dyson } 46795e5e988SJohn Dyson #endif 46895e5e988SJohn Dyson 46903fa5b34SKonstantin Belousov if (object->ref_count > 1) { 47095e5e988SJohn Dyson object->ref_count--; 47189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 47203fa5b34SKonstantin Belousov /* vrele may need the vnode lock. */ 47347221757SJohn Dyson vrele(vp); 47403fa5b34SKonstantin Belousov } else { 47586769ac0SKonstantin Belousov vhold(vp); 47689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 47703fa5b34SKonstantin Belousov vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 47886769ac0SKonstantin Belousov vdrop(vp); 47989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 48003fa5b34SKonstantin Belousov object->ref_count--; 48186769ac0SKonstantin Belousov if (object->type == OBJT_DEAD) { 48289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 48386769ac0SKonstantin Belousov VOP_UNLOCK(vp, 0); 48486769ac0SKonstantin Belousov } else { 48503fa5b34SKonstantin Belousov if (object->ref_count == 0) 486877d24acSKonstantin Belousov VOP_UNSET_TEXT(vp); 48789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 48803fa5b34SKonstantin Belousov vput(vp); 48903fa5b34SKonstantin Belousov } 490df8bae1dSRodney W. Grimes } 49186769ac0SKonstantin Belousov } 492df8bae1dSRodney W. Grimes 493df8bae1dSRodney W. Grimes /* 494df8bae1dSRodney W. Grimes * vm_object_deallocate: 495df8bae1dSRodney W. Grimes * 496df8bae1dSRodney W. Grimes * Release a reference to the specified object, 497df8bae1dSRodney W. Grimes * gained either through a vm_object_allocate 498df8bae1dSRodney W. Grimes * or a vm_object_reference call. When all references 499df8bae1dSRodney W. Grimes * are gone, storage associated with this object 500df8bae1dSRodney W. Grimes * may be relinquished. 501df8bae1dSRodney W. Grimes * 502df8bae1dSRodney W. Grimes * No object may be locked. 503df8bae1dSRodney W. Grimes */ 50426f9a767SRodney W. Grimes void 5051b40f8c0SMatthew Dillon vm_object_deallocate(vm_object_t object) 506df8bae1dSRodney W. Grimes { 507df8bae1dSRodney W. Grimes vm_object_t temp; 5086f2af3fcSKonstantin Belousov struct vnode *vp; 509df8bae1dSRodney W. Grimes 510df8bae1dSRodney W. Grimes while (object != NULL) { 51189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 5123b68228cSAlan Cox if (object->type == OBJT_VNODE) { 51395e5e988SJohn Dyson vm_object_vndeallocate(object); 51423b186d3SAlan Cox return; 5155050aa86SKonstantin Belousov } 51695e5e988SJohn Dyson 5178125b1e6SAlfred Perlstein KASSERT(object->ref_count != 0, 5188125b1e6SAlfred Perlstein ("vm_object_deallocate: object deallocated too many times: %d", object->type)); 5192be70f79SJohn Dyson 5202be70f79SJohn Dyson /* 5218125b1e6SAlfred Perlstein * If the reference count goes to 0 we start calling 5228125b1e6SAlfred Perlstein * vm_object_terminate() on the object chain. 5238125b1e6SAlfred Perlstein * A ref count of 1 may be a special case depending on the 5248125b1e6SAlfred Perlstein * shadow count being 0 or 1. 5252be70f79SJohn Dyson */ 526c0877f10SJohn Dyson object->ref_count--; 5278125b1e6SAlfred Perlstein if (object->ref_count > 1) { 52889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 52923b186d3SAlan Cox return; 5308125b1e6SAlfred Perlstein } else if (object->ref_count == 1) { 5316f2af3fcSKonstantin Belousov if (object->type == OBJT_SWAP && 5326f2af3fcSKonstantin Belousov (object->flags & OBJ_TMPFS) != 0) { 5336f2af3fcSKonstantin Belousov vp = object->un_pager.swp.swp_tmpfs; 5346f2af3fcSKonstantin Belousov vhold(vp); 5356f2af3fcSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 5366f2af3fcSKonstantin Belousov vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 5376f2af3fcSKonstantin Belousov vdrop(vp); 5386f2af3fcSKonstantin Belousov VM_OBJECT_WLOCK(object); 5397560005cSKonstantin Belousov if (object->type == OBJT_DEAD || 5407560005cSKonstantin Belousov object->ref_count != 1) { 5416f2af3fcSKonstantin Belousov VM_OBJECT_WUNLOCK(object); 5426f2af3fcSKonstantin Belousov VOP_UNLOCK(vp, 0); 5436f2af3fcSKonstantin Belousov return; 5447560005cSKonstantin Belousov } 5457560005cSKonstantin Belousov if ((object->flags & OBJ_TMPFS) != 0) 5466f2af3fcSKonstantin Belousov VOP_UNSET_TEXT(vp); 5476f2af3fcSKonstantin Belousov VOP_UNLOCK(vp, 0); 5486f2af3fcSKonstantin Belousov } 5494c8e0452SAlan Cox if (object->shadow_count == 0 && 5504c8e0452SAlan Cox object->handle == NULL && 5514c8e0452SAlan Cox (object->type == OBJT_DEFAULT || 5526f2af3fcSKonstantin Belousov (object->type == OBJT_SWAP && 5536f2af3fcSKonstantin Belousov (object->flags & OBJ_TMPFS) == 0))) { 5548125b1e6SAlfred Perlstein vm_object_set_flag(object, OBJ_ONEMAPPING); 5558125b1e6SAlfred Perlstein } else if ((object->shadow_count == 1) && 5568125b1e6SAlfred Perlstein (object->handle == NULL) && 55724a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || 55824a1cce3SDavid Greenman object->type == OBJT_SWAP)) { 5596f2af3fcSKonstantin Belousov KASSERT((object->flags & OBJ_TMPFS) == 0, 560782d4a63SKonstantin Belousov ("shadowed tmpfs v_object %p", object)); 561a1f6d91cSDavid Greenman vm_object_t robject; 56295e5e988SJohn Dyson 5631c500307SAlan Cox robject = LIST_FIRST(&object->shadow_head); 5645526d2d9SEivind Eklund KASSERT(robject != NULL, 565219cbf59SEivind Eklund ("vm_object_deallocate: ref_count: %d, shadow_count: %d", 5665526d2d9SEivind Eklund object->ref_count, 5675526d2d9SEivind Eklund object->shadow_count)); 56889f6b863SAttilio Rao if (!VM_OBJECT_TRYWLOCK(robject)) { 569b72b0115SAlan Cox /* 570b72b0115SAlan Cox * Avoid a potential deadlock. 571b72b0115SAlan Cox */ 572b72b0115SAlan Cox object->ref_count++; 57389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 574a7d86121SAlan Cox /* 575a7d86121SAlan Cox * More likely than not the thread 576a7d86121SAlan Cox * holding robject's lock has lower 577a7d86121SAlan Cox * priority than the current thread. 578a7d86121SAlan Cox * Let the lower priority thread run. 579a7d86121SAlan Cox */ 5808db5fc58SJohn Baldwin pause("vmo_de", 1); 581b72b0115SAlan Cox continue; 582b72b0115SAlan Cox } 583d936694fSAlan Cox /* 584d936694fSAlan Cox * Collapse object into its shadow unless its 585d936694fSAlan Cox * shadow is dead. In that case, object will 586d936694fSAlan Cox * be deallocated by the thread that is 587d936694fSAlan Cox * deallocating its shadow. 588d936694fSAlan Cox */ 589d936694fSAlan Cox if ((robject->flags & OBJ_DEAD) == 0 && 590d936694fSAlan Cox (robject->handle == NULL) && 59124a1cce3SDavid Greenman (robject->type == OBJT_DEFAULT || 59224a1cce3SDavid Greenman robject->type == OBJT_SWAP)) { 593a1f6d91cSDavid Greenman 59495e5e988SJohn Dyson robject->ref_count++; 595138449dcSAlan Cox retry: 596138449dcSAlan Cox if (robject->paging_in_progress) { 59789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 598138449dcSAlan Cox vm_object_pip_wait(robject, 599138449dcSAlan Cox "objde1"); 6002e9f4a69SAlan Cox temp = robject->backing_object; 6012e9f4a69SAlan Cox if (object == temp) { 60289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 603138449dcSAlan Cox goto retry; 6042e9f4a69SAlan Cox } 605138449dcSAlan Cox } else if (object->paging_in_progress) { 60689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(robject); 607138449dcSAlan Cox object->flags |= OBJ_PIPWNT; 6080dde287bSAttilio Rao VM_OBJECT_SLEEP(object, object, 609138449dcSAlan Cox PDROP | PVM, "objde2", 0); 61089f6b863SAttilio Rao VM_OBJECT_WLOCK(robject); 6112e9f4a69SAlan Cox temp = robject->backing_object; 6122e9f4a69SAlan Cox if (object == temp) { 61389f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 614138449dcSAlan Cox goto retry; 615a1f6d91cSDavid Greenman } 6162e9f4a69SAlan Cox } else 61789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 6182e9f4a69SAlan Cox 61995e5e988SJohn Dyson if (robject->ref_count == 1) { 62095e5e988SJohn Dyson robject->ref_count--; 621ba8da839SDavid Greenman object = robject; 62295e5e988SJohn Dyson goto doterm; 62395e5e988SJohn Dyson } 62495e5e988SJohn Dyson object = robject; 62595e5e988SJohn Dyson vm_object_collapse(object); 62689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 627ba8da839SDavid Greenman continue; 628a1f6d91cSDavid Greenman } 62989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(robject); 63095e5e988SJohn Dyson } 63189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 63223b186d3SAlan Cox return; 63395e5e988SJohn Dyson } 63495e5e988SJohn Dyson doterm: 63524a1cce3SDavid Greenman temp = object->backing_object; 636c9917419SAlan Cox if (temp != NULL) { 63789f6b863SAttilio Rao VM_OBJECT_WLOCK(temp); 6381c500307SAlan Cox LIST_REMOVE(object, shadow_list); 63995e5e988SJohn Dyson temp->shadow_count--; 64089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(temp); 64195461b45SJohn Dyson object->backing_object = NULL; 642de5f6a77SJohn Dyson } 643245df27cSMatthew Dillon /* 644245df27cSMatthew Dillon * Don't double-terminate, we could be in a termination 645245df27cSMatthew Dillon * recursion due to the terminate having to sync data 646245df27cSMatthew Dillon * to disk. 647245df27cSMatthew Dillon */ 648245df27cSMatthew Dillon if ((object->flags & OBJ_DEAD) == 0) 649df8bae1dSRodney W. Grimes vm_object_terminate(object); 650c829b9d0SAlan Cox else 65189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 652df8bae1dSRodney W. Grimes object = temp; 653df8bae1dSRodney W. Grimes } 654df8bae1dSRodney W. Grimes } 655df8bae1dSRodney W. Grimes 656df8bae1dSRodney W. Grimes /* 6572ac78f0eSStephan Uphoff * vm_object_destroy removes the object from the global object list 6582ac78f0eSStephan Uphoff * and frees the space for the object. 6592ac78f0eSStephan Uphoff */ 6602ac78f0eSStephan Uphoff void 6612ac78f0eSStephan Uphoff vm_object_destroy(vm_object_t object) 6622ac78f0eSStephan Uphoff { 6632ac78f0eSStephan Uphoff 6642ac78f0eSStephan Uphoff /* 6652ac78f0eSStephan Uphoff * Remove the object from the global object list. 6662ac78f0eSStephan Uphoff */ 6672ac78f0eSStephan Uphoff mtx_lock(&vm_object_list_mtx); 6682ac78f0eSStephan Uphoff TAILQ_REMOVE(&vm_object_list, object, object_list); 6692ac78f0eSStephan Uphoff mtx_unlock(&vm_object_list_mtx); 6702ac78f0eSStephan Uphoff 6712ac78f0eSStephan Uphoff /* 6723364c323SKonstantin Belousov * Release the allocation charge. 6733364c323SKonstantin Belousov */ 674ef694c1aSEdward Tomasz Napierala if (object->cred != NULL) { 6753364c323SKonstantin Belousov KASSERT(object->type == OBJT_DEFAULT || 6763364c323SKonstantin Belousov object->type == OBJT_SWAP, 677ef694c1aSEdward Tomasz Napierala ("vm_object_terminate: non-swap obj %p has cred", 6783364c323SKonstantin Belousov object)); 679ef694c1aSEdward Tomasz Napierala swap_release_by_cred(object->charge, object->cred); 6803364c323SKonstantin Belousov object->charge = 0; 681ef694c1aSEdward Tomasz Napierala crfree(object->cred); 682ef694c1aSEdward Tomasz Napierala object->cred = NULL; 6833364c323SKonstantin Belousov } 6843364c323SKonstantin Belousov 6853364c323SKonstantin Belousov /* 6862ac78f0eSStephan Uphoff * Free the space for the object. 6872ac78f0eSStephan Uphoff */ 6882ac78f0eSStephan Uphoff uma_zfree(obj_zone, object); 6892ac78f0eSStephan Uphoff } 6902ac78f0eSStephan Uphoff 6912ac78f0eSStephan Uphoff /* 692df8bae1dSRodney W. Grimes * vm_object_terminate actually destroys the specified object, freeing 693df8bae1dSRodney W. Grimes * up all previously used resources. 694df8bae1dSRodney W. Grimes * 695df8bae1dSRodney W. Grimes * The object must be locked. 6961c7c3c6aSMatthew Dillon * This routine may block. 697df8bae1dSRodney W. Grimes */ 69895e5e988SJohn Dyson void 6991b40f8c0SMatthew Dillon vm_object_terminate(vm_object_t object) 700df8bae1dSRodney W. Grimes { 70117ea6f00SAlan Cox vm_page_t p, p_next; 702df8bae1dSRodney W. Grimes 70389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 7040cddd8f0SMatthew Dillon 70595e5e988SJohn Dyson /* 70695e5e988SJohn Dyson * Make sure no one uses us. 70795e5e988SJohn Dyson */ 708069e9bc1SDoug Rabson vm_object_set_flag(object, OBJ_DEAD); 7093c631446SJohn Dyson 710df8bae1dSRodney W. Grimes /* 711f6b04d2bSDavid Greenman * wait for the pageout daemon to be done with the object 712df8bae1dSRodney W. Grimes */ 71366095752SJohn Dyson vm_object_pip_wait(object, "objtrm"); 714df8bae1dSRodney W. Grimes 7155526d2d9SEivind Eklund KASSERT(!object->paging_in_progress, 7165526d2d9SEivind Eklund ("vm_object_terminate: pageout in progress")); 71726f9a767SRodney W. Grimes 71826f9a767SRodney W. Grimes /* 7190d94caffSDavid Greenman * Clean and free the pages, as appropriate. All references to the 7200d94caffSDavid Greenman * object are gone, so we don't need to lock it. 72126f9a767SRodney W. Grimes */ 72224a1cce3SDavid Greenman if (object->type == OBJT_VNODE) { 723f7dd7b63SAlan Cox struct vnode *vp = (struct vnode *)object->handle; 72495e5e988SJohn Dyson 72595e5e988SJohn Dyson /* 72695e5e988SJohn Dyson * Clean pages and flush buffers. 72795e5e988SJohn Dyson */ 7288f9110f6SJohn Dyson vm_object_page_clean(object, 0, 0, OBJPC_SYNC); 72989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 73095e5e988SJohn Dyson 7310d7935fdSAttilio Rao vinvalbuf(vp, V_SAVE, 0, 0); 732f7dd7b63SAlan Cox 73389f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 734bef608bdSJohn Dyson } 735bef608bdSJohn Dyson 736971dd342SAlfred Perlstein KASSERT(object->ref_count == 0, 737971dd342SAlfred Perlstein ("vm_object_terminate: object with references, ref_count=%d", 738971dd342SAlfred Perlstein object->ref_count)); 739996c772fSJohn Dyson 7400d94caffSDavid Greenman /* 74117ea6f00SAlan Cox * Free any remaining pageable pages. This also removes them from the 74217ea6f00SAlan Cox * paging queues. However, don't free wired pages, just remove them 74317ea6f00SAlan Cox * from the object. Rather than incrementally removing each page from 74417ea6f00SAlan Cox * the object, the page and object are reset to any empty state. 745df8bae1dSRodney W. Grimes */ 74617ea6f00SAlan Cox TAILQ_FOREACH_SAFE(p, &object->memq, listq, p_next) { 747*c7aebda8SAttilio Rao vm_page_assert_unbusied(p); 7482965a453SKip Macy vm_page_lock(p); 74917ea6f00SAlan Cox /* 75017ea6f00SAlan Cox * Optimize the page's removal from the object by resetting 75117ea6f00SAlan Cox * its "object" field. Specifically, if the page is not 75217ea6f00SAlan Cox * wired, then the effect of this assignment is that 75317ea6f00SAlan Cox * vm_page_free()'s call to vm_page_remove() will return 75417ea6f00SAlan Cox * immediately without modifying the page or the object. 75517ea6f00SAlan Cox */ 75617ea6f00SAlan Cox p->object = NULL; 7570b10ba98SDavid Greenman if (p->wire_count == 0) { 758df8bae1dSRodney W. Grimes vm_page_free(p); 75970721880SAlan Cox PCPU_INC(cnt.v_pfree); 76017ea6f00SAlan Cox } 7612965a453SKip Macy vm_page_unlock(p); 7622965a453SKip Macy } 76317ea6f00SAlan Cox /* 76417ea6f00SAlan Cox * If the object contained any pages, then reset it to an empty state. 76517ea6f00SAlan Cox * None of the object's fields, including "resident_page_count", were 76617ea6f00SAlan Cox * modified by the preceding loop. 76717ea6f00SAlan Cox */ 76817ea6f00SAlan Cox if (object->resident_page_count != 0) { 769774d251dSAttilio Rao vm_radix_reclaim_allnodes(&object->rtree); 77017ea6f00SAlan Cox TAILQ_INIT(&object->memq); 77117ea6f00SAlan Cox object->resident_page_count = 0; 77217ea6f00SAlan Cox if (object->type == OBJT_VNODE) 77317ea6f00SAlan Cox vdrop(object->handle); 77417ea6f00SAlan Cox } 775bef608bdSJohn Dyson 776f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 777f8a47341SAlan Cox if (__predict_false(!LIST_EMPTY(&object->rvq))) 778f8a47341SAlan Cox vm_reserv_break_all(object); 779f8a47341SAlan Cox #endif 780c9341161SAttilio Rao if (__predict_false(!vm_object_cache_is_empty(object))) 781c9444914SAlan Cox vm_page_cache_free(object, 0, 0); 7827bfda801SAlan Cox 7832d8acc0fSJohn Dyson /* 7849fcfb650SDavid Greenman * Let the pager know object is dead. 7859fcfb650SDavid Greenman */ 7869fcfb650SDavid Greenman vm_pager_deallocate(object); 78789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 7889fcfb650SDavid Greenman 7892ac78f0eSStephan Uphoff vm_object_destroy(object); 79047221757SJohn Dyson } 791df8bae1dSRodney W. Grimes 792edf93b25SAlan Cox /* 793edf93b25SAlan Cox * Make the page read-only so that we can clear the object flags. However, if 794edf93b25SAlan Cox * this is a nosync mmap then the object is likely to stay dirty so do not 795edf93b25SAlan Cox * mess with the page and do not clear the object flags. Returns TRUE if the 796edf93b25SAlan Cox * page should be flushed, and FALSE otherwise. 797edf93b25SAlan Cox */ 7983280870dSKonstantin Belousov static boolean_t 799126d6082SKonstantin Belousov vm_object_page_remove_write(vm_page_t p, int flags, boolean_t *clearobjflags) 8003280870dSKonstantin Belousov { 8013280870dSKonstantin Belousov 8023280870dSKonstantin Belousov /* 8033280870dSKonstantin Belousov * If we have been asked to skip nosync pages and this is a 8043280870dSKonstantin Belousov * nosync page, skip it. Note that the object flags were not 8053280870dSKonstantin Belousov * cleared in this case so we do not have to set them. 8063280870dSKonstantin Belousov */ 8073280870dSKonstantin Belousov if ((flags & OBJPC_NOSYNC) != 0 && (p->oflags & VPO_NOSYNC) != 0) { 808126d6082SKonstantin Belousov *clearobjflags = FALSE; 8093280870dSKonstantin Belousov return (FALSE); 8103280870dSKonstantin Belousov } else { 8113280870dSKonstantin Belousov pmap_remove_write(p); 8123280870dSKonstantin Belousov return (p->dirty != 0); 8133280870dSKonstantin Belousov } 8143280870dSKonstantin Belousov } 8153280870dSKonstantin Belousov 816df8bae1dSRodney W. Grimes /* 817df8bae1dSRodney W. Grimes * vm_object_page_clean 818df8bae1dSRodney W. Grimes * 8194f79d873SMatthew Dillon * Clean all dirty pages in the specified range of object. Leaves page 8204f79d873SMatthew Dillon * on whatever queue it is currently on. If NOSYNC is set then do not 821b146f9e5SAlan Cox * write out pages with VPO_NOSYNC set (originally comes from MAP_NOSYNC), 8224f79d873SMatthew Dillon * leaving the object dirty. 82326f9a767SRodney W. Grimes * 82443b7990eSMatthew Dillon * When stuffing pages asynchronously, allow clustering. XXX we need a 82543b7990eSMatthew Dillon * synchronous clustering mode implementation. 82643b7990eSMatthew Dillon * 82726f9a767SRodney W. Grimes * Odd semantics: if start == end, we clean everything. 82826f9a767SRodney W. Grimes * 82926f9a767SRodney W. Grimes * The object must be locked. 830126d6082SKonstantin Belousov * 831126d6082SKonstantin Belousov * Returns FALSE if some page from the range was not written, as 832126d6082SKonstantin Belousov * reported by the pager, and TRUE otherwise. 83326f9a767SRodney W. Grimes */ 834126d6082SKonstantin Belousov boolean_t 83517f3095dSAlan Cox vm_object_page_clean(vm_object_t object, vm_ooffset_t start, vm_ooffset_t end, 836e239bb97SKonstantin Belousov int flags) 837f6b04d2bSDavid Greenman { 838e239bb97SKonstantin Belousov vm_page_t np, p; 83917f3095dSAlan Cox vm_pindex_t pi, tend, tstart; 840126d6082SKonstantin Belousov int curgeneration, n, pagerflags; 841126d6082SKonstantin Belousov boolean_t clearobjflags, eio, res; 842f6b04d2bSDavid Greenman 84389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 844e5f299ffSKonstantin Belousov 845e5f299ffSKonstantin Belousov /* 846e5f299ffSKonstantin Belousov * The OBJ_MIGHTBEDIRTY flag is only set for OBJT_VNODE 847e5f299ffSKonstantin Belousov * objects. The check below prevents the function from 848e5f299ffSKonstantin Belousov * operating on non-vnode objects. 849e5f299ffSKonstantin Belousov */ 850e239bb97SKonstantin Belousov if ((object->flags & OBJ_MIGHTBEDIRTY) == 0 || 851e239bb97SKonstantin Belousov object->resident_page_count == 0) 852126d6082SKonstantin Belousov return (TRUE); 853f6b04d2bSDavid Greenman 854e239bb97SKonstantin Belousov pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) != 0 ? 855e239bb97SKonstantin Belousov VM_PAGER_PUT_SYNC : VM_PAGER_CLUSTER_OK; 856e239bb97SKonstantin Belousov pagerflags |= (flags & OBJPC_INVAL) != 0 ? VM_PAGER_PUT_INVAL : 0; 857e239bb97SKonstantin Belousov 85817f3095dSAlan Cox tstart = OFF_TO_IDX(start); 85917f3095dSAlan Cox tend = (end == 0) ? object->size : OFF_TO_IDX(end + PAGE_MASK); 86017f3095dSAlan Cox clearobjflags = tstart == 0 && tend >= object->size; 861126d6082SKonstantin Belousov res = TRUE; 862f6b04d2bSDavid Greenman 863bd7e5f99SJohn Dyson rescan: 8642d8acc0fSJohn Dyson curgeneration = object->generation; 8652d8acc0fSJohn Dyson 86617f3095dSAlan Cox for (p = vm_page_find_least(object, tstart); p != NULL; p = np) { 867bd7e5f99SJohn Dyson pi = p->pindex; 868e239bb97SKonstantin Belousov if (pi >= tend) 869e239bb97SKonstantin Belousov break; 870e239bb97SKonstantin Belousov np = TAILQ_NEXT(p, listq); 871e239bb97SKonstantin Belousov if (p->valid == 0) 872aef922f5SJohn Dyson continue; 873*c7aebda8SAttilio Rao if (vm_page_sleep_if_busy(p, "vpcwai")) { 874e65919f9SKonstantin Belousov if (object->generation != curgeneration) { 875e65919f9SKonstantin Belousov if ((flags & OBJPC_SYNC) != 0) 876e239bb97SKonstantin Belousov goto rescan; 877e65919f9SKonstantin Belousov else 878126d6082SKonstantin Belousov clearobjflags = FALSE; 879e65919f9SKonstantin Belousov } 880780636b7SKonstantin Belousov np = vm_page_find_least(object, pi); 881780636b7SKonstantin Belousov continue; 882f6b04d2bSDavid Greenman } 8833280870dSKonstantin Belousov if (!vm_object_page_remove_write(p, flags, &clearobjflags)) 884bd7e5f99SJohn Dyson continue; 885e239bb97SKonstantin Belousov 8863280870dSKonstantin Belousov n = vm_object_page_collect_flush(object, p, pagerflags, 887126d6082SKonstantin Belousov flags, &clearobjflags, &eio); 888126d6082SKonstantin Belousov if (eio) { 889126d6082SKonstantin Belousov res = FALSE; 890126d6082SKonstantin Belousov clearobjflags = FALSE; 891126d6082SKonstantin Belousov } 892e65919f9SKonstantin Belousov if (object->generation != curgeneration) { 893e65919f9SKonstantin Belousov if ((flags & OBJPC_SYNC) != 0) 894b9b7a4beSMatthew Dillon goto rescan; 895e65919f9SKonstantin Belousov else 896126d6082SKonstantin Belousov clearobjflags = FALSE; 897e65919f9SKonstantin Belousov } 898031ec8c1SKonstantin Belousov 899031ec8c1SKonstantin Belousov /* 900031ec8c1SKonstantin Belousov * If the VOP_PUTPAGES() did a truncated write, so 901031ec8c1SKonstantin Belousov * that even the first page of the run is not fully 902031ec8c1SKonstantin Belousov * written, vm_pageout_flush() returns 0 as the run 903031ec8c1SKonstantin Belousov * length. Since the condition that caused truncated 904031ec8c1SKonstantin Belousov * write may be permanent, e.g. exhausted free space, 905031ec8c1SKonstantin Belousov * accepting n == 0 would cause an infinite loop. 906031ec8c1SKonstantin Belousov * 907031ec8c1SKonstantin Belousov * Forwarding the iterator leaves the unwritten page 908031ec8c1SKonstantin Belousov * behind, but there is not much we can do there if 909031ec8c1SKonstantin Belousov * filesystem refuses to write it. 910031ec8c1SKonstantin Belousov */ 911126d6082SKonstantin Belousov if (n == 0) { 912031ec8c1SKonstantin Belousov n = 1; 913126d6082SKonstantin Belousov clearobjflags = FALSE; 914126d6082SKonstantin Belousov } 915e239bb97SKonstantin Belousov np = vm_page_find_least(object, pi + n); 916b9b7a4beSMatthew Dillon } 917b9b7a4beSMatthew Dillon #if 0 918e239bb97SKonstantin Belousov VOP_FSYNC(vp, (pagerflags & VM_PAGER_PUT_SYNC) ? MNT_WAIT : 0); 919b9b7a4beSMatthew Dillon #endif 920b9b7a4beSMatthew Dillon 921edf93b25SAlan Cox if (clearobjflags) 9223280870dSKonstantin Belousov vm_object_clear_flag(object, OBJ_MIGHTBEDIRTY); 923126d6082SKonstantin Belousov return (res); 924b9b7a4beSMatthew Dillon } 925b9b7a4beSMatthew Dillon 926b9b7a4beSMatthew Dillon static int 9273280870dSKonstantin Belousov vm_object_page_collect_flush(vm_object_t object, vm_page_t p, int pagerflags, 928126d6082SKonstantin Belousov int flags, boolean_t *clearobjflags, boolean_t *eio) 929b9b7a4beSMatthew Dillon { 9303157c503SKonstantin Belousov vm_page_t ma[vm_pageout_page_count], p_first, tp; 9313157c503SKonstantin Belousov int count, i, mreq, runlen; 932b9b7a4beSMatthew Dillon 9337bec141bSKip Macy vm_page_lock_assert(p, MA_NOTOWNED); 93489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 9353157c503SKonstantin Belousov 9363157c503SKonstantin Belousov count = 1; 9373157c503SKonstantin Belousov mreq = 0; 9383157c503SKonstantin Belousov 9393157c503SKonstantin Belousov for (tp = p; count < vm_pageout_page_count; count++) { 9403157c503SKonstantin Belousov tp = vm_page_next(tp); 941*c7aebda8SAttilio Rao if (tp == NULL || vm_page_busied(tp)) 942bd7e5f99SJohn Dyson break; 9433280870dSKonstantin Belousov if (!vm_object_page_remove_write(tp, flags, clearobjflags)) 944bd7e5f99SJohn Dyson break; 945bd7e5f99SJohn Dyson } 946aef922f5SJohn Dyson 9473157c503SKonstantin Belousov for (p_first = p; count < vm_pageout_page_count; count++) { 9483157c503SKonstantin Belousov tp = vm_page_prev(p_first); 949*c7aebda8SAttilio Rao if (tp == NULL || vm_page_busied(tp)) 950bd7e5f99SJohn Dyson break; 9513280870dSKonstantin Belousov if (!vm_object_page_remove_write(tp, flags, clearobjflags)) 952bd7e5f99SJohn Dyson break; 9533157c503SKonstantin Belousov p_first = tp; 9543157c503SKonstantin Belousov mreq++; 955bd7e5f99SJohn Dyson } 956bd7e5f99SJohn Dyson 9573157c503SKonstantin Belousov for (tp = p_first, i = 0; i < count; tp = TAILQ_NEXT(tp, listq), i++) 9583157c503SKonstantin Belousov ma[i] = tp; 959cf2819ccSJohn Dyson 960126d6082SKonstantin Belousov vm_pageout_flush(ma, count, pagerflags, mreq, &runlen, eio); 9611e8a675cSKonstantin Belousov return (runlen); 96226f9a767SRodney W. Grimes } 963df8bae1dSRodney W. Grimes 9641efb74fbSJohn Dyson /* 965950f8459SAlan Cox * Note that there is absolutely no sense in writing out 966950f8459SAlan Cox * anonymous objects, so we track down the vnode object 967950f8459SAlan Cox * to write out. 968950f8459SAlan Cox * We invalidate (remove) all pages from the address space 969950f8459SAlan Cox * for semantic correctness. 970950f8459SAlan Cox * 9716bbee8e2SAlan Cox * If the backing object is a device object with unmanaged pages, then any 9726bbee8e2SAlan Cox * mappings to the specified range of pages must be removed before this 9736bbee8e2SAlan Cox * function is called. 9746bbee8e2SAlan Cox * 975950f8459SAlan Cox * Note: certain anonymous maps, such as MAP_NOSYNC maps, 976950f8459SAlan Cox * may start out with a NULL object. 977950f8459SAlan Cox */ 978126d6082SKonstantin Belousov boolean_t 979950f8459SAlan Cox vm_object_sync(vm_object_t object, vm_ooffset_t offset, vm_size_t size, 980950f8459SAlan Cox boolean_t syncio, boolean_t invalidate) 981950f8459SAlan Cox { 982950f8459SAlan Cox vm_object_t backing_object; 983950f8459SAlan Cox struct vnode *vp; 9843b582b4eSTor Egge struct mount *mp; 985126d6082SKonstantin Belousov int error, flags, fsync_after; 986126d6082SKonstantin Belousov boolean_t res; 987950f8459SAlan Cox 988950f8459SAlan Cox if (object == NULL) 989126d6082SKonstantin Belousov return (TRUE); 990126d6082SKonstantin Belousov res = TRUE; 991126d6082SKonstantin Belousov error = 0; 99289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 993950f8459SAlan Cox while ((backing_object = object->backing_object) != NULL) { 99489f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 99556e0670fSAlan Cox offset += object->backing_object_offset; 99689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 997950f8459SAlan Cox object = backing_object; 998950f8459SAlan Cox if (object->size < OFF_TO_IDX(offset + size)) 999950f8459SAlan Cox size = IDX_TO_OFF(object->size) - offset; 1000950f8459SAlan Cox } 1001950f8459SAlan Cox /* 1002950f8459SAlan Cox * Flush pages if writing is allowed, invalidate them 1003950f8459SAlan Cox * if invalidation requested. Pages undergoing I/O 1004950f8459SAlan Cox * will be ignored by vm_object_page_remove(). 1005950f8459SAlan Cox * 1006950f8459SAlan Cox * We cannot lock the vnode and then wait for paging 1007950f8459SAlan Cox * to complete without deadlocking against vm_fault. 1008950f8459SAlan Cox * Instead we simply call vm_object_page_remove() and 1009950f8459SAlan Cox * allow it to block internally on a page-by-page 1010950f8459SAlan Cox * basis when it encounters pages undergoing async 1011950f8459SAlan Cox * I/O. 1012950f8459SAlan Cox */ 1013950f8459SAlan Cox if (object->type == OBJT_VNODE && 1014950f8459SAlan Cox (object->flags & OBJ_MIGHTBEDIRTY) != 0) { 1015950f8459SAlan Cox vp = object->handle; 101689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 10173b582b4eSTor Egge (void) vn_start_write(vp, &mp, V_WAIT); 1018cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 101975ff604aSKonstantin Belousov if (syncio && !invalidate && offset == 0 && 102075ff604aSKonstantin Belousov OFF_TO_IDX(size) == object->size) { 102175ff604aSKonstantin Belousov /* 102275ff604aSKonstantin Belousov * If syncing the whole mapping of the file, 102375ff604aSKonstantin Belousov * it is faster to schedule all the writes in 102475ff604aSKonstantin Belousov * async mode, also allowing the clustering, 102575ff604aSKonstantin Belousov * and then wait for i/o to complete. 102675ff604aSKonstantin Belousov */ 102775ff604aSKonstantin Belousov flags = 0; 102875ff604aSKonstantin Belousov fsync_after = TRUE; 102975ff604aSKonstantin Belousov } else { 1030950f8459SAlan Cox flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 103175ff604aSKonstantin Belousov flags |= invalidate ? (OBJPC_SYNC | OBJPC_INVAL) : 0; 103275ff604aSKonstantin Belousov fsync_after = FALSE; 103375ff604aSKonstantin Belousov } 103489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1035126d6082SKonstantin Belousov res = vm_object_page_clean(object, offset, offset + size, 1036126d6082SKonstantin Belousov flags); 103789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 103875ff604aSKonstantin Belousov if (fsync_after) 1039126d6082SKonstantin Belousov error = VOP_FSYNC(vp, MNT_WAIT, curthread); 104022db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 10413b582b4eSTor Egge vn_finished_write(mp); 1042126d6082SKonstantin Belousov if (error != 0) 1043126d6082SKonstantin Belousov res = FALSE; 104489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1045950f8459SAlan Cox } 1046950f8459SAlan Cox if ((object->type == OBJT_VNODE || 1047950f8459SAlan Cox object->type == OBJT_DEVICE) && invalidate) { 10486bbee8e2SAlan Cox if (object->type == OBJT_DEVICE) 10496bbee8e2SAlan Cox /* 10506bbee8e2SAlan Cox * The option OBJPR_NOTMAPPED must be passed here 10516bbee8e2SAlan Cox * because vm_object_page_remove() cannot remove 10526bbee8e2SAlan Cox * unmanaged mappings. 10536bbee8e2SAlan Cox */ 10546bbee8e2SAlan Cox flags = OBJPR_NOTMAPPED; 10556bbee8e2SAlan Cox else if (old_msync) 1056ebf5d94eSKonstantin Belousov flags = OBJPR_NOTWIRED; 10576bbee8e2SAlan Cox else 1058ebf5d94eSKonstantin Belousov flags = OBJPR_CLEANONLY | OBJPR_NOTWIRED; 10596bbee8e2SAlan Cox vm_object_page_remove(object, OFF_TO_IDX(offset), 10606bbee8e2SAlan Cox OFF_TO_IDX(offset + size + PAGE_MASK), flags); 1061950f8459SAlan Cox } 106289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1063126d6082SKonstantin Belousov return (res); 1064950f8459SAlan Cox } 1065950f8459SAlan Cox 1066950f8459SAlan Cox /* 1067867a482dSJohn Dyson * vm_object_madvise: 1068867a482dSJohn Dyson * 1069867a482dSJohn Dyson * Implements the madvise function at the object/page level. 10701c7c3c6aSMatthew Dillon * 1071193b9358SAlan Cox * MADV_WILLNEED (any object) 1072193b9358SAlan Cox * 1073193b9358SAlan Cox * Activate the specified pages if they are resident. 1074193b9358SAlan Cox * 1075193b9358SAlan Cox * MADV_DONTNEED (any object) 1076193b9358SAlan Cox * 1077193b9358SAlan Cox * Deactivate the specified pages if they are resident. 1078193b9358SAlan Cox * 1079193b9358SAlan Cox * MADV_FREE (OBJT_DEFAULT/OBJT_SWAP objects, 1080193b9358SAlan Cox * OBJ_ONEMAPPING only) 1081193b9358SAlan Cox * 1082193b9358SAlan Cox * Deactivate and clean the specified pages if they are 1083193b9358SAlan Cox * resident. This permits the process to reuse the pages 1084193b9358SAlan Cox * without faulting or the kernel to reclaim the pages 1085193b9358SAlan Cox * without I/O. 1086867a482dSJohn Dyson */ 1087867a482dSJohn Dyson void 108892a59946SJohn Baldwin vm_object_madvise(vm_object_t object, vm_pindex_t pindex, vm_pindex_t end, 108992a59946SJohn Baldwin int advise) 1090867a482dSJohn Dyson { 109192a59946SJohn Baldwin vm_pindex_t tpindex; 109234567de7SAlan Cox vm_object_t backing_object, tobject; 1093867a482dSJohn Dyson vm_page_t m; 1094867a482dSJohn Dyson 1095867a482dSJohn Dyson if (object == NULL) 1096867a482dSJohn Dyson return; 109789f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 10981c7c3c6aSMatthew Dillon /* 10991c7c3c6aSMatthew Dillon * Locate and adjust resident pages 11001c7c3c6aSMatthew Dillon */ 11011c7c3c6aSMatthew Dillon for (; pindex < end; pindex += 1) { 11026e20a165SJohn Dyson relookup: 11036e20a165SJohn Dyson tobject = object; 11046e20a165SJohn Dyson tpindex = pindex; 11056e20a165SJohn Dyson shadowlookup: 110658b4e6ccSAlan Cox /* 110758b4e6ccSAlan Cox * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages 110858b4e6ccSAlan Cox * and those pages must be OBJ_ONEMAPPING. 110958b4e6ccSAlan Cox */ 111058b4e6ccSAlan Cox if (advise == MADV_FREE) { 111158b4e6ccSAlan Cox if ((tobject->type != OBJT_DEFAULT && 111258b4e6ccSAlan Cox tobject->type != OBJT_SWAP) || 111358b4e6ccSAlan Cox (tobject->flags & OBJ_ONEMAPPING) == 0) { 111434567de7SAlan Cox goto unlock_tobject; 11156e20a165SJohn Dyson } 111628634820SAlan Cox } else if ((tobject->flags & OBJ_UNMANAGED) != 0) 11176a2a3d73SAlan Cox goto unlock_tobject; 11181c7c3c6aSMatthew Dillon m = vm_page_lookup(tobject, tpindex); 11197bfda801SAlan Cox if (m == NULL && advise == MADV_WILLNEED) { 11207bfda801SAlan Cox /* 11217bfda801SAlan Cox * If the page is cached, reactivate it. 11227bfda801SAlan Cox */ 1123f3a2ed4bSAlan Cox m = vm_page_alloc(tobject, tpindex, VM_ALLOC_IFCACHED | 1124f3a2ed4bSAlan Cox VM_ALLOC_NOBUSY); 11257bfda801SAlan Cox } 11261c7c3c6aSMatthew Dillon if (m == NULL) { 11271ce137beSMatthew Dillon /* 11281ce137beSMatthew Dillon * There may be swap even if there is no backing page 11291ce137beSMatthew Dillon */ 11301ce137beSMatthew Dillon if (advise == MADV_FREE && tobject->type == OBJT_SWAP) 11311ce137beSMatthew Dillon swap_pager_freespace(tobject, tpindex, 1); 11321ce137beSMatthew Dillon /* 11331ce137beSMatthew Dillon * next object 11341ce137beSMatthew Dillon */ 113534567de7SAlan Cox backing_object = tobject->backing_object; 113634567de7SAlan Cox if (backing_object == NULL) 113734567de7SAlan Cox goto unlock_tobject; 113889f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 113956e0670fSAlan Cox tpindex += OFF_TO_IDX(tobject->backing_object_offset); 11409b98b796SAlan Cox if (tobject != object) 114189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(tobject); 114234567de7SAlan Cox tobject = backing_object; 11436e20a165SJohn Dyson goto shadowlookup; 11446a2a3d73SAlan Cox } else if (m->valid != VM_PAGE_BITS_ALL) 11456a2a3d73SAlan Cox goto unlock_tobject; 1146867a482dSJohn Dyson /* 11476a2a3d73SAlan Cox * If the page is not in a normal state, skip it. 1148867a482dSJohn Dyson */ 11492965a453SKip Macy vm_page_lock(m); 11506a2a3d73SAlan Cox if (m->hold_count != 0 || m->wire_count != 0) { 11512965a453SKip Macy vm_page_unlock(m); 115234567de7SAlan Cox goto unlock_tobject; 11536e20a165SJohn Dyson } 1154d98d0ce2SKonstantin Belousov KASSERT((m->flags & PG_FICTITIOUS) == 0, 1155d98d0ce2SKonstantin Belousov ("vm_object_madvise: page %p is fictitious", m)); 1156d98d0ce2SKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) == 0, 1157567e51e1SAlan Cox ("vm_object_madvise: page %p is not managed", m)); 1158*c7aebda8SAttilio Rao if (vm_page_busied(m)) { 1159567e51e1SAlan Cox if (advise == MADV_WILLNEED) { 1160b11b56b5SAlan Cox /* 1161b11b56b5SAlan Cox * Reference the page before unlocking and 1162b11b56b5SAlan Cox * sleeping so that the page daemon is less 1163b11b56b5SAlan Cox * likely to reclaim it. 1164b11b56b5SAlan Cox */ 11653407fefeSKonstantin Belousov vm_page_aflag_set(m, PGA_REFERENCED); 1166567e51e1SAlan Cox } 11679b98b796SAlan Cox if (object != tobject) 116889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1169*c7aebda8SAttilio Rao VM_OBJECT_WUNLOCK(tobject); 1170*c7aebda8SAttilio Rao vm_page_busy_sleep(m, "madvpo"); 117189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 11726e20a165SJohn Dyson goto relookup; 117334567de7SAlan Cox } 1174867a482dSJohn Dyson if (advise == MADV_WILLNEED) { 1175867a482dSJohn Dyson vm_page_activate(m); 11762051980fSAlan Cox } else { 11772051980fSAlan Cox vm_page_advise(m, advise); 1178867a482dSJohn Dyson } 11792965a453SKip Macy vm_page_unlock(m); 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) 118489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(tobject); 1185867a482dSJohn Dyson } 118689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(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) { 121489f6b863SAttilio Rao VM_OBJECT_WLOCK(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)) { 121989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(source); 12209a2f6362SAlan Cox return; 12219917e010SAlan Cox } 122289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(source); 1223570a2f4aSAlan Cox } 12249a2f6362SAlan Cox 12259a2f6362SAlan Cox /* 1226570a2f4aSAlan Cox * Allocate a new object with the given length. 1227df8bae1dSRodney W. Grimes */ 12280cc74f14SAlan Cox result = vm_object_allocate(OBJT_DEFAULT, atop(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) { 124789f6b863SAttilio Rao VM_OBJECT_WLOCK(source); 12481c500307SAlan Cox LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list); 1249eaf13dd7SJohn Dyson source->shadow_count++; 1250f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 12517b54b1a9SAlan Cox result->flags |= source->flags & OBJ_COLORED; 1252f8a47341SAlan Cox result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & 1253f8a47341SAlan Cox ((1 << (VM_NFREEORDER - 1)) - 1); 1254f8a47341SAlan Cox #endif 125589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(source); 1256de5f6a77SJohn Dyson } 1257df8bae1dSRodney W. Grimes 1258df8bae1dSRodney W. Grimes 1259df8bae1dSRodney W. Grimes /* 1260df8bae1dSRodney W. Grimes * Return the new things 1261df8bae1dSRodney W. Grimes */ 1262df8bae1dSRodney W. Grimes *offset = 0; 1263df8bae1dSRodney W. Grimes *object = result; 1264df8bae1dSRodney W. Grimes } 1265df8bae1dSRodney W. Grimes 1266c5aaa06dSAlan Cox /* 1267c5aaa06dSAlan Cox * vm_object_split: 1268c5aaa06dSAlan Cox * 1269c5aaa06dSAlan Cox * Split the pages in a map entry into a new object. This affords 1270c5aaa06dSAlan Cox * easier removal of unused pages, and keeps object inheritance from 1271c5aaa06dSAlan Cox * being a negative impact on memory usage. 1272c5aaa06dSAlan Cox */ 1273c5aaa06dSAlan Cox void 1274c5aaa06dSAlan Cox vm_object_split(vm_map_entry_t entry) 1275c5aaa06dSAlan Cox { 127673000556SAlan Cox vm_page_t m, m_next; 1277c5aaa06dSAlan Cox vm_object_t orig_object, new_object, source; 127873000556SAlan Cox vm_pindex_t idx, offidxstart; 127973000556SAlan Cox vm_size_t size; 1280c5aaa06dSAlan Cox 1281c5aaa06dSAlan Cox orig_object = entry->object.vm_object; 1282c5aaa06dSAlan Cox if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP) 1283c5aaa06dSAlan Cox return; 1284c5aaa06dSAlan Cox if (orig_object->ref_count <= 1) 1285c5aaa06dSAlan Cox return; 128689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 1287c5aaa06dSAlan Cox 12884da9f125SAlan Cox offidxstart = OFF_TO_IDX(entry->offset); 128995442adfSAlan Cox size = atop(entry->end - entry->start); 1290c5aaa06dSAlan Cox 12914da9f125SAlan Cox /* 12924da9f125SAlan Cox * If swap_pager_copy() is later called, it will convert new_object 12934da9f125SAlan Cox * into a swap object. 12944da9f125SAlan Cox */ 12954da9f125SAlan Cox new_object = vm_object_allocate(OBJT_DEFAULT, size); 1296c5aaa06dSAlan Cox 1297c5474b8fSAlan Cox /* 1298c5474b8fSAlan Cox * At this point, the new object is still private, so the order in 1299c5474b8fSAlan Cox * which the original and new objects are locked does not matter. 1300c5474b8fSAlan Cox */ 130189f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 130289f6b863SAttilio Rao VM_OBJECT_WLOCK(orig_object); 1303c5aaa06dSAlan Cox source = orig_object->backing_object; 1304c5aaa06dSAlan Cox if (source != NULL) { 130589f6b863SAttilio Rao VM_OBJECT_WLOCK(source); 130619c244d0SAlan Cox if ((source->flags & OBJ_DEAD) != 0) { 130789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(source); 130889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 130989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 131019c244d0SAlan Cox vm_object_deallocate(new_object); 131189f6b863SAttilio Rao VM_OBJECT_WLOCK(orig_object); 131219c244d0SAlan Cox return; 131319c244d0SAlan Cox } 13141c500307SAlan Cox LIST_INSERT_HEAD(&source->shadow_head, 1315c5aaa06dSAlan Cox new_object, shadow_list); 13168e3a76fbSAlan Cox source->shadow_count++; 1317b921a12bSAlan Cox vm_object_reference_locked(source); /* for new_object */ 1318c5aaa06dSAlan Cox vm_object_clear_flag(source, OBJ_ONEMAPPING); 131989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(source); 1320c5aaa06dSAlan Cox new_object->backing_object_offset = 13214da9f125SAlan Cox orig_object->backing_object_offset + entry->offset; 1322c5aaa06dSAlan Cox new_object->backing_object = source; 1323c5aaa06dSAlan Cox } 1324ef694c1aSEdward Tomasz Napierala if (orig_object->cred != NULL) { 1325ef694c1aSEdward Tomasz Napierala new_object->cred = orig_object->cred; 1326ef694c1aSEdward Tomasz Napierala crhold(orig_object->cred); 13273364c323SKonstantin Belousov new_object->charge = ptoa(size); 13283364c323SKonstantin Belousov KASSERT(orig_object->charge >= ptoa(size), 13293364c323SKonstantin Belousov ("orig_object->charge < 0")); 13303364c323SKonstantin Belousov orig_object->charge -= ptoa(size); 13313364c323SKonstantin Belousov } 1332c5aaa06dSAlan Cox retry: 1333b382c10aSKonstantin Belousov m = vm_page_find_least(orig_object, offidxstart); 133473000556SAlan Cox for (; m != NULL && (idx = m->pindex - offidxstart) < size; 133573000556SAlan Cox m = m_next) { 133673000556SAlan Cox m_next = TAILQ_NEXT(m, listq); 1337c5aaa06dSAlan Cox 1338c5aaa06dSAlan Cox /* 1339c5aaa06dSAlan Cox * We must wait for pending I/O to complete before we can 1340c5aaa06dSAlan Cox * rename the page. 1341c5aaa06dSAlan Cox * 1342c5aaa06dSAlan Cox * We do not have to VM_PROT_NONE the page as mappings should 1343c5aaa06dSAlan Cox * not be changed by this operation. 1344c5aaa06dSAlan Cox */ 1345*c7aebda8SAttilio Rao if (vm_page_busied(m)) { 134689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 1347*c7aebda8SAttilio Rao vm_page_lock(m); 1348*c7aebda8SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 1349*c7aebda8SAttilio Rao vm_page_busy_sleep(m, "spltwt"); 1350*c7aebda8SAttilio Rao VM_OBJECT_WLOCK(orig_object); 135189f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 1352c5aaa06dSAlan Cox goto retry; 1353de33beddSAlan Cox } 1354b5f359b7SAlan Cox #if VM_NRESERVLEVEL > 0 1355b5f359b7SAlan Cox /* 1356b5f359b7SAlan Cox * If some of the reservation's allocated pages remain with 1357b5f359b7SAlan Cox * the original object, then transferring the reservation to 1358b5f359b7SAlan Cox * the new object is neither particularly beneficial nor 1359b5f359b7SAlan Cox * particularly harmful as compared to leaving the reservation 1360b5f359b7SAlan Cox * with the original object. If, however, all of the 1361b5f359b7SAlan Cox * reservation's allocated pages are transferred to the new 1362b5f359b7SAlan Cox * object, then transferring the reservation is typically 1363b5f359b7SAlan Cox * beneficial. Determining which of these two cases applies 1364b5f359b7SAlan Cox * would be more costly than unconditionally renaming the 1365b5f359b7SAlan Cox * reservation. 1366b5f359b7SAlan Cox */ 1367b5f359b7SAlan Cox vm_reserv_rename(m, new_object, orig_object, offidxstart); 1368b5f359b7SAlan Cox #endif 13692965a453SKip Macy vm_page_lock(m); 1370c5aaa06dSAlan Cox vm_page_rename(m, new_object, idx); 13712965a453SKip Macy vm_page_unlock(m); 1372c5aaa06dSAlan Cox /* page automatically made dirty by rename and cache handled */ 1373dfd55c0cSAttilio Rao if (orig_object->type == OBJT_SWAP) 1374*c7aebda8SAttilio Rao vm_page_xbusy(m); 1375c5aaa06dSAlan Cox } 1376d7a013c3SAlan Cox if (orig_object->type == OBJT_SWAP) { 1377c5aaa06dSAlan Cox /* 1378c7c8dd7eSAlan Cox * swap_pager_copy() can sleep, in which case the orig_object's 1379c7c8dd7eSAlan Cox * and new_object's locks are released and reacquired. 1380c5aaa06dSAlan Cox */ 1381c5aaa06dSAlan Cox swap_pager_copy(orig_object, new_object, offidxstart, 0); 1382dfd55c0cSAttilio Rao TAILQ_FOREACH(m, &new_object->memq, listq) 1383*c7aebda8SAttilio Rao vm_page_xunbusy(m); 13847bfda801SAlan Cox 13857bfda801SAlan Cox /* 13867bfda801SAlan Cox * Transfer any cached pages from orig_object to new_object. 1387571a1e92SAttilio Rao * If swap_pager_copy() found swapped out pages within the 1388571a1e92SAttilio Rao * specified range of orig_object, then it changed 1389571a1e92SAttilio Rao * new_object's type to OBJT_SWAP when it transferred those 1390571a1e92SAttilio Rao * pages to new_object. Otherwise, new_object's type 1391571a1e92SAttilio Rao * should still be OBJT_DEFAULT and orig_object should not 1392571a1e92SAttilio Rao * contain any cached pages within the specified range. 13937bfda801SAlan Cox */ 1394c9341161SAttilio Rao if (__predict_false(!vm_object_cache_is_empty(orig_object))) 13957bfda801SAlan Cox vm_page_cache_transfer(orig_object, offidxstart, 13967bfda801SAlan Cox new_object); 1397c5aaa06dSAlan Cox } 139889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(orig_object); 139989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_object); 1400c5aaa06dSAlan Cox entry->object.vm_object = new_object; 1401c5aaa06dSAlan Cox entry->offset = 0LL; 1402c5aaa06dSAlan Cox vm_object_deallocate(orig_object); 140389f6b863SAttilio Rao VM_OBJECT_WLOCK(new_object); 1404c5aaa06dSAlan Cox } 1405c5aaa06dSAlan Cox 14062ad1a3f7SMatthew Dillon #define OBSC_TEST_ALL_SHADOWED 0x0001 14072ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_NOWAIT 0x0002 14082ad1a3f7SMatthew Dillon #define OBSC_COLLAPSE_WAIT 0x0004 14092ad1a3f7SMatthew Dillon 1410b4ae4780SPoul-Henning Kamp static int 14112ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op) 14122ad1a3f7SMatthew Dillon { 14132ad1a3f7SMatthew Dillon int r = 1; 14142ad1a3f7SMatthew Dillon vm_page_t p; 14152ad1a3f7SMatthew Dillon vm_object_t backing_object; 14162ad1a3f7SMatthew Dillon vm_pindex_t backing_offset_index; 14172ad1a3f7SMatthew Dillon 141889f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 141989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object->backing_object); 14202ad1a3f7SMatthew Dillon 14212ad1a3f7SMatthew Dillon backing_object = object->backing_object; 14222ad1a3f7SMatthew Dillon backing_offset_index = OFF_TO_IDX(object->backing_object_offset); 14232ad1a3f7SMatthew Dillon 14242ad1a3f7SMatthew Dillon /* 14252ad1a3f7SMatthew Dillon * Initial conditions 14262ad1a3f7SMatthew Dillon */ 14272ad1a3f7SMatthew Dillon if (op & OBSC_TEST_ALL_SHADOWED) { 14282ad1a3f7SMatthew Dillon /* 14297bfda801SAlan Cox * We do not want to have to test for the existence of cache 14307bfda801SAlan Cox * or swap pages in the backing object. XXX but with the 14312ad1a3f7SMatthew Dillon * new swapper this would be pretty easy to do. 14322ad1a3f7SMatthew Dillon * 14332ad1a3f7SMatthew Dillon * XXX what about anonymous MAP_SHARED memory that hasn't 14342ad1a3f7SMatthew Dillon * been ZFOD faulted yet? If we do not test for this, the 14352ad1a3f7SMatthew Dillon * shadow test may succeed! XXX 14362ad1a3f7SMatthew Dillon */ 14372ad1a3f7SMatthew Dillon if (backing_object->type != OBJT_DEFAULT) { 14382ad1a3f7SMatthew Dillon return (0); 14392ad1a3f7SMatthew Dillon } 14402ad1a3f7SMatthew Dillon } 14412ad1a3f7SMatthew Dillon if (op & OBSC_COLLAPSE_WAIT) { 14422ad1a3f7SMatthew Dillon vm_object_set_flag(backing_object, OBJ_DEAD); 14432ad1a3f7SMatthew Dillon } 14442ad1a3f7SMatthew Dillon 14452ad1a3f7SMatthew Dillon /* 14462ad1a3f7SMatthew Dillon * Our scan 14472ad1a3f7SMatthew Dillon */ 14482ad1a3f7SMatthew Dillon p = TAILQ_FIRST(&backing_object->memq); 14492ad1a3f7SMatthew Dillon while (p) { 14502ad1a3f7SMatthew Dillon vm_page_t next = TAILQ_NEXT(p, listq); 14512ad1a3f7SMatthew Dillon vm_pindex_t new_pindex = p->pindex - backing_offset_index; 14522ad1a3f7SMatthew Dillon 14532ad1a3f7SMatthew Dillon if (op & OBSC_TEST_ALL_SHADOWED) { 14542ad1a3f7SMatthew Dillon vm_page_t pp; 14552ad1a3f7SMatthew Dillon 14562ad1a3f7SMatthew Dillon /* 14572ad1a3f7SMatthew Dillon * Ignore pages outside the parent object's range 14582ad1a3f7SMatthew Dillon * and outside the parent object's mapping of the 14592ad1a3f7SMatthew Dillon * backing object. 14602ad1a3f7SMatthew Dillon * 14612ad1a3f7SMatthew Dillon * note that we do not busy the backing object's 14622ad1a3f7SMatthew Dillon * page. 14632ad1a3f7SMatthew Dillon */ 14642ad1a3f7SMatthew Dillon if ( 14652ad1a3f7SMatthew Dillon p->pindex < backing_offset_index || 14662ad1a3f7SMatthew Dillon new_pindex >= object->size 14672ad1a3f7SMatthew Dillon ) { 14682ad1a3f7SMatthew Dillon p = next; 14692ad1a3f7SMatthew Dillon continue; 14702ad1a3f7SMatthew Dillon } 14712ad1a3f7SMatthew Dillon 14722ad1a3f7SMatthew Dillon /* 14732ad1a3f7SMatthew Dillon * See if the parent has the page or if the parent's 14742ad1a3f7SMatthew Dillon * object pager has the page. If the parent has the 14752ad1a3f7SMatthew Dillon * page but the page is not valid, the parent's 14762ad1a3f7SMatthew Dillon * object pager must have the page. 14772ad1a3f7SMatthew Dillon * 14782ad1a3f7SMatthew Dillon * If this fails, the parent does not completely shadow 14792ad1a3f7SMatthew Dillon * the object and we might as well give up now. 14802ad1a3f7SMatthew Dillon */ 14812ad1a3f7SMatthew Dillon 14822ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 14832ad1a3f7SMatthew Dillon if ( 14842ad1a3f7SMatthew Dillon (pp == NULL || pp->valid == 0) && 14852ad1a3f7SMatthew Dillon !vm_pager_has_page(object, new_pindex, NULL, NULL) 14862ad1a3f7SMatthew Dillon ) { 14872ad1a3f7SMatthew Dillon r = 0; 14882ad1a3f7SMatthew Dillon break; 14892ad1a3f7SMatthew Dillon } 14902ad1a3f7SMatthew Dillon } 14912ad1a3f7SMatthew Dillon 14922ad1a3f7SMatthew Dillon /* 14932ad1a3f7SMatthew Dillon * Check for busy page 14942ad1a3f7SMatthew Dillon */ 14952ad1a3f7SMatthew Dillon if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) { 14962ad1a3f7SMatthew Dillon vm_page_t pp; 14972ad1a3f7SMatthew Dillon 14982ad1a3f7SMatthew Dillon if (op & OBSC_COLLAPSE_NOWAIT) { 1499*c7aebda8SAttilio Rao if (!p->valid || vm_page_busied(p)) { 15002ad1a3f7SMatthew Dillon p = next; 15012ad1a3f7SMatthew Dillon continue; 15022ad1a3f7SMatthew Dillon } 15032ad1a3f7SMatthew Dillon } else if (op & OBSC_COLLAPSE_WAIT) { 1504*c7aebda8SAttilio Rao if (vm_page_busied(p)) { 150589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1506*c7aebda8SAttilio Rao vm_page_lock(p); 1507*c7aebda8SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 1508*c7aebda8SAttilio Rao vm_page_busy_sleep(p, "vmocol"); 150989f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 151089f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 15112ad1a3f7SMatthew Dillon /* 15122ad1a3f7SMatthew Dillon * If we slept, anything could have 15132ad1a3f7SMatthew Dillon * happened. Since the object is 15142ad1a3f7SMatthew Dillon * marked dead, the backing offset 15152ad1a3f7SMatthew Dillon * should not have changed so we 15162ad1a3f7SMatthew Dillon * just restart our scan. 15172ad1a3f7SMatthew Dillon */ 15182ad1a3f7SMatthew Dillon p = TAILQ_FIRST(&backing_object->memq); 15192ad1a3f7SMatthew Dillon continue; 15202ad1a3f7SMatthew Dillon } 15212ad1a3f7SMatthew Dillon } 15222ad1a3f7SMatthew Dillon 15232ad1a3f7SMatthew Dillon KASSERT( 15242ad1a3f7SMatthew Dillon p->object == backing_object, 15258e99783bSAlan Cox ("vm_object_backing_scan: object mismatch") 15262ad1a3f7SMatthew Dillon ); 15272ad1a3f7SMatthew Dillon 15282ad1a3f7SMatthew Dillon /* 15292ad1a3f7SMatthew Dillon * Destroy any associated swap 15302ad1a3f7SMatthew Dillon */ 15312ad1a3f7SMatthew Dillon if (backing_object->type == OBJT_SWAP) { 15322ad1a3f7SMatthew Dillon swap_pager_freespace( 15332ad1a3f7SMatthew Dillon backing_object, 15342ad1a3f7SMatthew Dillon p->pindex, 15352ad1a3f7SMatthew Dillon 1 15362ad1a3f7SMatthew Dillon ); 15372ad1a3f7SMatthew Dillon } 15382ad1a3f7SMatthew Dillon 15392ad1a3f7SMatthew Dillon if ( 15402ad1a3f7SMatthew Dillon p->pindex < backing_offset_index || 15412ad1a3f7SMatthew Dillon new_pindex >= object->size 15422ad1a3f7SMatthew Dillon ) { 15432ad1a3f7SMatthew Dillon /* 15442ad1a3f7SMatthew Dillon * Page is out of the parent object's range, we 15452ad1a3f7SMatthew Dillon * can simply destroy it. 15462ad1a3f7SMatthew Dillon */ 15472965a453SKip Macy vm_page_lock(p); 1548f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1549f6d89838SAlan Cox ("freeing mapped page %p", p)); 1550f6d89838SAlan Cox if (p->wire_count == 0) 15512ad1a3f7SMatthew Dillon vm_page_free(p); 1552f6d89838SAlan Cox else 1553f6d89838SAlan Cox vm_page_remove(p); 15542965a453SKip Macy vm_page_unlock(p); 15552ad1a3f7SMatthew Dillon p = next; 15562ad1a3f7SMatthew Dillon continue; 15572ad1a3f7SMatthew Dillon } 15582ad1a3f7SMatthew Dillon 15592ad1a3f7SMatthew Dillon pp = vm_page_lookup(object, new_pindex); 15602ad1a3f7SMatthew Dillon if ( 1561e18cc7bfSMax Laier (op & OBSC_COLLAPSE_NOWAIT) != 0 && 1562e18cc7bfSMax Laier (pp != NULL && pp->valid == 0) 1563e18cc7bfSMax Laier ) { 1564e18cc7bfSMax Laier /* 1565e18cc7bfSMax Laier * The page in the parent is not (yet) valid. 1566e18cc7bfSMax Laier * We don't know anything about the state of 1567e18cc7bfSMax Laier * the original page. It might be mapped, 1568e18cc7bfSMax Laier * so we must avoid the next if here. 1569e18cc7bfSMax Laier * 1570e18cc7bfSMax Laier * This is due to a race in vm_fault() where 1571e18cc7bfSMax Laier * we must unbusy the original (backing_obj) 1572e18cc7bfSMax Laier * page before we can (re)lock the parent. 1573e18cc7bfSMax Laier * Hence we can get here. 1574e18cc7bfSMax Laier */ 1575e18cc7bfSMax Laier p = next; 1576e18cc7bfSMax Laier continue; 1577e18cc7bfSMax Laier } 1578e18cc7bfSMax Laier if ( 15792ad1a3f7SMatthew Dillon pp != NULL || 15802ad1a3f7SMatthew Dillon vm_pager_has_page(object, new_pindex, NULL, NULL) 15812ad1a3f7SMatthew Dillon ) { 15822ad1a3f7SMatthew Dillon /* 15832ad1a3f7SMatthew Dillon * page already exists in parent OR swap exists 15842ad1a3f7SMatthew Dillon * for this location in the parent. Destroy 15852ad1a3f7SMatthew Dillon * the original page from the backing object. 15862ad1a3f7SMatthew Dillon * 15872ad1a3f7SMatthew Dillon * Leave the parent's page alone 15882ad1a3f7SMatthew Dillon */ 15892965a453SKip Macy vm_page_lock(p); 1590f6d89838SAlan Cox KASSERT(!pmap_page_is_mapped(p), 1591f6d89838SAlan Cox ("freeing mapped page %p", p)); 1592f6d89838SAlan Cox if (p->wire_count == 0) 15932ad1a3f7SMatthew Dillon vm_page_free(p); 1594f6d89838SAlan Cox else 1595f6d89838SAlan Cox vm_page_remove(p); 15962965a453SKip Macy vm_page_unlock(p); 15972ad1a3f7SMatthew Dillon p = next; 15982ad1a3f7SMatthew Dillon continue; 15992ad1a3f7SMatthew Dillon } 16002ad1a3f7SMatthew Dillon 1601f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1602f8a47341SAlan Cox /* 1603f8a47341SAlan Cox * Rename the reservation. 1604f8a47341SAlan Cox */ 1605f8a47341SAlan Cox vm_reserv_rename(p, object, backing_object, 1606f8a47341SAlan Cox backing_offset_index); 1607f8a47341SAlan Cox #endif 1608f8a47341SAlan Cox 16092ad1a3f7SMatthew Dillon /* 16102ad1a3f7SMatthew Dillon * Page does not exist in parent, rename the 16112ad1a3f7SMatthew Dillon * page from the backing object to the main object. 1612d1bf5d56SMatthew Dillon * 1613d1bf5d56SMatthew Dillon * If the page was mapped to a process, it can remain 1614d1bf5d56SMatthew Dillon * mapped through the rename. 16152ad1a3f7SMatthew Dillon */ 16162965a453SKip Macy vm_page_lock(p); 16172ad1a3f7SMatthew Dillon vm_page_rename(p, object, new_pindex); 16182965a453SKip Macy vm_page_unlock(p); 16192ad1a3f7SMatthew Dillon /* page automatically made dirty by rename */ 16202ad1a3f7SMatthew Dillon } 16212ad1a3f7SMatthew Dillon p = next; 16222ad1a3f7SMatthew Dillon } 16232ad1a3f7SMatthew Dillon return (r); 16242ad1a3f7SMatthew Dillon } 16252ad1a3f7SMatthew Dillon 1626df8bae1dSRodney W. Grimes 1627df8bae1dSRodney W. Grimes /* 16282fe6e4d7SDavid Greenman * this version of collapse allows the operation to occur earlier and 16292fe6e4d7SDavid Greenman * when paging_in_progress is true for an object... This is not a complete 16302fe6e4d7SDavid Greenman * operation, but should plug 99.9% of the rest of the leaks. 16312fe6e4d7SDavid Greenman */ 16322fe6e4d7SDavid Greenman static void 16331b40f8c0SMatthew Dillon vm_object_qcollapse(vm_object_t object) 16342fe6e4d7SDavid Greenman { 16352ad1a3f7SMatthew Dillon vm_object_t backing_object = object->backing_object; 16362fe6e4d7SDavid Greenman 163789f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 163889f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(backing_object); 16391b40f8c0SMatthew Dillon 16402fe6e4d7SDavid Greenman if (backing_object->ref_count != 1) 16412fe6e4d7SDavid Greenman return; 16422fe6e4d7SDavid Greenman 16432ad1a3f7SMatthew Dillon vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT); 16442fe6e4d7SDavid Greenman } 16452fe6e4d7SDavid Greenman 1646df8bae1dSRodney W. Grimes /* 1647df8bae1dSRodney W. Grimes * vm_object_collapse: 1648df8bae1dSRodney W. Grimes * 1649df8bae1dSRodney W. Grimes * Collapse an object with the object backing it. 1650df8bae1dSRodney W. Grimes * Pages in the backing object are moved into the 1651df8bae1dSRodney W. Grimes * parent, and the backing object is deallocated. 1652df8bae1dSRodney W. Grimes */ 165326f9a767SRodney W. Grimes void 16541b40f8c0SMatthew Dillon vm_object_collapse(vm_object_t object) 1655df8bae1dSRodney W. Grimes { 165689f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 165723955314SAlfred Perlstein 1658df8bae1dSRodney W. Grimes while (TRUE) { 16592ad1a3f7SMatthew Dillon vm_object_t backing_object; 16602ad1a3f7SMatthew Dillon 1661df8bae1dSRodney W. Grimes /* 1662df8bae1dSRodney W. Grimes * Verify that the conditions are right for collapse: 1663df8bae1dSRodney W. Grimes * 16642ad1a3f7SMatthew Dillon * The object exists and the backing object exists. 1665df8bae1dSRodney W. Grimes */ 166624a1cce3SDavid Greenman if ((backing_object = object->backing_object) == NULL) 16672ad1a3f7SMatthew Dillon break; 1668df8bae1dSRodney W. Grimes 1669f919ebdeSDavid Greenman /* 1670f919ebdeSDavid Greenman * we check the backing object first, because it is most likely 167124a1cce3SDavid Greenman * not collapsable. 1672f919ebdeSDavid Greenman */ 167389f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object); 167424a1cce3SDavid Greenman if (backing_object->handle != NULL || 167524a1cce3SDavid Greenman (backing_object->type != OBJT_DEFAULT && 167624a1cce3SDavid Greenman backing_object->type != OBJT_SWAP) || 1677f919ebdeSDavid Greenman (backing_object->flags & OBJ_DEAD) || 167824a1cce3SDavid Greenman object->handle != NULL || 167924a1cce3SDavid Greenman (object->type != OBJT_DEFAULT && 168024a1cce3SDavid Greenman object->type != OBJT_SWAP) || 168124a1cce3SDavid Greenman (object->flags & OBJ_DEAD)) { 168289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 16832ad1a3f7SMatthew Dillon break; 168424a1cce3SDavid Greenman } 16859b4814bbSDavid Greenman 16862ad1a3f7SMatthew Dillon if ( 16872ad1a3f7SMatthew Dillon object->paging_in_progress != 0 || 16882ad1a3f7SMatthew Dillon backing_object->paging_in_progress != 0 16892ad1a3f7SMatthew Dillon ) { 1690b9921222SDavid Greenman vm_object_qcollapse(object); 169189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 16922ad1a3f7SMatthew Dillon break; 1693df8bae1dSRodney W. Grimes } 169426f9a767SRodney W. Grimes /* 16950d94caffSDavid Greenman * We know that we can either collapse the backing object (if 16962ad1a3f7SMatthew Dillon * the parent is the only reference to it) or (perhaps) have 16972ad1a3f7SMatthew Dillon * the parent bypass the object if the parent happens to shadow 16982ad1a3f7SMatthew Dillon * all the resident pages in the entire backing object. 16992ad1a3f7SMatthew Dillon * 17002ad1a3f7SMatthew Dillon * This is ignoring pager-backed pages such as swap pages. 17012ad1a3f7SMatthew Dillon * vm_object_backing_scan fails the shadowing test in this 17022ad1a3f7SMatthew Dillon * case. 1703df8bae1dSRodney W. Grimes */ 1704df8bae1dSRodney W. Grimes if (backing_object->ref_count == 1) { 1705df8bae1dSRodney W. Grimes /* 17062ad1a3f7SMatthew Dillon * If there is exactly one reference to the backing 17072ad1a3f7SMatthew Dillon * object, we can collapse it into the parent. 1708df8bae1dSRodney W. Grimes */ 17092ad1a3f7SMatthew Dillon vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT); 1710df8bae1dSRodney W. Grimes 1711f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 1712f8a47341SAlan Cox /* 1713f8a47341SAlan Cox * Break any reservations from backing_object. 1714f8a47341SAlan Cox */ 1715f8a47341SAlan Cox if (__predict_false(!LIST_EMPTY(&backing_object->rvq))) 1716f8a47341SAlan Cox vm_reserv_break_all(backing_object); 1717f8a47341SAlan Cox #endif 1718f8a47341SAlan Cox 1719df8bae1dSRodney W. Grimes /* 1720df8bae1dSRodney W. Grimes * Move the pager from backing_object to object. 1721df8bae1dSRodney W. Grimes */ 17226be36525SAlan Cox if (backing_object->type == OBJT_SWAP) { 172324a1cce3SDavid Greenman /* 1724c7c8dd7eSAlan Cox * swap_pager_copy() can sleep, in which case 1725c7c8dd7eSAlan Cox * the backing_object's and object's locks are 1726c7c8dd7eSAlan Cox * released and reacquired. 1727571a1e92SAttilio Rao * Since swap_pager_copy() is being asked to 1728571a1e92SAttilio Rao * destroy the source, it will change the 1729571a1e92SAttilio Rao * backing_object's type to OBJT_DEFAULT. 173024a1cce3SDavid Greenman */ 17311c7c3c6aSMatthew Dillon swap_pager_copy( 17321c7c3c6aSMatthew Dillon backing_object, 17331c7c3c6aSMatthew Dillon object, 17341c7c3c6aSMatthew Dillon OFF_TO_IDX(object->backing_object_offset), TRUE); 17357bfda801SAlan Cox 17367bfda801SAlan Cox /* 17377bfda801SAlan Cox * Free any cached pages from backing_object. 17387bfda801SAlan Cox */ 1739c9341161SAttilio Rao if (__predict_false( 1740c9341161SAttilio Rao !vm_object_cache_is_empty(backing_object))) 1741c9444914SAlan Cox vm_page_cache_free(backing_object, 0, 0); 1742c0503609SDavid Greenman } 1743df8bae1dSRodney W. Grimes /* 1744df8bae1dSRodney W. Grimes * Object now shadows whatever backing_object did. 17452ad1a3f7SMatthew Dillon * Note that the reference to 17462ad1a3f7SMatthew Dillon * backing_object->backing_object moves from within 17472ad1a3f7SMatthew Dillon * backing_object to within object. 1748df8bae1dSRodney W. Grimes */ 17491c500307SAlan Cox LIST_REMOVE(object, shadow_list); 17504f7c7f6eSAlan Cox backing_object->shadow_count--; 1751de5f6a77SJohn Dyson if (backing_object->backing_object) { 175289f6b863SAttilio Rao VM_OBJECT_WLOCK(backing_object->backing_object); 17531c500307SAlan Cox LIST_REMOVE(backing_object, shadow_list); 175443186e53SAlan Cox LIST_INSERT_HEAD( 175543186e53SAlan Cox &backing_object->backing_object->shadow_head, 175643186e53SAlan Cox object, shadow_list); 175743186e53SAlan Cox /* 175843186e53SAlan Cox * The shadow_count has not changed. 175943186e53SAlan Cox */ 176089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object->backing_object); 1761de5f6a77SJohn Dyson } 176224a1cce3SDavid Greenman object->backing_object = backing_object->backing_object; 17632ad1a3f7SMatthew Dillon object->backing_object_offset += 17642ad1a3f7SMatthew Dillon backing_object->backing_object_offset; 17652ad1a3f7SMatthew Dillon 1766df8bae1dSRodney W. Grimes /* 1767df8bae1dSRodney W. Grimes * Discard backing_object. 1768df8bae1dSRodney W. Grimes * 17690d94caffSDavid Greenman * Since the backing object has no pages, no pager left, 17700d94caffSDavid Greenman * and no object references within it, all that is 17710d94caffSDavid Greenman * necessary is to dispose of it. 1772df8bae1dSRodney W. Grimes */ 17739b4d473aSKonstantin Belousov KASSERT(backing_object->ref_count == 1, ( 17749b4d473aSKonstantin Belousov "backing_object %p was somehow re-referenced during collapse!", 17759b4d473aSKonstantin Belousov backing_object)); 177689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 17779b4d473aSKonstantin Belousov vm_object_destroy(backing_object); 1778df8bae1dSRodney W. Grimes 1779df8bae1dSRodney W. Grimes object_collapses++; 17800d94caffSDavid Greenman } else { 178195e5e988SJohn Dyson vm_object_t new_backing_object; 1782df8bae1dSRodney W. Grimes 1783df8bae1dSRodney W. Grimes /* 17842ad1a3f7SMatthew Dillon * If we do not entirely shadow the backing object, 17852ad1a3f7SMatthew Dillon * there is nothing we can do so we give up. 1786df8bae1dSRodney W. Grimes */ 1787df59a0feSJeff Roberson if (object->resident_page_count != object->size && 1788df59a0feSJeff Roberson vm_object_backing_scan(object, 1789df59a0feSJeff Roberson OBSC_TEST_ALL_SHADOWED) == 0) { 179089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 17912ad1a3f7SMatthew Dillon break; 179224a1cce3SDavid Greenman } 1793df8bae1dSRodney W. Grimes 1794df8bae1dSRodney W. Grimes /* 17950d94caffSDavid Greenman * Make the parent shadow the next object in the 17960d94caffSDavid Greenman * chain. Deallocating backing_object will not remove 17970d94caffSDavid Greenman * it, since its reference count is at least 2. 1798df8bae1dSRodney W. Grimes */ 17991c500307SAlan Cox LIST_REMOVE(object, shadow_list); 1800eaf13dd7SJohn Dyson backing_object->shadow_count--; 180195e5e988SJohn Dyson 180295e5e988SJohn Dyson new_backing_object = backing_object->backing_object; 18038aef1712SMatthew Dillon if ((object->backing_object = new_backing_object) != NULL) { 180489f6b863SAttilio Rao VM_OBJECT_WLOCK(new_backing_object); 18051c500307SAlan Cox LIST_INSERT_HEAD( 18062ad1a3f7SMatthew Dillon &new_backing_object->shadow_head, 18072ad1a3f7SMatthew Dillon object, 18082ad1a3f7SMatthew Dillon shadow_list 18092ad1a3f7SMatthew Dillon ); 1810eaf13dd7SJohn Dyson new_backing_object->shadow_count++; 1811b921a12bSAlan Cox vm_object_reference_locked(new_backing_object); 181289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(new_backing_object); 181395e5e988SJohn Dyson object->backing_object_offset += 181495e5e988SJohn Dyson backing_object->backing_object_offset; 1815de5f6a77SJohn Dyson } 1816df8bae1dSRodney W. Grimes 1817df8bae1dSRodney W. Grimes /* 18180d94caffSDavid Greenman * Drop the reference count on backing_object. Since 181922ec553fSAlan Cox * its ref_count was at least 2, it will not vanish. 1820df8bae1dSRodney W. Grimes */ 182122ec553fSAlan Cox backing_object->ref_count--; 182289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(backing_object); 1823df8bae1dSRodney W. Grimes object_bypasses++; 1824df8bae1dSRodney W. Grimes } 1825df8bae1dSRodney W. Grimes 1826df8bae1dSRodney W. Grimes /* 1827df8bae1dSRodney W. Grimes * Try again with this object's new backing object. 1828df8bae1dSRodney W. Grimes */ 1829df8bae1dSRodney W. Grimes } 1830df8bae1dSRodney W. Grimes } 1831df8bae1dSRodney W. Grimes 1832df8bae1dSRodney W. Grimes /* 1833bff99f0dSAlan Cox * vm_object_page_remove: 1834df8bae1dSRodney W. Grimes * 183568855966SAlan Cox * For the given object, either frees or invalidates each of the 18366bbee8e2SAlan Cox * specified pages. In general, a page is freed. However, if a page is 18376bbee8e2SAlan Cox * wired for any reason other than the existence of a managed, wired 18386bbee8e2SAlan Cox * mapping, then it may be invalidated but not removed from the object. 18396bbee8e2SAlan Cox * Pages are specified by the given range ["start", "end") and the option 18406bbee8e2SAlan Cox * OBJPR_CLEANONLY. As a special case, if "end" is zero, then the range 18416bbee8e2SAlan Cox * extends from "start" to the end of the object. If the option 18426bbee8e2SAlan Cox * OBJPR_CLEANONLY is specified, then only the non-dirty pages within the 18436bbee8e2SAlan Cox * specified range are affected. If the option OBJPR_NOTMAPPED is 18446bbee8e2SAlan Cox * specified, then the pages within the specified range must have no 18456bbee8e2SAlan Cox * mappings. Otherwise, if this option is not specified, any mappings to 18466bbee8e2SAlan Cox * the specified pages are removed before the pages are freed or 18476bbee8e2SAlan Cox * invalidated. 184868855966SAlan Cox * 18496bbee8e2SAlan Cox * In general, this operation should only be performed on objects that 18506bbee8e2SAlan Cox * contain managed pages. There are, however, two exceptions. First, it 18516bbee8e2SAlan Cox * is performed on the kernel and kmem objects by vm_map_entry_delete(). 18526bbee8e2SAlan Cox * Second, it is used by msync(..., MS_INVALIDATE) to invalidate device- 18536bbee8e2SAlan Cox * backed pages. In both of these cases, the option OBJPR_CLEANONLY must 18546bbee8e2SAlan Cox * not be specified and the option OBJPR_NOTMAPPED must be specified. 1855df8bae1dSRodney W. Grimes * 1856df8bae1dSRodney W. Grimes * The object must be locked. 1857df8bae1dSRodney W. Grimes */ 185826f9a767SRodney W. Grimes void 1859ecde4b32SAlan Cox vm_object_page_remove(vm_object_t object, vm_pindex_t start, vm_pindex_t end, 18606bbee8e2SAlan Cox int options) 1861df8bae1dSRodney W. Grimes { 1862d031cff1SMatthew Dillon vm_page_t p, next; 186359677d3cSAlan Cox int wirings; 1864df8bae1dSRodney W. Grimes 186589f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 186628634820SAlan Cox KASSERT((object->flags & OBJ_UNMANAGED) == 0 || 18676bbee8e2SAlan Cox (options & (OBJPR_CLEANONLY | OBJPR_NOTMAPPED)) == OBJPR_NOTMAPPED, 18686bbee8e2SAlan Cox ("vm_object_page_remove: illegal options for object %p", object)); 1869ecde4b32SAlan Cox if (object->resident_page_count == 0) 187025732691SAlan Cox goto skipmemq; 1871d474eaaaSDoug Rabson vm_object_pip_add(object, 1); 187226f9a767SRodney W. Grimes again: 1873b382c10aSKonstantin Belousov p = vm_page_find_least(object, start); 18742965a453SKip Macy 187575741c04SAlan Cox /* 18766bbee8e2SAlan Cox * Here, the variable "p" is either (1) the page with the least pindex 18776bbee8e2SAlan Cox * greater than or equal to the parameter "start" or (2) NULL. 187875741c04SAlan Cox */ 18796bbee8e2SAlan Cox for (; p != NULL && (p->pindex < end || end == 0); p = next) { 1880b18bfc3dSJohn Dyson next = TAILQ_NEXT(p, listq); 188175741c04SAlan Cox 188259677d3cSAlan Cox /* 18836bbee8e2SAlan Cox * If the page is wired for any reason besides the existence 18846bbee8e2SAlan Cox * of managed, wired mappings, then it cannot be freed. For 18856bbee8e2SAlan Cox * example, fictitious pages, which represent device memory, 18866bbee8e2SAlan Cox * are inherently wired and cannot be freed. They can, 18876bbee8e2SAlan Cox * however, be invalidated if the option OBJPR_CLEANONLY is 18886bbee8e2SAlan Cox * not specified. 188959677d3cSAlan Cox */ 18902965a453SKip Macy vm_page_lock(p); 189159677d3cSAlan Cox if ((wirings = p->wire_count) != 0 && 189259677d3cSAlan Cox (wirings = pmap_page_wired_mappings(p)) != p->wire_count) { 1893ebf5d94eSKonstantin Belousov if ((options & (OBJPR_NOTWIRED | OBJPR_NOTMAPPED)) == 1894ebf5d94eSKonstantin Belousov 0) { 18954fec79beSAlan Cox pmap_remove_all(p); 18966bbee8e2SAlan Cox /* Account for removal of wired mappings. */ 18976bbee8e2SAlan Cox if (wirings != 0) 189859677d3cSAlan Cox p->wire_count -= wirings; 18996bbee8e2SAlan Cox } 19006bbee8e2SAlan Cox if ((options & OBJPR_CLEANONLY) == 0) { 1901bd7e5f99SJohn Dyson p->valid = 0; 1902a28042d1SAlan Cox vm_page_undirty(p); 1903a28042d1SAlan Cox } 1904ebf5d94eSKonstantin Belousov goto next; 19050d94caffSDavid Greenman } 1906*c7aebda8SAttilio Rao if (vm_page_busied(p)) { 1907*c7aebda8SAttilio Rao VM_OBJECT_WUNLOCK(object); 1908*c7aebda8SAttilio Rao vm_page_busy_sleep(p, "vmopar"); 1909*c7aebda8SAttilio Rao VM_OBJECT_WLOCK(object); 191026f9a767SRodney W. Grimes goto again; 1911*c7aebda8SAttilio Rao } 191268855966SAlan Cox KASSERT((p->flags & PG_FICTITIOUS) == 0, 191368855966SAlan Cox ("vm_object_page_remove: page %p is fictitious", p)); 19146bbee8e2SAlan Cox if ((options & OBJPR_CLEANONLY) != 0 && p->valid != 0) { 19156bbee8e2SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0) 191678985e42SAlan Cox pmap_remove_write(p); 1917ebf5d94eSKonstantin Belousov if (p->dirty) 1918ebf5d94eSKonstantin Belousov goto next; 19192965a453SKip Macy } 19206bbee8e2SAlan Cox if ((options & OBJPR_NOTMAPPED) == 0) { 1921ebf5d94eSKonstantin Belousov if ((options & OBJPR_NOTWIRED) != 0 && wirings != 0) 1922ebf5d94eSKonstantin Belousov goto next; 19234fec79beSAlan Cox pmap_remove_all(p); 19246bbee8e2SAlan Cox /* Account for removal of wired mappings. */ 19255f9c767bSKonstantin Belousov if (wirings != 0) { 19265f9c767bSKonstantin Belousov KASSERT(p->wire_count == wirings, 19275f9c767bSKonstantin Belousov ("inconsistent wire count %d %d %p", 19285f9c767bSKonstantin Belousov p->wire_count, wirings, p)); 19295f9c767bSKonstantin Belousov p->wire_count = 0; 19305f9c767bSKonstantin Belousov atomic_subtract_int(&cnt.v_wire_count, 1); 19315f9c767bSKonstantin Belousov } 19326bbee8e2SAlan Cox } 1933df8bae1dSRodney W. Grimes vm_page_free(p); 1934ebf5d94eSKonstantin Belousov next: 19352965a453SKip Macy vm_page_unlock(p); 19362965a453SKip Macy } 1937f919ebdeSDavid Greenman vm_object_pip_wakeup(object); 193825732691SAlan Cox skipmemq: 1939c9341161SAttilio Rao if (__predict_false(!vm_object_cache_is_empty(object))) 1940c9444914SAlan Cox vm_page_cache_free(object, start, end); 1941c0503609SDavid Greenman } 1942df8bae1dSRodney W. Grimes 1943df8bae1dSRodney W. Grimes /* 1944936c09acSJohn Baldwin * vm_object_page_cache: 1945936c09acSJohn Baldwin * 1946936c09acSJohn Baldwin * For the given object, attempt to move the specified clean 1947936c09acSJohn Baldwin * pages to the cache queue. If a page is wired for any reason, 1948936c09acSJohn Baldwin * then it will not be changed. Pages are specified by the given 1949936c09acSJohn Baldwin * range ["start", "end"). As a special case, if "end" is zero, 1950936c09acSJohn Baldwin * then the range extends from "start" to the end of the object. 1951936c09acSJohn Baldwin * Any mappings to the specified pages are removed before the 1952936c09acSJohn Baldwin * pages are moved to the cache queue. 1953936c09acSJohn Baldwin * 1954936c09acSJohn Baldwin * This operation should only be performed on objects that 195528634820SAlan Cox * contain non-fictitious, managed pages. 1956936c09acSJohn Baldwin * 1957936c09acSJohn Baldwin * The object must be locked. 1958936c09acSJohn Baldwin */ 1959936c09acSJohn Baldwin void 1960936c09acSJohn Baldwin vm_object_page_cache(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 1961936c09acSJohn Baldwin { 1962936c09acSJohn Baldwin struct mtx *mtx, *new_mtx; 1963936c09acSJohn Baldwin vm_page_t p, next; 1964936c09acSJohn Baldwin 196589f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 196628634820SAlan Cox KASSERT((object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0, 1967936c09acSJohn Baldwin ("vm_object_page_cache: illegal object %p", object)); 1968936c09acSJohn Baldwin if (object->resident_page_count == 0) 1969936c09acSJohn Baldwin return; 1970936c09acSJohn Baldwin p = vm_page_find_least(object, start); 1971936c09acSJohn Baldwin 1972936c09acSJohn Baldwin /* 1973936c09acSJohn Baldwin * Here, the variable "p" is either (1) the page with the least pindex 1974936c09acSJohn Baldwin * greater than or equal to the parameter "start" or (2) NULL. 1975936c09acSJohn Baldwin */ 1976936c09acSJohn Baldwin mtx = NULL; 1977936c09acSJohn Baldwin for (; p != NULL && (p->pindex < end || end == 0); p = next) { 1978936c09acSJohn Baldwin next = TAILQ_NEXT(p, listq); 1979936c09acSJohn Baldwin 1980936c09acSJohn Baldwin /* 1981936c09acSJohn Baldwin * Avoid releasing and reacquiring the same page lock. 1982936c09acSJohn Baldwin */ 1983936c09acSJohn Baldwin new_mtx = vm_page_lockptr(p); 1984936c09acSJohn Baldwin if (mtx != new_mtx) { 1985936c09acSJohn Baldwin if (mtx != NULL) 1986936c09acSJohn Baldwin mtx_unlock(mtx); 1987936c09acSJohn Baldwin mtx = new_mtx; 1988936c09acSJohn Baldwin mtx_lock(mtx); 1989936c09acSJohn Baldwin } 1990936c09acSJohn Baldwin vm_page_try_to_cache(p); 1991936c09acSJohn Baldwin } 1992936c09acSJohn Baldwin if (mtx != NULL) 1993936c09acSJohn Baldwin mtx_unlock(mtx); 1994936c09acSJohn Baldwin } 1995936c09acSJohn Baldwin 1996936c09acSJohn Baldwin /* 1997387aabc5SAlan Cox * Populate the specified range of the object with valid pages. Returns 1998387aabc5SAlan Cox * TRUE if the range is successfully populated and FALSE otherwise. 1999387aabc5SAlan Cox * 2000387aabc5SAlan Cox * Note: This function should be optimized to pass a larger array of 2001387aabc5SAlan Cox * pages to vm_pager_get_pages() before it is applied to a non- 2002387aabc5SAlan Cox * OBJT_DEVICE object. 2003387aabc5SAlan Cox * 2004387aabc5SAlan Cox * The object must be locked. 2005387aabc5SAlan Cox */ 2006387aabc5SAlan Cox boolean_t 2007387aabc5SAlan Cox vm_object_populate(vm_object_t object, vm_pindex_t start, vm_pindex_t end) 2008387aabc5SAlan Cox { 2009387aabc5SAlan Cox vm_page_t m, ma[1]; 2010387aabc5SAlan Cox vm_pindex_t pindex; 2011387aabc5SAlan Cox int rv; 2012387aabc5SAlan Cox 201389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 2014387aabc5SAlan Cox for (pindex = start; pindex < end; pindex++) { 2015387aabc5SAlan Cox m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL | 2016387aabc5SAlan Cox VM_ALLOC_RETRY); 2017387aabc5SAlan Cox if (m->valid != VM_PAGE_BITS_ALL) { 2018387aabc5SAlan Cox ma[0] = m; 2019387aabc5SAlan Cox rv = vm_pager_get_pages(object, ma, 1, 0); 2020387aabc5SAlan Cox m = vm_page_lookup(object, pindex); 2021387aabc5SAlan Cox if (m == NULL) 2022387aabc5SAlan Cox break; 2023387aabc5SAlan Cox if (rv != VM_PAGER_OK) { 20242965a453SKip Macy vm_page_lock(m); 2025387aabc5SAlan Cox vm_page_free(m); 20262965a453SKip Macy vm_page_unlock(m); 2027387aabc5SAlan Cox break; 2028387aabc5SAlan Cox } 2029387aabc5SAlan Cox } 2030387aabc5SAlan Cox /* 2031387aabc5SAlan Cox * Keep "m" busy because a subsequent iteration may unlock 2032387aabc5SAlan Cox * the object. 2033387aabc5SAlan Cox */ 2034387aabc5SAlan Cox } 2035387aabc5SAlan Cox if (pindex > start) { 2036387aabc5SAlan Cox m = vm_page_lookup(object, start); 2037387aabc5SAlan Cox while (m != NULL && m->pindex < pindex) { 2038*c7aebda8SAttilio Rao vm_page_xunbusy(m); 2039387aabc5SAlan Cox m = TAILQ_NEXT(m, listq); 2040387aabc5SAlan Cox } 2041387aabc5SAlan Cox } 2042387aabc5SAlan Cox return (pindex == end); 2043387aabc5SAlan Cox } 2044387aabc5SAlan Cox 2045387aabc5SAlan Cox /* 2046df8bae1dSRodney W. Grimes * Routine: vm_object_coalesce 2047df8bae1dSRodney W. Grimes * Function: Coalesces two objects backing up adjoining 2048df8bae1dSRodney W. Grimes * regions of memory into a single object. 2049df8bae1dSRodney W. Grimes * 2050df8bae1dSRodney W. Grimes * returns TRUE if objects were combined. 2051df8bae1dSRodney W. Grimes * 2052df8bae1dSRodney W. Grimes * NOTE: Only works at the moment if the second object is NULL - 2053df8bae1dSRodney W. Grimes * if it's not, which object do we lock first? 2054df8bae1dSRodney W. Grimes * 2055df8bae1dSRodney W. Grimes * Parameters: 2056df8bae1dSRodney W. Grimes * prev_object First object to coalesce 2057df8bae1dSRodney W. Grimes * prev_offset Offset into prev_object 2058df8bae1dSRodney W. Grimes * prev_size Size of reference to prev_object 205957a21abaSAlan Cox * next_size Size of reference to the second object 20603364c323SKonstantin Belousov * reserved Indicator that extension region has 20613364c323SKonstantin Belousov * swap accounted for 2062df8bae1dSRodney W. Grimes * 2063df8bae1dSRodney W. Grimes * Conditions: 2064df8bae1dSRodney W. Grimes * The object must *not* be locked. 2065df8bae1dSRodney W. Grimes */ 20660d94caffSDavid Greenman boolean_t 206757a21abaSAlan Cox vm_object_coalesce(vm_object_t prev_object, vm_ooffset_t prev_offset, 20683364c323SKonstantin Belousov vm_size_t prev_size, vm_size_t next_size, boolean_t reserved) 2069df8bae1dSRodney W. Grimes { 2070ea41812fSAlan Cox vm_pindex_t next_pindex; 2071df8bae1dSRodney W. Grimes 207200e1854aSAlan Cox if (prev_object == NULL) 2073df8bae1dSRodney W. Grimes return (TRUE); 207489f6b863SAttilio Rao VM_OBJECT_WLOCK(prev_object); 20754112823fSMatthew Dillon if (prev_object->type != OBJT_DEFAULT && 20764112823fSMatthew Dillon prev_object->type != OBJT_SWAP) { 207789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 207830dcfc09SJohn Dyson return (FALSE); 207930dcfc09SJohn Dyson } 208030dcfc09SJohn Dyson 2081df8bae1dSRodney W. Grimes /* 2082df8bae1dSRodney W. Grimes * Try to collapse the object first 2083df8bae1dSRodney W. Grimes */ 2084df8bae1dSRodney W. Grimes vm_object_collapse(prev_object); 2085df8bae1dSRodney W. Grimes 2086df8bae1dSRodney W. Grimes /* 20870d94caffSDavid Greenman * Can't coalesce if: . more than one reference . paged out . shadows 20880d94caffSDavid Greenman * another object . has a copy elsewhere (any of which mean that the 20890d94caffSDavid Greenman * pages not mapped to prev_entry may be in use anyway) 2090df8bae1dSRodney W. Grimes */ 20918cc7e047SJohn Dyson if (prev_object->backing_object != NULL) { 209289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 2093df8bae1dSRodney W. Grimes return (FALSE); 2094df8bae1dSRodney W. Grimes } 2095a316d390SJohn Dyson 2096a316d390SJohn Dyson prev_size >>= PAGE_SHIFT; 2097a316d390SJohn Dyson next_size >>= PAGE_SHIFT; 209857a21abaSAlan Cox next_pindex = OFF_TO_IDX(prev_offset) + prev_size; 20998cc7e047SJohn Dyson 21008cc7e047SJohn Dyson if ((prev_object->ref_count > 1) && 2101ea41812fSAlan Cox (prev_object->size != next_pindex)) { 210289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 21038cc7e047SJohn Dyson return (FALSE); 21048cc7e047SJohn Dyson } 21058cc7e047SJohn Dyson 2106df8bae1dSRodney W. Grimes /* 21073364c323SKonstantin Belousov * Account for the charge. 21083364c323SKonstantin Belousov */ 2109ef694c1aSEdward Tomasz Napierala if (prev_object->cred != NULL) { 21103364c323SKonstantin Belousov 21113364c323SKonstantin Belousov /* 21123364c323SKonstantin Belousov * If prev_object was charged, then this mapping, 21133364c323SKonstantin Belousov * althought not charged now, may become writable 2114ef694c1aSEdward Tomasz Napierala * later. Non-NULL cred in the object would prevent 21153364c323SKonstantin Belousov * swap reservation during enabling of the write 21163364c323SKonstantin Belousov * access, so reserve swap now. Failed reservation 21173364c323SKonstantin Belousov * cause allocation of the separate object for the map 21183364c323SKonstantin Belousov * entry, and swap reservation for this entry is 21193364c323SKonstantin Belousov * managed in appropriate time. 21203364c323SKonstantin Belousov */ 2121ef694c1aSEdward Tomasz Napierala if (!reserved && !swap_reserve_by_cred(ptoa(next_size), 2122ef694c1aSEdward Tomasz Napierala prev_object->cred)) { 21233364c323SKonstantin Belousov return (FALSE); 21243364c323SKonstantin Belousov } 21253364c323SKonstantin Belousov prev_object->charge += ptoa(next_size); 21263364c323SKonstantin Belousov } 21273364c323SKonstantin Belousov 21283364c323SKonstantin Belousov /* 21290d94caffSDavid Greenman * Remove any pages that may still be in the object from a previous 21300d94caffSDavid Greenman * deallocation. 2131df8bae1dSRodney W. Grimes */ 2132ea41812fSAlan Cox if (next_pindex < prev_object->size) { 21336bbee8e2SAlan Cox vm_object_page_remove(prev_object, next_pindex, next_pindex + 21346bbee8e2SAlan Cox next_size, 0); 2135ea41812fSAlan Cox if (prev_object->type == OBJT_SWAP) 2136ea41812fSAlan Cox swap_pager_freespace(prev_object, 2137ea41812fSAlan Cox next_pindex, next_size); 21383364c323SKonstantin Belousov #if 0 2139ef694c1aSEdward Tomasz Napierala if (prev_object->cred != NULL) { 21403364c323SKonstantin Belousov KASSERT(prev_object->charge >= 21413364c323SKonstantin Belousov ptoa(prev_object->size - next_pindex), 21423364c323SKonstantin Belousov ("object %p overcharged 1 %jx %jx", prev_object, 21433364c323SKonstantin Belousov (uintmax_t)next_pindex, (uintmax_t)next_size)); 21443364c323SKonstantin Belousov prev_object->charge -= ptoa(prev_object->size - 21453364c323SKonstantin Belousov next_pindex); 21463364c323SKonstantin Belousov } 21473364c323SKonstantin Belousov #endif 2148ea41812fSAlan Cox } 2149df8bae1dSRodney W. Grimes 2150df8bae1dSRodney W. Grimes /* 2151df8bae1dSRodney W. Grimes * Extend the object if necessary. 2152df8bae1dSRodney W. Grimes */ 2153ea41812fSAlan Cox if (next_pindex + next_size > prev_object->size) 2154ea41812fSAlan Cox prev_object->size = next_pindex + next_size; 2155df8bae1dSRodney W. Grimes 215689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(prev_object); 2157df8bae1dSRodney W. Grimes return (TRUE); 2158df8bae1dSRodney W. Grimes } 2159df8bae1dSRodney W. Grimes 21607a5a6352SMatthew Dillon void 21617a5a6352SMatthew Dillon vm_object_set_writeable_dirty(vm_object_t object) 21627a5a6352SMatthew Dillon { 21637a5a6352SMatthew Dillon 216489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 21653280870dSKonstantin Belousov if (object->type != OBJT_VNODE) 21663280870dSKonstantin Belousov return; 21673280870dSKonstantin Belousov object->generation++; 21683280870dSKonstantin Belousov if ((object->flags & OBJ_MIGHTBEDIRTY) != 0) 2169ee39666aSJeff Roberson return; 2170af51d7bfSAlan Cox vm_object_set_flag(object, OBJ_MIGHTBEDIRTY); 21717a5a6352SMatthew Dillon } 21727a5a6352SMatthew Dillon 2173c7c34a24SBruce Evans #include "opt_ddb.h" 2174c3cb3e12SDavid Greenman #ifdef DDB 2175c7c34a24SBruce Evans #include <sys/kernel.h> 2176c7c34a24SBruce Evans 2177ce9edcf5SPoul-Henning Kamp #include <sys/cons.h> 2178c7c34a24SBruce Evans 2179c7c34a24SBruce Evans #include <ddb/ddb.h> 2180c7c34a24SBruce Evans 2181cac597e4SBruce Evans static int 21821b40f8c0SMatthew Dillon _vm_object_in_map(vm_map_t map, vm_object_t object, vm_map_entry_t entry) 2183a1f6d91cSDavid Greenman { 2184a1f6d91cSDavid Greenman vm_map_t tmpm; 2185a1f6d91cSDavid Greenman vm_map_entry_t tmpe; 2186a1f6d91cSDavid Greenman vm_object_t obj; 2187a1f6d91cSDavid Greenman int entcount; 2188a1f6d91cSDavid Greenman 2189a1f6d91cSDavid Greenman if (map == 0) 2190a1f6d91cSDavid Greenman return 0; 2191a1f6d91cSDavid Greenman 2192a1f6d91cSDavid Greenman if (entry == 0) { 2193a1f6d91cSDavid Greenman tmpe = map->header.next; 2194a1f6d91cSDavid Greenman entcount = map->nentries; 2195a1f6d91cSDavid Greenman while (entcount-- && (tmpe != &map->header)) { 2196a1f6d91cSDavid Greenman if (_vm_object_in_map(map, object, tmpe)) { 2197a1f6d91cSDavid Greenman return 1; 2198a1f6d91cSDavid Greenman } 2199a1f6d91cSDavid Greenman tmpe = tmpe->next; 2200a1f6d91cSDavid Greenman } 22019fdfe602SMatthew Dillon } else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 22029fdfe602SMatthew Dillon tmpm = entry->object.sub_map; 2203a1f6d91cSDavid Greenman tmpe = tmpm->header.next; 2204a1f6d91cSDavid Greenman entcount = tmpm->nentries; 2205a1f6d91cSDavid Greenman while (entcount-- && tmpe != &tmpm->header) { 2206a1f6d91cSDavid Greenman if (_vm_object_in_map(tmpm, object, tmpe)) { 2207a1f6d91cSDavid Greenman return 1; 2208a1f6d91cSDavid Greenman } 2209a1f6d91cSDavid Greenman tmpe = tmpe->next; 2210a1f6d91cSDavid Greenman } 22118aef1712SMatthew Dillon } else if ((obj = entry->object.vm_object) != NULL) { 221224a1cce3SDavid Greenman for (; obj; obj = obj->backing_object) 2213a1f6d91cSDavid Greenman if (obj == object) { 2214a1f6d91cSDavid Greenman return 1; 2215a1f6d91cSDavid Greenman } 2216a1f6d91cSDavid Greenman } 2217a1f6d91cSDavid Greenman return 0; 2218a1f6d91cSDavid Greenman } 2219a1f6d91cSDavid Greenman 2220cac597e4SBruce Evans static int 22211b40f8c0SMatthew Dillon vm_object_in_map(vm_object_t object) 2222a1f6d91cSDavid Greenman { 2223a1f6d91cSDavid Greenman struct proc *p; 22241005a129SJohn Baldwin 222560517fd1SJohn Baldwin /* sx_slock(&allproc_lock); */ 2226f67af5c9SXin LI FOREACH_PROC_IN_SYSTEM(p) { 2227a1f6d91cSDavid Greenman if (!p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */) 2228a1f6d91cSDavid Greenman continue; 2229553629ebSJake Burkholder if (_vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) { 223060517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2231a1f6d91cSDavid Greenman return 1; 2232a1f6d91cSDavid Greenman } 2233553629ebSJake Burkholder } 223460517fd1SJohn Baldwin /* sx_sunlock(&allproc_lock); */ 2235a1f6d91cSDavid Greenman if (_vm_object_in_map(kernel_map, object, 0)) 2236a1f6d91cSDavid Greenman return 1; 2237a1f6d91cSDavid Greenman return 0; 2238a1f6d91cSDavid Greenman } 2239a1f6d91cSDavid Greenman 2240c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check) 2241f708ef1bSPoul-Henning Kamp { 2242a1f6d91cSDavid Greenman vm_object_t object; 2243a1f6d91cSDavid Greenman 2244a1f6d91cSDavid Greenman /* 2245a1f6d91cSDavid Greenman * make sure that internal objs are in a map somewhere 2246a1f6d91cSDavid Greenman * and none have zero ref counts. 2247a1f6d91cSDavid Greenman */ 2248cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 224924a1cce3SDavid Greenman if (object->handle == NULL && 225024a1cce3SDavid Greenman (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 2251a1f6d91cSDavid Greenman if (object->ref_count == 0) { 22523efc015bSPeter Wemm db_printf("vmochk: internal obj has zero ref count: %ld\n", 22533efc015bSPeter Wemm (long)object->size); 2254a1f6d91cSDavid Greenman } 2255a1f6d91cSDavid Greenman if (!vm_object_in_map(object)) { 2256fc62ef1fSBruce Evans db_printf( 2257fc62ef1fSBruce Evans "vmochk: internal obj is not in a map: " 2258fc62ef1fSBruce Evans "ref: %d, size: %lu: 0x%lx, backing_object: %p\n", 2259fc62ef1fSBruce Evans object->ref_count, (u_long)object->size, 2260fc62ef1fSBruce Evans (u_long)object->size, 2261fc62ef1fSBruce Evans (void *)object->backing_object); 2262a1f6d91cSDavid Greenman } 2263a1f6d91cSDavid Greenman } 2264a1f6d91cSDavid Greenman } 2265a1f6d91cSDavid Greenman } 2266a1f6d91cSDavid Greenman 226726f9a767SRodney W. Grimes /* 2268df8bae1dSRodney W. Grimes * vm_object_print: [ debug ] 2269df8bae1dSRodney W. Grimes */ 2270c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static) 2271df8bae1dSRodney W. Grimes { 2272c7c34a24SBruce Evans /* XXX convert args. */ 2273c7c34a24SBruce Evans vm_object_t object = (vm_object_t)addr; 2274c7c34a24SBruce Evans boolean_t full = have_addr; 2275c7c34a24SBruce Evans 2276d031cff1SMatthew Dillon vm_page_t p; 2277df8bae1dSRodney W. Grimes 2278c7c34a24SBruce Evans /* XXX count is an (unused) arg. Avoid shadowing it. */ 2279c7c34a24SBruce Evans #define count was_count 2280c7c34a24SBruce Evans 2281d031cff1SMatthew Dillon int count; 2282df8bae1dSRodney W. Grimes 2283df8bae1dSRodney W. Grimes if (object == NULL) 2284df8bae1dSRodney W. Grimes return; 2285df8bae1dSRodney W. Grimes 2286eb95adefSBruce Evans db_iprintf( 2287ef694c1aSEdward Tomasz Napierala "Object %p: type=%d, size=0x%jx, res=%d, ref=%d, flags=0x%x ruid %d charge %jx\n", 2288e47cd172SMaxime Henrion object, (int)object->type, (uintmax_t)object->size, 22893364c323SKonstantin Belousov object->resident_page_count, object->ref_count, object->flags, 2290ef694c1aSEdward Tomasz Napierala object->cred ? object->cred->cr_ruid : -1, (uintmax_t)object->charge); 2291e47cd172SMaxime Henrion db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%jx\n", 22921c7c3c6aSMatthew Dillon object->shadow_count, 2293eb95adefSBruce Evans object->backing_object ? object->backing_object->ref_count : 0, 2294e47cd172SMaxime Henrion object->backing_object, (uintmax_t)object->backing_object_offset); 2295df8bae1dSRodney W. Grimes 2296df8bae1dSRodney W. Grimes if (!full) 2297df8bae1dSRodney W. Grimes return; 2298df8bae1dSRodney W. Grimes 2299c7c34a24SBruce Evans db_indent += 2; 2300df8bae1dSRodney W. Grimes count = 0; 2301fc2ffbe6SPoul-Henning Kamp TAILQ_FOREACH(p, &object->memq, listq) { 2302df8bae1dSRodney W. Grimes if (count == 0) 2303c7c34a24SBruce Evans db_iprintf("memory:="); 2304df8bae1dSRodney W. Grimes else if (count == 6) { 2305c7c34a24SBruce Evans db_printf("\n"); 2306c7c34a24SBruce Evans db_iprintf(" ..."); 2307df8bae1dSRodney W. Grimes count = 0; 2308df8bae1dSRodney W. Grimes } else 2309c7c34a24SBruce Evans db_printf(","); 2310df8bae1dSRodney W. Grimes count++; 2311df8bae1dSRodney W. Grimes 2312e47cd172SMaxime Henrion db_printf("(off=0x%jx,page=0x%jx)", 2313e47cd172SMaxime Henrion (uintmax_t)p->pindex, (uintmax_t)VM_PAGE_TO_PHYS(p)); 2314df8bae1dSRodney W. Grimes } 2315df8bae1dSRodney W. Grimes if (count != 0) 2316c7c34a24SBruce Evans db_printf("\n"); 2317c7c34a24SBruce Evans db_indent -= 2; 2318df8bae1dSRodney W. Grimes } 23195070c7f8SJohn Dyson 2320c7c34a24SBruce Evans /* XXX. */ 2321c7c34a24SBruce Evans #undef count 2322c7c34a24SBruce Evans 2323c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */ 23245070c7f8SJohn Dyson void 23251b40f8c0SMatthew Dillon vm_object_print( 23261b40f8c0SMatthew Dillon /* db_expr_t */ long addr, 23271b40f8c0SMatthew Dillon boolean_t have_addr, 23281b40f8c0SMatthew Dillon /* db_expr_t */ long count, 23291b40f8c0SMatthew Dillon char *modif) 2330c7c34a24SBruce Evans { 2331c7c34a24SBruce Evans vm_object_print_static(addr, have_addr, count, modif); 2332c7c34a24SBruce Evans } 2333c7c34a24SBruce Evans 2334c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages) 23355070c7f8SJohn Dyson { 23365070c7f8SJohn Dyson vm_object_t object; 2337bb2ac86fSKonstantin Belousov vm_pindex_t fidx; 2338bb2ac86fSKonstantin Belousov vm_paddr_t pa; 2339bb2ac86fSKonstantin Belousov vm_page_t m, prev_m; 2340bb2ac86fSKonstantin Belousov int rcount, nl, c; 2341cc64b484SAlfred Perlstein 2342bb2ac86fSKonstantin Belousov nl = 0; 2343cc64b484SAlfred Perlstein TAILQ_FOREACH(object, &vm_object_list, object_list) { 2344fc62ef1fSBruce Evans db_printf("new object: %p\n", (void *)object); 23455070c7f8SJohn Dyson if (nl > 18) { 23465070c7f8SJohn Dyson c = cngetc(); 23475070c7f8SJohn Dyson if (c != ' ') 23485070c7f8SJohn Dyson return; 23495070c7f8SJohn Dyson nl = 0; 23505070c7f8SJohn Dyson } 23515070c7f8SJohn Dyson nl++; 23525070c7f8SJohn Dyson rcount = 0; 23535070c7f8SJohn Dyson fidx = 0; 2354bb2ac86fSKonstantin Belousov pa = -1; 2355bb2ac86fSKonstantin Belousov TAILQ_FOREACH(m, &object->memq, listq) { 2356bb2ac86fSKonstantin Belousov if (m->pindex > 128) 2357bb2ac86fSKonstantin Belousov break; 2358bb2ac86fSKonstantin Belousov if ((prev_m = TAILQ_PREV(m, pglist, listq)) != NULL && 2359bb2ac86fSKonstantin Belousov prev_m->pindex + 1 != m->pindex) { 23605070c7f8SJohn Dyson if (rcount) { 23613efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 23623efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 23635070c7f8SJohn Dyson if (nl > 18) { 23645070c7f8SJohn Dyson c = cngetc(); 23655070c7f8SJohn Dyson if (c != ' ') 23665070c7f8SJohn Dyson return; 23675070c7f8SJohn Dyson nl = 0; 23685070c7f8SJohn Dyson } 23695070c7f8SJohn Dyson nl++; 23705070c7f8SJohn Dyson rcount = 0; 23715070c7f8SJohn Dyson } 23725070c7f8SJohn Dyson } 23735070c7f8SJohn Dyson if (rcount && 23745070c7f8SJohn Dyson (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) { 23755070c7f8SJohn Dyson ++rcount; 23765070c7f8SJohn Dyson continue; 23775070c7f8SJohn Dyson } 23785070c7f8SJohn Dyson if (rcount) { 23792446e4f0SAlan Cox db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 23803efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 23815070c7f8SJohn Dyson if (nl > 18) { 23825070c7f8SJohn Dyson c = cngetc(); 23835070c7f8SJohn Dyson if (c != ' ') 23845070c7f8SJohn Dyson return; 23855070c7f8SJohn Dyson nl = 0; 23865070c7f8SJohn Dyson } 23875070c7f8SJohn Dyson nl++; 23885070c7f8SJohn Dyson } 2389bb2ac86fSKonstantin Belousov fidx = m->pindex; 23905070c7f8SJohn Dyson pa = VM_PAGE_TO_PHYS(m); 23915070c7f8SJohn Dyson rcount = 1; 23925070c7f8SJohn Dyson } 23935070c7f8SJohn Dyson if (rcount) { 23943efc015bSPeter Wemm db_printf(" index(%ld)run(%d)pa(0x%lx)\n", 23953efc015bSPeter Wemm (long)fidx, rcount, (long)pa); 23965070c7f8SJohn Dyson if (nl > 18) { 23975070c7f8SJohn Dyson c = cngetc(); 23985070c7f8SJohn Dyson if (c != ' ') 23995070c7f8SJohn Dyson return; 24005070c7f8SJohn Dyson nl = 0; 24015070c7f8SJohn Dyson } 24025070c7f8SJohn Dyson nl++; 24035070c7f8SJohn Dyson } 24045070c7f8SJohn Dyson } 24055070c7f8SJohn Dyson } 2406c3cb3e12SDavid Greenman #endif /* DDB */ 2407